--- /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.utils;
+
+import java.util.List;
+
+import com.liferay.portal.NoSuchOrganizationException;
+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.Organization;
+import com.liferay.portal.model.OrganizationConstants;
+import com.liferay.portal.service.OrganizationLocalServiceUtil;
+import com.liferay.portal.service.ServiceContext;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.pentila.entSavoie.ENTDisplayUtil;
+import com.pentila.entSavoie.communityInfos.service.OrganizationMappingLocalServiceUtil;
+import com.pentila.entSavoie.institutionalList.model.ListeInst;
+import com.pentila.entSavoie.institutionalList.model.ListeInstDetails;
+import com.pentila.entSavoie.institutionalList.service.ListeInstDetailsLocalServiceUtil;
+import com.pentila.entSavoie.institutionalList.service.ListeInstLocalServiceUtil;
+
+/**
+ * @author Lancelot SIX
+ *
+ * Cette classe fourni des fonctions simplifiéer pour manipuler les organizations.
+ *
+ */
+public class ENTOrganizationsUtil {
+
+ private static Log logger = LogFactoryUtil.getLog(ENTOrganizationsUtil.class);
+
+ public static String NOT_PARENT_ORGANISATION = "0";
+
+ /**
+ * Get or create the root organization
+ * @param companyId
+ * @return
+ */
+ public static Organization getOrCreateRootOrg(long companyId)
+ throws PortalException, SystemException {
+ return getOrCreateOrganization(companyId, ENTMainUtilsLocalServiceUtil.getENTOrgRootName(companyId));
+ }
+
+ /**
+ * Get or create the school given by its name
+ * @param companyId
+ * @param name
+ * @return the organization wanted or created
+ */
+ public static Organization getOrCreateSchool(long companyId, String name) throws PortalException, SystemException {
+ return getOrCreateOrganization(companyId, name, getOrCreateRootOrg(companyId).getOrganizationId());
+ }
+
+ /**
+ * Get or create the school given by its name and UAI
+ * @param companyId
+ * @param name
+ * @param entStructureJointure long which represent the ENTStructureJointure in LDAP for a parent institution
+ * @return the organization wanted or created
+ */
+ public static Organization getOrCreateSchool(long companyId, String name, String entStructureUAI) throws PortalException, SystemException {
+ return getOrCreateOrganization(companyId, name, getOrCreateRootOrg(companyId).getOrganizationId(),entStructureUAI);
+ }
+
+
+ /**
+ * Get or create organization given by its name
+ * @param companyId
+ * @param orgName
+ * @return the organization wanted or created
+ */
+ public static Organization getOrCreateOrganization(long companyId, String orgName) throws PortalException, SystemException {
+ return getOrCreateOrganization(companyId, orgName, OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID);
+ }
+
+ /**
+ * Get or create organization given by its name
+ * @param companyId
+ * @param orgName
+ * @param parentOrgId
+ * @return the organization wanted or created
+ */
+ public static Organization getOrCreateOrganization(long companyId, String orgName, long parentOrgId) throws PortalException, SystemException {
+ return getOrCreateOrganization(companyId, orgName, parentOrgId, false, NOT_PARENT_ORGANISATION);
+ }
+
+ /**
+ * Get or create organization given by its name
+ * If school, add it to the mapping table
+ * @param companyId
+ * @param orgName
+ * @param parentOrgId
+ * @param entStructureJointure long which represent the ENTStructureJointure in LDAP for a parent institution
+ * @return the organization wanted or created
+ */
+ public static Organization getOrCreateOrganization(long companyId, String orgName, long parentOrgId, String entStrunctureUAI) throws PortalException, SystemException {
+ return getOrCreateOrganization(companyId,orgName,parentOrgId,true,entStrunctureUAI);
+ }
+
+ /**
+ * Get or create organization given by its name
+ * If school, add it to the mapping table
+ * @param companyId
+ * @param orgName
+ * @param parentOrgId
+ * @param isParentInstitution boolean which represent if the institution is a parent one
+ * @param entStructureUAI long which represent the ENTStructureJointure in LDAP for a parent institution
+ * @return the organization wanted or created
+ */
+ private static Organization getOrCreateOrganization(long companyId, String orgName, long parentOrgId, boolean isParentInstitution, String entStructureUAI) throws PortalException, SystemException {
+ Organization org = null;
+ long defUserId = -1;
+ try {
+ org = OrganizationLocalServiceUtil.getOrganization(companyId,
+ orgName);
+ } catch (NoSuchOrganizationException e) {
+ defUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
+ org = OrganizationLocalServiceUtil.addOrganization(
+ defUserId,
+ parentOrgId,
+ orgName,
+ OrganizationConstants.TYPE_REGULAR_ORGANIZATION,
+ true,
+ 0,
+ 0,
+ 12017, //Le 12017 est une valeur magique
+ //dont nous avons constaté la presence dans
+ //la base de donnee.
+ "",
+ true,
+ new ServiceContext());
+ }
+
+ // if its parent org has a parent org with id = 0 => it is an 'etab' institution, add her to mapping
+ try {
+ Organization parentOrg = OrganizationLocalServiceUtil.getOrganization(parentOrgId);
+ if (parentOrg.getParentOrganizationId() == 0) {
+ if (!entStructureUAI.equals("0")){
+ OrganizationMappingLocalServiceUtil.addOrganizationMapping(org, entStructureUAI);
+ }
+
+ //If we are forced to recreate the detail lists, we add the non recursive here
+ if (ENTMainUtilsLocalServiceUtil.isENTSynchroForceListeInst(companyId)) {
+ List<ListeInst> lis = ListeInstLocalServiceUtil.getListeInstByOrgId(org.getOrganizationId());
+
+ for (ListeInst li : lis) {
+ try{
+ if (!li.getIncludeInOrgLoop() && li.getOrgId() == org.getOrganizationId()) {
+ // test if the list inst detail already exist
+ List<ListeInstDetails> listInstDetail = ListeInstDetailsLocalServiceUtil.
+ getListeInstDetailsByOrgIdRoleIdListeInstIdSubOrgIdRoleFilter(
+ li.getOrgId(),
+ li.getRoleId(),
+ li.getListeInstId(),
+ li.getOrgFilter()?String.valueOf(org.getOrganizationId()):"",
+ li.getRoleFilter());
+
+ //it doesn't exist so create it
+ if(listInstDetail == null || listInstDetail.size() == 0){
+
+ ListeInstDetailsLocalServiceUtil.addListeInstDetails(
+ li.getListeInstId(),
+ li.getListeInstName(),
+ li.getOrgId(),
+ li.getRoleId(),
+ li.getOrgFilter()?String.valueOf(org.getOrganizationId()):"",
+ li.getRoleFilter(),
+ li.getParentList(),
+ li.getChildrenList());
+ }
+ }
+ }
+ catch(SystemException e){
+ logger.error(e);
+ }
+ }
+ }
+ }
+ //If the creation of lists is forced in the LDAP Synchronization we create the details
+ //Else it's a class and we have to add it to the institutionnal details list (only if it's a new list though)
+ else if (defUserId != -1 || ENTMainUtilsLocalServiceUtil.isENTSynchroForceListeInst(companyId)) {
+ //Get the institutional lists concerned
+ List<ListeInst> lis = ListeInstLocalServiceUtil.getListeInstByOrgId(parentOrgId);
+
+ for (ListeInst li : lis) {
+ try{
+ if ( li.getIncludeInOrgLoop() && !li.getParentList() && !li.getChildrenList()) {
+ // test if the list inst detail already exist
+ List<ListeInstDetails> listInstDetail = ListeInstDetailsLocalServiceUtil.
+ getListeInstDetailsByOrgIdRoleIdListeInstIdSubOrgIdRoleFilter(
+ li.getOrgId(),
+ li.getRoleId(),
+ li.getListeInstId(),
+ String.valueOf(org.getOrganizationId()),
+ li.getRoleFilter());
+
+ //it doesn't exist so create it
+ if(listInstDetail == null || listInstDetail.size() == 0){
+
+ String[] toFind = li.getOnameFind().split("\\|\\|");
+ String[] toNotFind = li.getOnameNotFind().split("\\|\\|");
+
+ //Have to check that the list respect creation rules
+ if (((toFind.length==1 && toFind[0].equals(""))
+ || checkToFind(toFind, org.getName())
+ )
+ && ((toNotFind.length==1 && toNotFind[0].equals(""))
+ || checkToNotFind(toNotFind, org.getName())
+ )) {
+
+ String orgname = ENTDisplayUtil.formatSCONETName(null, org.getName(), false);
+ String suffixToRemove = " - Parents";
+
+ if(orgname.endsWith(suffixToRemove)){
+ orgname = orgname.substring(0 , (orgname.length()-suffixToRemove.length()));
+ }
+ if (!li.getListeInstName().endsWith(" ")) {
+ orgname = " "+orgname;
+ }
+
+ ListeInstDetailsLocalServiceUtil.addListeInstDetails(
+ li.getListeInstId(),
+ li.getListeInstName() + orgname,
+ li.getOrgId(),
+ li.getRoleId(),
+ String.valueOf(org.getOrganizationId()),
+ li.getRoleFilter(),
+ li.getParentList(),
+ li.getChildrenList());
+ }
+ }
+ }
+ }
+ catch(SystemException e){
+ logger.error(e);
+ }
+ }
+ }
+
+ } catch (NoSuchOrganizationException e) {
+ logger.debug(e);
+ }
+ return org;
+ }
+
+ private static Boolean checkToFind(String[] toFind, String orgName) {
+ for (int i=0; i<toFind.length; i++) {
+ if (orgName.contains(toFind[i])) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static Boolean checkToNotFind(String[] toNotFind, String orgName) {
+ for (int i=0; i<toNotFind.length; i++) {
+ if (orgName.contains(toNotFind[i])) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+
+}