1 package com.liferay.contacts.util;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
9 import com.liferay.portal.kernel.exception.PortalException;
10 import com.liferay.portal.kernel.exception.SystemException;
11 import com.liferay.portal.kernel.dao.orm.Criterion;
12 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
13 import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
14 import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
15 import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
16 import com.liferay.portal.kernel.dao.orm.ProjectionList;
17 import com.liferay.portal.kernel.log.Log;
18 import com.liferay.portal.kernel.log.LogFactoryUtil;
19 import com.liferay.portal.model.Organization;
20 import com.liferay.portal.model.Role;
21 import com.liferay.portal.model.User;
22 import com.liferay.portal.service.OrganizationLocalServiceUtil;
23 import com.liferay.portal.service.RoleLocalServiceUtil;
24 import com.pentila.entSavoie.ENTDisplayUtil;
25 import com.pentila.entSavoie.ENTRoleUtilFactory;
26 import com.pentila.entSavoie.ENTRolesConstants;
27 import com.pentila.entSavoie.ENTRolesUtil;
28 import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
29 import com.pentila.entSavoie.institutionalList.model.ListeInst;
30 import com.pentila.entSavoie.institutionalList.service.ListeInstLocalServiceUtil;
31 import com.pentila.entSavoie.institutionalList.model.ListeInstDetails;
32 import com.pentila.entSavoie.institutionalList.service.ListeInstDetailsLocalServiceUtil;
33 import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
34 import com.pentila.entSavoie.institutionalList.service.ClpSerializer;
36 public class InstitutionalListsFactory {
38 private static Log logger = LogFactoryUtil.getLog(InstitutionalListsFactory.class);
43 // The list of all user's etabs
44 List<Organization> etabOrganizations = null;
46 public InstitutionalListsFactory(User user) {
48 setRoleID(ENTRoleUtilFactory.getInstance(user.getCompanyId()).getENTRoles(user));
53 * Get the list of etabs attached to a user : attached etab (and its associated etabs from school complex) + other attached etabs (ENTPersonFonctions)
55 private void initEtabList() {
57 Organization etabRattach = OrganizationFinderServiceUtil.getEtabRatachement(user);
58 List<Organization> etabList = OrganizationFilter.getSchoolComplexSchools(etabRattach);
61 List<Organization> otherEtabList = OrganizationFilter.getUserSchools(user);
62 if (otherEtabList != null) {
63 for (Organization org : otherEtabList) {
64 if (!etabList.contains(org)) {
70 setEtabOrganizations(etabList);
72 } catch (Exception e) {
77 public List<ProxyList> getInstitutionnalProxyLists() throws PortalException, SystemException {
79 List<ProxyList> result = new ArrayList<ProxyList>(); // Returned list
81 if (getEtabOrganizations() == null) {
82 return new ArrayList<ProxyList>();
85 // Get user's list of organizations
86 List<Organization> userOrgsList = new ArrayList<Organization>();
87 Role[] userRoles = new ENTRolesUtil(user.getCompanyId()).getENTRoles(user);
89 boolean isAllowedToDisplayAllClasses = false;
90 for(Role aRoleForUser : userRoles){
91 isAllowedToDisplayAllClasses = ENTRolesUtil.isAllowedToHaveAllEtabsClasses(aRoleForUser.getName());
92 // if it found a role allowed. Stop the loop
93 if(isAllowedToDisplayAllClasses){
98 if (isAllowedToDisplayAllClasses) {
99 userOrgsList = OrganizationFilter.getUserClassesAndGroups(user);
102 // Loop over all etabs
103 for (Organization etab : getEtabOrganizations()) {
105 List<ProxyList> etabProxyLists = getInstitutionnalProxyListsForEtab(etab, userOrgsList);
106 result.addAll(etabProxyLists);
108 // Add parent and children lists
109 List<ProxyList> parentChildrenProxyLists = getParentAndChildrenList(etab);
110 result.addAll(parentChildrenProxyLists);
113 Collections.sort(result);
119 * Returns the institutional lists for a given etab
120 * @param etab : the etab organization
121 * @param userOrgsList the list of user's authorized sub-orgs
123 * @throws PortalException
124 * @throws SystemException
126 public List<ProxyList> getInstitutionnalProxyListsForEtab (Organization etab, List<Organization> userOrgsList) throws PortalException, SystemException {
127 List<ProxyList> result = new ArrayList<ProxyList>();
129 final long etabId = etab.getOrganizationId();
131 User user = getUser();
133 List<String> orgIdList = new ArrayList<String>(); // To avoid duplicates : key is list inst name + orgId
135 // Copy the list to avoid read-only list exception
136 List<Organization> userOrgsListCopy = new ArrayList<Organization>();
137 for (Organization org : userOrgsList) {
138 userOrgsListCopy.add(org);
141 if (userOrgsListCopy.isEmpty()) {
142 List<Organization> orgList = OrganizationLocalServiceUtil.getOrganizations(etab.getCompanyId(), etab.getOrganizationId());
143 userOrgsListCopy.addAll(orgList);
144 userOrgsListCopy.add(etab);
146 // Loop over user's roles to get its lists
147 List<ListeInst> userLists = new ArrayList<ListeInst>();
148 for (Role userRole: RoleLocalServiceUtil.getUserRoles(user.getUserId())) {
150 userLists.addAll(ListeInstLocalServiceUtil.getListeInstByParentChild(etabId, userRole.getRoleId(), false, false));
154 // If administrator (Group_admin), then get lists for National_4 role
155 Role groupAdminRole = RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.GROUP_ADMIN);
156 Role nat4Role = RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_4);
157 Role nat23Role = RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_23);
158 if (RoleLocalServiceUtil.hasUserRole(user.getUserId(), groupAdminRole.getRoleId())) {
159 userLists.addAll(ListeInstLocalServiceUtil.getListeInstByParentChild(etabId, nat4Role.getRoleId(), false, false));
160 userLists.addAll(ListeInstLocalServiceUtil.getListeInstByParentChild(etabId, nat23Role.getRoleId(), false, false));
163 for (ListeInst list : userLists) {
164 if (!list.getIncludeInOrgLoop()) {
165 // Do not include sub-orgs
166 result.add(new ProxyList(
167 list.getListeInstName(),
169 ""+list.getListeInstId(),
170 list.getOrgFilter()?String.valueOf(etabId):"",
171 String.valueOf(etabId),
172 list.getRoleFilter(),
173 list.getIncludeInOrgLoop(),
174 list.getParentList(),
175 list.getChildrenList()));
179 // Add all sub-orgs of given etab
180 String[] toFind = list.getOnameFind().split("\\|\\|");
181 String[] toNotFind = list.getOnameNotFind().split("\\|\\|");
183 for (Organization org : userOrgsListCopy) {
185 // Check if this org is sub-org of the main organization of the school complex (to avoid duplicates)
187 if (!orgIdList.contains(list.getListeInstName()+org.getOrganizationId())) {
188 orgIdList.add(list.getListeInstName()+org.getOrganizationId());
191 if (((toFind.length==1 && toFind[0].equals(""))
192 || checkToFind(toFind, org.getName())
194 && ((toNotFind.length==1 && toNotFind[0].equals(""))
195 || checkToNotFind(toNotFind, org.getName())
197 // Name is suffixed by the class name
198 result.add(new ProxyList(
199 list.getListeInstName() + ENTDisplayUtil.formatSCONETName(user, org.getName(), false),
201 ""+list.getListeInstId(),
202 String.valueOf(org.getOrganizationId()),
203 String.valueOf(etabId),
204 list.getRoleFilter(),
205 list.getIncludeInOrgLoop(),
206 list.getParentList(),
207 list.getChildrenList()));
217 public List<ListeInstDetails> getInstitutionnalLists() throws PortalException, SystemException {
218 List<ListeInstDetails> result = new ArrayList<ListeInstDetails>(); //Returned list
220 if (getEtabOrganizations() == null) {
221 return new ArrayList<ListeInstDetails>();
223 // Get user's list of organizations
224 List<Organization> userOrgsList = new ArrayList<Organization>();
225 Role[] userRoles = new ENTRolesUtil(user.getCompanyId()).getENTRoles(user);
227 boolean isAllowedToDisplayAllClasses = false;
228 for(Role aRoleForUser : userRoles){
229 isAllowedToDisplayAllClasses = ENTRolesUtil.isAllowedToHaveAllEtabsClasses(aRoleForUser.getName());
230 // if it found a role allowed. Stop the loop
231 if(isAllowedToDisplayAllClasses){
236 if (!isAllowedToDisplayAllClasses) {
237 userOrgsList = OrganizationFilter.getUserClassesAndGroups(user);
240 // Loop over all etabs
241 for (Organization etab : getEtabOrganizations()) {
242 List<ListeInstDetails> etabLists = getInstitutionnalListsForEtab(etab, userOrgsList);
243 if (etabLists != null) {
244 result.addAll(etabLists);
245 Collections.sort(result);
253 * Returns the institutional lists for a given etab
254 * @param etab : the etab organization
255 * @param userOrgsList the list of user's authorized sub-orgs
257 * @throws PortalException
258 * @throws SystemException
260 private List<ListeInstDetails> getInstitutionnalListsForEtab (Organization etab, List<Organization> userOrgsList) throws PortalException, SystemException {
261 List<ListeInstDetails> result = new ArrayList<ListeInstDetails>();
263 final long etabId = etab.getOrganizationId();
265 User user = getUser();
267 ClassLoader classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(),"portletClassLoader");
268 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(ListeInstDetails.class,classLoader);
269 Criterion criterion = null;
271 //We build the SQL query to take care of all the user's roles
272 for (Role userRole: RoleLocalServiceUtil.getUserRoles(user.getUserId())) {
273 if(criterion != null){
274 criterion = RestrictionsFactoryUtil.or(criterion , RestrictionsFactoryUtil.eq("roleId",userRole.getRoleId()));
277 criterion = RestrictionsFactoryUtil.eq("roleId",userRole.getRoleId());
281 // If administrator (Group_admin), then get lists for National_4 role
282 Role groupAdminRole = RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.GROUP_ADMIN);
283 Role nat4Role = RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_4);
284 if (RoleLocalServiceUtil.hasUserRole(user.getUserId(), groupAdminRole.getRoleId())) {
285 criterion = RestrictionsFactoryUtil.or(criterion, RestrictionsFactoryUtil.eq("roleId",nat4Role.getRoleId()));
288 //Finally we add the subOrg filter if the user is limited or the org filter to the query
289 if (!userOrgsList.isEmpty()) {
290 Criterion criterion2 = null;
291 for (Organization org : userOrgsList) {
292 if(criterion2 != null){
293 criterion2 = RestrictionsFactoryUtil.or(criterion2, RestrictionsFactoryUtil.eq("subOrgId", String.valueOf(org.getOrganizationId())));
296 criterion2 = RestrictionsFactoryUtil.eq("subOrgId", String.valueOf(org.getOrganizationId()));
299 criterion2 = RestrictionsFactoryUtil.or(criterion2, RestrictionsFactoryUtil.eq("subOrgId", String.valueOf(etabId)));
300 criterion = RestrictionsFactoryUtil.and(criterion, criterion2);
302 criterion = RestrictionsFactoryUtil.and(criterion, RestrictionsFactoryUtil.eq("orgId",etabId));
304 //Add a "group by" projection to avoid doubles (but we must get the other columns as well)
305 ProjectionList projectionList = ProjectionFactoryUtil.projectionList();
306 projectionList.add(ProjectionFactoryUtil.property("listeInstDetailsId"));
307 projectionList.add(ProjectionFactoryUtil.property("listeInstId"));
308 projectionList.add(ProjectionFactoryUtil.groupProperty("listeInstDetailsName"));
309 projectionList.add(ProjectionFactoryUtil.groupProperty("orgId"));
310 projectionList.add(ProjectionFactoryUtil.property("roleId"));
311 projectionList.add(ProjectionFactoryUtil.property("subOrgId"));
312 projectionList.add(ProjectionFactoryUtil.property("roleFilter"));
313 projectionList.add(ProjectionFactoryUtil.property("parentList"));
314 projectionList.add(ProjectionFactoryUtil.property("childrenList"));
315 dynamicQuery.setProjection(projectionList);
316 dynamicQuery.add(criterion);
321 List<Object[]> objs = ListeInstDetailsLocalServiceUtil.dynamicQuery(dynamicQuery);
322 for (Object[] obj : objs){
323 ListeInstDetails lid = ListeInstDetailsLocalServiceUtil.createListeInstDetails((Long) obj[0]);
324 lid.setListeInstId((Long) obj[1]);
325 lid.setListeInstDetailsName(String.valueOf(obj[2]));
326 lid.setOrgId((Long) obj[3]);
327 lid.setRoleId((Long) obj[4]);
328 lid.setSubOrgId(String.valueOf(obj[5]));
329 lid.setRoleFilter(String.valueOf(obj[6]));
330 lid.setParentList((Boolean) obj[7]);
331 lid.setChildrenList((Boolean) obj[8]);
334 } catch (SystemException e1) {
338 if(result == null || result.size() == 0){
347 * Returns the parent and children list for given user and given organization (etab)
351 public List<ProxyList> getParentAndChildrenList(Organization etab) throws PortalException, SystemException {
353 List<ProxyList> result = new ArrayList<ProxyList>();
354 User user = getUser();
356 for (Role userRole: RoleLocalServiceUtil.getUserRoles(user.getUserId())) {
357 List<ListeInst> parentLists = ListeInstLocalServiceUtil.getListeInstByParentChild(etab.getOrganizationId(), userRole.getRoleId(), true, false);
358 for (ListeInst listeInstParent : parentLists) {
359 result.add(new ProxyList(
360 listeInstParent.getListeInstName(),
362 ""+listeInstParent.getListeInstId(),
363 String.valueOf(etab.getOrganizationId()),
364 String.valueOf(etab.getOrganizationId()),
365 listeInstParent.getRoleFilter(),
366 listeInstParent.getIncludeInOrgLoop(),
367 listeInstParent.getParentList(),
368 listeInstParent.getChildrenList()));
370 List<ListeInst> childrenLists = ListeInstLocalServiceUtil.getListeInstByParentChild(etab.getOrganizationId(), userRole.getRoleId(), false, true);
371 for (ListeInst listeInstChild : childrenLists) {
372 result.add(new ProxyList(
373 listeInstChild.getListeInstName(),
375 ""+listeInstChild.getListeInstId(),
376 String.valueOf(etab.getOrganizationId()),
377 String.valueOf(etab.getOrganizationId()),
378 listeInstChild.getRoleFilter(),
379 listeInstChild.getIncludeInOrgLoop(),
380 listeInstChild.getParentList(),
381 listeInstChild.getChildrenList()));
387 private Boolean checkToFind(String[] toFind, String orgName) {
388 for (int i=0; i<toFind.length; i++) {
389 if (orgName.contains(toFind[i])) {
396 private Boolean checkToNotFind(String[] toNotFind, String orgName) {
397 for (int i=0; i<toNotFind.length; i++) {
398 if (orgName.contains(toNotFind[i])) {
405 public Role[] getRoleID() {
409 public void setRoleID(Role[] roleID) {
410 this.roleID = roleID;
413 public User getUser() {
417 public void setUser(User user) {
421 public List<Organization> getEtabOrganizations() {
422 return etabOrganizations;
425 public void setEtabOrganizations(List<Organization> etabOrganizations) {
426 this.etabOrganizations = etabOrganizations;