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; 019 020import javax.jcr.Node; 021import javax.jcr.Property; 022import javax.jcr.PropertyType; 023import javax.jcr.RepositoryException; 024import javax.jcr.Session; 025 026import org.ametys.cms.repository.Content; 027import org.ametys.cms.repository.ModifiableContentHelper; 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 ModifiableContentHelper.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 ModifiableContentHelper.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 ModifiableContentHelper.setLanguage(this, language); 144 } 145 146 /** 147 * Set the title. 148 * @param title the title. 149 * @throws AmetysRepositoryException if an error occurs. 150 */ 151 public void setTitle(String title) throws AmetysRepositoryException 152 { 153 ModifiableContentHelper.setTitle(this, title); 154 } 155 156 /** 157 * Set the login of the creator. 158 * @param user the creator. 159 * @throws AmetysRepositoryException if an error occurs. 160 */ 161 public void setCreator(UserIdentity user) throws AmetysRepositoryException 162 { 163 ModifiableContentHelper.setCreator(this, user); 164 } 165 166 /** 167 * Set the creation date. 168 * @param creationDate the creation date. 169 * @throws AmetysRepositoryException if an error occurs. 170 */ 171 public void setCreationDate(Date creationDate) throws AmetysRepositoryException 172 { 173 ModifiableContentHelper.setCreationDate(this, creationDate); 174 } 175 176 /** 177 * Set the login of the last contributor. 178 * @param user the last contributor. 179 * @throws AmetysRepositoryException if an error occurs. 180 */ 181 public void setLastContributor(UserIdentity user) 182 { 183 ModifiableContentHelper.setLastContributor(this, user); 184 } 185 186 /** 187 * Set the last modification date. 188 * @param lastModified the last modification date. 189 * @throws AmetysRepositoryException if an error occurs. 190 */ 191 public void setLastModified(Date lastModified) throws AmetysRepositoryException 192 { 193 ModifiableContentHelper.setLastModified(this, lastModified); 194 } 195 196 /** 197 * Set the last validation date 198 * @param validationDate the validation date. 199 * @throws AmetysRepositoryException if an error occurs. 200 */ 201 public void setLastValidationDate(Date validationDate) throws AmetysRepositoryException 202 { 203 ModifiableContentHelper.setLastValidationDate(this, validationDate); 204 } 205 206 /** 207 * Set the last validation date resulting from a major modification 208 * @param validationDate the validation date. 209 * @throws AmetysRepositoryException if an error occurs. 210 */ 211 public void setLastMajorValidationDate(Date validationDate) throws AmetysRepositoryException 212 { 213 ModifiableContentHelper.setLastMajorValidationDate(this, validationDate); 214 } 215 216 @Override 217 public void tag(String tag) throws AmetysRepositoryException 218 { 219 TaggableAmetysObjectHelper.tag(this, tag); 220 } 221 222 @Override 223 public void untag(String tag) throws AmetysRepositoryException 224 { 225 TaggableAmetysObjectHelper.untag(this, tag); 226 } 227 228}