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