001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.model.control.accounting.server.control.AccountingControl; 022import com.echothree.model.control.accounting.server.logic.CurrencyLogic; 023import com.echothree.model.control.core.common.ComponentVendors; 024import com.echothree.model.control.core.common.EntityTypes; 025import com.echothree.model.control.core.common.EventTypes; 026import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 027import com.echothree.model.data.accounting.server.entity.Currency; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.server.control.BaseSingleEntityCommand; 034import com.echothree.util.server.persistence.Session; 035import java.util.Arrays; 036import java.util.Collections; 037import java.util.List; 038import javax.enterprise.context.RequestScoped; 039 040@RequestScoped 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("Uuid", FieldType.UUID, false, null, null) 052 )); 053 } 054 055 /** Creates a new instance of GetCurrencyCommand */ 056 public GetCurrencyCommand() { 057 super(null, FORM_FIELD_DEFINITIONS, true); 058 } 059 060 @Override 061 protected Currency getEntity() { 062 var accountingControl = Session.getModelController(AccountingControl.class); 063 Currency currency = null; 064 var currencyIsoName = form.getCurrencyIsoName(); 065 var parameterCount = (currencyIsoName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form); 066 067 switch(parameterCount) { 068 case 0 -> currency = accountingControl.getDefaultCurrency(); 069 case 1 -> { 070 if(currencyIsoName == null) { 071 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, 072 ComponentVendors.ECHO_THREE.name(), EntityTypes.Currency.name()); 073 074 if(!hasExecutionErrors()) { 075 currency = accountingControl.getCurrencyByEntityInstance(entityInstance); 076 } 077 } else { 078 currency = CurrencyLogic.getInstance().getCurrencyByName(this, currencyIsoName); 079 } 080 } 081 default -> addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 082 } 083 084 if(currency != null) { 085 sendEvent(currency.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 086 } 087 088 return currency; 089 } 090 091 @Override 092 protected BaseResult getResult(Currency currency) { 093 var accountingControl = Session.getModelController(AccountingControl.class); 094 var result = AccountingResultFactory.getGetCurrencyResult(); 095 096 if(currency != null) { 097 result.setCurrency(accountingControl.getCurrencyTransfer(getUserVisit(), currency)); 098 } 099 100 return result; 101 } 102 103}