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.apache.avalon.framework.service.ServiceException;
021
022import org.ametys.plugins.repository.AmetysObjectFactory;
023import org.ametys.plugins.repository.AmetysRepositoryException;
024import org.ametys.plugins.repository.data.type.ModelItemTypeExtensionPoint;
025import org.ametys.plugins.repository.jcr.SimpleAmetysObjectFactory;
026import org.ametys.runtime.model.DefaultElementDefinition;
027import org.ametys.runtime.model.Model;
028import org.ametys.runtime.model.exception.BadItemTypeException;
029import org.ametys.runtime.model.exception.UnknownTypeException;
030import org.ametys.runtime.model.type.ModelItemTypeConstants;
031
032/**
033 * {@link AmetysObjectFactory} for handling {@link FormPageRule}s.
034 */
035public class FormPageRuleFactory extends SimpleAmetysObjectFactory
036{
037    @Override
038    public FormPageRule getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
039    {
040        return new FormPageRule(node, parentPath, this);
041    }
042    
043    /**
044     * Get the form entry model
045     * @return the form entry model
046     * @throws AmetysRepositoryException if an error occurs.
047     */
048    public Model getFormRuleModel() throws AmetysRepositoryException
049    {
050        String role = ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC;
051        
052        try
053        {
054            return Model.of(
055                "form.page.rule.model.id", 
056                "form.page.rule.model.family.id",
057                DefaultElementDefinition.of(FormPageRule.ATTRIBUTE_OPTION_VALUE, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
058                DefaultElementDefinition.of(FormPageRule.ATTRIBUTE_TARGET_ID, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
059                DefaultElementDefinition.of(FormPageRule.ATTRIBUTE_TYPE, false, ModelItemTypeConstants.STRING_TYPE_ID, role)
060            );
061        }
062        catch (UnknownTypeException | BadItemTypeException | ServiceException e)
063        {
064            throw new AmetysRepositoryException("An error occurred while creating the Rule model", e);
065        }
066    }
067}