001/* 002 * Copyright 2012 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.web.repository.content.jcr; 017 018import java.util.Date; 019import java.util.Locale; 020 021import javax.jcr.Node; 022import javax.jcr.Property; 023import javax.jcr.PropertyType; 024import javax.jcr.RepositoryException; 025import javax.jcr.Session; 026 027import org.ametys.cms.repository.Content; 028import org.ametys.cms.tag.jcr.TaggableAmetysObjectHelper; 029import org.ametys.core.user.UserIdentity; 030import org.ametys.plugins.repository.AmetysObject; 031import org.ametys.plugins.repository.AmetysRepositoryException; 032import org.ametys.plugins.repository.RepositoryConstants; 033import org.ametys.web.repository.content.SharedContent; 034import org.ametys.web.repository.site.Site; 035 036/** 037 * Default {@link SharedContent} implementation, backed by a JCR node. 038 * @param <F> the actual type of factory. 039 */ 040public class DefaultSharedContent<F extends DefaultSharedContentFactory> extends DefaultWebContent<F> implements SharedContent 041{ 042 043 /** The initial content property. */ 044 public static final String INITIAL_CONTENT_PROPERTY = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":initial-content"; 045 046 /** 047 * Creates a {@link DefaultSharedContent}. 048 * @param node the node backing this {@link AmetysObject} 049 * @param parentPath the parentPath in the Ametys hierarchy 050 * @param factory the DefaultSharedContentFactory which created the AmetysObject. 051 */ 052 public DefaultSharedContent(Node node, String parentPath, F factory) 053 { 054 super(node, parentPath, factory); 055 } 056 057 @Override 058 public Content getInitialContent() throws AmetysRepositoryException 059 { 060 Session session = null; 061 try 062 { 063 Content content = null; 064 065 // The difficulty is that we have to switch to 'default' workspace, 066 // because the property does not exists in 'live', 067 // because it could reference a non existing node 068 session = getNode().getSession().getRepository().login("default"); 069 Node defaultWorkspaceNode = session.getNodeByIdentifier(getNode().getIdentifier()); 070 071 if (defaultWorkspaceNode.hasProperty(INITIAL_CONTENT_PROPERTY)) 072 { 073 Property contentProp = defaultWorkspaceNode.getProperty(INITIAL_CONTENT_PROPERTY); 074 if (contentProp.getType() == PropertyType.REFERENCE) 075 { 076 String contentNodeId = contentProp.getValue().getString(); 077 Node contentNode = getNode().getSession().getNodeByIdentifier(contentNodeId); 078 content = _getFactory().resolveAmetysObject(contentNode); 079 } 080 } 081 return content; 082 } 083 catch (RepositoryException e) 084 { 085 throw new AmetysRepositoryException("Unable to resolve the initial content of shared content " + toString(), e); 086 } 087 finally 088 { 089 if (session != null) 090 { 091 session.logout(); 092 } 093 } 094 } 095 096 @Override 097 public Site getSite() 098 { 099 return getParent().getParent(); 100 } 101 102 @Override 103 public String getSiteName() 104 { 105 return getSite().getName(); 106 } 107 108 @Override 109 public void setSiteName(String siteName) 110 { 111 getMetadataHolder().setMetadata(METADATA_SITE, siteName); 112 } 113 114 /** 115 * Set the type of this Content.<br> 116 * This method may only be called on a new Content, ie. before its first save. 117 * @param type the type of this content. 118 * @throws AmetysRepositoryException if an error occurs. 119 */ 120 public void setType(String type) throws AmetysRepositoryException 121 { 122 _getFactory().getModifiableContentHelper().setType(this, type); 123 } 124 125 /** 126 * Set the types of this Content.<br> 127 * This method may only be called on a new Content, ie. before its first save. 128 * @param types the types of this content. 129 * @throws AmetysRepositoryException if an error occurs. 130 */ 131 public void setTypes(String[] types) throws AmetysRepositoryException 132 { 133 _getFactory().getModifiableContentHelper().setTypes(this, types); 134 } 135 136 /** 137 * Set the type of this Content.<br> 138 * This method may only be called on a new Content, ie. before its first save. 139 * @param language the language of this content. 140 * @throws AmetysRepositoryException if an error occurs. 141 */ 142 public void setLanguage(String language) throws AmetysRepositoryException 143 { 144 _getFactory().getModifiableContentHelper().setLanguage(this, language); 145 } 146 147 /** 148 * Set the title. 149 * @param title the title. 150 * @throws AmetysRepositoryException if an error occurs. 151 */ 152 @Deprecated 153 public void setTitle(String title) throws AmetysRepositoryException 154 { 155 _getFactory().getModifiableContentHelper().setTitle(this, title); 156 } 157 158 /** 159 * Set the title from the given locale 160 * @param title the title. 161 * @param locale The locale 162 * @throws AmetysRepositoryException if an error occurs. 163 */ 164 public void setTitle(String title, Locale locale) throws AmetysRepositoryException 165 { 166 _getFactory().getModifiableContentHelper().setTitle(this, title, locale); 167 } 168 169 /** 170 * Set the login of the creator. 171 * @param user the creator. 172 * @throws AmetysRepositoryException if an error occurs. 173 */ 174 public void setCreator(UserIdentity user) throws AmetysRepositoryException 175 { 176 _getFactory().getModifiableContentHelper().setCreator(this, user); 177 } 178 179 /** 180 * Set the creation date. 181 * @param creationDate the creation date. 182 * @throws AmetysRepositoryException if an error occurs. 183 */ 184 public void setCreationDate(Date creationDate) throws AmetysRepositoryException 185 { 186 _getFactory().getModifiableContentHelper().setCreationDate(this, creationDate); 187 } 188 189 /** 190 * Set the login of the last contributor. 191 * @param user the last contributor. 192 * @throws AmetysRepositoryException if an error occurs. 193 */ 194 public void setLastContributor(UserIdentity user) 195 { 196 _getFactory().getModifiableContentHelper().setLastContributor(this, user); 197 } 198 199 /** 200 * Set the last modification date. 201 * @param lastModified the last modification date. 202 * @throws AmetysRepositoryException if an error occurs. 203 */ 204 public void setLastModified(Date lastModified) throws AmetysRepositoryException 205 { 206 _getFactory().getModifiableContentHelper().setLastModified(this, lastModified); 207 } 208 209 /** 210 * Set the first validation date 211 * @param validationDate the validation date. 212 * @throws AmetysRepositoryException if an error occurs. 213 */ 214 public void setFirstValidationDate(Date validationDate) throws AmetysRepositoryException 215 { 216 _getFactory().getModifiableContentHelper().setFirstValidationDate(this, validationDate); 217 } 218 219 /** 220 * Set the last validation date 221 * @param validationDate the validation date. 222 * @throws AmetysRepositoryException if an error occurs. 223 */ 224 public void setLastValidationDate(Date validationDate) throws AmetysRepositoryException 225 { 226 _getFactory().getModifiableContentHelper().setLastValidationDate(this, validationDate); 227 } 228 229 /** 230 * Set the last validation date resulting from a major modification 231 * @param validationDate the validation date. 232 * @throws AmetysRepositoryException if an error occurs. 233 */ 234 public void setLastMajorValidationDate(Date validationDate) throws AmetysRepositoryException 235 { 236 _getFactory().getModifiableContentHelper().setLastMajorValidationDate(this, validationDate); 237 } 238 239 @Override 240 public void tag(String tag) throws AmetysRepositoryException 241 { 242 TaggableAmetysObjectHelper.tag(this, tag); 243 } 244 245 @Override 246 public void untag(String tag) throws AmetysRepositoryException 247 { 248 TaggableAmetysObjectHelper.untag(this, tag); 249 } 250 251}