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;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
024import org.ametys.core.group.GroupManager;
025import org.ametys.plugins.forms.dao.FormDirectoryDAO;
026import org.ametys.plugins.repository.AmetysObjectFactory;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.repository.RepositoryConstants;
029import org.ametys.plugins.repository.data.type.ModelItemTypeExtensionPoint;
030import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObjectFactory;
031import org.ametys.runtime.model.DefaultElementDefinition;
032import org.ametys.runtime.model.Model;
033import org.ametys.runtime.model.exception.BadItemTypeException;
034import org.ametys.runtime.model.exception.UnknownTypeException;
035import org.ametys.runtime.model.type.ModelItemTypeConstants;
036
037/**
038 * {@link AmetysObjectFactory} for handling {@link Form}s.
039 */
040public class FormFactory extends DefaultTraversableAmetysObjectFactory
041{
042    /** JCR nodetype for form */
043    public static final String FORM_NODETYPE = RepositoryConstants.NAMESPACE_PREFIX + ":form";
044    
045    /** Group manager */
046    protected GroupManager _groupManager;
047    
048    /** Content type extension point */
049    protected ContentTypeExtensionPoint _cTypeEP;
050
051    /** The Form DAO */
052    protected FormDirectoryDAO _formDAO;
053    
054    /** The Form model */
055    protected Model _model;
056    
057    @Override
058    public void service(ServiceManager manager) throws ServiceException
059    {
060        super.service(manager);
061        _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
062        _groupManager = (GroupManager) manager.lookup(GroupManager.ROLE);
063        _formDAO = (FormDirectoryDAO) manager.lookup(FormDirectoryDAO.ROLE);
064    }
065    
066    @Override
067    public Form getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException
068    {
069        return new Form(node, parentPath, this);
070    }
071    
072    /**
073     * Group manager getter.
074     * @return The group manager
075     */
076    GroupManager _getGroupManager()
077    {
078        return _groupManager;
079    }
080    
081    /**
082     * Getter for the content type extension point
083     * @return The content type EP.
084     */
085    ContentTypeExtensionPoint _getContentTypeExtensionPoint()
086    {
087        return _cTypeEP;
088    }
089    
090    /**
091     * Getter for the ForDAO
092     * @return the form DAO
093     */
094    FormDirectoryDAO getFormDirectoriesDAO()
095    {
096        return _formDAO;
097    }
098
099    /**
100     * Retrieves the Form model
101     * @return the Form model
102     */
103    public Model getModel()
104    {
105        if (_model == null)
106        {
107            _model = _createFormModel();
108        }
109        return _model;
110    }
111    
112    /**
113     * Creates the Form model
114     * @return the created model
115     * @throws AmetysRepositoryException if an error occurs.
116     */
117    protected Model _createFormModel() throws AmetysRepositoryException
118    {
119        try
120        {
121            String role = ModelItemTypeExtensionPoint.ROLE_MODEL_AWARE_BASIC;
122            return Model.of(Form.class.getName(), Form.class.getName(),
123                    DefaultElementDefinition.of(Form.TITLE, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
124                    DefaultElementDefinition.of(Form.DESCRIPTION, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
125                    DefaultElementDefinition.of(Form.CREATIONDATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, role),
126                    DefaultElementDefinition.of(Form.LASTMODIFICATIONDATE, false, ModelItemTypeConstants.DATETIME_TYPE_ID, role),
127                    DefaultElementDefinition.of(Form.AUTHOR, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, role),
128                    DefaultElementDefinition.of(Form.CONTRIBUTOR, false, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, role),
129                    DefaultElementDefinition.of(Form.LIMIT_TO_ONE_ENTRY_BY_USER, false, ModelItemTypeConstants.BOOLEAN_TYPE_ID, role),
130                    DefaultElementDefinition.of(Form.LIMIT_ENTRIES_ENABLED, false, ModelItemTypeConstants.BOOLEAN_TYPE_ID, role),
131                    DefaultElementDefinition.of(Form.QUEUE_ENABLED, false, ModelItemTypeConstants.BOOLEAN_TYPE_ID, role),
132                    DefaultElementDefinition.of(Form.MAX_ENTRIES, false, ModelItemTypeConstants.LONG_TYPE_ID, role),
133                    DefaultElementDefinition.of(Form.CLOSED_MSG, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
134                    DefaultElementDefinition.of(Form.REMAINING_MSG, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
135                    DefaultElementDefinition.of(Form.QUEUE_SIZE, false, ModelItemTypeConstants.LONG_TYPE_ID, role),
136                    DefaultElementDefinition.of(Form.QUEUE_CLOSED_MSG, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
137                    DefaultElementDefinition.of(Form.QUEUE_SENDER, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
138                    DefaultElementDefinition.of(Form.QUEUE_RECEIVER, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
139                    DefaultElementDefinition.of(Form.QUEUE_SUBJECT, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
140                    DefaultElementDefinition.of(Form.QUEUE_BODY, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
141                    DefaultElementDefinition.of(Form.ADMIN_EMAILS, true, ModelItemTypeConstants.STRING_TYPE_ID, role),
142                    DefaultElementDefinition.of(Form.RECEIPT_SENDER, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
143                    DefaultElementDefinition.of(Form.RECEIPT_RECEIVER, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
144                    DefaultElementDefinition.of(Form.RECEIPT_SUBJECT, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
145                    DefaultElementDefinition.of(Form.RECEIPT_BODY, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
146                    DefaultElementDefinition.of(Form.WORKFLOWNAME, false, ModelItemTypeConstants.STRING_TYPE_ID, role),
147                    DefaultElementDefinition.of(Form.START_DATE, false, ModelItemTypeConstants.DATE_TYPE_ID, role),
148                    DefaultElementDefinition.of(Form.END_DATE, false, ModelItemTypeConstants.DATE_TYPE_ID, role));
149        }
150        catch (UnknownTypeException | BadItemTypeException | ServiceException e)
151        {
152            throw new AmetysRepositoryException("An error occurred while creating the Form model", e);
153        }
154    }
155}