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.cms.repository;
017
018import java.util.Date;
019
020import javax.jcr.Node;
021import javax.jcr.RepositoryException;
022
023import org.ametys.plugins.repository.AmetysRepositoryException;
024import org.ametys.plugins.repository.RepositoryConstants;
025import org.ametys.plugins.repository.jcr.JCRAmetysObject;
026import org.ametys.plugins.repository.version.ModifiableMetadataAwareVersionableAmetysObject;
027
028/**
029 * Provides helper methods to use the {@link WorkflowAwareContent} API on {@link JCRAmetysObject}s.
030 */
031public final class WorkflowAwareContentHelper
032{
033    
034    /** Constants for workflowId Metadata* */
035    public static final String METADATA_WORKFLOW_ID = "workflowId";
036    
037    /** Constants for currentStepId Metadata* */
038    public static final String METADATA_CURRENT_STEP_ID = "currentStepId";
039    
040    /** Name of the metadata storing the proposal date. */
041    public static final String METADATA_LAST_PROPOSAL_DATE = "proposalDate";
042    
043    private WorkflowAwareContentHelper()
044    {
045        // Hide default constructor.
046    }
047    
048    /**
049     * Get an object's workflow ID.
050     * @param object the object.
051     * @return the object's workflow ID.
052     * @throws AmetysRepositoryException if an error occurs.
053     */
054    public static long getWorkflowId(JCRAmetysObject object) throws AmetysRepositoryException
055    {
056        try
057        {
058            return object.getNode().getProperty(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ':' + METADATA_WORKFLOW_ID).getLong();
059        }
060        catch (RepositoryException e)
061        {
062            throw new AmetysRepositoryException("Unable to get workflowId property", e);
063        }
064    }
065    
066    /**
067     * Set an object's workflow ID.
068     * @param object the object.
069     * @param workflowId the workflow ID to set.
070     * @throws AmetysRepositoryException if an error occurs.
071     */
072    public static void setWorkflowId(JCRAmetysObject object, long workflowId) throws AmetysRepositoryException
073    {
074        Node node = object.getNode();
075        
076        try
077        {
078            if (node.hasProperty(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ':' + METADATA_WORKFLOW_ID))
079            {
080                throw new AmetysRepositoryException("Cannot call setWorkflowId on an already persisted Content");
081            }
082            
083            node.setProperty(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ':' + METADATA_WORKFLOW_ID, workflowId);
084        }
085        catch (RepositoryException e)
086        {
087            throw new AmetysRepositoryException("Unable to set workflowId property", e);
088        }
089    }
090    
091    /**
092     * Remove the workflow ID property if exists on an object
093     * @param object The Ametys object
094     */
095    public static void removeWorkflowId (JCRAmetysObject object)
096    {
097        Node node = object.getNode();
098        
099        try
100        {
101            if (node.hasProperty(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ':' + METADATA_WORKFLOW_ID))
102            {
103                node.getProperty(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ':' + METADATA_WORKFLOW_ID).remove();
104            }
105        }
106        catch (RepositoryException e)
107        {
108            throw new AmetysRepositoryException("Unable to remove workflowId property", e);
109        }
110    }
111    
112    /**
113     * Get an object's current step ID.
114     * @param object the object.
115     * @return the object's current step ID.
116     * @throws AmetysRepositoryException if an error occurs.
117     */
118    public static long getCurrentStepId(JCRAmetysObject object) throws AmetysRepositoryException
119    {
120        try
121        {
122            return object.getNode().getProperty(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ':' + METADATA_CURRENT_STEP_ID).getLong();
123        }
124        catch (RepositoryException e)
125        {
126            throw new AmetysRepositoryException("Unable to get currentStepId property", e);
127        }
128    }
129    
130    /**
131     * Set an object's current step ID.
132     * @param object the object.
133     * @param stepId the current step ID to set.
134     * @throws AmetysRepositoryException if an error occurs.
135     */
136    public static void setCurrentStepId(JCRAmetysObject object, long stepId) throws AmetysRepositoryException
137    {
138        Node node = object.getNode();
139        
140        try
141        {
142            node.setProperty(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ':' + METADATA_CURRENT_STEP_ID, stepId);
143        }
144        catch (RepositoryException e)
145        {
146            throw new AmetysRepositoryException("Unable to set currentStepId property", e);
147        }
148    }
149    
150    /**
151     * Get an object's proposal date.
152     * @param object the object.
153     * @return the object's proposal date.
154     * @throws AmetysRepositoryException if an error occurs.
155     */
156    public static Date getProposalDate(ModifiableMetadataAwareVersionableAmetysObject object) throws AmetysRepositoryException
157    {
158        if (object.getUnversionedMetadataHolder().hasMetadata(METADATA_LAST_PROPOSAL_DATE))
159        {
160            return object.getUnversionedMetadataHolder().getDate(METADATA_LAST_PROPOSAL_DATE);
161        }
162        return null;
163    }
164    
165    /**
166     * Set an object's proposal date.
167     * @param object the object.
168     * @param proposalDate the proposal date to set.
169     * @throws AmetysRepositoryException if an error occurs.
170     */
171    public static void setProposalDate(ModifiableMetadataAwareVersionableAmetysObject object, Date proposalDate) throws AmetysRepositoryException
172    {
173        if (proposalDate == null)
174        {
175            if (object.getUnversionedMetadataHolder().hasMetadata(METADATA_LAST_PROPOSAL_DATE))
176            {
177                object.getUnversionedMetadataHolder().removeMetadata(METADATA_LAST_PROPOSAL_DATE);
178            }
179        }
180        else
181        {
182            object.getUnversionedMetadataHolder().setMetadata(METADATA_LAST_PROPOSAL_DATE, proposalDate);
183        }
184    }
185}