001/*
002 * (C) Copyright 2014 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 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
018 */
019package org.nuxeo.ecm.collections.core.automation;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Locale;
025import java.util.Map;
026
027import net.sf.json.JSONArray;
028import net.sf.json.JSONObject;
029
030import org.apache.commons.lang.StringUtils;
031import org.nuxeo.ecm.automation.AutomationService;
032import org.nuxeo.ecm.automation.OperationChain;
033import org.nuxeo.ecm.automation.OperationContext;
034import org.nuxeo.ecm.automation.OperationException;
035import org.nuxeo.ecm.automation.OperationParameters;
036import org.nuxeo.ecm.automation.core.Constants;
037import org.nuxeo.ecm.automation.core.annotations.Context;
038import org.nuxeo.ecm.automation.core.annotations.Operation;
039import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
040import org.nuxeo.ecm.automation.core.annotations.Param;
041import org.nuxeo.ecm.automation.core.operations.services.DocumentPageProviderOperation;
042import org.nuxeo.ecm.automation.core.util.StringList;
043import org.nuxeo.ecm.automation.features.SuggestConstants;
044import org.nuxeo.ecm.collections.api.CollectionConstants;
045import org.nuxeo.ecm.collections.api.CollectionManager;
046import org.nuxeo.ecm.core.api.Blob;
047import org.nuxeo.ecm.core.api.Blobs;
048import org.nuxeo.ecm.core.api.CoreSession;
049import org.nuxeo.ecm.core.api.DocumentModel;
050import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider;
051
052/**
053 * @since 5.9.3
054 */
055@Operation(id = SuggestCollectionEntry.ID, category = Constants.CAT_SERVICES, label = "Get collection suggestion", description = "Get the collection list accessible by the current user. This is returning a blob containing a serialized JSON array..", addToStudio = false)
056public class SuggestCollectionEntry {
057
058    public static final String ID = "Collection.Suggestion";
059
060    private static final String PATH = "path";
061
062    @Param(name = "currentPageIndex", required = false)
063    protected Integer currentPageIndex = 0;
064
065    @Param(name = "pageSize", required = false)
066    protected Integer pageSize = 20;
067
068    @Context
069    protected AutomationService service;
070
071    @Context
072    protected OperationContext ctx;
073
074    @Context
075    protected CoreSession session;
076
077    @Context
078    protected CollectionManager collectionManager;
079
080    @Param(name = "lang", required = false)
081    protected String lang;
082
083    @Param(name = "searchTerm", required = false)
084    protected String searchTerm;
085
086    @OperationMethod
087    public Blob run() throws OperationException {
088        JSONArray result = new JSONArray();
089        Map<String, Serializable> props = new HashMap<String, Serializable>();
090        props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) session);
091
092        Map<String, Object> vars = ctx.getVars();
093
094        StringList sl = new StringList();
095        sl.add(searchTerm + (searchTerm.endsWith("%") ? "" : "%"));
096        sl.add(DocumentPageProviderOperation.CURRENT_USERID_PATTERN);
097        vars.put("queryParams", sl);
098        vars.put("providerName", CollectionConstants.COLLECTION_PAGE_PROVIDER);
099        OperationContext subctx = new OperationContext(ctx.getCoreSession(), vars);
100        OperationChain chain = new OperationChain("operation");
101        OperationParameters oparams = new OperationParameters(DocumentPageProviderOperation.ID, vars);
102        chain.add(oparams);
103        @SuppressWarnings("unchecked")
104        List<DocumentModel> docs = (List<DocumentModel>) service.run(subctx, chain);
105
106        boolean found = false;
107        for (DocumentModel doc : docs) {
108            JSONObject obj = new JSONObject();
109            if (collectionManager.canAddToCollection(doc, session)) {
110                obj.element(SuggestConstants.ID, doc.getId());
111            }
112            if (doc.getTitle().equals(searchTerm)) {
113                found = true;
114            }
115            obj.element(SuggestConstants.LABEL, doc.getTitle());
116            if (StringUtils.isNotBlank((String) doc.getProperty("common", "icon"))) {
117                obj.element(SuggestConstants.ICON, doc.getProperty("common", "icon"));
118            }
119            obj.element(PATH, doc.getPath().toString());
120            result.add(obj);
121        }
122
123        if (!found && StringUtils.isNotBlank(searchTerm)) {
124            JSONObject obj = new JSONObject();
125            obj.element(SuggestConstants.LABEL, searchTerm);
126            obj.element(SuggestConstants.ID, CollectionConstants.MAGIC_PREFIX_ID + searchTerm);
127            result.add(0, obj);
128        }
129
130        return Blobs.createBlob(result.toString(), "application/json");
131    }
132
133    protected Locale getLocale() {
134        return new Locale(getLang());
135    }
136
137    protected String getLang() {
138        if (lang == null) {
139            lang = (String) ctx.get("lang");
140            if (lang == null) {
141                lang = SuggestConstants.DEFAULT_LANG;
142            }
143        }
144        return lang;
145    }
146
147}