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.Property;
022import javax.jcr.RepositoryException;
023
024import org.ametys.plugins.explorer.ExplorerNode;
025import org.ametys.plugins.explorer.resources.ModifiableResourceCollection;
026import org.ametys.plugins.explorer.resources.Resource;
027import org.ametys.plugins.repository.AmetysObject;
028import org.ametys.plugins.repository.AmetysObjectIterable;
029import org.ametys.plugins.repository.AmetysRepositoryException;
030import org.ametys.plugins.repository.CopiableAmetysObject;
031import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
032import org.ametys.plugins.repository.data.ametysobject.ModifiableModelLessDataAwareAmetysObject;
033import org.ametys.plugins.repository.data.holder.ModifiableModelLessDataHolder;
034import org.ametys.plugins.repository.data.holder.impl.DefaultModifiableModelLessDataHolder;
035import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
036import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
037import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
038import org.ametys.plugins.repository.jcr.JCRTraversableAmetysObject;
039import org.ametys.plugins.repository.jcr.NameHelper;
040import org.ametys.plugins.repository.jcr.NameHelper.NameComputationMode;
041import org.ametys.plugins.repository.jcr.NodeHelper;
042import org.ametys.plugins.repository.jcr.NodeTypeHelper;
043import org.ametys.plugins.repository.trash.TrashElement;
044import org.ametys.plugins.repository.trash.TrashableAmetysObject;
045
046/**
047 * {@link ExplorerNode} representing a collection of resources.
048 * @param <F> the actual type of factory.
049 */
050public class JCRResourcesCollection<F extends JCRResourcesCollectionFactory> extends DefaultTraversableAmetysObject<F> implements ModifiableResourceCollection, CopiableAmetysObject, ModifiableModelLessDataAwareAmetysObject, TrashableAmetysObject
051{
052    /** application id for resources collections. */
053    public static final String APPLICATION_ID = "Ametys.plugins.explorer.applications.resources.Resources";
054    
055    /** Constants for description metadata */
056    protected static final String DATA_DESCRIPTION = "description";
057    
058    /**
059     * Constructor
060     * @param node The jcr node
061     * @param parentPath The parent path
062     * @param factory The factory
063     */
064    public JCRResourcesCollection(Node node, String parentPath, F factory)
065    {
066        super(node, parentPath, factory);
067    }
068
069    @Override
070    public String getIconCls()
071    {
072        return "ametysicon-folder249";
073    }
074    
075    @Override
076    public String getApplicationId()
077    {
078        return APPLICATION_ID;
079    }
080    
081    @Override
082    public String getCollectionType()
083    {
084        return JCRResourcesCollectionFactory.RESOURCESCOLLECTION_NODETYPE;
085    }
086    
087    @Override
088    public String getResourceType()
089    {
090        return JCRResourceFactory.RESOURCES_NODETYPE;
091    }
092    
093    @Override
094    public String getResourcePath() throws AmetysRepositoryException
095    {
096        return getExplorerPath();
097    }
098
099    @Override
100    public String getExplorerPath()
101    {
102        AmetysObject parent = getParent();
103        
104        if (parent instanceof ExplorerNode)
105        {
106            return ((ExplorerNode) parent).getExplorerPath() + "/" + getName();
107        }
108        else
109        {
110            return "";
111        }
112    }
113    
114    public boolean hasChildResources() throws AmetysRepositoryException
115    {
116        AmetysObjectIterable<AmetysObject> children = getChildren();
117        for (AmetysObject child : children)
118        {
119            if (child instanceof Resource)
120            {
121                return true;
122            }
123        }
124
125        return false;
126    }
127    
128    public boolean hasChildExplorerNodes() throws AmetysRepositoryException
129    {
130        AmetysObjectIterable<AmetysObject> children = getChildren();
131        for (AmetysObject child : children)
132        {
133            if (child instanceof ExplorerNode)
134            {
135                return true;
136            }
137        }
138
139        return false;
140    }
141    
142    @Override
143    public AmetysObject copyTo(ModifiableTraversableAmetysObject parent, String name) throws AmetysRepositoryException
144    {
145        try
146        {
147            String nodeTypeName = NodeTypeHelper.getNodeTypeName(getNode());
148            
149            JCRResourcesCollection copiedCollection = parent.createChild(name, nodeTypeName);
150            parent.saveChanges();
151            
152            for (AmetysObject object : getChildren())
153            {
154                if (object instanceof CopiableAmetysObject)
155                {
156                    ((CopiableAmetysObject) object).copyTo(copiedCollection, object.getName());
157                }
158            }
159            
160            return copiedCollection;
161        }
162        catch (RepositoryException e)
163        {
164            throw new AmetysRepositoryException("Error copying the collection " + getId() + " into " + parent.getId(), e);
165        }
166    }
167    
168    @Override
169    public AmetysObject copyTo(ModifiableTraversableAmetysObject parent, String name, List<String> restrictTo) throws AmetysRepositoryException
170    {
171        return copyTo(parent, name);
172    }
173
174    @Override
175    public String getDescription()
176    {
177        return getValue(DATA_DESCRIPTION, null);
178    }
179
180    @Override
181    public void setDescription(String description)
182    {
183        setValue(DATA_DESCRIPTION, description);
184    }
185    
186    public ModifiableModelLessDataHolder getDataHolder()
187    {
188        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
189        return new DefaultModifiableModelLessDataHolder(_getFactory().getDataTypesExtensionPoint(), repositoryData);
190    }
191    
192    public TrashElement moveToTrash()
193    {
194        TrashElement trashElement = _getFactory()._getTrashElementDAO().createTrashElement(this, getName());
195        // Clone the ametys object from the original session to trash session
196        Node storedNode = NodeHelper.cloneNode(getNode(), trashElement.getNode());
197        
198        try
199        {
200            storedNode.setProperty("ametys-internal:path", this.getParentPath());
201        }
202        catch (RepositoryException e)
203        {
204            throw new AmetysRepositoryException("failed to store the content path", e);
205        }
206
207        // Remove the node from the original session
208        remove();
209        
210        return trashElement;
211    }
212    
213    public JCRResourcesCollection restoreFromTrash()
214    {
215        try
216        {
217            Property property = getNode().getProperty("ametys-internal:path");
218            String parentPath = property.getValue().getString();
219            property.remove();
220            JCRTraversableAmetysObject parent = _getFactory()._getResolver().resolveByPath(parentPath);
221            // Get the node name, can be adjust if another content has already the same node name
222            String nodeName = NameHelper.getUniqueAmetysObjectName(parent, getName(), NameComputationMode.USER_FRIENDLY, true);
223            
224            // Clone the ametys object from the trash session to default session
225            NodeHelper.cloneNode(getNode(), parent.getNode(), nodeName);
226            
227            // Remove the node from the trash session
228            remove();
229            
230            return parent.getChild(nodeName);
231        }
232        catch (RepositoryException e)
233        {
234            throw new AmetysRepositoryException("failed to store the content path", e);
235        }
236    }
237}