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