001/* 002 * Copyright 2021 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.question.sources; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.LinkedHashMap; 021import java.util.List; 022import java.util.Map; 023import java.util.Map.Entry; 024 025import org.ametys.plugins.forms.helper.FormElementDefinitionHelper; 026import org.ametys.plugins.forms.repository.FormQuestion; 027import org.ametys.runtime.i18n.I18nizableText; 028import org.ametys.runtime.model.ElementDefinition; 029import org.ametys.runtime.model.ModelItem; 030import org.ametys.runtime.model.ViewElement; 031import org.ametys.runtime.model.ViewItem; 032import org.ametys.runtime.model.type.ModelItemTypeConstants; 033 034/** 035 * Class for creating choice list from manual source 036 */ 037public class ManualSourceType extends AbstractSourceType 038{ 039 /** Constant for element definition name of the select grid*/ 040 public static final String ATTRIBUTE_GRID = "select-grid"; 041 042 /** Map of ModelItems specific to ManualSourceType */ 043 protected Map<String, ModelItem> _manualChoicesItems; 044 045 public Map<String, ModelItem> getModelItems() 046 { 047 _manualChoicesItems = new HashMap<>(); 048 049 ElementDefinition<String> grid = FormElementDefinitionHelper.getElementDefinition(ATTRIBUTE_GRID, ModelItemTypeConstants.STRING_TYPE_ID, "PLUGINS_FORMS_QUESTIONS_DIALOG_CHOICE_GRID", "PLUGINS_FORMS_QUESTIONS_DIALOG_CHOICE_GRID_DESC", null); 050 grid.setWidget("manual-source-grid"); 051 Map<String, I18nizableText> widgetParameters = new HashMap<>(); 052 widgetParameters.put("displayCost", new I18nizableText("false")); 053 grid.setWidgetParameters(widgetParameters); 054 _manualChoicesItems.put(grid.getName(), grid); 055 056 return _manualChoicesItems; 057 } 058 059 public List<ViewItem> getViewItems() 060 { 061 List<ViewItem> viewElements = new ArrayList<>(); 062 063 ViewElement grid = new ViewElement(); 064 grid.setDefinition((ElementDefinition< ? >) _manualChoicesItems.get(ATTRIBUTE_GRID)); 065 viewElements.add(grid); 066 067 return viewElements; 068 } 069 070 @Override 071 public List<String> getFieldToDisableIfFormPublished() 072 { 073 List<String> fieldNames = super.getFieldToDisableIfFormPublished(); 074 fieldNames.add(ATTRIBUTE_GRID); 075 return fieldNames; 076 } 077 078 public boolean remoteData() 079 { 080 return false; 081 } 082 083 public I18nizableText getEntry(ChoiceOption value, Map<String, Object> contextParams) throws Exception 084 { 085 Map<ChoiceOption, I18nizableText> typedEntries = getTypedEntries(contextParams); 086 return typedEntries.get(value); 087 } 088 089 public Map<ChoiceOption, I18nizableText> getTypedEntries(Map<String, Object> contextParams) throws Exception 090 { 091 Map<ChoiceOption, I18nizableText> entries = new LinkedHashMap<>(); 092 093 FormQuestion question = _getQuestionFromParam(contextParams); 094 String gridOptionAsJson = question.getValue(ATTRIBUTE_GRID); 095 Map<String, Object> options = _jsonUtils.convertJsonToMap(gridOptionAsJson); 096 for (Entry<String, Object> entry : options.entrySet()) 097 { 098 @SuppressWarnings("unchecked") 099 Map<String, Object> values = (Map<String, Object>) entry.getValue(); 100 String label = entry.getKey(); 101 ChoiceOption choice = new ChoiceOption(values.get("value")); 102 entries.put(choice, new I18nizableText(label)); 103 } 104 105 return entries; 106 } 107 108 public Map<ChoiceOption, I18nizableText> searchEntries(Map<String, Object> contextParams, int limit, Object paginationData) throws Exception 109 { 110 throw new UnsupportedOperationException("Method searchEntries can't be called for ManualSourceType"); 111 } 112}