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 javax.jcr.Node;
019
020import org.ametys.cms.data.ametysobject.ModifiableModelAwareDataAwareAmetysObject;
021import org.ametys.cms.data.holder.ModifiableIndexableDataHolder;
022import org.ametys.cms.data.holder.impl.DefaultModifiableModelAwareDataHolder;
023import org.ametys.plugins.repository.AmetysObject;
024import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
025import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
026import org.ametys.plugins.repository.jcr.SimpleAmetysObject;
027
028/**
029 * Class representing a form rule on a page.
030 */
031public class FormPageRule extends SimpleAmetysObject<FormPageRuleFactory> implements ModifiableModelAwareDataAwareAmetysObject
032{
033    /** Constant for the date and time of submission attribute. */
034    public static final String ATTRIBUTE_OPTION_VALUE = "option-value";
035    
036    /** Constant for the rule type attribute. */
037    public static final String ATTRIBUTE_TYPE = "type";
038    
039    /** Constant for the page id attribute. */
040    public static final String ATTRIBUTE_TARGET_ID = "target-id";
041    
042    /** Type of a page rule. */
043    public enum PageRuleType
044    {
045        /** Jump. */
046        JUMP,
047        /** Skip. */
048        SKIP,
049        /** Finish. */
050        FINISH
051    }
052    
053    /**
054     * Creates an {@link FormPageRule}.
055     * @param node the node backing this {@link AmetysObject}
056     * @param parentPath the parentPath in the Ametys hierarchy
057     * @param factory the DefaultAmetysObjectFactory which created the AmetysObject
058     */
059    public FormPageRule(Node node, String parentPath, FormPageRuleFactory factory)
060    {
061        super(node, parentPath, factory);
062    }
063    
064    /**
065     * Get the type
066     * @return the type
067     */
068    public PageRuleType getType ()
069    {
070        String value = getValue(ATTRIBUTE_TYPE);
071        return PageRuleType.valueOf(value);
072    }
073    
074    /**
075     * Set the type
076     * @param type the rule type
077     */
078    public void setType (PageRuleType type)
079    {
080        setValue(ATTRIBUTE_TYPE, type.name());
081    }
082    
083    /**
084     * Get the page to jump or skip. Can be null.
085     * @return the page to jump or skip
086     */
087    public String getPageId ()
088    {
089        return getValue(ATTRIBUTE_TARGET_ID);
090    }
091
092    /**
093     * Set the page id
094     * @param id the page id
095     */
096    public void setPageId (String id)
097    {
098        setValue(ATTRIBUTE_TARGET_ID, id);
099    }
100    
101    /**
102     * Get the selected option
103     * @return the selected option
104     */
105    public String getOption ()
106    {
107        return getValue(ATTRIBUTE_OPTION_VALUE);
108    }
109    
110    /**
111     * Set the option value
112     * @param value the option value
113     */
114    public void setOption(String value)
115    {
116        setValue(ATTRIBUTE_OPTION_VALUE, value);
117    }
118    
119    @Override
120    public ModifiableIndexableDataHolder getDataHolder()
121    {
122        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
123        return new DefaultModifiableModelAwareDataHolder(repositoryData, _getFactory().getFormRuleModel());
124    }
125}