Subversion Repositories bacoAlunos

Rev

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