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