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 Renard</a> 018 */ 019package org.nuxeo.ecm.platform.tag.automation; 020 021import java.util.Collections; 022import java.util.List; 023 024import net.sf.json.JSONArray; 025import net.sf.json.JSONObject; 026 027import org.apache.commons.lang.StringUtils; 028import org.jboss.seam.Component; 029import org.nuxeo.ecm.automation.OperationContext; 030import org.nuxeo.ecm.automation.core.Constants; 031import org.nuxeo.ecm.automation.core.annotations.Context; 032import org.nuxeo.ecm.automation.core.annotations.Operation; 033import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 034import org.nuxeo.ecm.automation.core.annotations.Param; 035import org.nuxeo.ecm.core.api.Blob; 036import org.nuxeo.ecm.core.api.Blobs; 037import org.nuxeo.ecm.core.api.CoreSession; 038import org.nuxeo.ecm.core.api.DocumentModel; 039import org.nuxeo.ecm.platform.tag.Tag; 040import org.nuxeo.ecm.platform.tag.TagService; 041import org.nuxeo.ecm.platform.ui.select2.common.Select2Common; 042import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 043 044/** 045 * @since 6.0 046 */ 047@Operation(id = SuggestTagEntry.ID, category = Constants.CAT_SERVICES, label = "Get tag suggestion", description = "Get tag suggestion") 048public class SuggestTagEntry { 049 050 public static final String ID = "Tag.Suggestion"; 051 052 @Context 053 protected OperationContext ctx; 054 055 @Context 056 protected CoreSession documentManager; 057 058 @Context 059 protected TagService tagService; 060 061 @Param(name = "searchTerm", required = false) 062 protected String searchTerm; 063 064 @Param(name = "value", required = false) 065 protected String value; 066 067 @Param(name = "xpath", required = false) 068 protected String xpath; 069 070 @OperationMethod 071 public Blob run() { 072 JSONArray result = new JSONArray(); 073 if (tagService != null && tagService.isEnabled()) { 074 if (!StringUtils.isEmpty(value)) { 075 final NavigationContext navigationContext = (NavigationContext) Component.getInstance("navigationContext"); 076 DocumentModel currentDocument = navigationContext.getCurrentDocument(); 077 if (currentDocument == null) { 078 return null; 079 } else { 080 String docId = currentDocument.getId(); 081 List<Tag> tags = tagService.getDocumentTags(documentManager, docId, null); 082 Collections.sort(tags, Tag.LABEL_COMPARATOR); 083 for (Tag tag : tags) { 084 JSONObject obj = new JSONObject(); 085 obj.element(Select2Common.ID, tag.getLabel()); 086 obj.element(Select2Common.LABEL, tag.getLabel()); 087 result.add(obj); 088 } 089 } 090 } else { 091 if (!StringUtils.isBlank(searchTerm)) { 092 List<Tag> tags = tagService.getSuggestions(documentManager, searchTerm, null); 093 Collections.sort(tags, Tag.LABEL_COMPARATOR); 094 for (int i = 0; i < 10 && i < tags.size(); i++) { 095 JSONObject obj = new JSONObject(); 096 Tag tag = tags.get(i); 097 obj.element(Select2Common.ID, tag.getLabel()); 098 obj.element(Select2Common.LABEL, tag.getLabel()); 099 result.add(obj); 100 } 101 } 102 } 103 } 104 return Blobs.createBlob(result.toString(), "application/json"); 105 } 106 107}