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            vars.put("queryParams", sl);
094            vars.put("providerName", CollectionConstants.COLLECTION_PAGE_PROVIDER);
095        }
096        OperationChain chain = new OperationChain("operation");
097        OperationParameters oparams = new OperationParameters(DocumentPageProviderOperation.ID, vars);
098        chain.add(oparams);
099        @SuppressWarnings("unchecked")
100        List<DocumentModel> docs = (List<DocumentModel>) service.run(ctx, chain);
101
102        boolean found = false;
103        for (DocumentModel doc : docs) {
104            Map<String, Object> obj = new LinkedHashMap<>();
105            if (collectionManager.canAddToCollection(doc, session)) {
106                obj.put(SuggestConstants.ID, doc.getId());
107            }
108            if (doc.getTitle().equals(searchTerm)) {
109                found = true;
110            }
111            obj.put(SuggestConstants.LABEL, doc.getTitle());
112            if (StringUtils.isNotBlank((String) doc.getProperty("common", "icon"))) {
113                obj.put(SuggestConstants.ICON, doc.getProperty("common", "icon"));
114            }
115            obj.put(PATH, doc.getPath().toString());
116            result.add(obj);
117        }
118
119        if (!found && StringUtils.isNotBlank(searchTerm)) {
120            Map<String, Object> obj = new LinkedHashMap<>();
121            obj.put(SuggestConstants.LABEL, searchTerm);
122            obj.put(SuggestConstants.ID, CollectionConstants.MAGIC_PREFIX_ID + searchTerm);
123            result.add(0, obj);
124        }
125
126        return Blobs.createJSONBlobFromValue(result);
127    }
128
129    protected Locale getLocale() {
130        return new Locale(getLang());
131    }
132
133    protected String getLang() {
134        if (lang == null) {
135            lang = (String) ctx.get("lang");
136            if (lang == null) {
137                lang = SuggestConstants.DEFAULT_LANG;
138            }
139        }
140        return lang;
141    }
142
143}