001/*
002 * (C) Copyright 2011 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 *     matic
018 */
019package org.nuxeo.ecm.platform.annotations.repository.service;
020
021import java.io.IOException;
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.nuxeo.ecm.core.api.Blobs;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
032import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
033import org.nuxeo.ecm.core.convert.api.ConversionService;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * @author matic
038 */
039public class AnnotationsFulltextInjector {
040
041    public static final String RELATED_TEXT_PROPERTY = "relatedtext";
042
043    public static final String RELATED_TEXT_ID_PROPERTY = "relatedtextid";
044
045    public static final String RELATED_TEXT_LIST_PROPERTY = "relatedtext:relatedtextresources";
046
047    public static final String ANNOTATION_RESOURCE_ID_PREFIX = "annotation_";
048
049    public boolean removeAnnotationText(DocumentModel doc, String annotationId) {
050        @SuppressWarnings("unchecked")
051        List<Map<String, String>> relatedResources = doc.getProperty(RELATED_TEXT_LIST_PROPERTY).getValue(List.class);
052        String resourceIdToRemove = annotationId == null ? null : makeResourceId(annotationId);
053        List<Map<String, String>> resourcesToRemove = new ArrayList<Map<String, String>>();
054        for (Map<String, String> resource : relatedResources) {
055            String resourceId = resource.get(RELATED_TEXT_ID_PROPERTY);
056            if (resourceIdToRemove != null) {
057                if (resourceIdToRemove.equals(resourceId)) {
058                    resourcesToRemove.add(resource);
059                }
060            } else {
061                // remove all annotations
062                if (resourceId == null || resourceId.startsWith(ANNOTATION_RESOURCE_ID_PREFIX)) {
063                    resourcesToRemove.add(resource);
064                }
065            }
066        }
067        if (!resourcesToRemove.isEmpty()) {
068            relatedResources.removeAll(resourcesToRemove);
069            doc.setPropertyValue(RELATED_TEXT_LIST_PROPERTY, (Serializable) relatedResources);
070            return true;
071        }
072        return false;
073    }
074
075    public void setAnnotationText(DocumentModel doc, String annotationId, String annotationBody) {
076        if (annotationBody == null) {
077            return;
078        }
079        // strip HTML markup if any
080        BlobHolder bh = new SimpleBlobHolder(Blobs.createBlob(annotationBody, "text/html"));
081        ConversionService service = Framework.getService(ConversionService.class);
082        if (service != null) {
083            try {
084                annotationBody = service.convert("html2text", bh, null).getBlob().getString();
085            } catch (IOException e) {
086                throw new NuxeoException(e);
087            }
088        }
089        @SuppressWarnings("unchecked")
090        List<Map<String, String>> relatedResources = doc.getProperty(RELATED_TEXT_LIST_PROPERTY).getValue(List.class);
091        HashMap<String, String> resource = new HashMap<String, String>();
092        resource.put(RELATED_TEXT_ID_PROPERTY, makeResourceId(annotationId));
093        resource.put(RELATED_TEXT_PROPERTY, annotationBody);
094        relatedResources.add(resource);
095        doc.setPropertyValue(RELATED_TEXT_LIST_PROPERTY, (Serializable) relatedResources);
096    }
097
098    protected static String makeResourceId(String annotationId) {
099        return ANNOTATION_RESOURCE_ID_PREFIX + annotationId;
100    }
101
102}