1 /*******************************************************************************
2 * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
3 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
4 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
6 * This file is part of the work and learning management system Pentila Nero.
8 * Pentila Nero is free software. You can redistribute it and/or modify since
9 * you respect the terms of either (at least one of the both license) :
10 * - under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
14 * License, or any later version
15 * - the GNU Lesser General Public License as published by the
16 * Free Software Foundation, either version 3 of the license,
17 * or (at your option) any later version.
19 * There are special exceptions to the terms and conditions of the
20 * licenses as they are applied to this software. View the full text of
21 * the exception in file LICENSE-PROJECT.txt in the directory of this software
24 * Pentila Nero is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * Licenses for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * and the CeCILL-C and the GNU Lesser General Public License along with
31 * Pentila Nero. If not, see :
32 * <http://www.gnu.org/licenses/> and
33 * <http://www.cecill.info/licences.fr.html>.
34 ******************************************************************************/
35 package com.pentila.entSavoie.utils;
37 import java.util.List;
39 import com.liferay.portal.NoSuchOrganizationException;
40 import com.liferay.portal.kernel.exception.PortalException;
41 import com.liferay.portal.kernel.exception.SystemException;
42 import com.liferay.portal.kernel.log.Log;
43 import com.liferay.portal.kernel.log.LogFactoryUtil;
44 import com.liferay.portal.model.Organization;
45 import com.liferay.portal.model.OrganizationConstants;
46 import com.liferay.portal.service.OrganizationLocalServiceUtil;
47 import com.liferay.portal.service.ServiceContext;
48 import com.liferay.portal.service.UserLocalServiceUtil;
49 import com.pentila.entSavoie.ENTDisplayUtil;
50 import com.pentila.entSavoie.communityInfos.service.OrganizationMappingLocalServiceUtil;
51 import com.pentila.entSavoie.institutionalList.model.ListeInst;
52 import com.pentila.entSavoie.institutionalList.model.ListeInstDetails;
53 import com.pentila.entSavoie.institutionalList.service.ListeInstDetailsLocalServiceUtil;
54 import com.pentila.entSavoie.institutionalList.service.ListeInstLocalServiceUtil;
57 * @author Lancelot SIX
59 * Cette classe fourni des fonctions simplifiéer pour manipuler les organizations.
62 public class ENTOrganizationsUtil {
64 private static Log logger = LogFactoryUtil.getLog(ENTOrganizationsUtil.class);
66 public static String NOT_PARENT_ORGANISATION = "0";
69 * Get or create the root organization
73 public static Organization getOrCreateRootOrg(long companyId)
74 throws PortalException, SystemException {
75 return getOrCreateOrganization(companyId, ENTMainUtilsLocalServiceUtil.getENTOrgRootName(companyId));
79 * Get or create the school given by its name
82 * @return the organization wanted or created
84 public static Organization getOrCreateSchool(long companyId, String name) throws PortalException, SystemException {
85 return getOrCreateOrganization(companyId, name, getOrCreateRootOrg(companyId).getOrganizationId());
89 * Get or create the school given by its name and UAI
92 * @param entStructureJointure long which represent the ENTStructureJointure in LDAP for a parent institution
93 * @return the organization wanted or created
95 public static Organization getOrCreateSchool(long companyId, String name, String entStructureUAI) throws PortalException, SystemException {
96 return getOrCreateOrganization(companyId, name, getOrCreateRootOrg(companyId).getOrganizationId(),entStructureUAI);
101 * Get or create organization given by its name
104 * @return the organization wanted or created
106 public static Organization getOrCreateOrganization(long companyId, String orgName) throws PortalException, SystemException {
107 return getOrCreateOrganization(companyId, orgName, OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID);
111 * Get or create organization given by its name
115 * @return the organization wanted or created
117 public static Organization getOrCreateOrganization(long companyId, String orgName, long parentOrgId) throws PortalException, SystemException {
118 return getOrCreateOrganization(companyId, orgName, parentOrgId, false, NOT_PARENT_ORGANISATION);
122 * Get or create organization given by its name
123 * If school, add it to the mapping table
127 * @param entStructureJointure long which represent the ENTStructureJointure in LDAP for a parent institution
128 * @return the organization wanted or created
130 public static Organization getOrCreateOrganization(long companyId, String orgName, long parentOrgId, String entStrunctureUAI) throws PortalException, SystemException {
131 return getOrCreateOrganization(companyId,orgName,parentOrgId,true,entStrunctureUAI);
135 * Get or create organization given by its name
136 * If school, add it to the mapping table
140 * @param isParentInstitution boolean which represent if the institution is a parent one
141 * @param entStructureUAI long which represent the ENTStructureJointure in LDAP for a parent institution
142 * @return the organization wanted or created
144 private static Organization getOrCreateOrganization(long companyId, String orgName, long parentOrgId, boolean isParentInstitution, String entStructureUAI) throws PortalException, SystemException {
145 Organization org = null;
148 org = OrganizationLocalServiceUtil.getOrganization(companyId,
150 } catch (NoSuchOrganizationException e) {
151 defUserId = UserLocalServiceUtil.getDefaultUserId(companyId);
152 org = OrganizationLocalServiceUtil.addOrganization(
156 OrganizationConstants.TYPE_REGULAR_ORGANIZATION,
160 12017, //Le 12017 est une valeur magique
161 //dont nous avons constaté la presence dans
165 new ServiceContext());
168 // if its parent org has a parent org with id = 0 => it is an 'etab' institution, add her to mapping
170 Organization parentOrg = OrganizationLocalServiceUtil.getOrganization(parentOrgId);
171 if (parentOrg.getParentOrganizationId() == 0) {
172 if (!entStructureUAI.equals("0")){
173 OrganizationMappingLocalServiceUtil.addOrganizationMapping(org, entStructureUAI);
176 //If we are forced to recreate the detail lists, we add the non recursive here
177 if (ENTMainUtilsLocalServiceUtil.isENTSynchroForceListeInst(companyId)) {
178 List<ListeInst> lis = ListeInstLocalServiceUtil.getListeInstByOrgId(org.getOrganizationId());
180 for (ListeInst li : lis) {
182 if (!li.getIncludeInOrgLoop() && li.getOrgId() == org.getOrganizationId()) {
183 // test if the list inst detail already exist
184 List<ListeInstDetails> listInstDetail = ListeInstDetailsLocalServiceUtil.
185 getListeInstDetailsByOrgIdRoleIdListeInstIdSubOrgIdRoleFilter(
189 li.getOrgFilter()?String.valueOf(org.getOrganizationId()):"",
192 //it doesn't exist so create it
193 if(listInstDetail == null || listInstDetail.size() == 0){
195 ListeInstDetailsLocalServiceUtil.addListeInstDetails(
197 li.getListeInstName(),
200 li.getOrgFilter()?String.valueOf(org.getOrganizationId()):"",
203 li.getChildrenList());
207 catch(SystemException e){
213 //If the creation of lists is forced in the LDAP Synchronization we create the details
214 //Else it's a class and we have to add it to the institutionnal details list (only if it's a new list though)
215 else if (defUserId != -1 || ENTMainUtilsLocalServiceUtil.isENTSynchroForceListeInst(companyId)) {
216 //Get the institutional lists concerned
217 List<ListeInst> lis = ListeInstLocalServiceUtil.getListeInstByOrgId(parentOrgId);
219 for (ListeInst li : lis) {
221 if ( li.getIncludeInOrgLoop() && !li.getParentList() && !li.getChildrenList()) {
222 // test if the list inst detail already exist
223 List<ListeInstDetails> listInstDetail = ListeInstDetailsLocalServiceUtil.
224 getListeInstDetailsByOrgIdRoleIdListeInstIdSubOrgIdRoleFilter(
228 String.valueOf(org.getOrganizationId()),
231 //it doesn't exist so create it
232 if(listInstDetail == null || listInstDetail.size() == 0){
234 String[] toFind = li.getOnameFind().split("\\|\\|");
235 String[] toNotFind = li.getOnameNotFind().split("\\|\\|");
237 //Have to check that the list respect creation rules
238 if (((toFind.length==1 && toFind[0].equals(""))
239 || checkToFind(toFind, org.getName())
241 && ((toNotFind.length==1 && toNotFind[0].equals(""))
242 || checkToNotFind(toNotFind, org.getName())
245 String orgname = ENTDisplayUtil.formatSCONETName(null, org.getName(), false);
246 String suffixToRemove = " - Parents";
248 if(orgname.endsWith(suffixToRemove)){
249 orgname = orgname.substring(0 , (orgname.length()-suffixToRemove.length()));
251 if (!li.getListeInstName().endsWith(" ")) {
252 orgname = " "+orgname;
255 ListeInstDetailsLocalServiceUtil.addListeInstDetails(
257 li.getListeInstName() + orgname,
260 String.valueOf(org.getOrganizationId()),
263 li.getChildrenList());
268 catch(SystemException e){
274 } catch (NoSuchOrganizationException e) {
280 private static Boolean checkToFind(String[] toFind, String orgName) {
281 for (int i=0; i<toFind.length; i++) {
282 if (orgName.contains(toFind[i])) {
289 private static Boolean checkToNotFind(String[] toNotFind, String orgName) {
290 for (int i=0; i<toNotFind.length; i++) {
291 if (orgName.contains(toNotFind[i])) {