--- /dev/null
+/*******************************************************************************
+ * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
+ *
+ * This file is part of the work and learning management system Pentila Nero.
+ *
+ * Pentila Nero is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ * - the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the license,
+ * or (at your option) any later version.
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE-PROJECT.txt in the directory of this software
+ * distribution.
+ *
+ * Pentila Nero 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
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * and the CeCILL-C and the GNU Lesser General Public License along with
+ * Pentila Nero. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ ******************************************************************************/
+package com.pentila.entSavoie.agenda.service.impl;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.json.JSONArray;
+import com.liferay.portal.kernel.json.JSONObject;
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.model.Organization;
+import com.liferay.portal.model.User;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.pentila.entSavoie.agenda.model.Ressource;
+import com.pentila.entSavoie.agenda.service.EventAgendaLocalServiceUtil;
+import com.pentila.entSavoie.agenda.service.RessourceLocalServiceUtil;
+import com.pentila.entSavoie.agenda.service.RessourceRoleLocalServiceUtil;
+import com.pentila.entSavoie.agenda.service.RessourceUserLocalServiceUtil;
+import com.pentila.entSavoie.agenda.service.base.RessourceLocalServiceBaseImpl;
+import com.pentila.entSavoie.agenda.util.AgendaKind;
+import com.pentila.entSavoie.agenda.util.AgendaUtil;
+import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
+
+
+public class RessourceLocalServiceImpl extends RessourceLocalServiceBaseImpl {
+
+ private static Log logger = LogFactoryUtil.getLog(RessourceLocalServiceImpl.class);
+ private static final String GROUP_AGENDA = "Agenda de groupe ";
+
+ public Ressource addRessource() throws SystemException {
+ final long ressourceId = counterLocalService.increment();
+ final Ressource ressource = this.createRessource(ressourceId);
+ return ressource;
+ }
+
+ public Ressource addRessource(String ressourceName, long etabId, long creatorId) throws SystemException{
+ return this.addRessource(ressourceName, etabId, creatorId, 1, creatorId);
+ }
+
+ public boolean createCDT(User user, boolean isEleve, boolean isParent, boolean isEnseignant, boolean isPrincipal, JSONArray child){
+ try{
+ List<Ressource> listCDT = ressourcePersistence.findByidcreator_kind(user.getUserId(), 2);
+
+ if(listCDT.size() < 1){
+ String cdtName;
+ if(isEleve || isEnseignant || isPrincipal){
+ cdtName = "Mon EDT";
+
+ long etabId = OrganizationFinderServiceUtil.getEtabRatachement(user).getOrganizationId();
+ // on creer la ressource
+ Ressource ressource = RessourceLocalServiceUtil.addRessource(cdtName, etabId, user.getUserId(), 2, user.getUserId());
+
+ if(isEleve){
+ ressource.setOnlyMyClass(false);
+ ressource.setFiltreCours(0);
+ RessourceLocalServiceUtil.updateRessource(ressource);
+ }
+
+ // comme on est le createur on s ajoute en ressourceUser
+ RessourceUserLocalServiceUtil.addRessourceUser(ressource.getIdRessource(), user.getUserId(), false);
+ }
+
+ if(isParent){
+ cdtName = "";
+ for(int i=0; i < child.length(); i++){
+ JSONObject u = child.getJSONObject(i);
+ long childId = u.getLong("childId");
+ String childName = u.getString("childName");
+
+ if(AgendaUtil.startVoyelle(childName)){
+ cdtName = "EDT d'" + childName;
+ }else{
+ cdtName = "EDT de " + childName;
+ }
+
+ long etabId = OrganizationFinderServiceUtil.getEtabRatachement(user).getOrganizationId();
+ // on creer la ressource
+ Ressource ressource = RessourceLocalServiceUtil.addRessource(cdtName, etabId, user.getUserId(), 2, childId);
+
+ ressource.setOnlyMyClass(false);
+ ressource.setFiltreCours(0);
+ RessourceLocalServiceUtil.updateRessource(ressource);
+
+
+ // comme on est le createur on s ajoute en ressourceUser
+ RessourceUserLocalServiceUtil.addRessourceUser(ressource.getIdRessource(), user.getUserId(), false);
+ }
+ }
+
+
+ }
+ } catch(Exception e){
+ return false;
+ }
+ return true;
+ }
+
+ public Ressource addRessource(String ressourceName, long etabId, long creatorId, int kind, long targetId) throws SystemException{
+
+ Ressource ressource = addRessource();
+
+ ressource.setName(ressourceName);
+ ressource.setDescription("");
+ ressource.setEtabId(etabId);
+ ressource.setIdcreator(creatorId);
+ ressource.setScope(1); // restreint a mon etab
+ ressource.setRessourceType(2); // ressource privee
+ ressource.setColor("22");
+ ressource.setRessource(false);
+ ressource.setEcriture(false);
+ ressource.setKind(kind);
+ ressource.setForcePubli(false);
+ ressource.setCdtUserId(targetId);
+ ressource.setFiltreCours(1);
+ ressource.setClasse(-1);
+ ressource.setMatiere("[-1]");
+ ressource.setOnlyMyClass(true);
+ ressourcePersistence.update(ressource, false);
+
+ return ressource;
+ }
+
+ public Ressource getOrCreatePersonalRessource(long creatorId) {
+ Ressource res = null;
+ boolean doCreate = false;
+
+ try {
+ List<Ressource> resourceList = ressourcePersistence.findByidcreator_kind(creatorId, 0);
+ if (resourceList != null && resourceList.size() > 0) {
+ res = resourceList.get(0);
+ }
+ } catch (Exception e) {
+ doCreate = true;
+ }
+
+ // No personal agenda => let's create it
+ if (res == null || doCreate) {
+ User u;
+ try {
+ u = UserLocalServiceUtil.getUser(creatorId);
+ Organization etab = OrganizationFinderServiceUtil.getEtabRatachement(u);
+ res = addRessource("Agenda de " + u.getFullName(), etab.getOrganizationId(), creatorId, 0, creatorId);
+ } catch (Exception e1) {
+ logger.error(e1);
+ // difficile de le creer dans ce cas
+ }
+ }
+
+ RessourceUserLocalServiceUtil.getOrCreateRessourceUserByRessourceIdUserId(res.getIdRessource(), creatorId, true);
+
+ return res;
+ }
+
+ public Ressource getPersonalRessource(long creatorId) {
+ try {
+ List<Ressource> resourceList = ressourcePersistence.findByidcreator_kind(creatorId, 0);
+ if (resourceList != null && resourceList.size() > 0) {
+ return resourceList.get(0);
+ }
+ } catch (Exception e) {
+ logger.error(e);
+ }
+ return null;
+ }
+
+
+ public Ressource updateRessource(Ressource ressource) throws SystemException{
+ ressource.setNew(false);
+ ressourcePersistence.update(ressource, true);
+
+ return ressource;
+ }
+
+ /**
+ * Delete a ressource all all its instances (shared via role or shared via user list)
+ */
+ public boolean deleteRessourceAndDep(Ressource ressource) throws PortalException, SystemException {
+
+ try {
+ RessourceRoleLocalServiceUtil.deleteRessourceRoleAndDepByRessourceId(ressource.getIdRessource());
+ } catch (Exception e) {
+ logger.error("Error when deleting shared ressource by role : "+ressource.getIdRessource());
+ }
+
+ try {
+ RessourceUserLocalServiceUtil.deleteRessourceUserAndDepByRessourceId(ressource.getIdRessource());
+ } catch (Exception e) {
+ logger.error("Error when deleting shared ressource by user : "+ressource.getIdRessource());
+ }
+
+ try {
+ EventAgendaLocalServiceUtil.deleteEventAgendaByCallId(ressource.getIdRessource());
+ } catch (Exception e) {
+ logger.error("Error when deleting event agenda : "+ressource.getIdRessource());
+ }
+
+ ressourcePersistence.remove(ressource);
+ return true;
+ }
+
+ public List<Ressource> getRessourcesByScopeTypeKindForcePubli(int scope, int ressourceType, int kind, boolean forcePubli) throws SystemException {
+ return ressourcePersistence.findByressourceType_scope_kind_forcePubli(ressourceType, scope, kind, forcePubli);
+ }
+
+ public List<Ressource> getRessourcesByScopeTypeEtabIdKindForcePubli(int scope, int ressourceType, long etabId, int kind, boolean forcePubli) throws SystemException {
+ return ressourcePersistence.findByetabId_ressourceType_scope_kind_forcePubli(etabId, ressourceType, scope, kind, forcePubli);
+ }
+
+ public List<Ressource> getRessourcesByScopeTypeForcePubli(int scope, int ressourceType, boolean forcePubli) throws SystemException {
+ return ressourcePersistence.findByressourceType_scope_forcePubli(ressourceType, scope, forcePubli);
+ }
+
+ public List<Ressource> getRessourcesByScopeTypeEtabIdForcePubli(int scope, int ressourceType, long etabId, boolean forcePubli) throws SystemException {
+ return ressourcePersistence.findByetabId_ressourceType_scope_forcePubli(etabId, ressourceType, scope, forcePubli);
+ }
+
+ /**
+ * Get all duplicate entry and fill a map to manage resource per user
+ * @return a map where the key identify the user by it's id and list of his ressource as valu
+ */
+ public Map<Long, List<Ressource>> getRessourcesWhereUserHasMultiplePersonnalRessource() {
+ List<Ressource> duplicateAgenda = ressourceFinder.getDuplicatePersonnalAgenda();
+ Map<Long, List<Ressource>> ressourceListPerUser = new HashMap<Long, List<Ressource>>();
+ // Creator is the user concern by the personnal agenda
+ for(Ressource aDuplicateRessource : duplicateAgenda){
+ List<Ressource> userListRessource = ressourceListPerUser.get(aDuplicateRessource.getIdcreator());
+ if(userListRessource == null){
+ userListRessource = new ArrayList<Ressource>();
+ }
+ userListRessource.add(aDuplicateRessource);
+ ressourceListPerUser.put(aDuplicateRessource.getIdcreator(), userListRessource);
+ }
+ return ressourceListPerUser;
+ }
+
+ public List<Ressource> getRessourcesByEtabIdName (Long etabId, String ressourceName) throws SystemException {
+ return ressourcePersistence.findByetabId_name(etabId, ressourceName);
+ }
+
+ /**
+ * Returns the agenda ressource of an organization
+ * @param organization
+ * @return
+ */
+ public Ressource getOrCreateOrganizationRessource(long companyId, Organization org) {
+
+ Ressource res = null;
+ boolean doCreate = false;
+ String agendaRessourceName = GROUP_AGENDA + org.getName();
+ long schoolId = org.getParentOrganizationId();
+
+ try {
+ List<Ressource> groupRessources = ressourcePersistence.findBykind_etabId_name(AgendaKind.GROUP.ordinal(), schoolId, agendaRessourceName);
+ if (groupRessources != null && groupRessources.size() > 0) {
+ res = groupRessources.get(0);
+ }
+ } catch (Exception e) {
+ doCreate = true;
+ }
+ if (res == null || doCreate) {
+ try {
+ // Ressource creator is the default userId
+ long defaultUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
+
+ logger.info("Creating group ressource in agenda for organization "+org.getName());
+ res = addRessource(agendaRessourceName, schoolId, defaultUserId, AgendaKind.GROUP.ordinal(), defaultUserId);
+ } catch (Exception e) {
+ logger.error("Failed to create a group ressource in agenda for organization "+org.getName(), e);
+ }
+ }
+ return res;
+ }
+
+ /**
+ * Returns the personal CDT ressource for given user
+ */
+ public Ressource getOrCreateCdtRessource(long userId) {
+ Ressource res = null;
+ boolean doCreate = false;
+ try {
+ List<Ressource> resourceList = ressourcePersistence.findByidcreator_kind(userId, AgendaKind.RESSOURCE.ordinal());
+ if (resourceList != null && resourceList.size() > 0) {
+ res = resourceList.get(0);
+ }
+ } catch (Exception e) {
+ doCreate = true;
+ }
+
+ // No personal agenda => let's create it
+ if (res == null || doCreate) {
+ try {
+ User user = UserLocalServiceUtil.getUser(userId);
+ Organization etab = OrganizationFinderServiceUtil.getEtabRatachement(user);
+ logger.info("Create cdt ressource for userId "+userId);
+ res = addRessource("Mon EDT", etab.getOrganizationId(), userId, AgendaKind.RESSOURCE.ordinal(), userId);
+ } catch (Exception e) {
+ logger.error(e);
+ }
+ }
+
+ RessourceUserLocalServiceUtil.getOrCreateRessourceUserByRessourceIdUserId(res.getIdRessource(), userId, false);
+
+ return res;
+ }
+
+
+ /**
+ * Returns the list of CDT ressources for given userId
+ * @param userId
+ * @return
+ */
+ public List<Ressource> getUserCdtRessources(long userId) {
+
+ List<Ressource> listCDTRessources = new ArrayList<Ressource>();
+ try {
+ listCDTRessources = ressourcePersistence.findByidcreator_kind(userId, AgendaKind.RESSOURCE.ordinal());
+ } catch (Exception e) {
+ logger.error("Error while retrieving the list of CDT ressources for userId "+userId);
+ }
+ return listCDTRessources;
+ }
+
+
+ /**
+ * Returns the parent CDT ressource for given child
+ * @param userId
+ * @return
+ */
+ public Ressource getParentCdtRessource(long parentUserId, long childUserId) {
+
+ List<Ressource> listCDTRessources = new ArrayList<Ressource>();
+ try {
+ listCDTRessources = ressourcePersistence.findByidcreator_cdtUserId(parentUserId, childUserId);
+ } catch (Exception e) {
+ logger.error("Error while retrieving the list of CDT ressources for parentUserId "+parentUserId+" and childUserId "+childUserId);
+ }
+ if (listCDTRessources == null || listCDTRessources.size() != 1) {
+ return null;
+ }
+ return listCDTRessources.get(0);
+ }
+
+
+
+}