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.plugins.repository.data.holder.ModifiableModelAwareDataHolder;
033import org.ametys.plugins.repository.data.holder.impl.DefaultModifiableModelAwareDataHolder;
034import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
035import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
036import org.ametys.plugins.repository.tag.TaggableAmetysObjectHelper;
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        // TODO CMS-9336 this metadata should be stored as internal using data holders
116        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
117        repositoryData.setValue(METADATA_SITE, siteName);
118    }
119    
120    /**
121     * Set the title.
122     * @param title the title.
123     * @throws AmetysRepositoryException if an error occurs.
124     */
125    @Deprecated
126    public void setTitle(String title) throws AmetysRepositoryException
127    {
128        _getFactory().getModifiableContentHelper().setTitle(this, title);
129    }
130    
131    /**
132     * Set the title from the given locale
133     * @param title the title.
134     * @param locale The locale
135     * @throws AmetysRepositoryException if an error occurs.
136     */
137    public void setTitle(String title, Locale locale) throws AmetysRepositoryException
138    {
139        _getFactory().getModifiableContentHelper().setTitle(this, title, locale);
140    }
141    
142    /**
143     * Set the login of the creator.
144     * @param user the creator.
145     * @throws AmetysRepositoryException if an error occurs.
146     */
147    public void setCreator(UserIdentity user) throws AmetysRepositoryException
148    {
149        _getFactory().getModifiableContentHelper().setCreator(this, user);
150    }
151    
152    /**
153     * Set the creation date.
154     * @param creationDate the creation date.
155     * @throws AmetysRepositoryException if an error occurs.
156     */
157    public void setCreationDate(Date creationDate) throws AmetysRepositoryException
158    {
159        _getFactory().getModifiableContentHelper().setCreationDate(this, creationDate);
160    }
161    
162    /**
163     * Set the login of the last contributor.
164     * @param user the last contributor.
165     * @throws AmetysRepositoryException if an error occurs.
166     */
167    public void setLastContributor(UserIdentity user)
168    {
169        _getFactory().getModifiableContentHelper().setLastContributor(this, user);
170    }
171    
172    /**
173     * Set the last modification date.
174     * @param lastModified the last modification date.
175     * @throws AmetysRepositoryException if an error occurs.
176     */
177    public void setLastModified(Date lastModified) throws AmetysRepositoryException
178    {
179        _getFactory().getModifiableContentHelper().setLastModified(this, lastModified);
180    }
181    
182    /**
183     * Set the first validation date
184     * @param validationDate the validation date.
185     * @throws AmetysRepositoryException if an error occurs.
186     */
187    public void setFirstValidationDate(Date validationDate) throws AmetysRepositoryException
188    {
189        _getFactory().getModifiableContentHelper().setFirstValidationDate(this, validationDate);
190    }
191    
192    /**
193     * Set the last validation date
194     * @param validationDate the validation date.
195     * @throws AmetysRepositoryException if an error occurs.
196     */
197    public void setLastValidationDate(Date validationDate) throws AmetysRepositoryException
198    {
199        _getFactory().getModifiableContentHelper().setLastValidationDate(this, validationDate);
200    }
201    
202    /**
203     * Set the last validation date resulting from a major modification
204     * @param validationDate the validation date.
205     * @throws AmetysRepositoryException if an error occurs.
206     */
207    public void setLastMajorValidationDate(Date validationDate) throws AmetysRepositoryException
208    {
209        _getFactory().getModifiableContentHelper().setLastMajorValidationDate(this, validationDate);
210    }
211    
212    @Override
213    public ModifiableModelAwareDataHolder getDataHolder()
214    {
215        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
216        return new DefaultModifiableModelAwareDataHolder(repositoryData, _getFactory().getContentHelper().getContentTypes(this));
217    }
218    
219    @Override
220    public void tag(String tag) throws AmetysRepositoryException
221    {
222        TaggableAmetysObjectHelper.tag(this, tag);
223    }
224    
225    @Override
226    public void untag(String tag) throws AmetysRepositoryException
227    {
228        TaggableAmetysObjectHelper.untag(this, tag);
229    }
230    
231}