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