001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Benjamin Jalon 016 */ 017 018package org.nuxeo.ecm.platform.computedgroups; 019 020import java.io.Serializable; 021import java.util.ArrayList; 022import java.util.List; 023import java.util.Map; 024 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.ecm.core.api.IterableQueryResult; 028import org.nuxeo.ecm.core.api.NuxeoException; 029import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner; 030import org.nuxeo.ecm.core.api.repository.RepositoryManager; 031import org.nuxeo.ecm.platform.usermanager.NuxeoPrincipalImpl; 032import org.nuxeo.runtime.api.Framework; 033 034/** 035 * Configurable Group Computer based on Metadata of Documents. Documents Selection is managed by NXQL where clause. 036 * 037 * @since 5.7.3 038 */ 039public class DocumentMetadataGroupComputer extends AbstractGroupComputer { 040 041 public static final Log log = LogFactory.getLog(DocumentMetadataGroupComputer.class); 042 043 private String groupPattern; 044 045 private String whereClause; 046 047 private String xpath; 048 049 public DocumentMetadataGroupComputer(String whereClause, String groupPattern, String xpath) { 050 this.whereClause = whereClause; 051 this.xpath = xpath; 052 this.groupPattern = groupPattern; 053 if (whereClause == null || whereClause.isEmpty() || groupPattern == null || groupPattern.isEmpty()) { 054 throw new NuxeoException("Bad Contribution Document Metadata Computer Group Configuration"); 055 } 056 } 057 058 @Override 059 public List<String> getAllGroupIds() { 060 List<String> groupIds = new ArrayList<String>(); 061 return groupIds; 062 } 063 064 @Override 065 public List<String> getGroupMembers(String groupId) { 066 067 List<String> participants = new ArrayList<String>(); 068 return participants; 069 } 070 071 @Override 072 public List<String> getGroupsForUser(NuxeoPrincipalImpl user) { 073 String username = user.getName(); 074 GetDocumentsFromUsername runner = new GetDocumentsFromUsername(getRepository(), whereClause, username, xpath); 075 runner.runUnrestricted(); 076 077 List<String> groupIds = new ArrayList<String>(); 078 String groupId = null; 079 080 for (String value : runner.result) { 081 groupId = getGroupIdFromValue(value); 082 log.debug("Virtual Group Id found: " + groupId); 083 groupIds.add(groupId); 084 } 085 return groupIds; 086 } 087 088 @Override 089 public List<String> getParentsGroupNames(String groupID) { 090 return new ArrayList<String>(); 091 } 092 093 @Override 094 public List<String> getSubGroupsNames(String groupID) { 095 return new ArrayList<String>(); 096 } 097 098 @Override 099 public boolean hasGroup(String groupId) { 100 return false; 101 } 102 103 protected class GetDocumentsFromUsername extends UnrestrictedSessionRunner { 104 private static final String QUERY_PATTERN = "SELECT %s " + "FROM Document %s"; 105 106 protected String username; 107 108 protected String xpath; 109 110 protected String whereClausePattern; 111 112 public List<String> result = new ArrayList<String>(); 113 114 protected GetDocumentsFromUsername(String repositoryName, String whereClause, String username, String xpath) { 115 super(repositoryName); 116 this.username = username; 117 whereClausePattern = whereClause; 118 this.xpath = xpath; 119 } 120 121 @Override 122 public void run() { 123 String whereClause = String.format(whereClausePattern, username); 124 String query = String.format(QUERY_PATTERN, xpath, whereClause); 125 126 IterableQueryResult docs = session.queryAndFetch(query, "NXQL"); 127 for (Map<String, Serializable> doc : docs) { 128 String value = (String) doc.get(xpath); 129 if (value != null && !value.isEmpty() && !result.contains(value)) { 130 result.add(value); 131 } 132 } 133 134 } 135 } 136 137 private String getRepository() { 138 RepositoryManager mgr = Framework.getLocalService(RepositoryManager.class); 139 return mgr.getDefaultRepositoryName(); 140 } 141 142 private String getGroupIdFromValue(String value) { 143 return String.format(groupPattern, value); 144 } 145 146}