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.tag.ws;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.platform.api.ws.DocumentLoader;
027import org.nuxeo.ecm.platform.api.ws.DocumentProperty;
028import org.nuxeo.ecm.platform.api.ws.session.WSRemotingSession;
029import org.nuxeo.ecm.platform.tag.TagService;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * @author matic
034 */
035public class TagsLoader implements DocumentLoader {
036
037    @Override
038    public void fillProperties(DocumentModel doc, List<DocumentProperty> props, WSRemotingSession rs)
039            {
040        CoreSession session = rs.getDocumentManager();
041        TagService srv = Framework.getService(TagService.class);
042        if (srv == null) {
043            return;
044        }
045        List<String> tags = new ArrayList<>(srv.getTags(session, doc.getId()));
046        String value = "";
047        String sep = "";
048        for (String tag : tags) {
049            value = value + sep + tag;
050            sep = ",";
051        }
052        DocumentProperty prop = new DocumentProperty("tags", value);
053        props.add(prop);
054    }
055
056}