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