001/*
002 * (C) Copyright 2014-2018 Nuxeo (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.automation.core.operations.collections;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.LinkedHashMap;
025import java.util.List;
026import java.util.Locale;
027import java.util.Map;
028
029import org.apache.commons.lang3.StringUtils;
030import org.nuxeo.ecm.automation.AutomationService;
031import org.nuxeo.ecm.automation.OperationChain;
032import org.nuxeo.ecm.automation.OperationContext;
033import org.nuxeo.ecm.automation.OperationException;
034import org.nuxeo.ecm.automation.OperationParameters;
035import org.nuxeo.ecm.automation.core.Constants;
036import org.nuxeo.ecm.automation.core.annotations.Context;
037import org.nuxeo.ecm.automation.core.annotations.Operation;
038import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
039import org.nuxeo.ecm.automation.core.annotations.Param;
040import org.nuxeo.ecm.automation.core.operations.services.DocumentPageProviderOperation;
041import org.nuxeo.ecm.automation.core.util.PageProviderHelper;
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;
050
051/**
052 * @since 5.9.3
053 */
054@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)
055public class SuggestCollectionEntry {
056
057    public static final String ID = "Collection.Suggestion";
058
059    private static final String PATH = "path";
060
061    @Param(name = "currentPageIndex", required = false)
062    protected Integer currentPageIndex = 0;
063
064    @Param(name = "pageSize", required = false)
065    protected Integer pageSize = 20;
066
067    @Context
068    protected AutomationService service;
069
070    @Context
071    protected OperationContext ctx;
072
073    @Context
074    protected CoreSession session;
075
076    @Context
077    protected CollectionManager collectionManager;
078
079    @Param(name = "lang", required = false)
080    protected String lang;
081
082    @Param(name = "searchTerm", required = false)
083    protected String searchTerm;
084
085    @OperationMethod
086    public Blob run() throws OperationException, IOException {
087        List<Map<String, Object>> result = new ArrayList<>();
088
089        Map<String, Object> vars = new HashMap<>();
090        {
091            StringList sl = new StringList();
092            sl.add(searchTerm + (searchTerm.endsWith("%") ? "" : "%"));
093            sl.add(PageProviderHelper.CURRENT_USERID_PATTERN);
094            vars.put("queryParams", sl);
095            vars.put("providerName", CollectionConstants.COLLECTION_PAGE_PROVIDER);
096        }
097        OperationChain chain = new OperationChain("operation");
098        OperationParameters oparams = new OperationParameters(DocumentPageProviderOperation.ID, vars);
099        chain.add(oparams);
100        @SuppressWarnings("unchecked")
101        List<DocumentModel> docs = (List<DocumentModel>) service.run(ctx, chain);
102
103        boolean found = false;
104        for (DocumentModel doc : docs) {
105            Map<String, Object> obj = new LinkedHashMap<>();
106            if (collectionManager.canAddToCollection(doc, session)) {
107                obj.put(SuggestConstants.ID, doc.getId());
108            }
109            if (doc.getTitle().equals(searchTerm)) {
110                found = true;
111            }
112            obj.put(SuggestConstants.LABEL, doc.getTitle());
113            if (StringUtils.isNotBlank((String) doc.getProperty("common", "icon"))) {
114                obj.put(SuggestConstants.ICON, doc.getProperty("common", "icon"));
115            }
116            obj.put(PATH, doc.getPath().toString());
117            result.add(obj);
118        }
119
120        if (!found && StringUtils.isNotBlank(searchTerm)) {
121            Map<String, Object> obj = new LinkedHashMap<>();
122            obj.put(SuggestConstants.LABEL, searchTerm);
123            obj.put(SuggestConstants.ID, CollectionConstants.MAGIC_PREFIX_ID + searchTerm);
124            result.add(0, obj);
125        }
126
127        return Blobs.createJSONBlobFromValue(result);
128    }
129
130    protected Locale getLocale() {
131        return new Locale(getLang());
132    }
133
134    protected String getLang() {
135        if (lang == null) {
136            lang = (String) ctx.get("lang");
137            if (lang == null) {
138                lang = SuggestConstants.DEFAULT_LANG;
139            }
140        }
141        return lang;
142    }
143
144}