001/*
002 *  Copyright 2016 Anyware Services
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 */
016package org.ametys.plugins.contentio.synchronize.rights;
017
018import org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.service.ServiceException;
020import org.apache.avalon.framework.service.ServiceManager;
021import org.apache.avalon.framework.service.Serviceable;
022
023import org.ametys.cms.repository.Content;
024import org.ametys.plugins.repository.AmetysObject;
025import org.ametys.plugins.repository.AmetysObjectResolver;
026import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
027import org.ametys.plugins.repository.RepositoryConstants;
028import org.ametys.plugins.repository.UnknownAmetysObjectException;
029import org.ametys.plugins.repository.collection.AmetysObjectCollection;
030import org.ametys.runtime.plugin.component.AbstractLogEnabled;
031
032/**
033 * Helper for retrieving root of synchronized contents
034 */
035public class SynchronizedRootContentHelper extends AbstractLogEnabled implements Component, Serviceable
036{
037    /** The component role. */
038    public static final String ROLE = SynchronizedRootContentHelper.class.getName();
039    
040    /** The root node name of the plugin */
041    public static final String CONTENTIO_ROOT_NODE = "contentio";
042    
043    /** The root node name of the imported contents */
044    public static final String IMPORTED_CONTENTS_ROOT_NODE = RepositoryConstants.NAMESPACE_PREFIX + ":contents";
045    
046    /** Ametys object resolver */
047    protected AmetysObjectResolver _resolver;
048    
049    @Override
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
053    }
054    
055    /**
056     * Gets the root of contents
057     * @param create <code>true</code> to create automatically the root when missing.
058     * @return the root of contents
059     * @throws UnknownAmetysObjectException If root node does not exist
060     */
061    public AmetysObjectCollection getRootContent(boolean create)
062    {
063        ModifiableTraversableAmetysObject pluginsNode = _resolver.resolveByPath("/ametys:plugins/");
064        
065        boolean needSave = false;
066        if (!pluginsNode.hasChild(CONTENTIO_ROOT_NODE))
067        {
068            if (create)
069            {
070                pluginsNode.createChild(CONTENTIO_ROOT_NODE, "ametys:unstructured");
071                needSave = true;
072            }
073            else
074            {
075                throw new UnknownAmetysObjectException("Node '/ametys:plugins/contentio' is missing");
076            }
077        }
078        
079        ModifiableTraversableAmetysObject contentIoNode = pluginsNode.getChild(CONTENTIO_ROOT_NODE);
080        if (!contentIoNode.hasChild(IMPORTED_CONTENTS_ROOT_NODE))
081        {
082            if (create)
083            {
084                contentIoNode.createChild(IMPORTED_CONTENTS_ROOT_NODE, "ametys:collection");
085                needSave = true;
086            }
087            else
088            {
089                throw new UnknownAmetysObjectException("Node '/ametys:plugins/contentio/ametys:contents' is missing");
090            }
091        }
092        
093        if (needSave)
094        {
095            pluginsNode.saveChanges();
096        }
097        
098        return contentIoNode.getChild(IMPORTED_CONTENTS_ROOT_NODE);
099    }
100    
101    /**
102     * Tests if the given content is child of the root of contents, i.e. the given content is managed by the CMS workspace
103     * @param content The content to test
104     * @return true if the given content is child of the root of contents
105     */
106    public boolean isChildOfRootContent(Content content)
107    {
108        try
109        {
110            AmetysObjectCollection rootContent = getRootContent(false);
111            
112            AmetysObject parent = content.getParent();
113            while (parent != null)
114            {
115                if (parent.equals(rootContent))
116                {
117                    return true;
118                }
119                
120                parent = parent.getParent();
121            }
122            
123            return false;
124        }
125        catch (UnknownAmetysObjectException e)
126        {
127            return false;
128        }
129    }
130}