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