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.party.server.command; 018 019import com.echothree.control.user.party.common.form.GetLanguageForm; 020import com.echothree.control.user.party.common.result.PartyResultFactory; 021import com.echothree.control.user.party.common.result.GetLanguageResult; 022import com.echothree.model.control.core.common.ComponentVendors; 023import com.echothree.model.control.core.common.EntityTypes; 024import com.echothree.model.control.core.common.EventTypes; 025import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.control.party.server.logic.LanguageLogic; 028import com.echothree.model.data.core.server.entity.EntityInstance; 029import com.echothree.model.data.party.server.entity.Language; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.server.control.BaseSingleEntityCommand; 036import com.echothree.util.server.persistence.Session; 037import java.util.Arrays; 038import java.util.Collections; 039import java.util.List; 040 041public class GetLanguageCommand 042 extends BaseSingleEntityCommand<Language, GetLanguageForm> { 043 044 // No COMMAND_SECURITY_DEFINITION, anyone may execute this command. 045 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 046 047 static { 048 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 049 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 050 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 051 new FieldDefinition("Key", FieldType.KEY, false, null, null), 052 new FieldDefinition("Guid", FieldType.GUID, false, null, null), 053 new FieldDefinition("Ulid", FieldType.ULID, false, null, null) 054 )); 055 } 056 057 /** Creates a new instance of GetLanguageCommand */ 058 public GetLanguageCommand(UserVisitPK userVisitPK, GetLanguageForm form) { 059 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true); 060 } 061 062 @Override 063 protected Language getEntity() { 064 var partyControl = Session.getModelController(PartyControl.class); 065 Language language = null; 066 String languageIsoName = form.getLanguageIsoName(); 067 var parameterCount = (languageIsoName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form); 068 069 switch(parameterCount) { 070 case 0: 071 language = partyControl.getDefaultLanguage(); 072 break; 073 case 1: 074 if(languageIsoName == null) { 075 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, 076 ComponentVendors.ECHO_THREE.name(), EntityTypes.Language.name()); 077 078 if(!hasExecutionErrors()) { 079 language = partyControl.getLanguageByEntityInstance(entityInstance); 080 } 081 } else { 082 language = LanguageLogic.getInstance().getLanguageByName(this, languageIsoName); 083 } 084 break; 085 default: 086 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 087 break; 088 } 089 090 if(language != null) { 091 sendEvent(language.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 092 } 093 094 return language; 095 } 096 097 @Override 098 protected BaseResult getResult(Language language) { 099 var partyControl = Session.getModelController(PartyControl.class); 100 GetLanguageResult result = PartyResultFactory.getGetLanguageResult(); 101 102 if(language != null) { 103 result.setLanguage(partyControl.getLanguageTransfer(getUserVisit(), language)); 104 } 105 106 return result; 107 } 108 109}