001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Benjamin Jalon
018 */
019
020package org.nuxeo.ecm.platform.computedgroups;
021
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.core.api.IterableQueryResult;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
032import org.nuxeo.ecm.core.api.repository.RepositoryManager;
033import org.nuxeo.ecm.platform.usermanager.NuxeoPrincipalImpl;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Configurable Group Computer based on Metadata of Documents. Documents Selection is managed by NXQL where clause.
038 *
039 * @since 5.7.3
040 */
041public class DocumentMetadataGroupComputer extends AbstractGroupComputer {
042
043    public static final Log log = LogFactory.getLog(DocumentMetadataGroupComputer.class);
044
045    private String groupPattern;
046
047    private String whereClause;
048
049    private String xpath;
050
051    public DocumentMetadataGroupComputer(String whereClause, String groupPattern, String xpath) {
052        this.whereClause = whereClause;
053        this.xpath = xpath;
054        this.groupPattern = groupPattern;
055        if (whereClause == null || whereClause.isEmpty() || groupPattern == null || groupPattern.isEmpty()) {
056            throw new NuxeoException("Bad Contribution Document Metadata Computer Group Configuration");
057        }
058    }
059
060    @Override
061    public List<String> getAllGroupIds() {
062        List<String> groupIds = new ArrayList<>();
063        return groupIds;
064    }
065
066    @Override
067    public List<String> getGroupMembers(String groupId) {
068
069        List<String> participants = new ArrayList<>();
070        return participants;
071    }
072
073    @Override
074    public List<String> getGroupsForUser(NuxeoPrincipalImpl user) {
075        String username = user.getName();
076        GetDocumentsFromUsername runner = new GetDocumentsFromUsername(getRepository(), whereClause, username, xpath);
077        runner.runUnrestricted();
078
079        List<String> groupIds = new ArrayList<>();
080        String groupId = null;
081
082        for (String value : runner.result) {
083            groupId = getGroupIdFromValue(value);
084            log.debug("Virtual Group Id found: " + groupId);
085            groupIds.add(groupId);
086        }
087        return groupIds;
088    }
089
090    @Override
091    public List<String> getParentsGroupNames(String groupID) {
092        return new ArrayList<>();
093    }
094
095    @Override
096    public List<String> getSubGroupsNames(String groupID) {
097        return new ArrayList<>();
098    }
099
100    @Override
101    public boolean hasGroup(String groupId) {
102        return false;
103    }
104
105    protected class GetDocumentsFromUsername extends UnrestrictedSessionRunner {
106        private static final String QUERY_PATTERN = "SELECT %s " + "FROM Document %s";
107
108        protected String username;
109
110        protected String xpath;
111
112        protected String whereClausePattern;
113
114        public List<String> result = new ArrayList<>();
115
116        protected GetDocumentsFromUsername(String repositoryName, String whereClause, String username, String xpath) {
117            super(repositoryName);
118            this.username = username;
119            whereClausePattern = whereClause;
120            this.xpath = xpath;
121        }
122
123        @Override
124        public void run() {
125            String whereClause = String.format(whereClausePattern, username);
126            String query = String.format(QUERY_PATTERN, xpath, whereClause);
127
128            try (IterableQueryResult docs = session.queryAndFetch(query, "NXQL")) {
129                for (Map<String, Serializable> doc : docs) {
130                    String value = (String) doc.get(xpath);
131                    if (value != null && !value.isEmpty() && !result.contains(value)) {
132                        result.add(value);
133                    }
134                }
135            }
136        }
137    }
138
139    private String getRepository() {
140        RepositoryManager mgr = Framework.getService(RepositoryManager.class);
141        return mgr.getDefaultRepositoryName();
142    }
143
144    private String getGroupIdFromValue(String value) {
145        return String.format(groupPattern, value);
146    }
147
148}