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