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