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.repository.metadata.jcr; 017 018import java.util.ArrayList; 019import java.util.Collection; 020 021import javax.jcr.ItemExistsException; 022import javax.jcr.Node; 023import javax.jcr.NodeIterator; 024import javax.jcr.PathNotFoundException; 025import javax.jcr.RepositoryException; 026import javax.jcr.lock.Lock; 027 028import org.apache.jackrabbit.util.Text; 029 030import org.ametys.plugins.repository.AmetysRepositoryException; 031import org.ametys.plugins.repository.RepositoryConstants; 032import org.ametys.plugins.repository.RepositoryIntegrityViolationException; 033import org.ametys.plugins.repository.jcr.NodeTypeHelper; 034import org.ametys.plugins.repository.metadata.ModifiableFile; 035import org.ametys.plugins.repository.metadata.ModifiableFolder; 036import org.ametys.plugins.repository.metadata.UnknownMetadataException; 037 038/** 039 * Java binding of a nt:folder JCR Node.<p> 040 * {@link Text#escapeIllegalJcrChars(String)} is used for 041 * escaping invalid JCR characters or character sequences. 042 * @deprecated org.ametys.cms.data.RichText new class doesn't use folder anymore 043 */ 044@Deprecated 045public class JCRFolder implements ModifiableFolder 046{ 047 private Node _node; 048 private boolean _lockAlreadyChecked; 049 050 /** 051 * Constructor 052 * @param node the nt:folder Node 053 */ 054 public JCRFolder(Node node) 055 { 056 _node = node; 057 } 058 059 /** 060 * Retrieves the underlying node. 061 * @return the underlying node. 062 */ 063 public Node getNode() 064 { 065 return _node; 066 } 067 068 @Override 069 public String getName() throws AmetysRepositoryException 070 { 071 try 072 { 073 return Text.unescapeIllegalJcrChars(_node.getName()); 074 } 075 catch (RepositoryException e) 076 { 077 throw new AmetysRepositoryException(e); 078 } 079 } 080 081 public Collection<ModifiableFolder> getFolders() 082 { 083 ArrayList<ModifiableFolder> folders = new ArrayList<>(); 084 try 085 { 086 NodeIterator it = _node.getNodes(); 087 while (it.hasNext()) 088 { 089 Node node = it.nextNode(); 090 091 if (NodeTypeHelper.isNodeType(node, "nt:folder")) 092 { 093 folders.add(new JCRFolder(node)); 094 } 095 } 096 097 return folders; 098 } 099 catch (RepositoryException e) 100 { 101 throw new AmetysRepositoryException(e); 102 } 103 } 104 105 public ModifiableFolder getFolder(String folderName) 106 { 107 try 108 { 109 Node node = _node.getNode(Text.escapeIllegalJcrChars(folderName)); 110 return new JCRFolder(node); 111 } 112 catch (PathNotFoundException e) 113 { 114 throw new UnknownMetadataException(e); 115 } 116 catch (RepositoryException e) 117 { 118 throw new AmetysRepositoryException(e); 119 } 120 } 121 122 public ModifiableFolder addFolder(String folderName) 123 { 124 try 125 { 126 _checkLock(); 127 Node node = _node.addNode(Text.escapeIllegalJcrChars(folderName), "nt:folder"); 128 return new JCRFolder(node); 129 } 130 catch (ItemExistsException e) 131 { 132 throw new RepositoryIntegrityViolationException(e); 133 } 134 catch (RepositoryException e) 135 { 136 throw new AmetysRepositoryException(e); 137 } 138 } 139 140 public Collection<ModifiableFile> getFiles() 141 { 142 ArrayList<ModifiableFile> files = new ArrayList<>(); 143 try 144 { 145 NodeIterator it = _node.getNodes(); 146 while (it.hasNext()) 147 { 148 Node node = it.nextNode(); 149 150 if (NodeTypeHelper.isNodeType(node, "nt:file")) 151 { 152 files.add(new JCRFile(node)); 153 } 154 } 155 156 return files; 157 } 158 catch (RepositoryException e) 159 { 160 throw new AmetysRepositoryException(e); 161 } 162 } 163 164 @Override 165 public boolean hasFile(String fileName) throws AmetysRepositoryException 166 { 167 try 168 { 169 return _node.hasNode(Text.escapeIllegalJcrChars(fileName)); 170 } 171 catch (RepositoryException e) 172 { 173 throw new AmetysRepositoryException(e); 174 } 175 } 176 177 public ModifiableFile getFile(String fileName) 178 { 179 try 180 { 181 Node node = _node.getNode(Text.escapeIllegalJcrChars(fileName)); 182 return new JCRFile(node); 183 } 184 catch (PathNotFoundException e) 185 { 186 throw new UnknownMetadataException(e); 187 } 188 catch (RepositoryException e) 189 { 190 throw new AmetysRepositoryException(e); 191 } 192 } 193 194 public ModifiableFile addFile(String fileName) 195 { 196 try 197 { 198 _checkLock(); 199 Node node = _node.addNode(Text.escapeIllegalJcrChars(fileName), "nt:file"); 200 return new JCRFile(node); 201 } 202 catch (ItemExistsException e) 203 { 204 throw new RepositoryIntegrityViolationException(e); 205 } 206 catch (RepositoryException e) 207 { 208 throw new AmetysRepositoryException(e); 209 } 210 } 211 212 @Override 213 public void remove(String name) throws AmetysRepositoryException 214 { 215 try 216 { 217 _checkLock(); 218 Node node = _node.getNode(Text.escapeIllegalJcrChars(name)); 219 node.remove(); 220 } 221 catch (PathNotFoundException e) 222 { 223 throw new UnknownMetadataException(e); 224 } 225 catch (RepositoryException e) 226 { 227 throw new AmetysRepositoryException(e); 228 } 229 } 230 231 @Override 232 public void removeAll() throws AmetysRepositoryException 233 { 234 try 235 { 236 _checkLock(); 237 NodeIterator it = _node.getNodes(); 238 while (it.hasNext()) 239 { 240 Node node = it.nextNode(); 241 node.remove(); 242 } 243 } 244 catch (RepositoryException e) 245 { 246 throw new AmetysRepositoryException(e); 247 } 248 } 249 250 private void _checkLock() throws RepositoryException 251 { 252 if (!_lockAlreadyChecked && _node.isLocked()) 253 { 254 Lock lock = _node.getSession().getWorkspace().getLockManager().getLock(_node.getPath()); 255 Node lockHolder = lock.getNode(); 256 257 lockHolder.getSession().getWorkspace().getLockManager().addLockToken(lockHolder.getProperty(RepositoryConstants.METADATA_LOCKTOKEN).getString()); 258 _lockAlreadyChecked = true; 259 } 260 } 261}