1 package com.liferay.contacts.util;
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.LinkedHashMap;
9 import com.liferay.portal.kernel.dao.orm.QueryUtil;
10 import com.liferay.portal.kernel.exception.PortalException;
11 import com.liferay.portal.kernel.exception.SystemException;
12 import com.liferay.portal.kernel.log.Log;
13 import com.liferay.portal.kernel.log.LogFactoryUtil;
14 import com.liferay.portal.kernel.util.OrderByComparator;
15 import com.liferay.portal.kernel.workflow.WorkflowConstants;
16 import com.liferay.portal.model.Group;
17 import com.liferay.portal.model.User;
18 import com.liferay.portal.service.ExtUserLocalServiceUtil;
19 import com.liferay.portal.service.GroupLocalServiceUtil;
20 import com.liferay.portal.service.UserLocalServiceUtil;
21 import com.liferay.portal.util.comparator.UserLastNameComparator;
22 import com.pentila.entSavoie.carnetDAdresses.model.Fiche;
23 import com.pentila.entSavoie.carnetDAdresses.model.Liste;
24 import com.pentila.entSavoie.carnetDAdresses.service.FicheLocalServiceUtil;
25 import com.pentila.entSavoie.carnetDAdresses.service.ListeLocalServiceUtil;
26 import com.pentila.entSavoie.communityInfos.model.CommunityInfos;
27 import com.pentila.entSavoie.communityInfos.service.CommunityInfosLocalServiceUtil;
28 import com.pentila.entSavoie.institutionalList.model.ListeInst;
29 import com.pentila.entSavoie.institutionalList.model.ListeInstDetails;
30 import com.pentila.entSavoie.institutionalList.service.ListeInstLocalServiceUtil;
31 import com.pentila.entSavoie.userProperties.model.UserRelationship;
32 import com.pentila.entSavoie.userProperties.service.UserRelationshipLocalServiceUtil;
33 import com.pentila.entSavoie.userRelationship.UserRelationshipConstants;
35 // carnet d'adresse fichier
38 public class ListFicheFactory {
41 private static Log logger = LogFactoryUtil.getLog(ListFicheFactory.class);
43 public static List<ProxyFiche> createAllContacts(User u) {
44 Set<ProxyFiche> retFiches = new HashSet<ProxyFiche>();
46 retFiches.addAll(createPersoContacts(u));
47 retFiches.addAll(createInstContacts(u));
48 return new ArrayList<ProxyFiche>(retFiches);
51 public static List<ProxyFiche> createAllContactsWithLists(User u, List<ListeInstDetails> listeInstDetails, List<ListeInstDetails> listFilter, List<Group> groups) {
52 Set<ProxyFiche> retFiches = new HashSet<ProxyFiche>();
53 List<String> excludeIds = new ArrayList<String>();
55 //build the filters to do less data treatment and add the contacts from the global lists
56 for (ListeInstDetails lid: listFilter) {
58 Long orgId = new Long(0);
59 if (!lid.getSubOrgId().equals("")) {
60 orgId = Long.valueOf(lid.getSubOrgId());
62 Long roleId = new Long(0);
63 if (!lid.getRoleFilter().equals("")) {
64 roleId = Long.valueOf(lid.getRoleFilter());
67 if (!excludeIds.contains(lid.getOrgId()+lid.getRoleFilter())) {
68 retFiches.addAll(ListFicheFactory.createSearchContacts(
73 lid.getParentList()?"true":"false",
74 lid.getChildrenList()?"true":"false",
76 excludeIds.add(lid.getOrgId()+lid.getRoleFilter());
79 } catch (NumberFormatException e) {
80 logger.error("ERROR : Erreur lors de la récupération des filtres pour les contacts des listes institutionnelles : conversion impossible",e);
81 } catch (SystemException e) {
82 logger.error("ERROR : Erreur lors de la récupération des filtres pour les contacts des listes institutionnelles : impossible d'importer les contacts",e);
85 //Contacts from institutionnal lists
86 for (ListeInstDetails lid: listeInstDetails) {
88 Long orgId = new Long(0);
89 if (!lid.getSubOrgId().equals("")) {
90 orgId = Long.valueOf(lid.getSubOrgId());
92 Long roleId = new Long(0);
93 if (!lid.getRoleFilter().equals("")) {
94 roleId = Long.valueOf(lid.getRoleFilter());
97 //We add the contacts only if their role is not in a global list (filter)
98 if (!excludeIds.contains(lid.getOrgId()+lid.getRoleFilter())) {
99 retFiches.addAll(ListFicheFactory.createSearchContacts(
104 lid.getParentList()?"true":"false",
105 lid.getChildrenList()?"true":"false",
108 } catch (NumberFormatException e) {
109 logger.error("ERROR : Erreur lors de la récupération des contacts des listes institutionnelles : conversion impossible",e);
110 } catch (SystemException e) {
111 logger.error("ERROR : Erreur lors de la récupération des contacts des listes institutionnelles : impossible d'importer les contacts",e);
114 //Contacts from user's groups
116 for (Fiche f: FicheLocalServiceUtil.findByGroupId(u.getGroup().getGroupId())) {
117 retFiches.add(new ProxyFiche(u, f, false));
119 } catch (SystemException e) {
121 } catch (PortalException e) {
122 // TODO Auto-generated catch block
126 //CAS DES COMMUNAUTES
127 // presente l ensemble des groupes de l'utilisateur
129 for (Group grp: groups) {
130 // on ajoute les users du groupe
131 LinkedHashMap<String, Object> userParams = new LinkedHashMap<String, Object>();
132 userParams.put("usersGroups", grp.getGroupId());
133 final List<User> lesUsers = ExtUserLocalServiceUtil.search(
136 WorkflowConstants.STATUS_APPROVED,
140 (OrderByComparator) null);
141 for (User user: lesUsers) {
142 retFiches.add(new ProxyFiche(u, user, false));
145 } catch(Exception exc) {
148 return new ArrayList<ProxyFiche>(retFiches);
151 public static List<ProxyFiche> createInstContacts(User u) {
153 InstitutionalListsFactory ilf = new InstitutionalListsFactory(u);
154 List<ProxyList> pls = null;
156 pls = new ArrayList<ProxyList>(ilf.getInstitutionnalProxyLists());
157 } catch (PortalException e1) {
159 } catch (SystemException e1) {
163 Set<ProxyFiche> retFiches = new HashSet<ProxyFiche>();
165 for (ProxyList pl: pls) {
167 Long orgId = new Long(0);
168 if (!pl.getOrgId().equals("")) {
169 orgId = Long.valueOf(pl.getOrgId());
171 Long roleId = new Long(0);
172 if (!pl.getRoleId().equals("")) {
173 roleId = Long.valueOf(pl.getRoleId());
175 retFiches.addAll(ListFicheFactory.createSearchContacts(
181 pl.getChildrenList(),
183 } catch (NumberFormatException e) {
185 } catch (SystemException e) {
190 return new ArrayList<ProxyFiche>(retFiches);
193 public static List<ProxyFiche> createPersoContacts(User currentUser) {
194 List<ProxyFiche> retFiches = new ArrayList<ProxyFiche>();
196 for (Fiche f: FicheLocalServiceUtil.getFicheInUserListsFromUserGroup(currentUser)) {
197 retFiches.add(new ProxyFiche(currentUser, f));
199 } catch (SystemException e) {
202 catch (PortalException e) {
206 // on ajoute les communautes
207 //CAS DES COMMUNAUTES
208 // presente l ensemble des groupes de l'utilisateur
209 LinkedHashMap<String, Object> groupParams = new LinkedHashMap<String, Object>();
210 groupParams.put("usersGroups", new Long(currentUser.getUserId()));
211 groupParams.put("active", Boolean.TRUE);
213 List<Group> resultsTmp = new ArrayList<Group>();
215 resultsTmp = GroupLocalServiceUtil.search(currentUser.getCompanyId(), null, null, groupParams, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
217 List<Long> groupIds = new ArrayList<Long>();
218 for (Group gTmp: resultsTmp) {
219 groupIds.add(gTmp.getGroupId());
222 for (CommunityInfos ci: CommunityInfosLocalServiceUtil.getCommunityForGroups(groupIds).values()) {
223 if (ci.getStatus()==0 && ci.isGroupContactList()) {
224 // on ajoute les users du groupe
225 LinkedHashMap<String, Object> userParams = new LinkedHashMap<String, Object>();
226 userParams.put("usersGroups", ci.getGroupId());
227 final List<User> lesUsers = UserLocalServiceUtil.search(
228 currentUser.getCompanyId(),
230 WorkflowConstants.STATUS_APPROVED,
234 new UserLastNameComparator());
235 for (User u: lesUsers) {
236 retFiches.add(new ProxyFiche(currentUser, u));
240 } catch (SystemException e) {
242 } catch (PortalException e) {
250 public static List<ProxyFiche> createUserListContacts(User currentUser, Liste l) {
251 List<ProxyFiche> retFiches = new ArrayList<ProxyFiche>();
253 for (Fiche f: ListeLocalServiceUtil.getFiches(l.getListeId())) {
254 retFiches.add(new ProxyFiche(currentUser, f));
256 } catch (Exception e) {
262 public static List<ProxyFiche> createSearchContacts(User currentUser, Long orgId, Long roleId, Long listId) throws SystemException {
264 // Is parent or children list ?
265 boolean isParentList = false;
266 boolean isChildrenList = false;
267 ListeInst listeInst = null;
269 listeInst = ListeInstLocalServiceUtil.getListeInst(listId);
270 if (listeInst != null) {
271 isParentList = listeInst.isParentList();
272 isChildrenList = listeInst.isChildrenList();
274 } catch (Exception e){
275 logger.warn("Could not get liste inst " + listId);
278 return createSearchContacts(currentUser, orgId, roleId, listId, isParentList, isChildrenList, true);
281 public static List<ProxyFiche> createSearchContacts(User currentUser, Long orgId, Long roleId, Long listId, boolean parentList, boolean childrenList, boolean processSatut) throws SystemException {
283 List<ProxyFiche> retFiches = new ArrayList<ProxyFiche>();
284 final LinkedHashMap<String, Object> userParams = new LinkedHashMap<String, Object>();
286 userParams.put("usersOrgs", orgId);
288 userParams.put("usersGroups", listId);
291 userParams.put("usersRoles", new Long(roleId));
294 // Particular case of parents and children lists (created by webservice only)
297 List<UserRelationship> uRelations = UserRelationshipLocalServiceUtil.getUserRelationshipByFromUserIdRelationType(currentUser.getUserId(), UserRelationshipConstants.PARENT_RELATION);
298 for (UserRelationship urs: uRelations){
299 User parentUser = UserLocalServiceUtil.getUser(urs.getToUserId());
300 retFiches.add(new ProxyFiche(currentUser, parentUser, processSatut));
302 } catch (Exception e) {
309 List<UserRelationship> uRelations = UserRelationshipLocalServiceUtil.getUserRelationshipByToUserIdRelationType(currentUser.getUserId(), UserRelationshipConstants.PARENT_RELATION);
310 for (UserRelationship urs: uRelations){
311 User childUser = UserLocalServiceUtil.getUser(urs.getFromUserId());
312 retFiches.add(new ProxyFiche(currentUser, childUser, processSatut));
314 } catch (Exception e) {
320 final List<User> lesUsers = ExtUserLocalServiceUtil.search(
321 currentUser.getCompanyId(),
323 WorkflowConstants.STATUS_APPROVED,
327 (OrderByComparator) null);
328 for (User u: lesUsers) {
329 retFiches.add(new ProxyFiche(currentUser, u, processSatut));
337 public static List<ProxyFiche> createSearchContacts(User currentUser, Long orgId, Long roleId, Long listId, String parentList, String childrenList, boolean processSatut) throws SystemException {
338 return ListFicheFactory.createSearchContacts(currentUser,orgId,roleId,listId,parentList.equals("true"),childrenList.equals("true"),processSatut);
341 public static List<ProxyFiche> createAmisContacts(User currentUser, List<Long> lesAmis) throws SystemException, PortalException {
342 List<ProxyFiche> retFiches = new ArrayList<ProxyFiche>();
344 for (Long l: lesAmis) {
345 User u = UserLocalServiceUtil.getUserById(l);
346 retFiches.add(new ProxyFiche(currentUser, u));
351 public static List<ProxyFiche> createOrgContacts(User currentUser, Organization org) {
352 List<ProxyFiche> retFiches = new ArrayList<ProxyFiche>();
354 for (User u: ExtUserLocalServiceUtil.getOrganizationUsers(org.getOrganizationId())) {
355 retFiches.add(new ProxyFiche(currentUser, u));
357 } catch (Exception e) {