--- /dev/null
+package com.liferay.contacts.util;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Set;
+
+import com.liferay.portal.kernel.dao.orm.QueryUtil;
+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.kernel.util.OrderByComparator;
+import com.liferay.portal.kernel.workflow.WorkflowConstants;
+import com.liferay.portal.model.Group;
+import com.liferay.portal.model.User;
+import com.liferay.portal.service.ExtUserLocalServiceUtil;
+import com.liferay.portal.service.GroupLocalServiceUtil;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.liferay.portal.util.comparator.UserLastNameComparator;
+import com.pentila.entSavoie.carnetDAdresses.model.Fiche;
+import com.pentila.entSavoie.carnetDAdresses.model.Liste;
+import com.pentila.entSavoie.carnetDAdresses.service.FicheLocalServiceUtil;
+import com.pentila.entSavoie.carnetDAdresses.service.ListeLocalServiceUtil;
+import com.pentila.entSavoie.communityInfos.model.CommunityInfos;
+import com.pentila.entSavoie.communityInfos.service.CommunityInfosLocalServiceUtil;
+import com.pentila.entSavoie.institutionalList.model.ListeInst;
+import com.pentila.entSavoie.institutionalList.model.ListeInstDetails;
+import com.pentila.entSavoie.institutionalList.service.ListeInstLocalServiceUtil;
+import com.pentila.entSavoie.userProperties.model.UserRelationship;
+import com.pentila.entSavoie.userProperties.service.UserRelationshipLocalServiceUtil;
+import com.pentila.entSavoie.userRelationship.UserRelationshipConstants;
+
+// carnet d'adresse fichier
+
+
+public class ListFicheFactory {
+
+ // logger
+ private static Log logger = LogFactoryUtil.getLog(ListFicheFactory.class);
+
+ public static List<ProxyFiche> createAllContacts(User u) {
+ Set<ProxyFiche> retFiches = new HashSet<ProxyFiche>();
+
+ retFiches.addAll(createPersoContacts(u));
+ retFiches.addAll(createInstContacts(u));
+ return new ArrayList<ProxyFiche>(retFiches);
+ }
+
+ public static List<ProxyFiche> createAllContactsWithLists(User u, List<ListeInstDetails> listeInstDetails, List<ListeInstDetails> listFilter, List<Group> groups) {
+ Set<ProxyFiche> retFiches = new HashSet<ProxyFiche>();
+ List<String> excludeIds = new ArrayList<String>();
+
+ //build the filters to do less data treatment and add the contacts from the global lists
+ for (ListeInstDetails lid: listFilter) {
+ try {
+ Long orgId = new Long(0);
+ if (!lid.getSubOrgId().equals("")) {
+ orgId = Long.valueOf(lid.getSubOrgId());
+ }
+ Long roleId = new Long(0);
+ if (!lid.getRoleFilter().equals("")) {
+ roleId = Long.valueOf(lid.getRoleFilter());
+ }
+
+ if (!excludeIds.contains(lid.getOrgId()+lid.getRoleFilter())) {
+ retFiches.addAll(ListFicheFactory.createSearchContacts(
+ u,
+ orgId,
+ roleId,
+ new Long(0),
+ lid.getParentList()?"true":"false",
+ lid.getChildrenList()?"true":"false",
+ false));
+ excludeIds.add(lid.getOrgId()+lid.getRoleFilter());
+ }
+
+ } catch (NumberFormatException e) {
+ logger.error("ERROR : Erreur lors de la récupération des filtres pour les contacts des listes institutionnelles : conversion impossible",e);
+ } catch (SystemException e) {
+ logger.error("ERROR : Erreur lors de la récupération des filtres pour les contacts des listes institutionnelles : impossible d'importer les contacts",e);
+ }
+ }
+ //Contacts from institutionnal lists
+ for (ListeInstDetails lid: listeInstDetails) {
+ try {
+ Long orgId = new Long(0);
+ if (!lid.getSubOrgId().equals("")) {
+ orgId = Long.valueOf(lid.getSubOrgId());
+ }
+ Long roleId = new Long(0);
+ if (!lid.getRoleFilter().equals("")) {
+ roleId = Long.valueOf(lid.getRoleFilter());
+ }
+
+ //We add the contacts only if their role is not in a global list (filter)
+ if (!excludeIds.contains(lid.getOrgId()+lid.getRoleFilter())) {
+ retFiches.addAll(ListFicheFactory.createSearchContacts(
+ u,
+ orgId,
+ roleId,
+ new Long(0),
+ lid.getParentList()?"true":"false",
+ lid.getChildrenList()?"true":"false",
+ false));
+ }
+ } catch (NumberFormatException e) {
+ logger.error("ERROR : Erreur lors de la récupération des contacts des listes institutionnelles : conversion impossible",e);
+ } catch (SystemException e) {
+ logger.error("ERROR : Erreur lors de la récupération des contacts des listes institutionnelles : impossible d'importer les contacts",e);
+ }
+ }
+ //Contacts from user's groups
+ try {
+ for (Fiche f: FicheLocalServiceUtil.findByGroupId(u.getGroup().getGroupId())) {
+ retFiches.add(new ProxyFiche(u, f, false));
+ }
+ } catch (SystemException e) {
+ logger.error(e);
+ } catch (PortalException e) {
+ // TODO Auto-generated catch block
+ logger.error(e);
+ }
+
+ //CAS DES COMMUNAUTES
+ // presente l ensemble des groupes de l'utilisateur
+ try {
+ for (Group grp: groups) {
+ // on ajoute les users du groupe
+ LinkedHashMap<String, Object> userParams = new LinkedHashMap<String, Object>();
+ userParams.put("usersGroups", grp.getGroupId());
+ final List<User> lesUsers = ExtUserLocalServiceUtil.search(
+ u.getCompanyId(),
+ null,
+ WorkflowConstants.STATUS_APPROVED,
+ userParams,
+ QueryUtil.ALL_POS,
+ QueryUtil.ALL_POS,
+ (OrderByComparator) null);
+ for (User user: lesUsers) {
+ retFiches.add(new ProxyFiche(u, user, false));
+ }
+ }
+ } catch(Exception exc) {
+ logger.error(exc);
+ }
+ return new ArrayList<ProxyFiche>(retFiches);
+ }
+
+ public static List<ProxyFiche> createInstContacts(User u) {
+
+ InstitutionalListsFactory ilf = new InstitutionalListsFactory(u);
+ List<ProxyList> pls = null;
+ try {
+ pls = new ArrayList<ProxyList>(ilf.getInstitutionnalProxyLists());
+ } catch (PortalException e1) {
+ logger.error(e1);
+ } catch (SystemException e1) {
+ logger.error(e1);
+ }
+
+ Set<ProxyFiche> retFiches = new HashSet<ProxyFiche>();
+
+ for (ProxyList pl: pls) {
+ try {
+ Long orgId = new Long(0);
+ if (!pl.getOrgId().equals("")) {
+ orgId = Long.valueOf(pl.getOrgId());
+ }
+ Long roleId = new Long(0);
+ if (!pl.getRoleId().equals("")) {
+ roleId = Long.valueOf(pl.getRoleId());
+ }
+ retFiches.addAll(ListFicheFactory.createSearchContacts(
+ u,
+ orgId,
+ roleId,
+ new Long(0),
+ pl.getParentList(),
+ pl.getChildrenList(),
+ true));
+ } catch (NumberFormatException e) {
+ logger.error(e);
+ } catch (SystemException e) {
+ logger.error(e);
+ }
+ }
+
+ return new ArrayList<ProxyFiche>(retFiches);
+ }
+
+ public static List<ProxyFiche> createPersoContacts(User currentUser) {
+ List<ProxyFiche> retFiches = new ArrayList<ProxyFiche>();
+ try {
+ for (Fiche f: FicheLocalServiceUtil.getFicheInUserListsFromUserGroup(currentUser)) {
+ retFiches.add(new ProxyFiche(currentUser, f));
+ }
+ } catch (SystemException e) {
+ logger.error(e);
+ }
+ catch (PortalException e) {
+ logger.error(e);
+ }
+
+ // on ajoute les communautes
+ //CAS DES COMMUNAUTES
+ // presente l ensemble des groupes de l'utilisateur
+ LinkedHashMap<String, Object> groupParams = new LinkedHashMap<String, Object>();
+ groupParams.put("usersGroups", new Long(currentUser.getUserId()));
+ groupParams.put("active", Boolean.TRUE);
+
+ List<Group> resultsTmp = new ArrayList<Group>();
+ try {
+ resultsTmp = GroupLocalServiceUtil.search(currentUser.getCompanyId(), null, null, groupParams, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
+
+ List<Long> groupIds = new ArrayList<Long>();
+ for (Group gTmp: resultsTmp) {
+ groupIds.add(gTmp.getGroupId());
+ }
+
+ for (CommunityInfos ci: CommunityInfosLocalServiceUtil.getCommunityForGroups(groupIds).values()) {
+ if (ci.getStatus()==0 && ci.isGroupContactList()) {
+ // on ajoute les users du groupe
+ LinkedHashMap<String, Object> userParams = new LinkedHashMap<String, Object>();
+ userParams.put("usersGroups", ci.getGroupId());
+ final List<User> lesUsers = UserLocalServiceUtil.search(
+ currentUser.getCompanyId(),
+ null,
+ WorkflowConstants.STATUS_APPROVED,
+ userParams,
+ QueryUtil.ALL_POS,
+ QueryUtil.ALL_POS,
+ new UserLastNameComparator());
+ for (User u: lesUsers) {
+ retFiches.add(new ProxyFiche(currentUser, u));
+ }
+ }
+ }
+ } catch (SystemException e) {
+ logger.error(e);
+ } catch (PortalException e) {
+ logger.error(e);
+ }
+ return retFiches;
+ }
+
+
+
+ public static List<ProxyFiche> createUserListContacts(User currentUser, Liste l) {
+ List<ProxyFiche> retFiches = new ArrayList<ProxyFiche>();
+ try {
+ for (Fiche f: ListeLocalServiceUtil.getFiches(l.getListeId())) {
+ retFiches.add(new ProxyFiche(currentUser, f));
+ }
+ } catch (Exception e) {
+ logger.error(e);
+ }
+ return retFiches;
+ }
+
+ public static List<ProxyFiche> createSearchContacts(User currentUser, Long orgId, Long roleId, Long listId) throws SystemException {
+
+ // Is parent or children list ?
+ boolean isParentList = false;
+ boolean isChildrenList = false;
+ ListeInst listeInst = null;
+ try {
+ listeInst = ListeInstLocalServiceUtil.getListeInst(listId);
+ if (listeInst != null) {
+ isParentList = listeInst.isParentList();
+ isChildrenList = listeInst.isChildrenList();
+ }
+ } catch (Exception e){
+ logger.warn("Could not get liste inst " + listId);
+ }
+
+ return createSearchContacts(currentUser, orgId, roleId, listId, isParentList, isChildrenList, true);
+ }
+
+ public static List<ProxyFiche> createSearchContacts(User currentUser, Long orgId, Long roleId, Long listId, boolean parentList, boolean childrenList, boolean processSatut) throws SystemException {
+
+ List<ProxyFiche> retFiches = new ArrayList<ProxyFiche>();
+ final LinkedHashMap<String, Object> userParams = new LinkedHashMap<String, Object>();
+ if (orgId > 0) {
+ userParams.put("usersOrgs", orgId);
+ } else {
+ userParams.put("usersGroups", listId);
+ }
+ if (roleId>0) {
+ userParams.put("usersRoles", new Long(roleId));
+ }
+
+ // Particular case of parents and children lists (created by webservice only)
+ if (parentList) {
+ try {
+ List<UserRelationship> uRelations = UserRelationshipLocalServiceUtil.getUserRelationshipByFromUserIdRelationType(currentUser.getUserId(), UserRelationshipConstants.PARENT_RELATION);
+ for (UserRelationship urs: uRelations){
+ User parentUser = UserLocalServiceUtil.getUser(urs.getToUserId());
+ retFiches.add(new ProxyFiche(currentUser, parentUser, processSatut));
+ }
+ } catch (Exception e) {
+ logger.error(e);
+ }
+ return retFiches;
+ }
+ if (childrenList) {
+ try {
+ List<UserRelationship> uRelations = UserRelationshipLocalServiceUtil.getUserRelationshipByToUserIdRelationType(currentUser.getUserId(), UserRelationshipConstants.PARENT_RELATION);
+ for (UserRelationship urs: uRelations){
+ User childUser = UserLocalServiceUtil.getUser(urs.getFromUserId());
+ retFiches.add(new ProxyFiche(currentUser, childUser, processSatut));
+ }
+ } catch (Exception e) {
+ logger.error(e);
+ }
+ return retFiches;
+ }
+ // Classic search
+ final List<User> lesUsers = ExtUserLocalServiceUtil.search(
+ currentUser.getCompanyId(),
+ null,
+ WorkflowConstants.STATUS_APPROVED,
+ userParams,
+ -1,
+ -1,
+ (OrderByComparator) null);
+ for (User u: lesUsers) {
+ retFiches.add(new ProxyFiche(currentUser, u, processSatut));
+ }
+ return retFiches;
+ }
+
+ /**
+ * Just wrap
+ */
+ public static List<ProxyFiche> createSearchContacts(User currentUser, Long orgId, Long roleId, Long listId, String parentList, String childrenList, boolean processSatut) throws SystemException {
+ return ListFicheFactory.createSearchContacts(currentUser,orgId,roleId,listId,parentList.equals("true"),childrenList.equals("true"),processSatut);
+ }
+
+ public static List<ProxyFiche> createAmisContacts(User currentUser, List<Long> lesAmis) throws SystemException, PortalException {
+ List<ProxyFiche> retFiches = new ArrayList<ProxyFiche>();
+
+ for (Long l: lesAmis) {
+ User u = UserLocalServiceUtil.getUserById(l);
+ retFiches.add(new ProxyFiche(currentUser, u));
+ }
+ return retFiches;
+ }
+/*
+ public static List<ProxyFiche> createOrgContacts(User currentUser, Organization org) {
+ List<ProxyFiche> retFiches = new ArrayList<ProxyFiche>();
+ try {
+ for (User u: ExtUserLocalServiceUtil.getOrganizationUsers(org.getOrganizationId())) {
+ retFiches.add(new ProxyFiche(currentUser, u));
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return retFiches;
+ }*/
+
+
+
+}