001/*
002 *  Copyright 2010 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.explorer.resources.jcr;
017
018import java.util.List;
019
020import javax.jcr.Node;
021import javax.jcr.RepositoryException;
022
023import org.ametys.plugins.explorer.ExplorerNode;
024import org.ametys.plugins.explorer.resources.ModifiableResourceCollection;
025import org.ametys.plugins.explorer.resources.Resource;
026import org.ametys.plugins.repository.AmetysObject;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.repository.CopiableAmetysObject;
029import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
030import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
031import org.ametys.plugins.repository.jcr.NodeTypeHelper;
032
033/**
034 * {@link ExplorerNode} representing a collection of resources.
035 */
036public class JCRResourcesCollection extends DefaultTraversableAmetysObject<JCRResourcesCollectionFactory> implements ModifiableResourceCollection, CopiableAmetysObject
037{
038    /** application id for resources collections. */
039    public static final String APPLICATION_ID = "Ametys.plugins.explorer.applications.resources.Resources";
040    
041    /** Constants for description metadata */
042    protected static final String METADATA_DESCRIPTION = "description";
043    
044    /**
045     * Constructor
046     * @param node The jcr node
047     * @param parentPath The parent path
048     * @param factory The factory
049     */
050    public JCRResourcesCollection(Node node, String parentPath, JCRResourcesCollectionFactory factory)
051    {
052        super(node, parentPath, factory);
053    }
054
055    @Override
056    public String getIconCls()
057    {
058        return "folder";
059    }
060    
061    @Override
062    public String getApplicationId()
063    {
064        return APPLICATION_ID;
065    }
066    
067    @Override
068    public String getCollectionType()
069    {
070        return JCRResourcesCollectionFactory.RESOURCESCOLLECTION_NODETYPE;
071    }
072    
073    @Override
074    public String getResourceType()
075    {
076        return JCRResourceFactory.RESOURCES_NODETYPE;
077    }
078    
079    @Override
080    public String getResourcePath() throws AmetysRepositoryException
081    {
082        return getExplorerPath();
083    }
084
085    @Override
086    public String getExplorerPath()
087    {
088        AmetysObject parent = getParent();
089        
090        if (parent instanceof ExplorerNode)
091        {
092            return ((ExplorerNode) parent).getExplorerPath() + "/" + getName();
093        }
094        else
095        {
096            return "";
097        }
098    }
099    
100    public boolean hasChildResources() throws AmetysRepositoryException
101    {
102        for (AmetysObject child : getChildren())
103        {
104            if (child instanceof Resource)
105            {
106                return true;
107            }
108        }
109
110        return false;
111    }
112    
113    public boolean hasChildExplorerNodes() throws AmetysRepositoryException
114    {
115        for (AmetysObject child : getChildren())
116        {
117            if (child instanceof ExplorerNode)
118            {
119                return true;
120            }
121        }
122
123        return false;
124    }
125    
126    @Override
127    public AmetysObject copyTo(ModifiableTraversableAmetysObject parent, String name) throws AmetysRepositoryException
128    {
129        try
130        {
131            String nodeTypeName = NodeTypeHelper.getNodeTypeName(getNode());
132            
133            JCRResourcesCollection copiedCollection = parent.createChild(name, nodeTypeName);
134            parent.saveChanges();
135            
136            for (AmetysObject object : getChildren())
137            {
138                if (object instanceof CopiableAmetysObject)
139                {
140                    ((CopiableAmetysObject) object).copyTo(copiedCollection, object.getName());
141                }
142            }
143            
144            return copiedCollection;
145        }
146        catch (RepositoryException e)
147        {
148            throw new AmetysRepositoryException("Error copying the collection " + getId() + " into " + parent.getId(), e);
149        }
150    }
151    
152    @Override
153    public AmetysObject copyTo(ModifiableTraversableAmetysObject parent, String name, List<String> restrictTo) throws AmetysRepositoryException
154    {
155        return copyTo(parent, name);
156    }
157
158    @Override
159    public String getDescription()
160    {
161        return getMetadataHolder().getString(METADATA_DESCRIPTION, null);
162    }
163
164    @Override
165    public void setDescription(String description)
166    {
167        getMetadataHolder().setMetadata(METADATA_DESCRIPTION, description);
168    }
169    
170}