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