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