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 javax.jcr.Node; 019import javax.jcr.RepositoryException; 020 021import org.apache.commons.lang3.ArrayUtils; 022 023import org.ametys.plugins.repository.AmetysObjectResolver; 024import org.ametys.plugins.repository.AmetysRepositoryException; 025import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata; 026import org.ametys.plugins.repository.metadata.ModifiableFolder; 027import org.ametys.plugins.repository.metadata.ModifiableRichText; 028import org.ametys.plugins.repository.metadata.RichText; 029import org.ametys.plugins.repository.metadata.UnknownMetadataException; 030 031/** 032 * JCR implementation of a {@link RichText}, based on the ametys:richText nodetype.<br> 033 * @deprecated Use org.ametys.cms.data.RichText instead 034 */ 035@Deprecated 036public class JCRRichText extends JCRResource implements ModifiableRichText 037{ 038 039 /** The annotations node name. */ 040 private static final String _ANNOTATIONS_NODE_NAME = "annotations"; 041 042 private Node _rootDataNode; 043 private AmetysObjectResolver _resolver; 044 045 /** 046 * Constructor 047 * @param node the JCR Node backing this {@link JCRRichText} 048 * @param resolver The resolver, used to resolve object collections. 049 * @throws AmetysRepositoryException if an error occurs 050 */ 051 public JCRRichText(Node node, AmetysObjectResolver resolver) throws AmetysRepositoryException 052 { 053 super(node); 054 055 _resolver = resolver; 056 057 try 058 { 059 _rootDataNode = node.getNode("data"); 060 } 061 catch (RepositoryException e) 062 { 063 throw new AmetysRepositoryException(e); 064 } 065 } 066 067 public ModifiableFolder getAdditionalDataFolder() 068 { 069 return new JCRFolder(_rootDataNode); 070 } 071 072 @Override 073 public String[] getAnnotationNames() throws AmetysRepositoryException 074 { 075 try 076 { 077 ModifiableCompositeMetadata annotations = getAnnotationsNode(false); 078 079 return annotations.getMetadataNames(); 080 } 081 catch (UnknownMetadataException e) 082 { 083 // Ignore, just return an empty array. 084 } 085 086 return new String[0]; 087 } 088 089 @Override 090 public String[] getAnnotationValues(String name) throws AmetysRepositoryException 091 { 092 try 093 { 094 ModifiableCompositeMetadata annotations = getAnnotationsNode(false); 095 if (annotations.hasMetadata(name)) 096 { 097 return annotations.getStringArray(name); 098 } 099 } 100 catch (UnknownMetadataException e) 101 { 102 // Ignore, just return an empty array. 103 } 104 105 return new String[0]; 106 } 107 108 @Override 109 public void addAnnotation(String name, String value) throws AmetysRepositoryException 110 { 111 String[] currentValues = getAnnotationValues(name); 112 getAnnotationsNode(true).setMetadata(name, ArrayUtils.add(currentValues, value)); 113 } 114 115 @Override 116 public void addAnnotation(String name, String[] values) throws AmetysRepositoryException 117 { 118 String[] currentValues = getAnnotationValues(name); 119 getAnnotationsNode(true).setMetadata(name, ArrayUtils.addAll(currentValues, values)); 120 } 121 122 @Override 123 public void removeAnnotations() throws AmetysRepositoryException 124 { 125 try 126 { 127 _checkLock(); 128 if (getNode().hasNode(_ANNOTATIONS_NODE_NAME)) 129 { 130 getNode().getNode(_ANNOTATIONS_NODE_NAME).remove(); 131 } 132 } 133 catch (RepositoryException e) 134 { 135 throw new AmetysRepositoryException(e); 136 } 137 } 138 139 @Override 140 public void removeAnnotation(String name) throws AmetysRepositoryException 141 { 142 ModifiableCompositeMetadata annotations = getAnnotationsNode(true); 143 if (annotations.hasMetadata(name)) 144 { 145 annotations.removeMetadata(name); 146 } 147 } 148 149 /** 150 * Get the annotations container node. 151 * @param createNew <code>true</code> to create the node if it doesn't exist, <code>false</code> otherwise. 152 * @return The annotation container node. 153 * @throws UnknownMetadataException If the node doesn't exist and createNew is false. 154 * @throws AmetysRepositoryException If a repository error occurs. 155 */ 156 protected ModifiableCompositeMetadata getAnnotationsNode(boolean createNew) throws UnknownMetadataException, AmetysRepositoryException 157 { 158 try 159 { 160 if (getNode().hasNode(_ANNOTATIONS_NODE_NAME)) 161 { 162 Node node = getNode().getNode(_ANNOTATIONS_NODE_NAME); 163 return new JCRCompositeMetadata(node, _resolver); 164 } 165 else if (createNew) 166 { 167 _checkLock(); 168 Node node = getNode().addNode(_ANNOTATIONS_NODE_NAME, "ametys:compositeMetadata"); 169 return new JCRCompositeMetadata(node, _resolver); 170 } 171 else 172 { 173 throw new UnknownMetadataException("No annotation found"); 174 } 175 } 176 catch (RepositoryException e) 177 { 178 throw new AmetysRepositoryException(e); 179 } 180 } 181}