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