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.utils.FilesUtils; |
||
4 | import org.apache.log4j.Logger; |
||
5 | import pt.estgp.estgweb.Globals; |
||
6 | import pt.estgp.estgweb.domain.Survey; |
||
7 | import pt.estgp.estgweb.domain.SurveyResponse; |
||
8 | import pt.estgp.estgweb.domain.SurveyStructuralNode; |
||
9 | import pt.estgp.estgweb.domain.UserSession; |
||
10 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
||
11 | import pt.estgp.estgweb.filters.chains.ResourceAccessControlEnum; |
||
12 | import pt.estgp.estgweb.services.data.RepositoryService; |
||
13 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
||
14 | |||
15 | import java.io.*; |
||
16 | import java.util.ArrayList; |
||
17 | import java.util.Iterator; |
||
18 | import java.util.List; |
||
19 | |||
20 | /** |
||
21 | * @author: Duarte Santos |
||
22 | * @date: 15-08-2011 |
||
23 | * @time: 13:22 |
||
24 | * @email: a12564 [at] estgp [dot] pt |
||
25 | */ |
||
26 | |||
27 | public class GenerateSurveyStatsCsvService implements IService |
||
28 | { |
||
29 | 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(GenerateSurveyStatsCsvService.class); |
||
30 | private static RepositoryService repositoryService = new RepositoryService(); |
||
31 | |||
32 | public 1.5.0/docs/api/java/lang/String.html">String run(long surveyId, UserSession userSession) |
||
33 | { |
||
34 | if(surveyId <= 0) return null; |
||
35 | |||
36 | Survey survey = DaoFactory.getSurveyDaoImpl().load(surveyId); |
||
37 | if(survey != null) |
||
38 | { |
||
39 | try |
||
40 | { |
||
41 | 1.5.0/docs/api/java/lang/String.html">String filename = Globals.TMP_DIR + "/survey_"+survey.getId()+".csv"; |
||
42 | 1.5.0/docs/api/java/io/File.html">File file = new 1.5.0/docs/api/java/io/File.html">File(filename); |
||
43 | 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream fout = new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(file); |
||
684 | jmachado | 44 | fout.write(239); |
45 | fout.write(187); |
||
46 | fout.write(191); |
||
47 | 1.5.0/docs/api/java/io/OutputStreamWriter.html">OutputStreamWriter writer = new 1.5.0/docs/api/java/io/OutputStreamWriter.html">OutputStreamWriter(fout, "UTF-8"); |
||
671 | jmachado | 48 | |
684 | jmachado | 49 | 1.5.0/docs/api/java/lang/String.html">String[] csvHeader = {"IDResposta","IDQuestionário","Título do Questionário","IDPergunta","Pergunta","IDItem","Item","IDCandidata","Candidata","Resposta de Texto","IDUtilizador","Utilizador","Nós"}; |
671 | jmachado | 50 | writeCsvHeader(writer,csvHeader); |
51 | |||
52 | 5+0%2Fdocs%2Fapi+List">List list = DaoFactory.getSurveyQuestionAnswerDaoImpl().loadForCsv(survey.getId()); |
||
53 | 1.5.0/docs/api/java/util/Iterator.html">Iterator iter = list.iterator(); |
||
54 | while(iter.hasNext()) |
||
55 | { |
||
56 | 5+0%2Fdocs%2Fapi+Object">Object[] objs = (5+0%2Fdocs%2Fapi+Object">Object[]) iter.next(); |
||
57 | 1.5.0/docs/api/java/lang/String.html">String[] values = new 1.5.0/docs/api/java/lang/String.html">String[objs.length+1]; |
||
58 | |||
59 | int i; |
||
60 | for(i = 0; i < objs.length; i++) |
||
61 | { |
||
62 | if(objs[i] != null) |
||
63 | values[i] = objs[i].toString(); |
||
64 | else |
||
65 | values[i] = ""; |
||
66 | } |
||
67 | |||
68 | long responseId = 1.5.0/docs/api/java/lang/Long.html">Long.parseLong(values[0]); |
||
69 | if(responseId > 0) |
||
70 | { |
||
71 | SurveyResponse r = DaoFactory.getSurveyResponseDaoImpl().load(responseId); |
||
72 | if(r != null) |
||
73 | { |
||
74 | List<String> nodeNames = getAllBackwardNodes(r.getStructuralNode()); |
||
75 | 1.5.0/docs/api/java/lang/String.html">String normalizeNodesNames = normalizeNodesNames(nodeNames); |
||
76 | values[i] = normalizeNodesNames; |
||
77 | } |
||
78 | } |
||
79 | writeCsvData(writer,values); |
||
80 | } |
||
81 | writer.flush(); |
||
82 | writer.close(); |
||
83 | |||
84 | 1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.getExtension(filename); |
||
85 | 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream inputStream = new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(file); |
||
86 | 1.5.0/docs/api/java/lang/String.html">String identifier = survey.getIdentifier(); |
||
684 | jmachado | 87 | |
671 | jmachado | 88 | if(identifier == null) |
1703 | jmachado | 89 | identifier = repositoryService.storeRepositoryFile(inputStream,"text/comma-separated-values",extension,(int)file.length(),file.getName(),"CSV file of survey: "+survey.getId(), ResourceAccessControlEnum.surveyStatsDomain, null, userSession); |
671 | jmachado | 90 | else |
91 | repositoryService.updateRepositoryFile(identifier,inputStream,"text/comma-separated-values",extension,(int)file.length(),file.getName(),"CSV file of survey: "+survey.getId(),ResourceAccessControlEnum.surveyStatsDomain); |
||
92 | |||
684 | jmachado | 93 | survey = DaoFactory.getSurveyDaoImpl().load(surveyId); |
671 | jmachado | 94 | survey.setIdentifier(identifier); |
95 | |||
96 | boolean deleted = file.delete(); |
||
97 | if(!deleted) |
||
98 | logger.info("Error deleting file: "+filename); |
||
99 | } |
||
100 | catch (1.5.0/docs/api/java/io/IOException.html">IOException e) |
||
101 | { |
||
102 | logger.info(e); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | return null; |
||
107 | } |
||
108 | |||
109 | private void writeCsvHeader(1.5.0/docs/api/java/io/OutputStreamWriter.html">OutputStreamWriter writer, 1.5.0/docs/api/java/lang/String.html">String[] values) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
110 | { |
||
111 | writeCsvData(writer,values); |
||
112 | } |
||
113 | |||
114 | private void writeCsvData(1.5.0/docs/api/java/io/OutputStreamWriter.html">OutputStreamWriter writer, 1.5.0/docs/api/java/lang/String.html">String[] values) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
115 | { |
||
116 | for(int i = 0; i < values.length; i++) |
||
117 | { |
||
118 | if(i > 0) |
||
119 | writer.write(";"); |
||
120 | |||
121 | if(values[i] != null) |
||
122 | writer.write(values[i]); |
||
123 | } |
||
124 | writer.write("\n"); |
||
125 | } |
||
126 | |||
127 | private void getAllBackwardNodesRecursive(List<String> msgs, SurveyStructuralNode node) |
||
128 | { |
||
129 | if(node != null) |
||
130 | { |
||
131 | msgs.add(node.getMsg()); |
||
132 | getAllBackwardNodesRecursive(msgs,node.getParentNode()); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | private List<String> getAllBackwardNodes(SurveyStructuralNode leaf) |
||
137 | { |
||
138 | if(leaf != null) |
||
139 | { |
||
140 | List<String> msgs = new ArrayList<String>(); |
||
141 | getAllBackwardNodesRecursive(msgs, leaf); |
||
142 | return msgs; |
||
143 | } |
||
144 | return null; |
||
145 | } |
||
146 | |||
147 | private 1.5.0/docs/api/java/lang/String.html">String normalizeNodesNames(List<String> names) |
||
148 | { |
||
149 | 1.5.0/docs/api/java/lang/String.html">String normalizedName = ""; |
||
150 | for(int i=names.size()-1; i>=0; i--) |
||
151 | { |
||
152 | if(i < names.size()-1 && i >= 0) |
||
153 | normalizedName = normalizedName + "/"; |
||
154 | normalizedName = normalizedName + names.get(i); |
||
155 | } |
||
156 | return normalizedName; |
||
157 | } |
||
158 | } |