Subversion Repositories bacoAlunos

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1353 jmachado 1
package pt.estgp.estgweb.services.courses;
2
 
3
import jomm.dao.impl.AbstractDao;
4
import org.apache.log4j.Logger;
5
import org.hibernate.Query;
6
import org.json.JSONArray;
7
import pt.estgp.estgweb.domain.CourseUnit;
8
import pt.estgp.estgweb.domain.CourseUnitImpl;
9
import pt.estgp.estgweb.domain.dao.DaoFactory;
10
import pt.estgp.estgweb.domain.views.CourseUnitView;
11
import pt.estgp.estgweb.services.expceptions.ServiceException;
12
import pt.estgp.estgweb.services.jobs.ServiceJob;
13
import pt.estgp.estgweb.services.logresults.ILogMessages;
14
import pt.estgp.estgweb.services.logresults.LogMessageTypeEnum;
15
import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessage;
16
import pt.estgp.estgweb.services.logresults.impl.DefaultLogMessages;
17
import pt.utl.ist.berserk.logic.serviceManager.IService;
18
 
19
import java.io.IOException;
20
import java.util.List;
21
 
22
 
23
/**
24
 * Created by jorgemachado on 22/03/16.
25
 */
26
public class CourseUnitsStatisticsJobService extends ServiceJob implements IService
27
{
28
 
29
    public 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(CourseUnitsStatisticsJobService.class);
30
 
31
    private static final int MAX_COMMIT = 20;
32
 
33
    public ILogMessages run(1.5.0/docs/api/java/lang/String.html">String year, int institutionCode) throws ServiceException
34
    {
35
        DefaultLogMessages logMessages = new DefaultLogMessages();
36
        logMessages.addMessage(new DefaultLogMessage("courseunits.statistics", LogMessageTypeEnum.INFO, "instituicao " + institutionCode + " ano : "  + year));
37
        logger.info(logMessages.getLastMessage());
38
        serviceLogInfo(logMessages.getLastMessage());
39
 
40
        int foundTotal = 0;
41
 
42
 
43
        1.5.0/docs/api/javax/management/Query.html">Query q = AbstractDao.getCurrentSession().createQuery("select count(c.id) from c in class " + CourseUnit.class.getName() + " where c.institutionCode = " + institutionCode + " and c.importYear = '" + year + "'");
44
        1.5.0/docs/api/java/lang/Long.html">Long count = (1.5.0/docs/api/java/lang/Long.html">Long) q.list().get(0);
45
 
46
        q = AbstractDao.getCurrentSession().createQuery("select c.id from c in class " + CourseUnit.class.getName() + " where c.institutionCode = " + institutionCode + " and c.importYear = '" + year + "'");
47
 
48
        IFilesServiceFacade filesServiceFacade = null;
49
        try {
50
            filesServiceFacade = CourseUnitFilesFacadeFactory.createCourseUnitFilesFacade();
51
        } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
52
            logger.error(e, e);
53
            //AbstractDao.getCurrentSession().getTransaction().rollback();
54
            logMessages.addMessage(new DefaultLogMessage("courseunits.statistics.error", e.toString(), "see log for details", LogMessageTypeEnum.ERROR));
55
            serviceLogError(logMessages.getLastMessage());
56
        }
57
        List<Long> ids = q.list();
58
        int i = 0;
59
        for(1.5.0/docs/api/java/lang/Long.html">Long id: ids)
60
        {
61
            foundTotal++;
62
            if (i++ > MAX_COMMIT)
63
            {
64
                i = 0;
65
                setProgress((int) (((float)foundTotal)/((float)count)*100.0f));
66
                commitPartially();
67
            }
68
            try
69
            {
70
                CourseUnitImpl cu = (CourseUnitImpl) DaoFactory.getCourseUnitDaoImpl().load(id);
71
                1.5.0/docs/api/java/lang/String.html">String msg = "Unit: " + cu.getCode() + " " + cu.getName() + " " + cu.getCourseName() + " (" + cu.getCourseCode() + ")";
72
                logger.info(msg);
73
                serviceLogInfo(msg);
74
 
75
                if(cu.getAnnouncements() != null)
76
                    cu.setStatanuncios(cu.getAnnouncements().size());
77
                if(cu.getBlogs()!=null && cu.getBlogs().size() > 0)
78
                    cu.setStatblogPosts(cu.getBlogs().iterator().next().getPosts().size());
79
                if(cu.getStudents() != null)
80
                    cu.setStatstudentsCount(cu.getStudents().size());
81
                if(cu.getTeachers() != null)
82
                    cu.setStatteachersCount(cu.getTeachers().size());
83
                if(cu.getTeachers() != null)
84
                    cu.setStatteachersCount(cu.getTeachers().size());
85
                if(cu.getAssignements() != null)
86
                    cu.setStatworks(cu.getAssignements().size());
87
 
88
                CourseUnitView courseUnitView = new CourseUnitView(cu);
89
                if(courseUnitView.getSummariesFromJson() != null)
90
                {
91
                    cu.setStatdtpSumaries(courseUnitView.getSummariesFromJson().size());
92
                    cu.setStatdtpSumariesMissing(courseUnitView.getLateSummaries());
93
                    cu.setStatdtpSumariesPrelancados(courseUnitView.getPrelancadosSummaries());
94
                }
95
 
96
                cu.setStatdtpFichaCurricularValid(cu.isValidProgram());
97
                if(cu.getCourseUnitEvaluation() == null)
98
                    cu.setStatdtpEvaluationReportState("INEXISTENTE");
99
                else if(cu.getCourseUnitEvaluation().isClosed())
100
                    cu.setStatdtpEvaluationReportState("VALIDO");
101
                else if(cu.getCourseUnitEvaluation().isTeacherComplete())
102
                    cu.setStatdtpEvaluationReportState("ENTREGUE");
103
                else if(cu.getEvaluationStreamId() != null)
104
                    cu.setStatdtpEvaluationReportState("INICIADO");
105
                else
106
                    cu.setStatdtpEvaluationReportState("INEXISTENTE");
107
 
108
 
109
                if(filesServiceFacade != null)
110
                {
111
                    filesServiceFacade.reset(cu);
112
                    try
113
                    {
114
                        cu.setStatcontentsFiles((int) filesServiceFacade.getContentsNumberOfFiles());
115
                        cu.setStatcontentsFolders((int) filesServiceFacade.getContentsNumberOfFolders());
116
                        cu.setStatcontentsLastUpdate(filesServiceFacade.getContentsLastUpdate());
117
                        cu.setStatcontentsSize(filesServiceFacade.getContentsSize());
118
 
119
                        cu.setStatdtpPlaneamentoFiles((int) filesServiceFacade.getDtpPlaneamentoNumberOfFiles());
120
                        cu.setStatdtpPlaneamentoLastUpdate(filesServiceFacade.getDtpPlaneamentoLastUpdate());
121
 
122
 
123
                        cu.setStatdtpFichaCurricularLastUpdate(filesServiceFacade.getDtpFichaCurricularLastUpdate());
124
                        cu.setStatdtpEvaluationReportLastUpdate(filesServiceFacade.getDtpRelatorioAvaliacaoLastUpdate());
125
 
126
 
127
                        cu.setStatdtpInqueritoPedagogicoFiles((int) filesServiceFacade.getDtpInqueritoPedagogicoNumberOfFiles());
128
                        cu.setStatdtpInqueritoPedagogicoFilesLastUpdate(filesServiceFacade.getDtpInqueritoPedagogicoLastUpdate());
129
 
130
                        cu.setStatdtpAvaliacaoEnunciadosFiles((int) filesServiceFacade.getDtpAvaliacaoEnunciadosNumberOfFiles());
131
                        cu.setStatdtpAvaliacaoEnunciadosLastUpdate(filesServiceFacade.getDtpAvaliacaoEnunciadosLastUpdate());
132
 
133
                        cu.setStatdtpAvaliacaoPautasFiles((int) filesServiceFacade.getDtpAvaliacaoNotasNumberOfFiles());
134
                        cu.setStatdtpAvaliacaoPautasLastUpdate(filesServiceFacade.getDtpAvaliacaoNotasLastUpdate());
135
 
136
                        JSONArray files = filesServiceFacade.getAllFilesJsonFields();
137
                        for(int j = 0; j < files.length();j++ )
138
                        {
139
                            files.getJSONObject(j).put("id","baco/courseunit/" + cu.getId() + "/" + files.getJSONObject(j).getString("file"));
140
                            files.getJSONObject(j).put("obj_sub_type","baco/courseunit:" + cu.getId());
141
                            files.getJSONObject(j).put("obj_type","baco/courseunit:file");
142
                        }
143
                        cu.setFilesJson(files.toString());
144
                    }
145
                    catch(1.5.0/docs/api/java/lang/Exception.html">Exception e)
146
                    {   logger.warn(e, e);
147
                        //AbstractDao.getCurrentSession().getTransaction().rollback();
148
                        logMessages.addMessage(new DefaultLogMessage("courseunits.statistics.warning", e.toString(), "see log for details", LogMessageTypeEnum.ERROR));
149
                        serviceLogWarn(logMessages.getLastMessage());
150
                    }
151
                 }
152
 
153
 
154
 
155
 
156
            }
157
            catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e)
158
            {
159
                logger.error(e, e);
160
                //AbstractDao.getCurrentSession().getTransaction().rollback();
161
                logMessages.addMessage(new DefaultLogMessage("courseunits.statistics.error", e.toString(), "see log for details", LogMessageTypeEnum.ERROR));
162
                serviceLogError(logMessages.getLastMessage());
163
            }
164
        }
165
 
166
        if(filesServiceFacade != null)
167
            try {
168
                filesServiceFacade.close();
169
            } catch (1.5.0/docs/api/java/io/IOException.html">IOException e) {
170
                logger.error(e, e);
171
                //AbstractDao.getCurrentSession().getTransaction().rollback();
172
                logMessages.addMessage(new DefaultLogMessage("courseunits.statistics.error", e.toString(), "see log for details", LogMessageTypeEnum.ERROR));
173
                serviceLogError(logMessages.getLastMessage());
174
            }
175
 
176
        setProgress(100);
177
        logger.info("######################################");
178
        serviceLogInfo("######################################");
179
        logger.info("######################################");
180
        serviceLogInfo("######################################");
181
        logger.info("STATS " + year);
182
        serviceLogInfo("STATS " + year);
183
        logger.info("Total Notas: " + foundTotal);
184
        serviceLogInfo("Total Notas: " + foundTotal);
185
 
186
 
187
        logMessages.addMessage(new DefaultLogMessage("courseunits.statistics.terminating", LogMessageTypeEnum.INFO));
188
        logger.info(logMessages.getLastMessage());
189
        serviceLogInfo(logMessages.getLastMessage());
190
        return logMessages;
191
    }
192
 
193
    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws ServiceException
194
    {
195
        1.5.0/docs/api/java/lang/String.html">String year = DaoFactory.getConfigurationDaoImpl().getImportsDefaultImportYearCreateTransaction();;
196
        1.5.0/docs/api/java/lang/String.html">String institutionCode = "1";
197
        if(args != null && args.length > 0)
198
            year = args[0];
199
        if(args != null && args.length > 1)
200
            institutionCode = args[1];
201
        AbstractDao.getCurrentSession().beginTransaction();
202
        new CourseUnitsStatisticsJobService().run(year,1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(institutionCode));
203
        AbstractDao.getCurrentSession().getTransaction().commit();
204
    }
205
 
206
    @1.5.0/docs/api/java/lang/Override.html">Override
207
    protected ILogMessages runJobServiceTask() throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable {
208
        1.5.0/docs/api/java/lang/String.html">String importYear =  getParametersMap().get(JOB_importYear_KEY).getObject();
209
        1.5.0/docs/api/java/lang/String.html">String institutionCode = getParametersMap().get(JOB_institution_KEY).getObject();
210
        return run(importYear,1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(institutionCode));
211
    }
212
}