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