001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 Echo Three, LLC 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// -------------------------------------------------------------------------------- 016 017package com.echothree.control.user.selector.server.command; 018 019import com.echothree.control.user.selector.common.form.GetSelectorChoicesForm; 020import com.echothree.control.user.selector.common.result.GetSelectorChoicesResult; 021import com.echothree.control.user.selector.common.result.SelectorResultFactory; 022import com.echothree.model.control.selector.server.control.SelectorControl; 023import com.echothree.model.data.party.server.entity.Language; 024import com.echothree.model.data.selector.server.entity.SelectorKind; 025import com.echothree.model.data.selector.server.entity.SelectorType; 026import com.echothree.model.data.user.common.pk.UserVisitPK; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.validation.FieldDefinition; 029import com.echothree.util.common.validation.FieldType; 030import com.echothree.util.common.command.BaseResult; 031import com.echothree.util.server.control.BaseSimpleCommand; 032import com.echothree.util.server.persistence.Session; 033import java.util.Arrays; 034import java.util.Collections; 035import java.util.List; 036 037public class GetSelectorChoicesCommand 038 extends BaseSimpleCommand<GetSelectorChoicesForm> { 039 040 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 041 042 static { 043 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 044 new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, true, null, null), 045 new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, true, null, null), 046 new FieldDefinition("DefaultSelectorChoice", FieldType.ENTITY_NAME, false, null, null), 047 new FieldDefinition("AllowNullChoice", FieldType.BOOLEAN, true, null, null) 048 )); 049 } 050 051 /** Creates a new instance of GetSelectorChoicesCommand */ 052 public GetSelectorChoicesCommand(UserVisitPK userVisitPK, GetSelectorChoicesForm form) { 053 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false); 054 } 055 056 @Override 057 protected BaseResult execute() { 058 var selectorControl = Session.getModelController(SelectorControl.class); 059 GetSelectorChoicesResult result = SelectorResultFactory.getGetSelectorChoicesResult(); 060 String selectorKindName = form.getSelectorKindName(); 061 SelectorKind selectorKind = selectorControl.getSelectorKindByName(selectorKindName); 062 063 if(selectorKind != null) { 064 String selectorTypeName = form.getSelectorTypeName(); 065 SelectorType selectorType = selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName); 066 067 if(selectorType != null) { 068 String defaultSelectorChoice = form.getDefaultSelectorChoice(); 069 Language language = getPreferredLanguage(); 070 boolean allowNullChoice = Boolean.parseBoolean(form.getAllowNullChoice()); 071 072 result.setSelectorChoices(selectorControl.getSelectorChoices(selectorType, defaultSelectorChoice, language, allowNullChoice)); 073 } else { 074 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName); 075 } 076 } else { 077 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName); 078 } 079 080 return result; 081 } 082 083}