001/*
002 * (C) Copyright 2009 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 *     Thomas Roger
018 *     Marwane Kalam-Alami
019 */
020
021package org.nuxeo.ecm.platform.publisher.web;
022
023import java.util.ArrayList;
024import java.util.List;
025import java.util.Map;
026
027import org.jboss.seam.annotations.In;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.security.SecurityConstants;
031import org.nuxeo.ecm.core.schema.types.Type;
032import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
033
034/**
035 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
036 */
037public abstract class AbstractPublishActions {
038
039    @In(create = true)
040    protected transient NavigationContext navigationContext;
041
042    @In(create = true, required = false)
043    protected transient CoreSession documentManager;
044
045    @In(create = true)
046    protected Map<String, String> messages;
047
048    public String getFormattedPath(DocumentModel documentModel) {
049        List<String> pathFragments = new ArrayList<>();
050        getPathFragments(documentModel, pathFragments);
051        return formatPathFragments(pathFragments);
052    }
053
054    protected static String formatPathFragments(List<String> pathFragments) {
055        String fullPath = "";
056        for (String aFragment : pathFragments) {
057            if (!"".equals(fullPath)) {
058                fullPath = ">" + fullPath;
059            }
060            fullPath = aFragment + fullPath;
061        }
062        return fullPath;
063    }
064
065    protected void getPathFragments(DocumentModel documentModel, List<String> pathFragments) {
066        String pathElementName = documentModel.getTitle();
067        String translatedPathElement = messages.get(pathElementName);
068        pathFragments.add(translatedPathElement);
069
070        if (isDomain(documentModel) || "/".equals(documentModel.getPathAsString())) {
071            return;
072        }
073
074        DocumentModel parentDocument = getParentDocument(documentModel);
075        if (parentDocument != null) {
076            getPathFragments(parentDocument, pathFragments);
077        }
078    }
079
080    protected DocumentModel getParentDocument(DocumentModel documentModel) {
081        if (documentManager.hasPermission(documentModel.getParentRef(), SecurityConstants.READ)) {
082            return documentManager.getDocument(documentModel.getParentRef());
083        }
084        return null;
085    }
086
087    protected boolean isDomain(DocumentModel documentModel) {
088        Type type = documentModel.getDocumentType();
089        while (type != null) {
090            if ("Domain".equals(type.getName())) {
091                return true;
092            }
093            type = type.getSuperType();
094        }
095        return false;
096    }
097
098}