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.newsletter.category; 017 018import java.util.ArrayList; 019import java.util.Collection; 020import java.util.Collections; 021import java.util.List; 022 023import javax.jcr.Node; 024import javax.jcr.PathNotFoundException; 025import javax.jcr.RepositoryException; 026import javax.jcr.Value; 027 028import org.ametys.plugins.repository.AmetysObject; 029import org.ametys.plugins.repository.AmetysRepositoryException; 030import org.ametys.plugins.repository.RepositoryConstants; 031import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject; 032import org.ametys.web.repository.site.Site; 033 034/** 035 * {@link AmetysObject} for storing tag informations. 036 */ 037public class JCRCategory extends DefaultTraversableAmetysObject<CategoryFactory> 038{ 039 /** Constants for title metadata. */ 040 private static final String __METADATA_TITLE = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":title"; 041 /** Constants for description metadata. */ 042 private static final String __METADATA_DESC = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":description"; 043 /** Constants for template metadata. */ 044 private static final String __METADATA_TEMPLATE = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":template"; 045 /** Constants for automatic newsletter ids. */ 046 private static final String __AUTOMATIC_IDS = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":automatic-ids"; 047 048 /** 049 * Creates a {@link JCRCategory}. 050 * @param node the node backing this {@link AmetysObject}. 051 * @param parentPath the parent path in the Ametys hierarchy. 052 * @param factory the {@link CategoryFactory} which creates the AmetysObject. 053 */ 054 public JCRCategory (Node node, String parentPath, CategoryFactory factory) 055 { 056 super(node, parentPath, factory); 057 } 058 059 /** 060 * Retrieves the title. 061 * @return the title. 062 * @throws AmetysRepositoryException if an error occurs. 063 */ 064 public String getTitle() throws AmetysRepositoryException 065 { 066 try 067 { 068 return getNode().getProperty(__METADATA_TITLE).getString(); 069 } 070 catch (PathNotFoundException e) 071 { 072 return null; 073 } 074 catch (RepositoryException e) 075 { 076 throw new AmetysRepositoryException("Unable to get title property", e); 077 } 078 } 079 080 /** 081 * Set the title. 082 * @param title the title. 083 * @throws AmetysRepositoryException if an error occurs. 084 */ 085 public void setTitle(String title) throws AmetysRepositoryException 086 { 087 try 088 { 089 getNode().setProperty(__METADATA_TITLE, title); 090 } 091 catch (RepositoryException e) 092 { 093 throw new AmetysRepositoryException("Unable to set title property", e); 094 } 095 } 096 097 /** 098 * Retrieves the description. 099 * @return the description. 100 * @throws AmetysRepositoryException if an error occurs. 101 */ 102 public String getDescription() throws AmetysRepositoryException 103 { 104 try 105 { 106 return getNode().getProperty(__METADATA_DESC).getString(); 107 } 108 catch (PathNotFoundException e) 109 { 110 return null; 111 } 112 catch (RepositoryException e) 113 { 114 throw new AmetysRepositoryException("Unable to get description property", e); 115 } 116 } 117 118 /** 119 * Set the description. 120 * @param description the description. 121 * @throws AmetysRepositoryException if an error occurs. 122 */ 123 public void setDescription(String description) throws AmetysRepositoryException 124 { 125 try 126 { 127 getNode().setProperty(__METADATA_DESC, description); 128 } 129 catch (RepositoryException e) 130 { 131 throw new AmetysRepositoryException("Unable to set description property", e); 132 } 133 } 134 135 /** 136 * Retrieves the template name. 137 * @return the template name. 138 * @throws AmetysRepositoryException if an error occurs. 139 */ 140 public String getTemplate() throws AmetysRepositoryException 141 { 142 try 143 { 144 return getNode().getProperty(__METADATA_TEMPLATE).getString(); 145 } 146 catch (PathNotFoundException e) 147 { 148 return null; 149 } 150 catch (RepositoryException e) 151 { 152 throw new AmetysRepositoryException("Unable to get template property", e); 153 } 154 } 155 156 /** 157 * Set the template. 158 * @param templateName the template. 159 * @throws AmetysRepositoryException if an error occurs. 160 */ 161 public void setTemplate(String templateName) throws AmetysRepositoryException 162 { 163 try 164 { 165 getNode().setProperty(__METADATA_TEMPLATE, templateName); 166 } 167 catch (RepositoryException e) 168 { 169 throw new AmetysRepositoryException("Unable to set template property", e); 170 } 171 } 172 173 /** 174 * Retrieves the template name. 175 * @return the template name. 176 * @throws AmetysRepositoryException if an error occurs. 177 */ 178 public Collection<String> getAutomaticIds() throws AmetysRepositoryException 179 { 180 try 181 { 182 List<String> automaticIds = new ArrayList<>(); 183 184 Value[] values = getNode().getProperty(__AUTOMATIC_IDS).getValues(); 185 for (Value value : values) 186 { 187 automaticIds.add(value.getString()); 188 } 189 190 return automaticIds; 191 } 192 catch (PathNotFoundException e) 193 { 194 return Collections.emptyList(); 195 } 196 catch (RepositoryException e) 197 { 198 throw new AmetysRepositoryException("Unable to get template property", e); 199 } 200 } 201 202 /** 203 * Set the template. 204 * @param autoIds the template. 205 * @throws AmetysRepositoryException if an error occurs. 206 */ 207 public void setAutomaticIds(Collection<String> autoIds) throws AmetysRepositoryException 208 { 209 try 210 { 211 // Convert to array. 212 String[] autoIdsArray = autoIds.toArray(new String[autoIds.size()]); 213 214 getNode().setProperty(__AUTOMATIC_IDS, autoIdsArray); 215 } 216 catch (RepositoryException e) 217 { 218 throw new AmetysRepositoryException("Unable to set template property", e); 219 } 220 } 221 222 /** 223 * Get the site name 224 * @return The site name 225 */ 226 public String getSiteName () 227 { 228 AmetysObject parent = getParent(); 229 while (parent != null) 230 { 231 if (parent instanceof Site) 232 { 233 return ((Site) parent).getName(); 234 } 235 parent = parent.getParent(); 236 } 237 return null; 238 } 239 240 /** 241 * Returns the language 242 * @return the language 243 */ 244 public String getLang () 245 { 246 AmetysObject parent = getParent(); 247 while (parent != null) 248 { 249 if (parent.getParent().getName().equals("ametys:categories")) 250 { 251 return parent.getName(); 252 } 253 parent = parent.getParent(); 254 } 255 return null; 256 } 257}