001/*
002 *  Copyright 2017 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.userdirectory;
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.plugins.contentio.synchronize.rights.SynchronizedRootContentHelper;
024import org.ametys.plugins.repository.AmetysObjectResolver;
025import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
026import org.ametys.plugins.repository.UnknownAmetysObjectException;
027import org.ametys.plugins.repository.collection.AmetysObjectCollection;
028import org.ametys.runtime.plugin.component.AbstractLogEnabled;
029
030/**
031 * Helper for user directory.
032 */
033public class UserDirectoryHelper extends AbstractLogEnabled implements Component, Serviceable
034{
035    /** The avalon role. */
036    public static final String ROLE = UserDirectoryHelper.class.getName();
037    
038    /** The root node name of the plugin */
039    public static final String USER_DIRECTORY_ROOT_NODE = "userdirectory";
040    
041    /** The ametys object resolver */
042    protected AmetysObjectResolver _resolver;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
048    }
049    
050    /**
051     * Gets the root of user directory contents
052     * @return the root of user directory contents
053     */
054    public AmetysObjectCollection getUserDirectoryRootContent()
055    {
056        return getUserDirectoryRootContent(false);
057    }
058    
059    /**
060     * Gets the root of user directory contents
061     * @param create <code>true</code> to create automatically the root when missing.
062     * @return the root of user directory contents
063     */
064    public AmetysObjectCollection getUserDirectoryRootContent(boolean create)
065    {
066        ModifiableTraversableAmetysObject pluginsNode = _resolver.resolveByPath("/ametys:plugins/");
067        
068        boolean needSave = false;
069        if (!pluginsNode.hasChild(USER_DIRECTORY_ROOT_NODE))
070        {
071            if (create)
072            {
073                pluginsNode.createChild(USER_DIRECTORY_ROOT_NODE, "ametys:unstructured");
074                needSave = true;
075            }
076            else
077            {
078                throw new UnknownAmetysObjectException("Node '/ametys:plugins/userdirectory' is missing");
079            }
080        }
081        
082        ModifiableTraversableAmetysObject userdirectoryNode = pluginsNode.getChild(USER_DIRECTORY_ROOT_NODE);
083        if (!userdirectoryNode.hasChild(SynchronizedRootContentHelper.IMPORTED_CONTENTS_ROOT_NODE))
084        {
085            if (create)
086            {
087                userdirectoryNode.createChild(SynchronizedRootContentHelper.IMPORTED_CONTENTS_ROOT_NODE, "ametys:collection");
088                needSave = true;
089            }
090            else
091            {
092                throw new UnknownAmetysObjectException("Node '/ametys:plugins/userdirectory/ametys:contents' is missing");
093            }
094        }
095        
096        if (needSave)
097        {
098            pluginsNode.saveChanges();
099        }
100        
101        return userdirectoryNode.getChild(SynchronizedRootContentHelper.IMPORTED_CONTENTS_ROOT_NODE);
102    }
103}