Rev 1310 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
207 | jmachado | 1 | package pt.estgp.estgweb.services.courseunits; |
2 | |||
3 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
||
4 | import pt.estgp.estgweb.domain.views.CourseUnitView; |
||
5 | import pt.estgp.estgweb.domain.views.CourseUnitAssignementView; |
||
6 | import pt.estgp.estgweb.domain.views.CourseUnitDeliverableView; |
||
7 | import pt.estgp.estgweb.domain.*; |
||
419 | jmachado | 8 | import pt.estgp.estgweb.domain.enums.LangEnum; |
207 | jmachado | 9 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
10 | import pt.estgp.estgweb.services.expceptions.ServiceException; |
||
11 | import pt.estgp.estgweb.services.data.RepositoryService; |
||
419 | jmachado | 12 | import pt.estgp.estgweb.services.email.SendEmailService; |
13 | import pt.estgp.estgweb.services.email.EMAILJob; |
||
14 | import pt.estgp.estgweb.services.jobs.JobScheduleService; |
||
207 | jmachado | 15 | import pt.estgp.estgweb.filters.chains.ResourceAccessControlEnum; |
208 | jmachado | 16 | import pt.estgp.estgweb.filters.exceptions.AccessDeniedException; |
419 | jmachado | 17 | import pt.estgp.estgweb.utils.Email; |
207 | jmachado | 18 | import org.apache.log4j.Logger; |
19 | |||
20 | import java.util.List; |
||
21 | import java.util.ArrayList; |
||
22 | import java.util.Date; |
||
23 | import java.io.InputStream; |
||
24 | |||
25 | import jomm.utils.FilesUtils; |
||
26 | |||
27 | /* |
||
28 | * @author Goncalo Luiz gedl [AT] rnl [DOT] ist [DOT] utl [DOT] pt |
||
29 | * |
||
30 | * |
||
31 | * Created at 17/Out/2003 , 23:45:24 |
||
32 | * |
||
33 | */ |
||
34 | /** |
||
35 | * @author Jorge Machado |
||
36 | * |
||
37 | * |
||
38 | * Created at 17/Out/2003 , 23:45:24 |
||
39 | * |
||
40 | */ |
||
41 | public class CourseUnitWorkService implements IService |
||
42 | { |
||
43 | |||
44 | RepositoryService repositoryService = new RepositoryService(); |
||
45 | LoadCourseUnitService loadCourseUnitService = new LoadCourseUnitService(); |
||
46 | |||
47 | |||
208 | jmachado | 48 | |
207 | jmachado | 49 | 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(CourseUnitWorkService.class); |
50 | |||
51 | |||
208 | jmachado | 52 | public CourseUnitView create(CourseUnitAssignementView cUV, long id, 5+0%2Fdocs%2Fapi+InputStream">InputStream file, 1.5.0/docs/api/java/lang/String.html">String name, int size, 1.5.0/docs/api/java/lang/String.html">String contentType, UserSession userSession) throws ServiceException |
207 | jmachado | 53 | { |
54 | CourseUnit courseUnit = DaoFactory.getCourseUnitDaoImpl().get(id); |
||
208 | jmachado | 55 | CourseUnitAssignement assignement; |
440 | jmachado | 56 | boolean newWork = true; |
207 | jmachado | 57 | if(cUV.getId() > 0) |
440 | jmachado | 58 | newWork = false; |
59 | if(cUV.getId() > 0) |
||
208 | jmachado | 60 | assignement = DaoFactory.getCourseUnitAssignementDaoImpl().get(cUV.getId()); |
207 | jmachado | 61 | else |
62 | { |
||
208 | jmachado | 63 | assignement = DomainObjectFactory.createCourseUnitAssignementImpl(); |
64 | assignement.setCourseUnit(courseUnit); |
||
236 | jmachado | 65 | DaoFactory.getCourseUnitAssignementDaoImpl().save(assignement); |
207 | jmachado | 66 | } |
208 | jmachado | 67 | cUV.persistViewInObject(assignement); |
68 | |||
69 | if(name != null && name.length() > 0 && size > 0 && file != null) |
||
70 | { |
||
71 | 1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.getExtension(name); |
||
72 | if(assignement.getRepositoryStream() == null || assignement.getRepositoryStream().trim().length() == 0) |
||
73 | { |
||
1703 | jmachado | 74 | 1.5.0/docs/api/java/lang/String.html">String identifier = repositoryService.storeRepositoryFile(file, contentType, extension, size, name, "courseunit.assignement.file " + courseUnit.getName() + " of " + userSession.getUser().getUsername(), ResourceAccessControlEnum.courseunitDomain, null, userSession); |
208 | jmachado | 75 | assignement.setRepositoryStream(identifier); |
76 | } |
||
77 | else |
||
78 | repositoryService.updateRepositoryFile(assignement.getRepositoryStream(), file, contentType, extension, size, name, "courseunit.assignement.file " + courseUnit.getName() + " of " + userSession.getUser().getUsername(), ResourceAccessControlEnum.courseunitDomain); |
||
79 | } |
||
80 | |||
419 | jmachado | 81 | if(assignement.isStatus()) |
440 | jmachado | 82 | trySendWorkEmail(assignement,userSession,newWork); |
207 | jmachado | 83 | return loadCourseUnitWorks(id,userSession); |
84 | } |
||
85 | |||
440 | jmachado | 86 | private void trySendWorkEmail(CourseUnitAssignement assignement, UserSession userSession,boolean newWork) |
419 | jmachado | 87 | { |
88 | CourseUnit c = assignement.getCourseUnit(); |
||
89 | List<User> users = new ArrayList<User>(); |
||
90 | if(c.getStudents() != null) |
||
91 | users.addAll(c.getStudents()); |
||
92 | if(c.getTeachers() != null) |
||
93 | users.addAll(c.getTeachers()); |
||
440 | jmachado | 94 | sendWorkEmail(assignement,c.getNormalizedName(), userSession, users,newWork); |
419 | jmachado | 95 | } |
96 | |||
97 | SendEmailService sendEmailService = new SendEmailService(); |
||
98 | |||
440 | jmachado | 99 | private void sendWorkEmail(CourseUnitAssignement a, 1.5.0/docs/api/java/lang/String.html">String desc, UserSession userSession, List<User> users, boolean newWork) |
419 | jmachado | 100 | { |
440 | jmachado | 101 | Email e = sendEmailService.getCourseUnitWorkEmail(a, LangEnum.PORTUGUESE,userSession,users,newWork); |
419 | jmachado | 102 | EMAILJob emailJob = new EMAILJob(e); |
103 | JobScheduleService.getInstance().scheduleNow(emailJob,"CourseUnit Assignement Email:" + desc,userSession); |
||
104 | } |
||
105 | |||
207 | jmachado | 106 | public CourseUnitView loadCourseUnitWorks(long id, UserSession userSession) throws ServiceException |
107 | { |
||
208 | jmachado | 108 | CourseUnitView cV = loadCourseUnitService.run(id,false,false,false,userSession); |
207 | jmachado | 109 | |
110 | if(cV == null) |
||
111 | return null; |
||
112 | |||
113 | CourseUnit courseUnit = DaoFactory.getCourseUnitDaoImpl().get(id); |
||
114 | |||
115 | if(courseUnit.getAssignements() != null) |
||
116 | { |
||
117 | boolean invalids = false; |
||
118 | List<CourseUnitAssignementView> views = new ArrayList<CourseUnitAssignementView>(); |
||
208 | jmachado | 119 | if(userSession.getUser() != null && ((CourseUnitImpl)courseUnit).isOwnedBy(userSession.getUser(),true)) |
207 | jmachado | 120 | { |
121 | invalids = true; |
||
122 | } |
||
123 | for(CourseUnitAssignement a: courseUnit.getAssignements()) |
||
124 | { |
||
125 | if(invalids || a.isStatus()) |
||
126 | { |
||
127 | long delivers = DaoFactory.getCourseUnitDeliverableDaoImpl().countAssignements(a.getId()); |
||
128 | CourseUnitAssignementView view = new CourseUnitAssignementView(a,delivers); |
||
208 | jmachado | 129 | if(view.getWorkFile() != null && view.getWorkFile().length() > 0) |
130 | { |
||
131 | RepositoryFileImpl repositoryFile = repositoryService.loadView(a.getRepositoryStream()); |
||
132 | view.setRepositoryFile(repositoryFile); |
||
133 | } |
||
207 | jmachado | 134 | views.add(view); |
135 | } |
||
136 | } |
||
137 | |||
138 | if(userSession.getUser() != null && userSession.getUser() instanceof StudentImpl && ((StudentImpl)userSession.getUser()).hasCourseUnit(id,true)) |
||
139 | { |
||
140 | for(CourseUnitAssignementView view: views) |
||
141 | { |
||
142 | CourseUnitDeliverable deliverable = DaoFactory.getCourseUnitDeliverableDaoImpl().loadDeliverable(userSession.getUser().getId(),view.getId()); |
||
143 | if(deliverable != null) |
||
144 | { |
||
145 | view.setCourseUnitDeliverableView(new CourseUnitDeliverableView(deliverable, view)); |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | |||
150 | cV.setCourseUnitAssignements(views); |
||
151 | } |
||
152 | return cV; |
||
153 | } |
||
154 | |||
155 | public CourseUnitView delete(long courseUnitWorkId, UserSession userSession) throws ServiceException |
||
156 | { |
||
157 | CourseUnitAssignement assignement = DaoFactory.getCourseUnitAssignementDaoImpl().load(courseUnitWorkId); |
||
158 | long courseUnitId = assignement.getCourseUnit().getId(); |
||
159 | assignement.getCourseUnit().getAssignements().remove(assignement); |
||
160 | assignement.setCourseUnit(null); |
||
161 | DaoFactory.getCourseUnitAssignementDaoImpl().delete(assignement); |
||
162 | return loadCourseUnitWorks(courseUnitId,userSession); |
||
163 | } |
||
164 | |||
165 | public CourseUnitView validateInvalidate(long courseUnitWorkId, UserSession userSession) throws ServiceException |
||
166 | { |
||
167 | CourseUnitAssignement assignement = DaoFactory.getCourseUnitAssignementDaoImpl().load(courseUnitWorkId); |
||
168 | long courseUnitId = assignement.getCourseUnit().getId(); |
||
169 | assignement.setStatus(!assignement.isStatus()); |
||
170 | return loadCourseUnitWorks(courseUnitId,userSession); |
||
171 | } |
||
172 | |||
173 | public CourseUnitView deliverWork(long courseUnitWorkId,5+0%2Fdocs%2Fapi+InputStream">InputStream file, 1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/String.html">String contentType, int size, UserSession userSession) throws ServiceException |
||
174 | { |
||
175 | CourseUnitAssignement assignement = DaoFactory.getCourseUnitAssignementDaoImpl().load(courseUnitWorkId); |
||
176 | long courseUnitId = assignement.getCourseUnit().getId(); |
||
177 | CourseUnitDeliverable deliverable = DaoFactory.getCourseUnitDeliverableDaoImpl().loadDeliverable(userSession.getUser().getId(),courseUnitWorkId); |
||
178 | 1.5.0/docs/api/java/lang/String.html">String extension = FilesUtils.getExtension(name); |
||
179 | if(deliverable == null) |
||
180 | { |
||
181 | deliverable = DomainObjectFactory.createCourseUnitDeliverableImpl(); |
||
1703 | jmachado | 182 | 1.5.0/docs/api/java/lang/String.html">String identifier = repositoryService.storeRepositoryFile(file, contentType, extension, size, name, "courseunit.assignement " + assignement.getCourseUnit().getName() + " of " + userSession.getUser().getUsername(), ResourceAccessControlEnum.courseunitDeliverableDomain, null, userSession); |
208 | jmachado | 183 | deliverable.setDeliverableRepositoryStream(identifier); |
207 | jmachado | 184 | deliverable.setCourseUnitAssignement(assignement); |
428 | jmachado | 185 | setStudentWorkDetails(deliverable,userSession); |
207 | jmachado | 186 | DaoFactory.getCourseUnitDeliverableDaoImpl().save(deliverable); |
187 | } |
||
188 | else |
||
189 | { |
||
428 | jmachado | 190 | setStudentWorkDetails(deliverable,userSession); |
208 | jmachado | 191 | repositoryService.updateRepositoryFile(deliverable.getDeliverableRepositoryStream(), file, contentType, extension, size, name, "courseunit.assignement " + assignement.getCourseUnit().getName() + " of " + userSession.getUser().getUsername(), ResourceAccessControlEnum.courseunitDeliverableDomain); |
207 | jmachado | 192 | } |
208 | jmachado | 193 | deliverable.setDeliverDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
207 | jmachado | 194 | return loadCourseUnitWorks(courseUnitId,userSession); |
195 | } |
||
428 | jmachado | 196 | |
197 | private void setStudentWorkDetails(CourseUnitDeliverable deliverable, UserSession userSession) |
||
198 | { |
||
199 | deliverable.setStudent((Student) userSession.getUser()); |
||
200 | deliverable.setStudentId(userSession.getUser().getId()); |
||
201 | deliverable.setStudentUsername(userSession.getUsername()); |
||
202 | deliverable.setStudentName(userSession.getUser().getName()); |
||
203 | if(userSession.getUser() instanceof SigesUser) |
||
204 | deliverable.setStudentCode("" + ((SigesUser)userSession.getUser()).getSigesCode()); |
||
205 | else |
||
206 | logger.error("BACO APPLICATION ERROR Student should be of type SigesUser"); |
||
207 | } |
||
208 | |||
207 | jmachado | 209 | //todo alterar o dominio de filtragem |
210 | |||
211 | |||
212 | public CourseUnitAssignementView loadAssignement(long courseUnitAssignementId, UserSession userSession) throws ServiceException |
||
213 | { |
||
214 | CourseUnitAssignement assignement = DaoFactory.getCourseUnitAssignementDaoImpl().load(courseUnitAssignementId); |
||
215 | List<CourseUnitDeliverableView> views = new ArrayList<CourseUnitDeliverableView>(); |
||
216 | CourseUnitAssignementView courseUnitAssignementView = new CourseUnitAssignementView(assignement,assignement.getDeliverables().size()); |
||
217 | for(CourseUnitDeliverable d: assignement.getDeliverables()) |
||
218 | { |
||
486 | jmachado | 219 | CourseUnitDeliverableView c = new CourseUnitDeliverableView(d,courseUnitAssignementView); |
220 | views.add(c); |
||
221 | RepositoryFileImpl repositoryFile = repositoryService.loadView(d.getDeliverableRepositoryStream()); |
||
222 | c.setVersions(new ArrayList<RepositoryFileVersion>(repositoryFile.getRepositoryFileVersions())); |
||
207 | jmachado | 223 | } |
224 | courseUnitAssignementView.setDeliverables(views); |
||
225 | courseUnitAssignementView.setCourseUnitView(new CourseUnitView(assignement.getCourseUnit())); |
||
208 | jmachado | 226 | if(courseUnitAssignementView.getWorkFile() != null && courseUnitAssignementView.getWorkFile().length() > 0) |
227 | { |
||
228 | RepositoryFileImpl repositoryFile = repositoryService.loadView(assignement.getRepositoryStream()); |
||
229 | courseUnitAssignementView.setRepositoryFile(repositoryFile); |
||
230 | } |
||
207 | jmachado | 231 | return courseUnitAssignementView; |
232 | } |
||
208 | jmachado | 233 | |
234 | public CourseUnitAssignementView updateDeliverables(long courseUnitId, long courseUnitAssignementId, List<CourseUnitDeliverableView> deliverableViews, UserSession userSession) throws ServiceException |
||
235 | { |
||
236 | CourseUnitAssignement assignement = DaoFactory.getCourseUnitAssignementDaoImpl().load(courseUnitAssignementId); |
||
237 | if(assignement.getCourseUnit().getId() != courseUnitId) |
||
238 | { |
||
239 | logger.warn("Trying change deliverable of a not owned course unit:" + userSession.getUser().getId() + " " + userSession.getUser().getName()); |
||
240 | throw new ServiceException("Access Denied", new AccessDeniedException("Trying change deliverable of a not owned course unit")); |
||
241 | } |
||
242 | for(CourseUnitDeliverableView deliverable: deliverableViews) |
||
243 | { |
||
244 | CourseUnitDeliverable courseUnitDeliverable = DaoFactory.getCourseUnitDeliverableDaoImpl().load(deliverable.getId()); |
||
245 | if(courseUnitDeliverable.getCourseUnitAssignement().getId() != assignement.getId()) |
||
246 | { |
||
247 | logger.warn("Trying change deliverable of a not owned course unit:" + userSession.getUser().getId() + " " + userSession.getUser().getName()); |
||
248 | throw new ServiceException("Access Denied", new AccessDeniedException("Trying change deliverable of a not owned course unit")); |
||
249 | } |
||
250 | deliverable.persistViewInObject(courseUnitDeliverable); |
||
251 | } |
||
252 | return loadAssignement(courseUnitAssignementId,userSession); |
||
253 | } |
||
254 | |||
255 | |||
207 | jmachado | 256 | } |