--- /dev/null
+package com.liferay.contacts.util;
+
+import java.util.ArrayList;
+import java.util.Collections;
+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.dao.orm.Criterion;
+import com.liferay.portal.kernel.dao.orm.DynamicQuery;
+import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
+import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
+import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
+import com.liferay.portal.kernel.dao.orm.ProjectionList;
+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.Role;
+import com.liferay.portal.model.User;
+import com.liferay.portal.service.OrganizationLocalServiceUtil;
+import com.liferay.portal.service.RoleLocalServiceUtil;
+import com.pentila.entSavoie.ENTDisplayUtil;
+import com.pentila.entSavoie.ENTRoleUtilFactory;
+import com.pentila.entSavoie.ENTRolesConstants;
+import com.pentila.entSavoie.ENTRolesUtil;
+import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
+import com.pentila.entSavoie.institutionalList.model.ListeInst;
+import com.pentila.entSavoie.institutionalList.service.ListeInstLocalServiceUtil;
+import com.pentila.entSavoie.institutionalList.model.ListeInstDetails;
+import com.pentila.entSavoie.institutionalList.service.ListeInstDetailsLocalServiceUtil;
+import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
+import com.pentila.entSavoie.institutionalList.service.ClpSerializer;
+
+public class InstitutionalListsFactory {
+
+ private static Log logger = LogFactoryUtil.getLog(InstitutionalListsFactory.class);
+
+ Role[] roleID;
+ User user = null;
+
+ // The list of all user's etabs
+ List<Organization> etabOrganizations = null;
+
+ public InstitutionalListsFactory(User user) {
+ setUser(user);
+ setRoleID(ENTRoleUtilFactory.getInstance(user.getCompanyId()).getENTRoles(user));
+ initEtabList();
+ }
+
+ /**
+ * Get the list of etabs attached to a user : attached etab (and its associated etabs from school complex) + other attached etabs (ENTPersonFonctions)
+ */
+ private void initEtabList() {
+ try {
+ Organization etabRattach = OrganizationFinderServiceUtil.getEtabRatachement(user);
+ List<Organization> etabList = OrganizationFilter.getSchoolComplexSchools(etabRattach);
+
+ // Add other etabs
+ List<Organization> otherEtabList = OrganizationFilter.getUserSchools(user);
+ if (otherEtabList != null) {
+ for (Organization org : otherEtabList) {
+ if (!etabList.contains(org)) {
+ etabList.add(org);
+ }
+ }
+ }
+
+ setEtabOrganizations(etabList);
+
+ } catch (Exception e) {
+ logger.error(e);
+ }
+ }
+
+ public List<ProxyList> getInstitutionnalProxyLists() throws PortalException, SystemException {
+
+ List<ProxyList> result = new ArrayList<ProxyList>(); // Returned list
+
+ if (getEtabOrganizations() == null) {
+ return new ArrayList<ProxyList>();
+ }
+
+ // Get user's list of organizations
+ List<Organization> userOrgsList = new ArrayList<Organization>();
+ Role[] userRoles = new ENTRolesUtil(user.getCompanyId()).getENTRoles(user);
+
+ boolean isAllowedToDisplayAllClasses = false;
+ for(Role aRoleForUser : userRoles){
+ isAllowedToDisplayAllClasses = ENTRolesUtil.isAllowedToHaveAllEtabsClasses(aRoleForUser.getName());
+ // if it found a role allowed. Stop the loop
+ if(isAllowedToDisplayAllClasses){
+ break;
+ }
+ }
+
+ if (isAllowedToDisplayAllClasses) {
+ userOrgsList = OrganizationFilter.getUserClassesAndGroups(user);
+ }
+
+ // Loop over all etabs
+ for (Organization etab : getEtabOrganizations()) {
+
+ List<ProxyList> etabProxyLists = getInstitutionnalProxyListsForEtab(etab, userOrgsList);
+ result.addAll(etabProxyLists);
+
+ // Add parent and children lists
+ List<ProxyList> parentChildrenProxyLists = getParentAndChildrenList(etab);
+ result.addAll(parentChildrenProxyLists);
+ }
+
+ Collections.sort(result);
+ return result;
+ }
+
+
+ /**
+ * Returns the institutional lists for a given etab
+ * @param etab : the etab organization
+ * @param userOrgsList the list of user's authorized sub-orgs
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ */
+ public List<ProxyList> getInstitutionnalProxyListsForEtab (Organization etab, List<Organization> userOrgsList) throws PortalException, SystemException {
+ List<ProxyList> result = new ArrayList<ProxyList>();
+
+ final long etabId = etab.getOrganizationId();
+
+ User user = getUser();
+
+ List<String> orgIdList = new ArrayList<String>(); // To avoid duplicates : key is list inst name + orgId
+
+ // Copy the list to avoid read-only list exception
+ List<Organization> userOrgsListCopy = new ArrayList<Organization>();
+ for (Organization org : userOrgsList) {
+ userOrgsListCopy.add(org);
+ }
+
+ if (userOrgsListCopy.isEmpty()) {
+ List<Organization> orgList = OrganizationLocalServiceUtil.getOrganizations(etab.getCompanyId(), etab.getOrganizationId());
+ userOrgsListCopy.addAll(orgList);
+ userOrgsListCopy.add(etab);
+ }
+ // Loop over user's roles to get its lists
+ List<ListeInst> userLists = new ArrayList<ListeInst>();
+ for (Role userRole: RoleLocalServiceUtil.getUserRoles(user.getUserId())) {
+
+ userLists.addAll(ListeInstLocalServiceUtil.getListeInstByParentChild(etabId, userRole.getRoleId(), false, false));
+
+ }
+
+ // If administrator (Group_admin), then get lists for National_4 role
+ Role groupAdminRole = RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.GROUP_ADMIN);
+ Role nat4Role = RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_4);
+ Role nat23Role = RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_23);
+ if (RoleLocalServiceUtil.hasUserRole(user.getUserId(), groupAdminRole.getRoleId())) {
+ userLists.addAll(ListeInstLocalServiceUtil.getListeInstByParentChild(etabId, nat4Role.getRoleId(), false, false));
+ userLists.addAll(ListeInstLocalServiceUtil.getListeInstByParentChild(etabId, nat23Role.getRoleId(), false, false));
+ }
+
+ for (ListeInst list : userLists) {
+ if (!list.getIncludeInOrgLoop()) {
+ // Do not include sub-orgs
+ result.add(new ProxyList(
+ list.getListeInstName(),
+ "search",
+ ""+list.getListeInstId(),
+ list.getOrgFilter()?String.valueOf(etabId):"",
+ String.valueOf(etabId),
+ list.getRoleFilter(),
+ list.getIncludeInOrgLoop(),
+ list.getParentList(),
+ list.getChildrenList()));
+ }
+
+ else {
+ // Add all sub-orgs of given etab
+ String[] toFind = list.getOnameFind().split("\\|\\|");
+ String[] toNotFind = list.getOnameNotFind().split("\\|\\|");
+
+ for (Organization org : userOrgsListCopy) {
+
+ // Check if this org is sub-org of the main organization of the school complex (to avoid duplicates)
+
+ if (!orgIdList.contains(list.getListeInstName()+org.getOrganizationId())) {
+ orgIdList.add(list.getListeInstName()+org.getOrganizationId());
+
+
+ if (((toFind.length==1 && toFind[0].equals(""))
+ || checkToFind(toFind, org.getName())
+ )
+ && ((toNotFind.length==1 && toNotFind[0].equals(""))
+ || checkToNotFind(toNotFind, org.getName())
+ )) {
+ // Name is suffixed by the class name
+ result.add(new ProxyList(
+ list.getListeInstName() + ENTDisplayUtil.formatSCONETName(user, org.getName(), false),
+ "search",
+ ""+list.getListeInstId(),
+ String.valueOf(org.getOrganizationId()),
+ String.valueOf(etabId),
+ list.getRoleFilter(),
+ list.getIncludeInOrgLoop(),
+ list.getParentList(),
+ list.getChildrenList()));
+
+ }
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ public List<ListeInstDetails> getInstitutionnalLists() throws PortalException, SystemException {
+ List<ListeInstDetails> result = new ArrayList<ListeInstDetails>(); //Returned list
+
+ if (getEtabOrganizations() == null) {
+ return new ArrayList<ListeInstDetails>();
+ }
+ // Get user's list of organizations
+ List<Organization> userOrgsList = new ArrayList<Organization>();
+ Role[] userRoles = new ENTRolesUtil(user.getCompanyId()).getENTRoles(user);
+
+ boolean isAllowedToDisplayAllClasses = false;
+ for(Role aRoleForUser : userRoles){
+ isAllowedToDisplayAllClasses = ENTRolesUtil.isAllowedToHaveAllEtabsClasses(aRoleForUser.getName());
+ // if it found a role allowed. Stop the loop
+ if(isAllowedToDisplayAllClasses){
+ break;
+ }
+ }
+
+ if (!isAllowedToDisplayAllClasses) {
+ userOrgsList = OrganizationFilter.getUserClassesAndGroups(user);
+ }
+
+ // Loop over all etabs
+ for (Organization etab : getEtabOrganizations()) {
+ List<ListeInstDetails> etabLists = getInstitutionnalListsForEtab(etab, userOrgsList);
+ if (etabLists != null) {
+ result.addAll(etabLists);
+ Collections.sort(result);
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * Returns the institutional lists for a given etab
+ * @param etab : the etab organization
+ * @param userOrgsList the list of user's authorized sub-orgs
+ * @return
+ * @throws PortalException
+ * @throws SystemException
+ */
+ private List<ListeInstDetails> getInstitutionnalListsForEtab (Organization etab, List<Organization> userOrgsList) throws PortalException, SystemException {
+ List<ListeInstDetails> result = new ArrayList<ListeInstDetails>();
+
+ final long etabId = etab.getOrganizationId();
+
+ User user = getUser();
+
+ ClassLoader classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(),"portletClassLoader");
+ DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(ListeInstDetails.class,classLoader);
+ Criterion criterion = null;
+
+ //We build the SQL query to take care of all the user's roles
+ for (Role userRole: RoleLocalServiceUtil.getUserRoles(user.getUserId())) {
+ if(criterion != null){
+ criterion = RestrictionsFactoryUtil.or(criterion , RestrictionsFactoryUtil.eq("roleId",userRole.getRoleId()));
+ }
+ else{
+ criterion = RestrictionsFactoryUtil.eq("roleId",userRole.getRoleId());
+ }
+ }
+
+ // If administrator (Group_admin), then get lists for National_4 role
+ Role groupAdminRole = RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.GROUP_ADMIN);
+ Role nat4Role = RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_4);
+ if (RoleLocalServiceUtil.hasUserRole(user.getUserId(), groupAdminRole.getRoleId())) {
+ criterion = RestrictionsFactoryUtil.or(criterion, RestrictionsFactoryUtil.eq("roleId",nat4Role.getRoleId()));
+ }
+
+ //Finally we add the subOrg filter if the user is limited or the org filter to the query
+ if (!userOrgsList.isEmpty()) {
+ Criterion criterion2 = null;
+ for (Organization org : userOrgsList) {
+ if(criterion2 != null){
+ criterion2 = RestrictionsFactoryUtil.or(criterion2, RestrictionsFactoryUtil.eq("subOrgId", String.valueOf(org.getOrganizationId())));
+ }
+ else{
+ criterion2 = RestrictionsFactoryUtil.eq("subOrgId", String.valueOf(org.getOrganizationId()));
+ }
+ }
+ criterion2 = RestrictionsFactoryUtil.or(criterion2, RestrictionsFactoryUtil.eq("subOrgId", String.valueOf(etabId)));
+ criterion = RestrictionsFactoryUtil.and(criterion, criterion2);
+ }
+ criterion = RestrictionsFactoryUtil.and(criterion, RestrictionsFactoryUtil.eq("orgId",etabId));
+
+ //Add a "group by" projection to avoid doubles (but we must get the other columns as well)
+ ProjectionList projectionList = ProjectionFactoryUtil.projectionList();
+ projectionList.add(ProjectionFactoryUtil.property("listeInstDetailsId"));
+ projectionList.add(ProjectionFactoryUtil.property("listeInstId"));
+ projectionList.add(ProjectionFactoryUtil.groupProperty("listeInstDetailsName"));
+ projectionList.add(ProjectionFactoryUtil.groupProperty("orgId"));
+ projectionList.add(ProjectionFactoryUtil.property("roleId"));
+ projectionList.add(ProjectionFactoryUtil.property("subOrgId"));
+ projectionList.add(ProjectionFactoryUtil.property("roleFilter"));
+ projectionList.add(ProjectionFactoryUtil.property("parentList"));
+ projectionList.add(ProjectionFactoryUtil.property("childrenList"));
+ dynamicQuery.setProjection(projectionList);
+ dynamicQuery.add(criterion);
+
+ try {
+
+ //Execute the query
+ List<Object[]> objs = ListeInstDetailsLocalServiceUtil.dynamicQuery(dynamicQuery);
+ for (Object[] obj : objs){
+ ListeInstDetails lid = ListeInstDetailsLocalServiceUtil.createListeInstDetails((Long) obj[0]);
+ lid.setListeInstId((Long) obj[1]);
+ lid.setListeInstDetailsName(String.valueOf(obj[2]));
+ lid.setOrgId((Long) obj[3]);
+ lid.setRoleId((Long) obj[4]);
+ lid.setSubOrgId(String.valueOf(obj[5]));
+ lid.setRoleFilter(String.valueOf(obj[6]));
+ lid.setParentList((Boolean) obj[7]);
+ lid.setChildrenList((Boolean) obj[8]);
+ result.add(lid);
+ }
+ } catch (SystemException e1) {
+ logger.error(e1);
+ }
+
+ if(result == null || result.size() == 0){
+ return null;
+ }
+
+ return result;
+ }
+
+
+ /**
+ * Returns the parent and children list for given user and given organization (etab)
+ * @param etab
+ * @return
+ */
+ public List<ProxyList> getParentAndChildrenList(Organization etab) throws PortalException, SystemException {
+
+ List<ProxyList> result = new ArrayList<ProxyList>();
+ User user = getUser();
+
+ for (Role userRole: RoleLocalServiceUtil.getUserRoles(user.getUserId())) {
+ List<ListeInst> parentLists = ListeInstLocalServiceUtil.getListeInstByParentChild(etab.getOrganizationId(), userRole.getRoleId(), true, false);
+ for (ListeInst listeInstParent : parentLists) {
+ result.add(new ProxyList(
+ listeInstParent.getListeInstName(),
+ "search",
+ ""+listeInstParent.getListeInstId(),
+ String.valueOf(etab.getOrganizationId()),
+ String.valueOf(etab.getOrganizationId()),
+ listeInstParent.getRoleFilter(),
+ listeInstParent.getIncludeInOrgLoop(),
+ listeInstParent.getParentList(),
+ listeInstParent.getChildrenList()));
+ }
+ List<ListeInst> childrenLists = ListeInstLocalServiceUtil.getListeInstByParentChild(etab.getOrganizationId(), userRole.getRoleId(), false, true);
+ for (ListeInst listeInstChild : childrenLists) {
+ result.add(new ProxyList(
+ listeInstChild.getListeInstName(),
+ "search",
+ ""+listeInstChild.getListeInstId(),
+ String.valueOf(etab.getOrganizationId()),
+ String.valueOf(etab.getOrganizationId()),
+ listeInstChild.getRoleFilter(),
+ listeInstChild.getIncludeInOrgLoop(),
+ listeInstChild.getParentList(),
+ listeInstChild.getChildrenList()));
+ }
+ }
+ return result;
+ }
+
+ private Boolean checkToFind(String[] toFind, String orgName) {
+ for (int i=0; i<toFind.length; i++) {
+ if (orgName.contains(toFind[i])) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private Boolean checkToNotFind(String[] toNotFind, String orgName) {
+ for (int i=0; i<toNotFind.length; i++) {
+ if (orgName.contains(toNotFind[i])) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public Role[] getRoleID() {
+ return roleID;
+ }
+
+ public void setRoleID(Role[] roleID) {
+ this.roleID = roleID;
+ }
+
+ public User getUser() {
+ return user;
+ }
+
+ public void setUser(User user) {
+ this.user = user;
+ }
+
+ public List<Organization> getEtabOrganizations() {
+ return etabOrganizations;
+ }
+
+ public void setEtabOrganizations(List<Organization> etabOrganizations) {
+ this.etabOrganizations = etabOrganizations;
+ }
+}
+