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.time.ZonedDateTime;
019
020import org.ametys.plugins.repository.AmetysRepositoryException;
021import org.ametys.plugins.repository.jcr.JCRAmetysObject;
022import org.ametys.runtime.model.type.ModelItemTypeConstants;
023
024/**
025 * Provides helper methods to use the {@link WorkflowAwareContent} API on {@link JCRAmetysObject}s.
026 */
027public final class WorkflowAwareContentHelper
028{
029    /** Name of the metadata storing the proposal date. */
030    public static final String METADATA_LAST_PROPOSAL_DATE = "proposalDate";
031    
032    private WorkflowAwareContentHelper()
033    {
034        // Hide default constructor.
035    }
036    
037    /**
038     * Get a content's proposal date.
039     * @param content the content.
040     * @return the content's proposal date.
041     * @throws AmetysRepositoryException if an error occurs.
042     */
043    public static ZonedDateTime getProposalDate(WorkflowAwareContent content) throws AmetysRepositoryException
044    {
045        return content.getInternalDataHolder().getValueOrDefault(METADATA_LAST_PROPOSAL_DATE, null);
046    }
047    
048    /**
049     * Set a content's proposal date.
050     * @param content the content.
051     * @param proposalDate the proposal date to set.
052     * @throws AmetysRepositoryException if an error occurs.
053     */
054    public static void setProposalDate(WorkflowAwareContent content, ZonedDateTime proposalDate) throws AmetysRepositoryException
055    {
056        if (proposalDate != null)
057        {
058            content.getInternalDataHolder().setValue(METADATA_LAST_PROPOSAL_DATE, proposalDate, ModelItemTypeConstants.DATETIME_TYPE_ID);
059        }
060        else
061        {
062            content.getInternalDataHolder().removeValue(METADATA_LAST_PROPOSAL_DATE);
063        }
064    }
065}