/*
 * (C) Copyright 2009 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Thomas Roger
 */

package org.nuxeo.ecm.platform.publisher.web;

import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.webapp.helpers.ResourcesAccessor;
import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
import org.jboss.seam.annotations.In;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.List;
import java.util.ArrayList;

/**
 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
 */
public abstract class AbstractPublishActions {

    private static final Log log = LogFactory.getLog(AbstractPublishActions.class);

    @In(create = true)
    protected transient NavigationContext navigationContext;

    @In(create = true, required = false)
    protected transient CoreSession documentManager;

    @In(create = true)
    protected transient ResourcesAccessor resourcesAccessor;

    public String getFormattedPath(DocumentModel documentModel)
            throws ClientException {
        List<String> pathFragments = new ArrayList<String>();
        getPathFragments(documentModel, pathFragments);
        return formatPathFragments(pathFragments);
    }

    protected static String formatPathFragments(List<String> pathFragments) {
        String fullPath = "";
        for (String aFragment : pathFragments) {
            if (!"".equals(fullPath)) {
                fullPath = ">" + fullPath;
            }
            fullPath = aFragment + fullPath;
        }
        return fullPath;
    }

    protected void getPathFragments(DocumentModel documentModel,
            List<String> pathFragments) throws ClientException {
        String pathElementName = documentModel.getTitle();
        String translatedPathElement = resourcesAccessor.getMessages().get(
                pathElementName);
        pathFragments.add(translatedPathElement);
        if ("Domain".equals(documentModel.getType())) {
            return;
        }

        DocumentModel parentDocument;
        try {
            parentDocument = documentManager.getDocument(documentModel.getParentRef());
        } catch (Exception e) {
            log.error("Error building path", e);
            return;
        }
        getPathFragments(parentDocument, pathFragments);
    }

}

@@ -693,424 +693,482 @@
 ort 
-org.nuxeo.ecm.core.api.DocumentModel;%0Aimport org.nuxeo.ecm.core.api.ClientException;%0Aimport org.nuxeo.ecm.core.api.CoreSession;%0Aimport org.nuxeo.ecm.webapp.helpers.ResourcesAccessor;%0Aimport org.nuxeo.ecm.platform.ui.web.api.NavigationContext;%0Aimport org.jboss.seam.annotations.In;%0Aimport org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A%0Aimport java.util.List;%0Aimport java.util.ArrayList
+java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0Aimport org.jboss.seam.annotations.In;%0Aimport org.nuxeo.ecm.core.api.ClientException;%0Aimport org.nuxeo.ecm.core.api.CoreSession;%0Aimport org.nuxeo.ecm.core.api.DocumentModel;%0Aimport org.nuxeo.ecm.core.api.security.SecurityConstants;%0Aimport org.nuxeo.ecm.platform.ui.web.api.NavigationContext;%0Aimport org.nuxeo.ecm.webapp.helpers.ResourcesAccessor
 ;%0A%0A/
@@ -2738,43 +2738,12 @@
     
-try %7B%0A            parentDocument = 
+if (
 docu
@@ -2758,19 +2758,21 @@
 ger.
-getDocument
+hasPermission
 (doc
@@ -2796,18 +2796,17 @@
 entRef()
-);
+,
 %0A       
@@ -2810,111 +2810,135 @@
     
-%7D catch (Exception e) %7B%0A            log.error(%22Error building path%22, e);%0A            return;%0A        %7D%0A
+        SecurityConstants.READ)) %7B%0A            parentDocument = documentManager.getDocument(documentModel.getParentRef());%0A    
     
@@ -2994,13 +2994,22 @@
 s);%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

Fix error in logs when accessing a published document without access to all its parents