package pt.estgp.estgweb.services.questionarios;
import jomm.dao.impl.AbstractDao;
import jomm.utils.StreamsUtils;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.json.JSONException;
import org.json.JSONObject;
import pt.estgp.estgweb.domain.*;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.services.jobs.JobDeamon;
import pt.estgp.estgweb.services.jobs.ServiceJob;
import pt.estgp.estgweb.services.questionarios.pedagogico.reportprocessors.utils.DataTable;
import pt.estgp.estgweb.utils.ConfigProperties;
import pt.estgp.estgweb.utils.Globals;
import pt.estgp.estgweb.web.exceptions.NotAuthorizedException;
import pt.estgp.estgweb.web.form.questionarios.widgets.ReportAdminAlunosWidgetForm;
import pt.utl.ist.berserk.logic.serviceManager.IService;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.
hibernate.
criterion.
Restrictions.
eq;
import static org.
hibernate.
criterion.
Restrictions.
like;
/**
* Created by jorgemachado on 21/03/16.
*/
public class QuestionariosReportsService
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(QuestionariosReportsService.
class);
public void saveReportGroup
(1.5.0/docs/api/java/lang/String.html">String title,
1.5.0/docs/api/java/lang/String.html">String classType, UserSession userSession
) throws 1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException,
1.5.0/docs/api/java/lang/IllegalAccessException.html">IllegalAccessException,
1.5.0/docs/api/java/lang/InstantiationException.html">InstantiationException
{
QuestionarioReportFileGroup reportFileGroup =
(QuestionarioReportFileGroup
) 1.5.0/docs/api/java/lang/Class.html">Class.
forName(classType
).
newInstance();
reportFileGroup.
setTitle(title
);
1.5.0/docs/api/java/lang/String.html">String processorClassForGroup = ConfigProperties.
getProperty("questionarios.processor." + classType
);
reportFileGroup.
setReportProcessorClass(processorClassForGroup
);
DaoFactory.
getQuestionarioReportFileGroupDaoImpl().
save(reportFileGroup
);
}
public void updateReportGroup
(long reportGroupId,
1.5.0/docs/api/java/lang/String.html">String title,
boolean active, UserSession userSession
) throws 1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException,
1.5.0/docs/api/java/lang/IllegalAccessException.html">IllegalAccessException,
1.5.0/docs/api/java/lang/InstantiationException.html">InstantiationException
{
QuestionarioReportFileGroupImpl reportGroup =
(QuestionarioReportFileGroupImpl
) DaoFactory.
getQuestionarioReportFileGroupDaoImpl().
load(reportGroupId
);
reportGroup.
setTitle(title
);
reportGroup.
setActive(active
);
}
public void updateReportFile
(long reportFileId,
boolean active, UserSession userSession
) throws 1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException,
1.5.0/docs/api/java/lang/IllegalAccessException.html">IllegalAccessException,
1.5.0/docs/api/java/lang/InstantiationException.html">InstantiationException
{
QuestionarioReportFile reportFile = DaoFactory.
getQuestionarioReportFileDaoImpl()
.
load(reportFileId
);
reportFile.
setActive(active
);
}
public QuestionarioReportFileGroupImpl runJobGenerate
(long reportGroupId,
1.5.0/docs/api/java/lang/String.html">String title,
boolean active,
1.5.0/docs/api/java/lang/String.html">String generateSigesCode, UserSession session
) throws NotAuthorizedException,
1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException {
QuestionarioReportFileGroupImpl reportGroup =
(QuestionarioReportFileGroupImpl
) DaoFactory.
getQuestionarioReportFileGroupDaoImpl().
load(reportGroupId
);
reportGroup.
setTitle(title
);
reportGroup.
setActive(active
);
if(reportGroup.
getReportGenerationTaskId() > 0
)
{
JobServiceTask serviceTask = DaoFactory.
getJobServiceTaskDaoImpl().
load(reportGroup.
getReportGenerationTaskId());
if(!serviceTask.
isExecuted())
{
//test 5 minutes
logger.
info("Tarefa já em execução");
if(1.5.0/docs/api/java/lang/System.html">System.
currentTimeMillis() - serviceTask.
getUpdateDate().
getTime() < (60000
*5
))
return reportGroup
;
}
}
Set
<JobServiceTaskParameter
> paramsJob =
new HashSet
<JobServiceTaskParameter
>();
JobServiceTaskParameterImpl paramJob = DomainObjectFactory.
createJobServiceTaskParameterImpl();
paramJob.
setName(ServiceJob.
JOB_questionario_report_id_KEY);
paramJob.
setDescription("ID do Report Group");
paramJob.
setObject("" + reportGroupId
);
paramsJob.
add(paramJob
);
if(generateSigesCode
!=
null && generateSigesCode.
trim().
length()>0
)
{
JobServiceTaskParameterImpl paramJobSigesCode = DomainObjectFactory.
createJobServiceTaskParameterImpl();
paramJobSigesCode.
setName(ServiceJob.
JOB_questionario_sigesCode_KEY);
paramJobSigesCode.
setDescription("Gerar apenas este SigesCode");
paramJobSigesCode.
setObject(generateSigesCode
);
paramsJob.
add(paramJobSigesCode
);
}
1.5.0/docs/api/java/lang/Class.html">Class reportGeneratorServiceClass =
1.5.0/docs/api/java/lang/Class.html">Class.
forName(reportGroup.
getReportProcessorClass());
JobServiceTask job = JobDeamon.
createServiceJob(reportGeneratorServiceClass, session.
getUser(), paramsJob,
"Service: Geração de Reports para o group " + reportGroupId +
" - " + reportGroup.
getTitle());
reportGroup.
setReportGenerationTaskId(job.
getId());
return reportGroup
;
}
/*************WIDGET ALUNOS REPORTS***************/
public void saveReportGroupAlunosParameters
(ReportAdminAlunosWidgetForm form, UserSession userSession
) throws 1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException,
1.5.0/docs/api/java/lang/IllegalAccessException.html">IllegalAccessException,
1.5.0/docs/api/java/lang/InstantiationException.html">InstantiationException
{
QuestionarioReportFileGroupAnoImpl reportGroup =
(QuestionarioReportFileGroupAnoImpl
) DaoFactory.
getQuestionarioReportFileGroupAnoDaoImpl().
load(form.
getQuestionarioReportGroupId());
List
<String
> periodos =
new ArrayList
<String
>();
List
<String
> degrees =
new ArrayList
<String
>();
reportGroup.
setImportYear(form.
getAno());
if(form.
isAnuais())
periodos.
add("A");
if(form.
isSemestre1())
periodos.
add("S1");
if(form.
isSemestre2())
periodos.
add("S2");
if(form.
isTrimestre1())
periodos.
add("T1");
if(form.
isTrimestre2())
periodos.
add("T2");
if(form.
isTrimestre3())
periodos.
add("T3");
if(form.
isTrimestre4())
periodos.
add("T4");
if(form.
isLicenciaturas())
degrees.
add("L");
if(form.
isMestrados())
degrees.
add("M");
if(form.
isTesps())
degrees.
add("T");
if(form.
isCets())
degrees.
add("E");
reportGroup.
setPeriodosList(periodos
);
reportGroup.
setDegreesList(degrees
);
}
/****************************************************************************/
/* SERVICOS WS-API
/****************************************************************************/
/**
*
* @param sigesCode
* @param year
* @param periodIncluded
* @return an encoded DataTable in JSON (pt.estgp.estgweb.services.questionarios.pedagogico.reportprocessors.utils.DataTable)
* @throws IOException
*/
public 1.5.0/docs/api/java/lang/String.html">String loadReportUnidadesSalasJson
(1.5.0/docs/api/java/lang/String.html">String sigesCode,
1.5.0/docs/api/java/lang/String.html">String year,
1.5.0/docs/api/java/lang/String.html">String periodIncluded
) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException
{
if(Globals.
SYSTEM_WSJSON_SERVICE_COURSE_REPORT_RESULTS.
indexOf("localhost")>=0
)
{
Criteria c = DaoFactory.
getQuestionarioReportCursoFileDaoImpl().
createCriteria();
c.
createAlias("reportGroup",
"rg");
c.
add(like
("rg.periodos",
"%" + periodIncluded +
"%"));
c.
add(eq
("rg.importYear",year
));
c.
add(eq
("entityCode",sigesCode
));
List
<QuestionarioReportCursoFile
> files = c.
list();
if(files.
size() == 0
)
{
return "{}";
}
return files.
get(0
).
getCourseReportQuestionsJson();
}
else
{
1.5.0/docs/api/java/net/URL.html">URL url =
new 1.5.0/docs/api/java/net/URL.html">URL(Globals.
SYSTEM_WSJSON_SERVICE_COURSE_REPORT_RESULTS +
"?code=" + sigesCode +
"&year=" + year
);
5+0%2Fdocs%2Fapi+InputStream">InputStream is = url.
openStream();
1.5.0/docs/api/java/lang/String.html">String json = StreamsUtils.
readString(is
);
is.
close();
JSONObject jsonObject =
new JSONObject
(json
);
return jsonObject.
getString("response").
toString();
}
}
public static void main
(1.5.0/docs/api/java/lang/String.html">String[] args
) throws 1.5.0/docs/api/java/io/IOException.html">IOException, JSONException
{
AbstractDao.
getCurrentSession().
beginTransaction();
1.5.0/docs/api/java/lang/String.html">String json =
new QuestionariosReportsService
().
loadReportUnidadesSalasJson("9119",
"201516",
"S2");
DataTable d = DataTable.
fromJson(json
);
1.5.0/docs/api/java/lang/System.html">System.
out.
println(d.
toJson());
1.5.0/docs/api/java/lang/System.html">System.
out.
println(d.
toJson().
equals(json
));
AbstractDao.
getCurrentSession().
getTransaction().
commit();
}
}