001/*
002 * (C) Copyright 2008 Nuxeo SAS (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 *     Nuxeo - initial API and implementation
016 *
017 * $Id: $
018 */
019package org.nuxeo.ecm.platform.indexing.gateway.adapter;
020
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.List;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.common.utils.StringUtils;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.IdRef;
031import org.nuxeo.ecm.platform.api.ws.DocumentProperty;
032
033/**
034 * Adapter to build a new property ecm:pathTitle holding the physical path in the repository with human readable titles
035 * instead of technical local path ids found in the default ecm:path property.
036 *
037 * @author Olivier Grisel <ogrisel@nuxeo.com>
038 */
039public class RepositoryPathTitleAdapter extends BaseIndexingAdapter {
040
041    public static final String PATH_TITLE_PROPERTY = "ecm:pathTitle";
042
043    @SuppressWarnings("unused")
044    private static final Log log = LogFactory.getLog(RepositoryPathTitleAdapter.class);
045
046    public static final String PATH_SEPARATOR = "/";
047
048    @Override
049    public DocumentProperty[] adaptDocumentNoBlobProperties(CoreSession session, String uuid,
050            DocumentProperty[] properties) {
051        return addPathTitleProperty(session, uuid, properties);
052    }
053
054    @Override
055    public DocumentProperty[] adaptDocumentProperties(CoreSession session, String uuid, DocumentProperty[] properties)
056            {
057        return addPathTitleProperty(session, uuid, properties);
058    }
059
060    protected DocumentProperty[] addPathTitleProperty(CoreSession session, String uuid, DocumentProperty[] properties)
061            {
062
063        IdRef docRef = new IdRef(uuid);
064        List<DocumentModel> parentDocuments = session.getParentDocuments(docRef);
065        // remove the current document from the list of ancestors
066        parentDocuments = parentDocuments.subList(0, parentDocuments.size() - 1);
067
068        // build a list with all the existing properties for document with ref
069        // uuid
070        List<DocumentProperty> enhancedProperties = new ArrayList<DocumentProperty>();
071        enhancedProperties.addAll(Arrays.asList(properties));
072
073        // fetch the list of ancestor titles and build the new property
074        List<String> titles = new ArrayList<String>(parentDocuments.size());
075        for (DocumentModel ancestor : parentDocuments) {
076            titles.add(ancestor.getTitle());
077        }
078        String pathTitle = StringUtils.join(titles, PATH_SEPARATOR);
079        enhancedProperties.add(new DocumentProperty(PATH_TITLE_PROPERTY, pathTitle));
080
081        return enhancedProperties.toArray(new DocumentProperty[enhancedProperties.size()]);
082    }
083}