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.util.LinkedHashMap; 019import java.util.Map; 020 021import javax.jcr.Node; 022 023import org.ametys.plugins.forms.helper.FormElementDefinitionHelper; 024import org.ametys.plugins.forms.question.FormQuestionType; 025import org.ametys.plugins.forms.question.types.ChoicesListQuestionType; 026import org.ametys.plugins.repository.AmetysObjectFactory; 027import org.ametys.plugins.repository.AmetysRepositoryException; 028import org.ametys.plugins.repository.jcr.SimpleAmetysObjectFactory; 029import org.ametys.runtime.model.ElementDefinition; 030import org.ametys.runtime.model.Model; 031import org.ametys.runtime.model.ModelItem; 032import org.ametys.runtime.model.type.ModelItemTypeConstants; 033 034/** 035 * {@link AmetysObjectFactory} for handling {@link FormEntry}s. 036 */ 037public class FormEntryFactory extends SimpleAmetysObjectFactory 038{ 039 @Override 040 public FormEntry getAmetysObject(Node node, String parentPath) throws AmetysRepositoryException 041 { 042 return new FormEntry(node, parentPath, this); 043 } 044 045 /** 046 * Get the form entry model 047 * @param form the form 048 * @return the form entry model 049 */ 050 public Model getFormEntryModel(Form form) 051 { 052 Map<String, ModelItem> items = new LinkedHashMap<>(); 053 for (FormQuestion question : form.getQuestions()) 054 { 055 FormQuestionType type = question.getType(); 056 if (!type.onlyForDisplay(question)) 057 { 058 Model entryModel = question.getType().getEntryModel(question); 059 for (ModelItem modelItem : entryModel.getModelItems()) 060 { 061 items.put(modelItem.getName(), modelItem); 062 } 063 064 if (type instanceof ChoicesListQuestionType cLType) 065 { 066 ModelItem otherFieldModel = cLType.getOtherFieldModel(question); 067 if (otherFieldModel != null) 068 { 069 items.put(ChoicesListQuestionType.OTHER_PREFIX_DATA_NAME + question.getNameForForm(), otherFieldModel); 070 } 071 } 072 } 073 } 074 075 ElementDefinition idModelItem = FormElementDefinitionHelper.getElementDefinition(FormEntry.ATTRIBUTE_ID, ModelItemTypeConstants.LONG_TYPE_ID, "PLUGIN_FORMS_MODEL_ITEM_ID_LABEL", null, null); 076 items.put(idModelItem.getName(), idModelItem); 077 078 ElementDefinition userModelItem = FormElementDefinitionHelper.getElementDefinition(FormEntry.ATTRIBUTE_USER, org.ametys.cms.data.type.ModelItemTypeConstants.USER_ELEMENT_TYPE_ID, "PLUGIN_FORMS_MODEL_ITEM_USER_LABEL", null, null); 079 items.put(userModelItem.getName(), userModelItem); 080 081 ElementDefinition activeModelItem = FormElementDefinitionHelper.getElementDefinition(FormEntry.ATTRIBUTE_ACTIVE, ModelItemTypeConstants.BOOLEAN_TYPE_ID, "PLUGIN_FORMS_MODEL_ITEM_ACTIVE_LABEL", null, null); 082 items.put(activeModelItem.getName(), activeModelItem); 083 084 ElementDefinition submitDateModelItem = FormElementDefinitionHelper.getElementDefinition(FormEntry.ATTRIBUTE_SUBMIT_DATE, ModelItemTypeConstants.DATETIME_TYPE_ID, "PLUGIN_FORMS_MODEL_ITEM_SUBMISSION_DATE_LABEL", null, null); 085 items.put(submitDateModelItem.getName(), submitDateModelItem); 086 087 ElementDefinition ipAddressModelItem = FormElementDefinitionHelper.getElementDefinition(FormEntry.ATTRIBUTE_IP, ModelItemTypeConstants.STRING_TYPE_ID, "PLUGIN_FORMS_MODEL_ITEM_IP_LABEL", null, null); 088 items.put(ipAddressModelItem.getName(), ipAddressModelItem); 089 090 return Model.of( 091 "form.entry.model.id", 092 "form.entry.model.family.id", 093 items.values().toArray(new ModelItem[items.size()]) 094 ); 095 } 096}