package pt.estgp.estgweb.services.surveys;
import jomm.utils.FilesUtils;
import org.apache.log4j.Logger;
import pt.estgp.estgweb.Globals;
import pt.estgp.estgweb.domain.Survey;
import pt.estgp.estgweb.domain.SurveyResponse;
import pt.estgp.estgweb.domain.SurveyStructuralNode;
import pt.estgp.estgweb.domain.UserSession;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.filters.chains.ResourceAccessControlEnum;
import pt.estgp.estgweb.services.data.RepositoryService;
import pt.utl.ist.berserk.logic.serviceManager.IService;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author: Duarte Santos
* @date: 15-08-2011
* @time: 13:22
* @email: a12564 [at] estgp [dot] pt
*/
public class GenerateSurveyStatsCsvService
implements IService
{
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);
private static RepositoryService repositoryService =
new RepositoryService
();
public 1.5.0/docs/api/java/lang/String.html">String run
(long surveyId, UserSession userSession
)
{
if(surveyId
<= 0
) return null;
Survey survey = DaoFactory.
getSurveyDaoImpl().
load(surveyId
);
if(survey
!=
null)
{
try
{
1.5.0/docs/api/java/lang/String.html">String filename = Globals.
TMP_DIR +
"/survey_"+survey.
getId()+
".csv";
1.5.0/docs/api/java/io/File.html">File file =
new 1.5.0/docs/api/java/io/File.html">File(filename
);
1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream fout =
new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(file
);
fout.
write(239
);
fout.
write(187
);
fout.
write(191
);
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");
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"};
writeCsvHeader
(writer,csvHeader
);
5+0%2Fdocs%2Fapi+List">List list = DaoFactory.
getSurveyQuestionAnswerDaoImpl().
loadForCsv(survey.
getId());
1.5.0/docs/api/java/util/Iterator.html">Iterator iter = list.
iterator();
while(iter.
hasNext())
{
5+0%2Fdocs%2Fapi+Object">Object[] objs =
(5+0%2Fdocs%2Fapi+Object">Object[]) iter.
next();
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
];
int i
;
for(i =
0; i
< objs.
length; i++
)
{
if(objs
[i
] !=
null)
values
[i
] = objs
[i
].
toString();
else
values
[i
] =
"";
}
long responseId =
1.5.0/docs/api/java/lang/Long.html">Long.
parseLong(values
[0
]);
if(responseId
> 0
)
{
SurveyResponse r = DaoFactory.
getSurveyResponseDaoImpl().
load(responseId
);
if(r
!=
null)
{
List
<String
> nodeNames = getAllBackwardNodes
(r.
getStructuralNode());
1.5.0/docs/api/java/lang/String.html">String normalizeNodesNames = normalizeNodesNames
(nodeNames
);
values
[i
] = normalizeNodesNames
;
}
}
writeCsvData
(writer,values
);
}
writer.
flush();
writer.
close();
1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.
getExtension(filename
);
1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream inputStream =
new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(file
);
1.5.0/docs/api/java/lang/String.html">String identifier = survey.
getIdentifier();
if(identifier ==
null)
identifier = repositoryService.
storeRepositoryFile(inputStream,
"text/comma-separated-values",extension,
(int)file.
length(),file.
getName(),
"CSV file of survey: "+survey.
getId(), ResourceAccessControlEnum.
surveyStatsDomain,
null, userSession
);
else
repositoryService.
updateRepositoryFile(identifier,inputStream,
"text/comma-separated-values",extension,
(int)file.
length(),file.
getName(),
"CSV file of survey: "+survey.
getId(),ResourceAccessControlEnum.
surveyStatsDomain);
survey = DaoFactory.
getSurveyDaoImpl().
load(surveyId
);
survey.
setIdentifier(identifier
);
boolean deleted = file.
delete();
if(!deleted
)
logger.
info("Error deleting file: "+filename
);
}
catch (1.5.0/docs/api/java/io/IOException.html">IOException e
)
{
logger.
info(e
);
}
}
return null;
}
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
{
writeCsvData
(writer,values
);
}
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
{
for(int i =
0; i
< values.
length; i++
)
{
if(i
> 0)
writer.
write(";");
if(values
[i
] !=
null)
writer.
write(values
[i
]);
}
writer.
write("\n");
}
private void getAllBackwardNodesRecursive
(List
<String
> msgs, SurveyStructuralNode node
)
{
if(node
!=
null)
{
msgs.
add(node.
getMsg());
getAllBackwardNodesRecursive
(msgs,node.
getParentNode());
}
}
private List
<String
> getAllBackwardNodes
(SurveyStructuralNode leaf
)
{
if(leaf
!=
null)
{
List
<String
> msgs =
new ArrayList
<String
>();
getAllBackwardNodesRecursive
(msgs, leaf
);
return msgs
;
}
return null;
}
private 1.5.0/docs/api/java/lang/String.html">String normalizeNodesNames
(List
<String
> names
)
{
1.5.0/docs/api/java/lang/String.html">String normalizedName =
"";
for(int i=names.
size()-
1; i
>=
0; i--
)
{
if(i
< names.
size()-
1 && i
>=
0)
normalizedName = normalizedName +
"/";
normalizedName = normalizedName + names.
get(i
);
}
return normalizedName
;
}
}