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.accounting.server.command; 018 019import com.echothree.control.user.accounting.common.form.GetCurrencyForm; 020import com.echothree.control.user.accounting.common.result.AccountingResultFactory; 021import com.echothree.control.user.accounting.common.result.GetCurrencyResult; 022import com.echothree.model.control.accounting.server.control.AccountingControl; 023import com.echothree.model.control.accounting.server.logic.CurrencyLogic; 024import com.echothree.model.control.core.common.ComponentVendors; 025import com.echothree.model.control.core.common.EntityTypes; 026import com.echothree.model.control.core.common.EventTypes; 027import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 028import com.echothree.model.data.accounting.server.entity.Currency; 029import com.echothree.model.data.core.server.entity.EntityInstance; 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 GetCurrencyCommand 042 extends BaseSingleEntityCommand<Currency, GetCurrencyForm> { 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("CurrencyIsoName", 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 GetCurrencyCommand */ 058 public GetCurrencyCommand(UserVisitPK userVisitPK, GetCurrencyForm form) { 059 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true); 060 } 061 062 @Override 063 protected Currency getEntity() { 064 var accountingControl = Session.getModelController(AccountingControl.class); 065 Currency currency = null; 066 String currencyIsoName = form.getCurrencyIsoName(); 067 var parameterCount = (currencyIsoName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form); 068 069 switch(parameterCount) { 070 case 0: 071 currency = accountingControl.getDefaultCurrency(); 072 break; 073 case 1: 074 if(currencyIsoName == null) { 075 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, 076 ComponentVendors.ECHO_THREE.name(), EntityTypes.Currency.name()); 077 078 if(!hasExecutionErrors()) { 079 currency = accountingControl.getCurrencyByEntityInstance(entityInstance); 080 } 081 } else { 082 currency = CurrencyLogic.getInstance().getCurrencyByName(this, currencyIsoName); 083 } 084 break; 085 default: 086 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 087 break; 088 } 089 090 if(currency != null) { 091 sendEvent(currency.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 092 } 093 094 return currency; 095 } 096 097 @Override 098 protected BaseResult getResult(Currency currency) { 099 var accountingControl = Session.getModelController(AccountingControl.class); 100 GetCurrencyResult result = AccountingResultFactory.getGetCurrencyResult(); 101 102 if(currency != null) { 103 result.setCurrency(accountingControl.getCurrencyTransfer(getUserVisit(), currency)); 104 } 105 106 return result; 107 } 108 109}