PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/fsnetWeb/src/main/java/fr/univartois/ili/fsnet/tags/ConsultationResultsTag.java

https://code.google.com/
Java | 272 lines | 211 code | 24 blank | 37 comment | 59 complexity | 5e00372c1d186acda99168a7e8ff4cfe MD5 | raw file
Possible License(s): LGPL-2.1
  1. package fr.univartois.ili.fsnet.tags;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.ResourceBundle;
  6. import java.util.SortedSet;
  7. import java.util.TreeSet;
  8. import javax.servlet.jsp.JspException;
  9. import javax.servlet.jsp.tagext.TagSupport;
  10. import fr.univartois.ili.fsnet.entities.Consultation;
  11. import fr.univartois.ili.fsnet.entities.Consultation.TypeConsultation;
  12. import fr.univartois.ili.fsnet.entities.ConsultationChoice;
  13. import fr.univartois.ili.fsnet.entities.ConsultationChoiceVote;
  14. import fr.univartois.ili.fsnet.entities.ConsultationVote;
  15. public class ConsultationResultsTag extends TagSupport {
  16. /**
  17. *
  18. */
  19. private static final long serialVersionUID = 1L;
  20. private Consultation consultation;
  21. private String number;
  22. private String percent;
  23. private String maximum;
  24. private String choice;
  25. private String histogram;
  26. private List<Double> nb;
  27. private Iterator<Double> currentNb;
  28. private List<String> choices;
  29. private Iterator<String> currentChoice;
  30. private double total;
  31. private double max;
  32. @Override
  33. public int doStartTag() throws JspException {
  34. if (consultation == null || consultation.getChoices().size() == 0){
  35. return SKIP_BODY;
  36. }
  37. init();
  38. currentNb = nb.iterator();
  39. currentChoice = choices.iterator();
  40. if (!currentNb.hasNext()){
  41. return SKIP_BODY;
  42. }
  43. setAttributes();
  44. return EVAL_BODY_INCLUDE;
  45. }
  46. @Override
  47. public int doAfterBody() throws JspException {
  48. if (!currentNb.hasNext()){
  49. return SKIP_BODY;
  50. }
  51. setAttributes();
  52. return EVAL_BODY_AGAIN;
  53. }
  54. @Override
  55. public int doEndTag() throws JspException {
  56. if (number != null){ pageContext.getRequest().removeAttribute(number); }
  57. if (percent != null){ pageContext.getRequest().removeAttribute(percent);}
  58. if (maximum != null){ pageContext.getRequest().removeAttribute(maximum);}
  59. if (choice != null){ pageContext.getRequest().removeAttribute(choice);}
  60. return super.doEndTag();
  61. }
  62. public void setAttributes(){
  63. double nbVotes = currentNb.next();
  64. if (number != null && !"".equals(number)) {
  65. if (consultation.getType() == TypeConsultation.YES_NO_IFNECESSARY){
  66. pageContext.getRequest().setAttribute(number, Double.valueOf(Math.round(nbVotes*10))/10);
  67. }else{
  68. pageContext.getRequest().setAttribute(number, Math.round(nbVotes));
  69. }
  70. }
  71. if (percent != null && !"".equals(percent)){ pageContext.getRequest().setAttribute(percent, Math.round(nbVotes*100/total));}
  72. if (maximum != null && !"".equals(maximum)){ pageContext.getRequest().setAttribute(maximum, nbVotes == max);}
  73. if (choice != null && !"".equals(choice)){ pageContext.getRequest().setAttribute(choice, currentChoice.next());}
  74. }
  75. public void init(){
  76. ConsultationChoice tmpChoice;
  77. Iterator<ConsultationVote> votes;
  78. ConsultationVote vote;
  79. Iterator<ConsultationChoiceVote> voteChoices;
  80. ConsultationChoiceVote voteChoice;
  81. Iterator<ConsultationChoice> copyChoices = consultation.getChoices().iterator();
  82. nb = new ArrayList<Double>();
  83. this.choices = new ArrayList<String>();
  84. while (copyChoices.hasNext()){
  85. copyChoices.next();
  86. nb.add(0.0);
  87. }
  88. copyChoices = consultation.getChoices().iterator();
  89. total = 0;
  90. max = 0;
  91. int i=0;
  92. while (copyChoices.hasNext()){
  93. tmpChoice = copyChoices.next();
  94. this.choices.add(tmpChoice.getIntituled());
  95. votes = consultation.getConsultationVotes().iterator();
  96. while (votes.hasNext()){
  97. vote = votes.next();
  98. voteChoices = vote.getChoices().iterator();
  99. while (voteChoices.hasNext()){
  100. voteChoice = voteChoices.next();
  101. if (tmpChoice.getId() == voteChoice.getChoice().getId()){
  102. if (voteChoice.isIfNecessary()){
  103. nb.set(i, nb.get(i)+consultation.getIfNecessaryWeight());
  104. total+=consultation.getIfNecessaryWeight();
  105. }
  106. else {
  107. if (consultation.getType() == TypeConsultation.PREFERENCE_ORDER){
  108. nb.set(i, nb.get(i)+voteChoice.getPreferenceOrder());
  109. }
  110. else {
  111. nb.set(i, nb.get(i)+1);
  112. total+=1;
  113. }
  114. }
  115. if (max < nb.get(i)){
  116. max = nb.get(i);
  117. }
  118. }
  119. }
  120. }
  121. i++;
  122. }
  123. if (consultation.getType() == Consultation.TypeConsultation.YES_NO_OTHER){
  124. initOther();
  125. }
  126. if (consultation.getType() == TypeConsultation.PREFERENCE_ORDER){
  127. initPreferenceOrder();
  128. }
  129. }
  130. public void initOther(){
  131. Iterator<ConsultationVote> votes=consultation.getConsultationVotes().iterator();
  132. ConsultationVote vote;
  133. int i = nb.size();
  134. if (histogram != null && "yes".equals(histogram)){
  135. int iChoice;
  136. while (votes.hasNext()){
  137. vote = votes.next();
  138. if (!"".equals(vote.getOther())){
  139. if (this.choices.contains(vote.getOther())){
  140. iChoice=this.choices.indexOf(vote.getOther());
  141. nb.set(iChoice, nb.get(iChoice)+1);
  142. }
  143. else {
  144. //iChoice=nb.size();
  145. nb.add(1.0);
  146. this.choices.add(vote.getOther());
  147. }
  148. total++;
  149. }
  150. }
  151. }
  152. else {
  153. nb.add(0.0);
  154. this.choices.add(ResourceBundle.getBundle("FSneti18n", pageContext.getRequest().getLocale()).getString("consultation.other"));
  155. while (votes.hasNext()){
  156. vote = votes.next();
  157. if (!"".equals(vote.getOther())){
  158. nb.set(i, nb.get(i)+1);
  159. if (max < nb.get(i)){
  160. max = nb.get(i);
  161. }
  162. total++;
  163. }
  164. }
  165. }
  166. }
  167. public void initPreferenceOrder(){
  168. SortedSet <Double>marks = new TreeSet<Double>();
  169. marks.addAll(nb);
  170. for (int i=0,j=0 ; i<nb.size() ; j=0,i++){
  171. for (Double mark : marks){
  172. j++;
  173. if (mark.equals(nb.get(i))){
  174. nb.set(i, Double.valueOf(j));
  175. }
  176. }
  177. }
  178. }
  179. // public void initPreferenceOrder(){
  180. // int i;
  181. // SortedSet<Integer> marks = new TreeSet<Integer>();
  182. // for (Double mark : nb){
  183. // marks.add(mark.intValue());
  184. // }
  185. //
  186. // for (int j=0 ; j<nb.size() ; j++){
  187. // i=0;
  188. // for (Integer mark : marks){
  189. // System.out.println("nb: "+nb.get(i)+" mark: "+mark);
  190. // if (mark.equals(nb.get(j).intValue())){
  191. // nb.set(j, Double.valueOf(j+1));
  192. // }
  193. // }
  194. // }
  195. //// for (Double number : nb){
  196. //// i=0;
  197. //// for (Integer mark : marks){
  198. //// System.out.println("mark:"+mark+" number:"+number);
  199. //// if (mark.equals(number.intValue())){
  200. //// number = Double.valueOf(i+1);
  201. //// System.out.println("plop: "+number);
  202. //// }
  203. //// i++;
  204. //// }
  205. //// }
  206. ////
  207. // for (Double number : nb){
  208. // System.out.println("hey:"+number);
  209. // }
  210. //
  211. // }
  212. public Consultation getConsultation() {
  213. return consultation;
  214. }
  215. public void setConsultation(Consultation consultation) {
  216. this.consultation = consultation;
  217. }
  218. public String getNumber() {
  219. return number;
  220. }
  221. public void setNumber(String nb) {
  222. this.number = nb;
  223. }
  224. public String getPercent() {
  225. return percent;
  226. }
  227. public void setPercent(String percent) {
  228. this.percent = percent;
  229. }
  230. public String getMaximum() {
  231. return maximum;
  232. }
  233. public void setMaximum(String maximum) {
  234. this.maximum = maximum;
  235. }
  236. public String getChoice() {
  237. return choice;
  238. }
  239. public void setChoice(String choice) {
  240. this.choice = choice;
  241. }
  242. public String getHistogram() {
  243. return histogram;
  244. }
  245. public void setHistogram(String histogram) {
  246. this.histogram = histogram;
  247. }
  248. }