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