001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.GetSymbolPositionForm; 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.SymbolPositionLogic; 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.SymbolPosition; 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 java.util.List; 035import javax.enterprise.context.Dependent; 036import javax.inject.Inject; 037 038@Dependent 039public class GetSymbolPositionCommand 040 extends BaseSingleEntityCommand<SymbolPosition, GetSymbolPositionForm> { 041 042 // No COMMAND_SECURITY_DEFINITION, anyone may execute this command. 043 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 044 045 static { 046 FORM_FIELD_DEFINITIONS = List.of( 047 new FieldDefinition("SymbolPositionName", FieldType.ENTITY_NAME, true, null, null), 048 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 049 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 050 ); 051 } 052 053 @Inject 054 AccountingControl accountingControl; 055 056 @Inject 057 EntityInstanceLogic entityInstanceLogic; 058 059 @Inject 060 SymbolPositionLogic symbolPositionLogic; 061 062 063 /** Creates a new instance of GetSymbolPositionCommand */ 064 public GetSymbolPositionCommand() { 065 super(null, FORM_FIELD_DEFINITIONS, true); 066 } 067 068 @Override 069 protected SymbolPosition getEntity() { 070 SymbolPosition symbolPosition = null; 071 var symbolPositionName = form.getSymbolPositionName(); 072 var parameterCount = (symbolPositionName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(form); 073 074 switch(parameterCount) { 075 case 0 -> symbolPosition = accountingControl.getDefaultSymbolPosition(); 076 case 1 -> { 077 if(symbolPositionName == null) { 078 var entityInstance = entityInstanceLogic.getEntityInstance(this, form, 079 ComponentVendors.ECHO_THREE.name(), EntityTypes.SymbolPosition.name()); 080 081 if(!hasExecutionErrors()) { 082 symbolPosition = accountingControl.getSymbolPositionByEntityInstance(entityInstance); 083 } 084 } else { 085 symbolPosition = symbolPositionLogic.getSymbolPositionByName(this, symbolPositionName); 086 } 087 } 088 default -> addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 089 } 090 091 if(symbolPosition != null) { 092 sendEvent(symbolPosition.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 093 } 094 095 return symbolPosition; 096 } 097 098 @Override 099 protected BaseResult getResult(SymbolPosition symbolPosition) { 100 var result = AccountingResultFactory.getGetSymbolPositionResult(); 101 102 if(symbolPosition != null) { 103 result.setSymbolPosition(accountingControl.getSymbolPositionTransfer(getUserVisit(), symbolPosition)); 104 } 105 106 return result; 107 } 108 109}