Rev 1310 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
671 | jmachado | 1 | package pt.estgp.estgweb.services.surveys; |
2 | |||
3 | import jomm.dao.impl.AbstractDao; |
||
4 | import jomm.utils.FilesUtils; |
||
5 | import jomm.utils.MessageResources; |
||
6 | import org.apache.log4j.Logger; |
||
7 | import org.jfree.chart.ChartFactory; |
||
8 | import org.jfree.chart.ChartUtilities; |
||
9 | import org.jfree.chart.JFreeChart; |
||
684 | jmachado | 10 | import org.jfree.chart.axis.*; |
671 | jmachado | 11 | import org.jfree.chart.plot.CategoryPlot; |
12 | import org.jfree.chart.plot.PlotOrientation; |
||
13 | import org.jfree.data.category.CategoryDataset; |
||
14 | import org.jfree.data.category.DefaultCategoryDataset; |
||
684 | jmachado | 15 | import org.jfree.text.TextBlockAnchor; |
16 | import org.jfree.ui.RectangleAnchor; |
||
671 | jmachado | 17 | import pt.estgp.estgweb.Globals; |
18 | import pt.estgp.estgweb.domain.*; |
||
19 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
||
20 | import pt.estgp.estgweb.filters.chains.ResourceAccessControlEnum; |
||
21 | import pt.estgp.estgweb.services.data.RepositoryService; |
||
22 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
||
23 | import pt.estgp.estgweb.utils.ConfigProperties; |
||
24 | |||
25 | import javax.servlet.http.HttpServletRequest; |
||
684 | jmachado | 26 | import java.awt.*; |
671 | jmachado | 27 | import java.io.*; |
28 | import java.util.ArrayList; |
||
29 | import java.util.List; |
||
30 | |||
31 | /** |
||
32 | * @author: Duarte Santos |
||
33 | * @date: 10-08-2011 |
||
34 | * @time: 17:38 |
||
35 | * @email: a12564 [at] estgp [dot] pt |
||
36 | */ |
||
37 | |||
38 | public class GenerateSurveyStatsService implements IService, 1.5.0/docs/api/java/lang/Runnable.html">Runnable |
||
39 | { |
||
40 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(GenerateSurveyStatsService.class); |
||
41 | private static final int MAX_COMMIT = 100; |
||
42 | private static RepositoryService repositoryService = new RepositoryService(); |
||
43 | |||
44 | private long surveyId; |
||
45 | private HttpServletRequest request; |
||
46 | private UserSession userSession; |
||
47 | |||
684 | jmachado | 48 | public void generateStatsService(long surveyId, HttpServletRequest request, UserSession userSession) |
671 | jmachado | 49 | { |
50 | if(surveyId <= 0) return; |
||
51 | |||
684 | jmachado | 52 | SurveyImpl survey = DaoFactory.getSurveyDaoImpl().load(surveyId); |
671 | jmachado | 53 | if(survey != null) |
54 | { |
||
684 | jmachado | 55 | GenerateSurveyStatsService s = new GenerateSurveyStatsService(); |
56 | s.request = request; |
||
57 | s.surveyId = surveyId; |
||
58 | s.userSession = userSession; |
||
671 | jmachado | 59 | |
60 | // start a thread |
||
684 | jmachado | 61 | 1.5.0/docs/api/java/lang/Thread.html">Thread thread = new 1.5.0/docs/api/java/lang/Thread.html">Thread(s); |
671 | jmachado | 62 | thread.start(); |
63 | logger.info("Starting thread with id: "+thread.getId()); |
||
64 | } |
||
65 | } |
||
66 | |||
684 | jmachado | 67 | private synchronized JFreeChart buildChart(HttpServletRequest request, SurveyQuestion q, SurveyStructuralNode node, UserSession userSession, boolean withCategories) |
671 | jmachado | 68 | { |
69 | // get counted values and labels |
||
70 | List<Integer> countedValues = new ArrayList<Integer>(); |
||
71 | 1.5.0/docs/api/java/lang/String.html">String itemLabels = ""; |
||
72 | 1.5.0/docs/api/java/lang/String.html">String candidateLabels = ""; |
||
73 | for(SurveyQuestionItem qi : q.getQuestionItems()) |
||
74 | { |
||
75 | for(SurveyQuestionCandidateAnswer ca : qi.getCandidadeAnswers()) |
||
76 | { |
||
77 | int count = DaoFactory.getSurveyQuestionAnswerDaoImpl().countAnswers(ca.getId(), node.getId()); |
||
78 | countedValues.add(count); |
||
79 | candidateLabels = candidateLabels + ca.getLabelpt() + ","; |
||
80 | } |
||
81 | itemLabels = itemLabels + qi.getLabelpt() + ","; |
||
82 | } |
||
83 | 1.5.0/docs/api/java/lang/String.html">String[] categories = itemLabels.split(","); |
||
84 | 1.5.0/docs/api/java/lang/String.html">String[] series = candidateLabels.split(","); |
||
85 | int[] values = new int[countedValues.size()]; |
||
86 | for(int i=0; i<countedValues.size(); i++) |
||
87 | values[i] = countedValues.get(i); |
||
88 | |||
89 | // create chart |
||
90 | 1.5.0/docs/api/java/lang/String.html">String domainAxisLabel = MessageResources.getMessage(request,"survey.stats.graph.axis.domain.label"); |
||
91 | PlotOrientation orientation = PlotOrientation.HORIZONTAL; |
||
92 | if(!withCategories) |
||
93 | { |
||
94 | categories = null; |
||
95 | domainAxisLabel = ""; |
||
96 | orientation = PlotOrientation.VERTICAL; |
||
97 | } |
||
98 | |||
99 | CategoryDataset dataset = buildCategoryDataset(categories,series,values); |
||
100 | JFreeChart chart = buildJFreeChart(q.getLabelpt(),domainAxisLabel,MessageResources.getMessage(request,"survey.stats.graph.axis.range.label"),dataset,orientation); |
||
101 | |||
684 | jmachado | 102 | // chart configs |
103 | final CategoryPlot plot = chart.getCategoryPlot(); |
||
104 | final ValueAxis valueAxis = plot.getRangeAxis(); |
||
105 | final CategoryAxis categoryAxis = plot.getDomainAxis(); |
||
106 | final CategoryLabelPositions positions = categoryAxis.getCategoryLabelPositions(); |
||
107 | 1.5.0/docs/api/java/awt/Font.html">Font font = new 1.5.0/docs/api/java/awt/Font.html">Font("Tahoma",1.5.0/docs/api/java/awt/Font.html">Font.PLAIN, 11); |
||
108 | categoryAxis.setMaximumCategoryLabelLines(3); |
||
109 | categoryAxis.setTickLabelFont(font); |
||
110 | CategoryLabelPosition right = new CategoryLabelPosition(RectangleAnchor.RIGHT, TextBlockAnchor.CENTER_RIGHT); |
||
111 | categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.replaceRightPosition(positions, right)); |
||
671 | jmachado | 112 | |
684 | jmachado | 113 | return chart; |
671 | jmachado | 114 | } |
115 | |||
116 | public synchronized 1.5.0/docs/api/java/lang/String.html">String storeChartRepository(JFreeChart chart, 1.5.0/docs/api/java/lang/String.html">String filename, 1.5.0/docs/api/java/lang/String.html">String description, 1.5.0/docs/api/java/lang/String.html">String identifier, UserSession userSession) |
||
117 | { |
||
118 | try |
||
119 | { |
||
120 | 1.5.0/docs/api/java/io/File.html">File file = new 1.5.0/docs/api/java/io/File.html">File(Globals.TMP_DIR + filename); |
||
121 | 1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.getExtension(filename); |
||
122 | ChartUtilities.saveChartAsPNG(file, chart, |
||
123 | ConfigProperties.getIntProperty("surveys.stats.graph.img.width"), |
||
124 | ConfigProperties.getIntProperty("surveys.stats.graph.img.height")); |
||
125 | 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream inputStream = new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(file); |
||
126 | |||
127 | if(identifier == null) |
||
1703 | jmachado | 128 | identifier = repositoryService.storeRepositoryFile(inputStream,"image/png",extension,(int)file.length(),file.getName(),description,ResourceAccessControlEnum.surveyStatsDomain, null, userSession); |
671 | jmachado | 129 | else |
130 | repositoryService.updateRepositoryFile(identifier,inputStream,"image/png",extension,(int)file.length(),file.getName(),description,ResourceAccessControlEnum.surveyStatsDomain); |
||
131 | |||
132 | boolean deleted = file.delete(); |
||
133 | if(!deleted) |
||
134 | logger.info("Error deleting file: "+filename); |
||
135 | } |
||
136 | catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
137 | { |
||
138 | logger.info(e); |
||
139 | } |
||
140 | return identifier; |
||
141 | } |
||
142 | |||
143 | public synchronized JFreeChart buildJFreeChart(1.5.0/docs/api/java/lang/String.html">String chartTitle,1.5.0/docs/api/java/lang/String.html">String domainAxisLabel,1.5.0/docs/api/java/lang/String.html">String rangeAxisLabel,CategoryDataset dataset,PlotOrientation orientation) |
||
144 | { |
||
145 | JFreeChart chart = ChartFactory.createBarChart(chartTitle,domainAxisLabel,rangeAxisLabel,dataset,orientation,true,true,false); |
||
146 | CategoryPlot plot = chart.getCategoryPlot(); |
||
147 | plot.getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits()); |
||
148 | return chart; |
||
149 | } |
||
150 | |||
151 | public synchronized CategoryDataset buildCategoryDataset(1.5.0/docs/api/java/lang/String.html">String[] categories, 1.5.0/docs/api/java/lang/String.html">String[] series, int[] values) |
||
152 | { |
||
153 | DefaultCategoryDataset dataset = new DefaultCategoryDataset(); |
||
154 | int k = 0; |
||
155 | int serLen = series.length; |
||
156 | if(categories != null) |
||
157 | { |
||
158 | int j = 0; |
||
159 | int catLen = categories.length; |
||
160 | for(int i = 0; i < series.length; i++) |
||
161 | { |
||
162 | if(i != 0 && i%(serLen/catLen) == 0) |
||
163 | j++; |
||
164 | dataset.addValue(values[k++], series[i], categories[j]); |
||
165 | } |
||
166 | } |
||
167 | else |
||
168 | { |
||
169 | for(1.5.0/docs/api/java/lang/String.html">String serie : series) |
||
170 | dataset.addValue(values[k++], serie, ""); |
||
171 | } |
||
172 | return dataset; |
||
173 | } |
||
174 | |||
175 | public void run() |
||
176 | { |
||
177 | logger.info("Running thread..."); |
||
178 | AbstractDao.getCurrentSession().beginTransaction(); |
||
179 | |||
684 | jmachado | 180 | SurveyImpl survey = DaoFactory.getSurveyDaoImpl().load(surveyId); |
181 | |||
182 | survey.setStatusPercentage(0); |
||
183 | survey.setStatus(SurveyStatusEnum.STATUS_GENERATE_STATS.getStatus()); |
||
184 | |||
185 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
186 | AbstractDao.getCurrentSession().beginTransaction(); |
||
187 | |||
188 | survey = DaoFactory.getSurveyDaoImpl().load(surveyId); |
||
189 | |||
190 | GenerateSurveyStatsCsvService service = new GenerateSurveyStatsCsvService(); |
||
191 | service.run(surveyId,userSession); |
||
192 | |||
671 | jmachado | 193 | List<SurveyStructuralNode> childNodes = ((SurveyStructuralNodeImpl)survey.getStructuralNode()).getChildsStructuralNodes(); |
684 | jmachado | 194 | |
195 | long total = childNodes.size() * survey.getQuestions().size(); |
||
196 | long i = 0; |
||
197 | int status; |
||
671 | jmachado | 198 | for(SurveyStructuralNode node : childNodes) |
199 | { |
||
200 | for(SurveyQuestion question : survey.getQuestions()) |
||
201 | { |
||
684 | jmachado | 202 | long nodeId = node.getId(); |
203 | long questionId = question.getId(); |
||
671 | jmachado | 204 | |
205 | if (i++ % MAX_COMMIT == 0) |
||
206 | { |
||
684 | jmachado | 207 | status = (int)(((double)i/(double)total)*100); |
208 | survey.setStatusPercentage(status); |
||
671 | jmachado | 209 | AbstractDao.getCurrentSession().getTransaction().commit(); |
210 | AbstractDao.getCurrentSession().beginTransaction(); |
||
684 | jmachado | 211 | survey = DaoFactory.getSurveyDaoImpl().load(surveyId); |
671 | jmachado | 212 | } |
684 | jmachado | 213 | node = DaoFactory.getSurveyStructuralNodeDaoImpl().load(nodeId); |
214 | question = DaoFactory.getSurveyQuestionDaoImpl().load(questionId); |
||
671 | jmachado | 215 | |
684 | jmachado | 216 | JFreeChart chart = null; |
217 | if(question.getType().equals(Globals.SURVEY_QUESTION_TYPE_MATRIX)) |
||
218 | chart = buildChart(request, question, node, userSession, true); |
||
219 | else if(question.getType().equals(Globals.SURVEY_QUESTION_TYPE_CHECK) || question.getType().equals(Globals.SURVEY_QUESTION_TYPE_RADIO)) |
||
220 | chart = buildChart(request, question, node, userSession, false); |
||
221 | |||
222 | if(chart != null) |
||
671 | jmachado | 223 | { |
684 | jmachado | 224 | node = DaoFactory.getSurveyStructuralNodeDaoImpl().load(nodeId); |
225 | question = DaoFactory.getSurveyQuestionDaoImpl().load(questionId); |
||
226 | 1.5.0/docs/api/java/lang/String.html">String identifier; |
||
227 | 1.5.0/docs/api/java/lang/String.html">String filename = "/survey_"+surveyId+"_question_"+question.getId()+"_node_"+node.getId()+".png"; |
||
228 | SurveyStatsGraphic graph = DaoFactory.getSurveyStatsGraphicDaoImpl().load(question.getId(),node.getId()); |
||
229 | if(graph == null) |
||
230 | { |
||
231 | identifier = storeChartRepository(chart,filename,"descricao",null,userSession); |
||
232 | if(identifier != null) |
||
233 | { |
||
234 | graph = DomainObjectFactory.createSurveyStatsGraphicImpl(); |
||
235 | graph.setQuestion(question); |
||
236 | graph.setStructuralNode(node); |
||
237 | graph.setIdentifier(identifier); |
||
238 | DaoFactory.getSurveyStatsGraphicDaoImpl().save(graph); |
||
239 | } |
||
240 | } |
||
241 | else |
||
242 | { |
||
243 | identifier = graph.getIdentifier(); |
||
244 | storeChartRepository(chart,filename,"descricao",identifier,userSession); |
||
245 | } |
||
671 | jmachado | 246 | } |
247 | } |
||
248 | } |
||
249 | |||
684 | jmachado | 250 | survey.setStatusPercentage(100); |
251 | survey.setStatus(SurveyStatusEnum.STATUS_STOPPED.getStatus()); |
||
252 | |||
671 | jmachado | 253 | AbstractDao.getCurrentSession().getTransaction().commit(); |
254 | logger.info("End of thread..."); |
||
255 | } |
||
256 | } |