001/*
002 *  Copyright 2022 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.forms.repository;
017
018import java.time.ZonedDateTime;
019
020import javax.jcr.Node;
021
022import org.ametys.cms.data.ametysobject.ModifiableModelAwareDataAwareAmetysObject;
023import org.ametys.cms.data.holder.ModifiableIndexableDataHolder;
024import org.ametys.cms.data.holder.impl.DefaultModifiableModelAwareDataHolder;
025import org.ametys.cms.repository.WorkflowAwareContentHelper;
026import org.ametys.core.user.UserIdentity;
027import org.ametys.plugins.repository.AmetysObject;
028import org.ametys.plugins.repository.AmetysRepositoryException;
029import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
030import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
031import org.ametys.plugins.repository.jcr.SimpleAmetysObject;
032import org.ametys.plugins.workflow.repository.WorkflowAwareAmetysObject;
033
034/**
035 * Class representing a form entry, backed by a JCR node.<br>
036 */
037public class FormEntry extends SimpleAmetysObject<FormEntryFactory> implements ModifiableModelAwareDataAwareAmetysObject, WorkflowAwareAmetysObject
038{
039    /** Constant for entry attribute prefix.*/
040    public static final String SYSTEM_ATTRIBUTE_PREFIX = "ametys-";
041    
042    /** Constant for id of an entry.*/
043    public static final String ATTRIBUTE_ID = SYSTEM_ATTRIBUTE_PREFIX + "id";
044    
045    /** Constant for the date and time of submission attribute. */
046    public static final String ATTRIBUTE_SUBMIT_DATE = SYSTEM_ATTRIBUTE_PREFIX + "submit-date";
047    
048    /** The IP address of the person who answered the form. */
049    public static final String ATTRIBUTE_IP = SYSTEM_ATTRIBUTE_PREFIX + "ipAddress";
050    
051    /** The id of the user who answered the form. */
052    public static final String ATTRIBUTE_USER = SYSTEM_ATTRIBUTE_PREFIX + "user";
053    
054    /** Constant for active status of entry. Default is true */
055    public static final String ATTRIBUTE_ACTIVE = SYSTEM_ATTRIBUTE_PREFIX + "active";
056    
057    /**
058     * Creates an {@link FormEntry}.
059     * @param node the node backing this {@link AmetysObject}
060     * @param parentPath the parentPath in the Ametys hierarchy
061     * @param factory the DefaultAmetysObjectFactory which created the AmetysObject
062     */
063    public FormEntry(Node node, String parentPath, FormEntryFactory factory)
064    {
065        super(node, parentPath, factory);
066    }
067    
068    /**
069     * Get form
070     * @return the form
071     */
072    public Form getForm()
073    {
074        return this.getParent().getParent();
075    }
076    
077    /**
078     * Retrieves the id.
079     * @return the id.
080     */
081    public Long getEntryId() 
082    {
083        return getValue(ATTRIBUTE_ID);
084    }
085    
086    /**
087     * Set the id.
088     * @param id the id.
089     */
090    public void setEntryId(Long id) 
091    {
092        setValue(ATTRIBUTE_ID, id);
093    }
094    
095    /**
096     * Retrieves the ip.
097     * @return the ip.
098     */
099    public String getIP() 
100    {
101        return getValue(ATTRIBUTE_IP);
102    }
103    
104    /**
105     * Set the ip.
106     * @param ip the ip.
107     */
108    public void setIP(String ip) 
109    {
110        setValue(ATTRIBUTE_IP, ip);
111    }
112    
113    /**
114     * Retrieves the submitDate.
115     * @return the submitDate.
116     */
117    public ZonedDateTime getSubmitDate() 
118    {
119        return getValue(ATTRIBUTE_SUBMIT_DATE);
120    }
121    
122    /**
123     * Set the submitDate.
124     * @param submitDate the submitDate.
125     */
126    public void setSubmitDate(ZonedDateTime submitDate) 
127    {
128        setValue(ATTRIBUTE_SUBMIT_DATE, submitDate);
129    }
130    
131    /**
132     * Set the identity of the user answering the form
133     * @param user the user identity
134     */
135    public void setUser(UserIdentity user)
136    {
137        setValue(ATTRIBUTE_USER, user);
138    }
139    
140    /**
141     * Get active status of the entry
142     * @return the active status
143     */
144    public Boolean isActive()
145    {
146        return getValue(ATTRIBUTE_ACTIVE, false, true);
147    }
148    
149    /**
150     * Set active status of the entry
151     * @param isActive the active status
152     */
153    public void setActive(Boolean isActive)
154    {
155        setValue(ATTRIBUTE_ACTIVE, isActive);
156    }
157    
158    /**
159     * Get the identity of the user answering the form
160     * @return the user identity
161     */
162    public UserIdentity getUser()
163    {
164        return getValue(ATTRIBUTE_USER);
165    }
166    
167    public long getWorkflowId() throws AmetysRepositoryException
168    {
169        return WorkflowAwareContentHelper.getWorkflowId(this);
170    }
171
172    public void setWorkflowId(long workflowId) throws AmetysRepositoryException
173    {
174        WorkflowAwareContentHelper.setWorkflowId(this, workflowId);
175    }
176
177    public long getCurrentStepId() throws AmetysRepositoryException
178    {
179        return WorkflowAwareContentHelper.getCurrentStepId(this);
180    }
181
182    public void setCurrentStepId(long stepId) throws AmetysRepositoryException
183    {
184        WorkflowAwareContentHelper.setCurrentStepId(this, stepId);
185    }
186    
187    @Override
188    public ModifiableIndexableDataHolder getDataHolder()
189    {
190        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
191        return new DefaultModifiableModelAwareDataHolder(repositoryData, _getFactory().getFormEntryModel(getForm()));
192    }
193}