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