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 * @deprecated since 9.1 not used anymore
041 */
042@Deprecated
043public class RepositoryPathTitleAdapter extends BaseIndexingAdapter {
044
045    public static final String PATH_TITLE_PROPERTY = "ecm:pathTitle";
046
047    @SuppressWarnings("unused")
048    private static final Log log = LogFactory.getLog(RepositoryPathTitleAdapter.class);
049
050    public static final String PATH_SEPARATOR = "/";
051
052    @Override
053    public DocumentProperty[] adaptDocumentNoBlobProperties(CoreSession session, String uuid,
054            DocumentProperty[] properties) {
055        return addPathTitleProperty(session, uuid, properties);
056    }
057
058    @Override
059    public DocumentProperty[] adaptDocumentProperties(CoreSession session, String uuid, DocumentProperty[] properties)
060            {
061        return addPathTitleProperty(session, uuid, properties);
062    }
063
064    protected DocumentProperty[] addPathTitleProperty(CoreSession session, String uuid, DocumentProperty[] properties)
065            {
066
067        IdRef docRef = new IdRef(uuid);
068        List<DocumentModel> parentDocuments = session.getParentDocuments(docRef);
069        // remove the current document from the list of ancestors
070        parentDocuments = parentDocuments.subList(0, parentDocuments.size() - 1);
071
072        // build a list with all the existing properties for document with ref
073        // uuid
074        List<DocumentProperty> enhancedProperties = new ArrayList<>();
075        enhancedProperties.addAll(Arrays.asList(properties));
076
077        // fetch the list of ancestor titles and build the new property
078        List<String> titles = new ArrayList<>(parentDocuments.size());
079        for (DocumentModel ancestor : parentDocuments) {
080            titles.add(ancestor.getTitle());
081        }
082        String pathTitle = String.join(PATH_SEPARATOR, titles);
083        enhancedProperties.add(new DocumentProperty(PATH_TITLE_PROPERTY, pathTitle));
084
085        return enhancedProperties.toArray(new DocumentProperty[enhancedProperties.size()]);
086    }
087}