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.item.server.command; 018 019import com.echothree.control.user.item.common.form.GetItemPriceForm; 020import com.echothree.control.user.item.common.result.ItemResultFactory; 021import com.echothree.model.control.accounting.server.logic.CurrencyLogic; 022import com.echothree.model.control.inventory.server.logic.InventoryConditionLogic; 023import com.echothree.model.control.item.server.control.ItemControl; 024import com.echothree.model.control.item.server.logic.ItemLogic; 025import com.echothree.model.control.security.common.SecurityRoleGroups; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.control.security.server.logic.SecurityRoleLogic; 028import com.echothree.model.control.uom.server.logic.UnitOfMeasureTypeLogic; 029import com.echothree.model.data.item.server.entity.ItemPrice; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.command.BaseResult; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 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; 040import javax.enterprise.context.RequestScoped; 041 042@RequestScoped 043public class GetItemPriceCommand 044 extends BaseSingleEntityCommand<ItemPrice, GetItemPriceForm> { 045 046 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 047 048 static { 049 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 050 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 051 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null), 052 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null), 053 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null), 054 new FieldDefinition("IncludeHistory", FieldType.BOOLEAN, false, null, null) 055 )); 056 } 057 058 /** Creates a new instance of GetItemPriceCommand */ 059 public GetItemPriceCommand() { 060 super(null, FORM_FIELD_DEFINITIONS, false); 061 } 062 063 @Override 064 protected boolean checkOptionalSecurityRoles() { 065 // This occurs before validation, and parseBoolean is more lax than our validation of what's permitted for a FieldType.BOOLEAN. 066 return Boolean.parseBoolean(form.getIncludeHistory()) ? SecurityRoleLogic.getInstance().hasSecurityRoleUsingNames(this, getParty(), 067 SecurityRoleGroups.ItemPrice.name(), SecurityRoles.History.name()) : true; 068 } 069 070 @Override 071 protected ItemPrice getEntity() { 072 var itemControl = Session.getModelController(ItemControl.class); 073 var item = ItemLogic.getInstance().getItemByName(this, form.getItemName()); 074 var inventoryCondition = InventoryConditionLogic.getInstance().getInventoryConditionByName(this, form.getInventoryConditionName()); 075 var currency = CurrencyLogic.getInstance().getCurrencyByName(this, form.getCurrencyIsoName()); 076 ItemPrice itemPrice = null; 077 078 if(!hasExecutionErrors()) { 079 var unitOfMeasureType = UnitOfMeasureTypeLogic.getInstance().getUnitOfMeasureTypeByName(this, item.getLastDetail().getUnitOfMeasureKind(), form.getUnitOfMeasureTypeName()); 080 081 if(!hasExecutionErrors()) { 082 itemPrice = itemControl.getItemPrice(item, inventoryCondition, unitOfMeasureType, currency); 083 084 if(itemPrice == null) { 085 addExecutionError(ExecutionErrors.UnknownItemPrice.name(), item.getLastDetail().getItemName(), 086 inventoryCondition.getLastDetail().getInventoryConditionName(), 087 unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(), currency.getCurrencyIsoName()); 088 } 089 } 090 091 } 092 093 return itemPrice; 094 } 095 096 @Override 097 protected BaseResult getResult(ItemPrice entity) { 098 var result = ItemResultFactory.getGetItemPriceResult(); 099 100 if(entity != null) { 101 var itemControl = Session.getModelController(ItemControl.class); 102 var userVisit = getUserVisit(); 103 104 result.setItemPrice(itemControl.getItemPriceTransfer(userVisit, entity)); 105 106 if(Boolean.parseBoolean(form.getIncludeHistory())) { 107 result.setHistory(itemControl.getItemPriceHistory(userVisit, entity)); 108 } 109 } 110 111 return result; 112 } 113 114}