001/*
002 *  Copyright 2014 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.plugins.explorer.threads.jcr;
017
018import java.util.Date;
019
020import javax.jcr.Node;
021import javax.jcr.RepositoryException;
022
023import org.ametys.core.user.UserIdentity;
024import org.ametys.plugins.explorer.threads.ModifiablePost;
025import org.ametys.plugins.repository.AmetysObject;
026import org.ametys.plugins.repository.AmetysRepositoryException;
027import org.ametys.plugins.repository.RepositoryConstants;
028import org.ametys.plugins.repository.jcr.DefaultAmetysObject;
029import org.ametys.plugins.repository.metadata.ModifiableRichText;
030
031/**
032 * Class representing of post, backed by a JCR node.<br>
033 */
034public class JCRPost extends DefaultAmetysObject<JCRPostFactory> implements ModifiablePost
035{
036    private static final String METADATA_CONTENT = "content";
037    private static final String METADATA_AUTHOR = "author";
038    private static final String METADATA_CREATIONDATE = "creationDate";
039    private static final String METADATA_LASTMODIFIED = "lastModified";
040    
041    /**
042     * Creates an {@link JCRPost}.
043     * @param node the node backing this {@link AmetysObject}
044     * @param parentPath the parentPath in the Ametys hierarchy
045     * @param factory the JCRPostFactory which created the AmetysObject
046     */
047    public JCRPost(Node node, String parentPath, JCRPostFactory factory)
048    {
049        super(node, parentPath, factory);
050    }
051    
052    @Override
053    public ModifiableRichText getContent ()
054    {
055        return getMetadataHolder().getRichText(METADATA_CONTENT, true);
056    }
057    
058    @Override
059    public UserIdentity getAuthor ()
060    {
061        try
062        {
063            Node authorNode = getNode().getNode(RepositoryConstants.NAMESPACE_PREFIX + ":" + METADATA_AUTHOR);
064            return new UserIdentity(authorNode.getProperty("ametys:login").getString(), authorNode.getProperty("ametys:population").getString());
065        }
066        catch (RepositoryException e)
067        {
068            throw new AmetysRepositoryException("Error while getting author property.", e);
069        }
070    }
071    
072    @Override
073    public void setAuthor(UserIdentity author)
074    {
075        try
076        {
077            Node authorNode = null;
078            if (getNode().hasNode(RepositoryConstants.NAMESPACE_PREFIX + ":" + METADATA_AUTHOR))
079            {
080                authorNode = getNode().getNode(RepositoryConstants.NAMESPACE_PREFIX + ":" + METADATA_AUTHOR);
081            }
082            else
083            {
084                authorNode = getNode().addNode(RepositoryConstants.NAMESPACE_PREFIX + ":" + METADATA_AUTHOR, RepositoryConstants.USER_NODETYPE);
085            }
086            
087            authorNode.setProperty("ametys:login", author.getLogin());
088            authorNode.setProperty("ametys:population", author.getPopulationId());
089        }
090        catch (RepositoryException e)
091        {
092            throw new AmetysRepositoryException("Error setting the author property.", e);
093        }
094    }
095    
096    @Override
097    public Date getCreationDate()
098    {
099        return getMetadataHolder().getDate(METADATA_CREATIONDATE);
100    }
101    
102    @Override
103    public void setCreationDate(Date startDate)
104    {
105        getMetadataHolder().setMetadata(METADATA_CREATIONDATE, startDate);
106    }
107    
108    @Override
109    public Date getLastModified()
110    {
111        return getMetadataHolder().getDate(METADATA_LASTMODIFIED);
112    }
113    
114    @Override
115    public void setLastModified(Date date)
116    {
117        getMetadataHolder().setMetadata(METADATA_LASTMODIFIED, date);
118    }
119}