001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
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 *     Thomas Roger <troger@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.multi.tenant.operations;
019
020import java.io.Serializable;
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.HashMap;
024import java.util.HashSet;
025import java.util.List;
026import java.util.Map;
027import java.util.Set;
028
029import net.sf.json.JSONArray;
030import net.sf.json.JSONObject;
031
032import org.apache.commons.lang.StringUtils;
033import org.nuxeo.ecm.automation.core.Constants;
034import org.nuxeo.ecm.automation.core.annotations.Context;
035import org.nuxeo.ecm.automation.core.annotations.Operation;
036import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
037import org.nuxeo.ecm.automation.core.annotations.Param;
038import org.nuxeo.ecm.core.api.Blob;
039import org.nuxeo.ecm.core.api.Blobs;
040import org.nuxeo.ecm.core.api.DocumentModel;
041import org.nuxeo.ecm.platform.usermanager.UserManager;
042
043/**
044 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
045 * @since 5.6
046 */
047@Operation(id = QueryUsers.ID, category = Constants.CAT_SERVICES, label = "Query users", description = "Query users filtered by a tenantId if provided.")
048public class QueryUsers {
049
050    public static final String ID = "Services.QueryUsers";
051
052    public static final Set<String> FULLTEXT_FIELDS = new HashSet<String>(Arrays.asList("username", "firstName",
053            "lastName"));
054
055    @Context
056    protected UserManager userManager;
057
058    @Param(name = "pattern", required = false)
059    protected String pattern;
060
061    @Param(name = "tenantId", required = false)
062    protected String tenantId;
063
064    @OperationMethod
065    public Blob run() {
066        List<DocumentModel> users = new ArrayList<DocumentModel>();
067        if (StringUtils.isBlank(pattern)) {
068            Map<String, Serializable> filter = new HashMap<String, Serializable>();
069            if (!StringUtils.isBlank(tenantId)) {
070                filter.put("tenantId", tenantId);
071            }
072
073            users.addAll(userManager.searchUsers(filter, filter.keySet()));
074        } else {
075            for (String field : FULLTEXT_FIELDS) {
076                Map<String, Serializable> filter = new HashMap<String, Serializable>();
077                filter.put(field, pattern);
078
079                if (!StringUtils.isBlank(tenantId)) {
080                    filter.put("tenantId", tenantId);
081                }
082
083                users.addAll(userManager.searchUsers(filter, filter.keySet()));
084            }
085        }
086
087        return buildResponse(users);
088    }
089
090    protected Blob buildResponse(List<DocumentModel> users) {
091
092        JSONArray array = new JSONArray();
093        for (DocumentModel user : users) {
094            JSONObject o = new JSONObject();
095            o.element("username", user.getProperty("user", "username"));
096            o.element("firstName", user.getProperty("user", "firstName"));
097            o.element("lastName", user.getProperty("user", "lastName"));
098            o.element("email", user.getProperty("user", "email"));
099            o.element("company", user.getProperty("user", "company"));
100            o.element("tenantId", user.getProperty("user", "tenantId"));
101            array.add(o);
102        }
103
104        JSONObject result = new JSONObject();
105        result.put("users", array);
106
107        return Blobs.createBlob(result.toString(), "application/json");
108    }
109}