Subversion Repositories bacoAlunos

Rev

Rev 1830 | Rev 1840 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1579 jmachado 1
package pt.estgp.estgweb.services.questionarios;
2
 
1830 jmachado 3
import jomm.dao.impl.AbstractDao;
4
import jomm.utils.StreamsUtils;
1579 jmachado 5
import org.apache.log4j.Logger;
1830 jmachado 6
import org.hibernate.Criteria;
7
import org.json.JSONException;
8
import org.json.JSONObject;
1579 jmachado 9
import pt.estgp.estgweb.domain.*;
10
import pt.estgp.estgweb.domain.dao.DaoFactory;
11
import pt.estgp.estgweb.services.jobs.JobDeamon;
12
import pt.estgp.estgweb.services.jobs.ServiceJob;
1830 jmachado 13
import pt.estgp.estgweb.services.questionarios.pedagogico.reportprocessors.utils.DataTable;
1579 jmachado 14
import pt.estgp.estgweb.utils.ConfigProperties;
1830 jmachado 15
import pt.estgp.estgweb.utils.Globals;
1579 jmachado 16
import pt.estgp.estgweb.web.exceptions.NotAuthorizedException;
17
import pt.estgp.estgweb.web.form.questionarios.widgets.ReportAdminAlunosWidgetForm;
18
import pt.utl.ist.berserk.logic.serviceManager.IService;
19
 
1830 jmachado 20
import java.io.IOException;
21
import java.io.InputStream;
22
import java.net.URL;
1579 jmachado 23
import java.util.ArrayList;
24
import java.util.HashSet;
25
import java.util.List;
26
import java.util.Set;
27
 
1830 jmachado 28
import static org.hibernate.criterion.Restrictions.eq;
29
import static org.hibernate.criterion.Restrictions.like;
30
 
1579 jmachado 31
/**
32
 * Created by jorgemachado on 21/03/16.
33
 */
34
public class QuestionariosReportsService implements IService
35
{
36
 
37
    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);
38
 
39
 
1830 jmachado 40
 
41
 
1579 jmachado 42
    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
43
    {
44
        QuestionarioReportFileGroup reportFileGroup = (QuestionarioReportFileGroup) 1.5.0/docs/api/java/lang/Class.html">Class.forName(classType).newInstance();
45
        reportFileGroup.setTitle(title);
46
        1.5.0/docs/api/java/lang/String.html">String processorClassForGroup = ConfigProperties.getProperty("questionarios.processor." + classType);
47
        reportFileGroup.setReportProcessorClass(processorClassForGroup);
1586 jmachado 48
        DaoFactory.getQuestionarioReportFileGroupDaoImpl().save(reportFileGroup);
1579 jmachado 49
    }
50
 
1586 jmachado 51
    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
1580 jmachado 52
    {
53
        QuestionarioReportFileGroupImpl reportGroup = (QuestionarioReportFileGroupImpl) DaoFactory.getQuestionarioReportFileGroupDaoImpl().load(reportGroupId);
54
        reportGroup.setTitle(title);
1586 jmachado 55
        reportGroup.setActive(active);
1580 jmachado 56
    }
1579 jmachado 57
 
1586 jmachado 58
    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
59
    {
60
        QuestionarioReportFile reportFile = DaoFactory.getQuestionarioReportFileDaoImpl()
61
                .load(reportFileId);
62
        reportFile.setActive(active);
63
    }
1580 jmachado 64
 
1830 jmachado 65
    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 {
1586 jmachado 66
 
1579 jmachado 67
        QuestionarioReportFileGroupImpl reportGroup = (QuestionarioReportFileGroupImpl) DaoFactory.getQuestionarioReportFileGroupDaoImpl().load(reportGroupId);
1580 jmachado 68
        reportGroup.setTitle(title);
1586 jmachado 69
        reportGroup.setActive(active);
1629 jmachado 70
 
71
        if(reportGroup.getReportGenerationTaskId() > 0)
72
        {
73
            JobServiceTask serviceTask = DaoFactory.getJobServiceTaskDaoImpl().load(reportGroup.getReportGenerationTaskId());
74
            if(!serviceTask.isExecuted())
75
            {
76
                //test 5 minutes
77
                logger.info("Tarefa já em execução");
78
                if(1.5.0/docs/api/java/lang/System.html">System.currentTimeMillis() - serviceTask.getUpdateDate().getTime() < (60000*5))
79
                    return reportGroup;
80
            }
81
        }
1579 jmachado 82
        Set<JobServiceTaskParameter> paramsJob = new HashSet<JobServiceTaskParameter>();
83
 
84
        JobServiceTaskParameterImpl paramJob = DomainObjectFactory.createJobServiceTaskParameterImpl();
85
        paramJob.setName(ServiceJob.JOB_questionario_report_id_KEY);
86
        paramJob.setDescription("ID do Report Group");
87
        paramJob.setObject("" + reportGroupId);
88
        paramsJob.add(paramJob);
89
 
1830 jmachado 90
        if(generateSigesCode != null && generateSigesCode.trim().length()>0)
91
        {
92
            JobServiceTaskParameterImpl paramJobSigesCode = DomainObjectFactory.createJobServiceTaskParameterImpl();
93
            paramJobSigesCode.setName(ServiceJob.JOB_questionario_sigesCode_KEY);
94
            paramJobSigesCode.setDescription("Gerar apenas este SigesCode");
95
            paramJobSigesCode.setObject(generateSigesCode);
96
            paramsJob.add(paramJobSigesCode);
97
        }
98
 
1579 jmachado 99
        1.5.0/docs/api/java/lang/Class.html">Class reportGeneratorServiceClass = 1.5.0/docs/api/java/lang/Class.html">Class.forName(reportGroup.getReportProcessorClass());
100
 
101
        JobServiceTask job = JobDeamon.createServiceJob(reportGeneratorServiceClass, session.getUser(), paramsJob,
102
                "Service: Geração de Reports para o group " + reportGroupId + " - " + reportGroup.getTitle());
103
        reportGroup.setReportGenerationTaskId(job.getId());
104
 
105
        return reportGroup;
106
    }
107
 
108
 
109
    /*************WIDGET ALUNOS REPORTS***************/
110
 
111
    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
112
    {
113
        QuestionarioReportFileGroupAnoImpl reportGroup = (QuestionarioReportFileGroupAnoImpl) DaoFactory.getQuestionarioReportFileGroupAnoDaoImpl().load(form.getQuestionarioReportGroupId());
114
        List<String> periodos = new ArrayList<String>();
115
        List<String> degrees = new ArrayList<String>();
116
        reportGroup.setImportYear(form.getAno());
117
 
118
        if(form.isAnuais())
119
            periodos.add("A");
120
        if(form.isSemestre1())
121
            periodos.add("S1");
122
        if(form.isSemestre2())
123
            periodos.add("S2");
124
        if(form.isTrimestre1())
125
            periodos.add("T1");
126
        if(form.isTrimestre2())
127
            periodos.add("T2");
128
        if(form.isTrimestre3())
129
            periodos.add("T3");
130
        if(form.isTrimestre4())
131
            periodos.add("T4");
132
 
133
        if(form.isLicenciaturas())
134
            degrees.add("L");
135
        if(form.isMestrados())
136
            degrees.add("M");
137
        if(form.isTesps())
138
            degrees.add("T");
139
        if(form.isCets())
140
            degrees.add("E");
141
 
142
 
143
        reportGroup.setPeriodosList(periodos);
144
        reportGroup.setDegreesList(degrees);
145
 
146
 
147
    }
148
 
149
 
150
 
151
 
1830 jmachado 152
    /****************************************************************************/
153
    /* SERVICOS WS-API
154
    /****************************************************************************/
155
 
156
 
157
    /**
158
     *
159
     * @param sigesCode
160
     * @param year
161
     * @param periodIncluded
162
     * @return an encoded DataTable in JSON (pt.estgp.estgweb.services.questionarios.pedagogico.reportprocessors.utils.DataTable)
163
     * @throws IOException
164
     */
165
    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 {
166
 
167
 
168
        if(Globals.SYSTEM_WSJSON_SERVICE_COURSE_REPORT_RESULTS.indexOf("localhost")>=0)
169
        {
170
            Criteria c = DaoFactory.getQuestionarioReportCursoFileDaoImpl().createCriteria();
171
            c.createAlias("reportGroup","rg");
172
            c.add(like("rg.periodos","%" + periodIncluded + "%"));
173
            c.add(eq("rg.importYear",year));
174
            c.add(eq("entityCode",sigesCode));
175
            List<QuestionarioReportCursoFile> files = c.list();
176
            if(files.size() == 0)
177
            {
178
                return "{}";
179
            }
1836 jmachado 180
            if(files.get(0).getCourseReportQuestionsJson() == null)
181
                return "{}";
1830 jmachado 182
            return files.get(0).getCourseReportQuestionsJson();
183
        }
184
        else
185
        {
186
            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);
187
            5+0%2Fdocs%2Fapi+InputStream">InputStream is = url.openStream();
188
 
189
            1.5.0/docs/api/java/lang/String.html">String json = StreamsUtils.readString(is);
190
            is.close();
191
            JSONObject jsonObject = new JSONObject(json);
1836 jmachado 192
            return jsonObject.get("response").toString();
1830 jmachado 193
 
194
        }
195
 
196
    }
197
 
198
 
199
 
200
 
201
 
202
    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 {
203
        AbstractDao.getCurrentSession().beginTransaction();
204
        1.5.0/docs/api/java/lang/String.html">String json = new QuestionariosReportsService().loadReportUnidadesSalasJson("9119", "201516", "S2");
205
 
206
        DataTable d = DataTable.fromJson(json);
207
        1.5.0/docs/api/java/lang/System.html">System.out.println(d.toJson());
208
 
209
        1.5.0/docs/api/java/lang/System.html">System.out.println(d.toJson().equals(json));
210
 
211
 
212
        AbstractDao.getCurrentSession().getTransaction().commit();
213
    }
214
 
215
 
216
 
217
 
1579 jmachado 218
}