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