--- /dev/null
+/**
+ * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ */
+
+package com.pentila.entSavoie.cdt.service.impl;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
+import com.liferay.portal.kernel.dao.orm.Disjunction;
+import com.liferay.portal.kernel.dao.orm.DynamicQuery;
+import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
+import com.liferay.portal.kernel.dao.orm.Property;
+import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
+import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.model.CompanyConstants;
+import com.liferay.portal.model.Group;
+import com.liferay.portal.model.User;
+import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
+import com.pentila.entSavoie.cahierDeTexte.util.CDTAttachConstants;
+import com.pentila.entSavoie.cdt.model.CDTSession;
+import com.pentila.entSavoie.cdt.service.CDTSessionLocalServiceUtil;
+import com.pentila.entSavoie.cdt.service.ClpSerializer;
+import com.pentila.entSavoie.cdt.service.SessionTeacherLocalServiceUtil;
+import com.pentila.entSavoie.cdt.service.base.CDTSessionLocalServiceBaseImpl;
+import com.pentila.entSavoie.cdt.service.persistence.CDTSessionFinderUtil;
+import com.pentila.entSavoie.cdt.util.CDTSessionsUtil;
+
+/**
+ * The implementation of the c d t session local service.
+ *
+ * <p>
+ * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.pentila.entSavoie.cdt.service.CDTSessionLocalService} interface.
+ *
+ * <p>
+ * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
+ * </p>
+ *
+ * @author Cedric Lecarpentier
+ * @see com.pentila.entSavoie.cdt.service.base.CDTSessionLocalServiceBaseImpl
+ * @see com.pentila.entSavoie.cdt.service.CDTSessionLocalServiceUtil
+ */
+public class CDTSessionLocalServiceImpl extends CDTSessionLocalServiceBaseImpl {
+
+ private static Log _log = LogFactoryUtil.getLog(CDTSessionLocalServiceImpl.class);
+
+ private static final boolean DELETED_SESSION = true;
+
+ public List<CDTSession> getSessions(long groupId, int weekId, int type) {
+
+ try {
+ return cdtSessionPersistence.findBygroupId_weekId_type(groupId, weekId, type, !DELETED_SESSION);
+ } catch (Exception e) {
+ _log.error("Error while retrieving CDTSesisons in DB", e);
+ }
+ return null;
+ }
+
+ /**
+ * Get the session for a list of group that begin between 2 dates
+ * @param userGroupToDisplay group use to search the sessions
+ * @param startDate minimium start date
+ * @param endDate maximum start date
+ * @return the list of session found
+ * @throws SystemException
+ */
+ public List<CDTSession> getSessionForGroupsBetweenDates(
+ List<Group> userGroupToDisplay, Date startDate, Date endDate) throws SystemException {
+
+ List<Long> userGroupIdToDisplay = new ArrayList<Long>();
+ for(Group group : userGroupToDisplay){
+ userGroupIdToDisplay.add(group.getGroupId());
+ }
+
+ return getSessionForGroupsIdsBetweenDates(userGroupIdToDisplay,startDate,endDate);
+ }
+
+
+ /**
+ * Get the session for a list of group id that begin between 2 dates
+ * @param userGroupToDisplay group use to search the sessions
+ * @param startDate minimium start date
+ * @param endDate maximum start date
+ * @return the list of session found
+ * @throws SystemException
+ */
+ public List<CDTSession> getSessionForGroupsIdsBetweenDates(
+ List<Long> userGroupIdToDisplay, Date startDate, Date endDate) throws SystemException {
+
+ ClassLoader classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(),"portletClassLoader");
+ DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(CDTSession.class,classLoader);
+ Disjunction disjunctionOrgsId = RestrictionsFactoryUtil.disjunction();
+ Property groupIdProperty = PropertyFactoryUtil.forName("groupId");
+ Property sessionStartProperty = PropertyFactoryUtil.forName("sessionStart");
+ Property deletedProperty = PropertyFactoryUtil.forName("deleted");
+
+ List<CDTSession> sessionsForGroups = new ArrayList<CDTSession>();
+ if (userGroupIdToDisplay != null && userGroupIdToDisplay.size() > 0) {
+
+ for(Long groupId : userGroupIdToDisplay){
+ disjunctionOrgsId.add(groupIdProperty.eq(groupId));
+ }
+ dynamicQuery.add(disjunctionOrgsId);
+
+ dynamicQuery.add(sessionStartProperty.between(startDate, endDate));
+ dynamicQuery.add(deletedProperty.eq(!DELETED_SESSION));
+
+
+ sessionsForGroups.addAll(CDTSessionLocalServiceUtil.dynamicQuery(dynamicQuery));
+ }
+
+ return sessionsForGroups;
+ }
+
+ /**
+ * get all session grom a session group ID
+ * @param sessionGroupId
+ * @return
+ * @throws SystemException
+ */
+ public List<CDTSession> getSessionsFromSessionGroup(long sessionGroupId) throws SystemException{
+ return cdtSessionPersistence.findBySessionGroupId(sessionGroupId,!DELETED_SESSION);
+ }
+
+ /**
+ * Create a CDTSession from all its properties
+ * @return
+ * @throws SystemException
+ */
+ public CDTSession createCDTSession(long schoolId, long groupId, long subjectId, Date startDate, Date endDate,
+ List<Long> teacherIdList, String room, String title, String description, int type, boolean broadcast,
+ boolean cancelled, long visa, boolean isManual, boolean hasAttachFile,
+ long previousSession, long nextSession, long sessionGroupId) throws SystemException {
+
+ long cdtSessionId = counterLocalService.increment();
+ CDTSession cdtSession = cdtSessionPersistence.create(cdtSessionId);
+
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(startDate);
+ int weekId = cal.get(Calendar.WEEK_OF_YEAR);
+
+ cdtSession.setSchoolId(schoolId);
+ cdtSession.setGroupId(groupId);
+ cdtSession.setSubjectId(subjectId);
+ cdtSession.setSessionStart(startDate);
+ cdtSession.setSessionEnd(endDate);
+ cdtSession.setRoom(room);
+ cdtSession.setTitle(title);
+ cdtSession.setDescription(description);
+ cdtSession.setType(type);
+ cdtSession.setPublish(broadcast);
+ cdtSession.setCancelled(cancelled);
+ cdtSession.setVisa(visa);
+ cdtSession.setIsManual(isManual);
+ cdtSession.setHasAttachFile(hasAttachFile);
+ cdtSession.setPreviousSession(previousSession);
+ cdtSession.setNextSession(nextSession);
+ cdtSession.setWeekId(weekId);
+ cdtSession.setDeleted(!DELETED_SESSION);
+ cdtSession.setSessionGroupId(sessionGroupId);
+ cdtSessionPersistence.update(cdtSession, false);
+
+ // Update the teacher list
+ SessionTeacherLocalServiceUtil.updateTeacherListForSession(cdtSession.getSessionId(), teacherIdList);
+
+ return cdtSession;
+ }
+
+
+
+ /**
+ * Get all sessions for a given teacher in a given date range
+ * @param teacherId
+ * @param minDate
+ * @param maxDate
+ * @return
+ */
+ public List<CDTSession> getCDTSessionsByTeacherRangeDate(long teacherId, Date minDate, Date maxDate) {
+
+ // Custom query
+ List<CDTSession> sessionList = CDTSessionFinderUtil.findByTeacherId_RangeDate(teacherId, minDate, maxDate);
+
+ return sessionList;
+ }
+
+ /**
+ * Get all sessions for a given teacher in a given date range
+ * @param teacherId
+ * @param minDate
+ * @param maxDate
+ * @return
+ */
+ public List<CDTSession> getCDTSessionsByStudentRangeDate(long studentId, Date minDate, Date maxDate) {
+
+ // Custom query
+ List<CDTSession> sessionList = CDTSessionFinderUtil.findByStudentId_RangeDate(studentId, minDate, maxDate);
+
+ return sessionList;
+ }
+
+ /**
+ * Returns all attach file names for this session
+ * @param companyId
+ * @param sessionId
+ * @return
+ */
+ public String[] getAttachFilesNames(long companyId, long sessionId) {
+
+ String[] fileNames = new String[0];
+ if (sessionId != 0) {
+
+ String dirName = CDTAttachConstants.ATTACH_FILE_PATH + CDTAttachConstants.ATTACH_FILE_PATH_SESSIONS + sessionId;
+
+ try {
+ fileNames = DLStoreUtil.getFileNames(companyId, CompanyConstants.SYSTEM, dirName);
+ }
+ catch (Exception e) {
+ // Nothing to raise in case of no attach file
+ }
+ }
+
+ return fileNames;
+ }
+
+ /**
+ * Method which permit to update the visa value for a range of date for a list of group
+ * This method only call the finderImpl
+ * @param visaValue
+ * @param groupsToFilterOn
+ * @param fromDate
+ * @param toDate
+ * @return
+ */
+ public boolean updateSessionVisaValueByTeacherId_RangeDate_GroupId(
+ long visaId, List<Group> groupsToFilterOn, Long teacherId, Date fromDate, Date toDate) throws SystemException{
+
+ List<CDTSession> sessionToUpdateVisa =
+ cdtSessionFinder.findByTeacherId_RangeDate_GroupId(groupsToFilterOn, teacherId, fromDate, toDate);
+
+ if (sessionToUpdateVisa.size() > 0) {
+ cdtSessionFinder.updateSessionVisaValueForSessionId(sessionToUpdateVisa, visaId);
+ }
+
+ return true;
+ }
+
+
+ public List<CDTSession> getManualSessionsForTeacher(long teacherId, Date minDate, Date maxDate, boolean manualSessionsOnly) {
+
+ // Custom query
+ return CDTSessionFinderUtil.findByTeacherId_RangeDate_Manual(teacherId, minDate, maxDate, manualSessionsOnly);
+
+ }
+
+ /**
+ * Method which permit to delete a CDTSession with all its dependencies (homeworks, attachments..)
+ * @param sessionId
+ * @param user which perform the deletion
+ * @return
+ */
+ public boolean deleteSessionAndDependancies(long sessionId, User user) throws PortalException, SystemException {
+
+ CDTSessionsUtil.deleteSessionAndDependancies(sessionId, user);
+
+ return true;
+ }
+
+ /**
+ * Returns all sessions for a given groupId and schoolId
+ * (Used to list all subClass sessions for given school)
+ * @param groupId
+ * @param schoolId
+ * @return
+ * @throws SystemException
+ */
+ public List<CDTSession> getSessionsForGroupIdSchoolId(long groupId, long schoolId) throws SystemException{
+ return cdtSessionPersistence.findBygroupId_schoolId(groupId, schoolId);
+ }
+
+
+}
\ No newline at end of file