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.offer.server.command; 018 019import com.echothree.control.user.offer.common.form.GetOfferItemPriceForm; 020import com.echothree.control.user.offer.common.result.GetOfferItemPriceResult; 021import com.echothree.control.user.offer.common.result.OfferResultFactory; 022import com.echothree.model.control.accounting.server.control.AccountingControl; 023import com.echothree.model.control.inventory.server.control.InventoryControl; 024import com.echothree.model.control.item.server.control.ItemControl; 025import com.echothree.model.control.offer.server.control.OfferControl; 026import com.echothree.model.control.offer.server.control.OfferItemControl; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.control.security.server.logic.SecurityRoleLogic; 031import com.echothree.model.control.uom.server.control.UomControl; 032import com.echothree.model.data.accounting.server.entity.Currency; 033import com.echothree.model.data.inventory.server.entity.InventoryCondition; 034import com.echothree.model.data.item.server.entity.Item; 035import com.echothree.model.data.offer.server.entity.Offer; 036import com.echothree.model.data.offer.server.entity.OfferItem; 037import com.echothree.model.data.offer.server.entity.OfferItemPrice; 038import com.echothree.model.data.uom.server.entity.UnitOfMeasureKind; 039import com.echothree.model.data.uom.server.entity.UnitOfMeasureType; 040import com.echothree.model.data.user.common.pk.UserVisitPK; 041import com.echothree.model.data.user.server.entity.UserVisit; 042import com.echothree.util.common.command.BaseResult; 043import com.echothree.util.common.message.ExecutionErrors; 044import com.echothree.util.common.validation.FieldDefinition; 045import com.echothree.util.common.validation.FieldType; 046import com.echothree.util.server.control.BaseSingleEntityCommand; 047import com.echothree.util.server.control.CommandSecurityDefinition; 048import com.echothree.util.server.control.PartyTypeDefinition; 049import com.echothree.util.server.control.SecurityRoleDefinition; 050import com.echothree.util.server.persistence.Session; 051import java.util.Arrays; 052import java.util.Collections; 053import java.util.List; 054 055public class GetOfferItemPriceCommand 056 extends BaseSingleEntityCommand<OfferItemPrice, GetOfferItemPriceForm> { 057 058 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 059 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 060 061 static { 062 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 063 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 064 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 065 new SecurityRoleDefinition(SecurityRoleGroups.OfferItemPrice.name(), SecurityRoles.Review.name()) 066 ))) 067 ))); 068 069 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 070 new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, 20L), 071 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 072 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null), 073 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null), 074 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null), 075 new FieldDefinition("IncludeHistory", FieldType.BOOLEAN, false, null, null) 076 )); 077 } 078 079 /** Creates a new instance of GetOfferItemPriceCommand */ 080 public GetOfferItemPriceCommand(UserVisitPK userVisitPK, GetOfferItemPriceForm form) { 081 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 082 } 083 084 @Override 085 protected boolean checkOptionalSecurityRoles() { 086 // This occurs before validation, and parseBoolean is more lax than our validation of what's permitted for a FieldType.BOOLEAN. 087 return Boolean.parseBoolean(form.getIncludeHistory()) ? SecurityRoleLogic.getInstance().hasSecurityRoleUsingNames(this, getParty(), 088 SecurityRoleGroups.OfferItemPrice.name(), SecurityRoles.History.name()) : true; 089 } 090 091 @Override 092 protected OfferItemPrice getEntity() { 093 var offerControl = Session.getModelController(OfferControl.class); 094 String offerName = form.getOfferName(); 095 Offer offer = offerControl.getOfferByName(offerName); 096 OfferItemPrice offerItemPrice = null; 097 098 if(offer != null) { 099 var itemControl = Session.getModelController(ItemControl.class); 100 String itemName = form.getItemName(); 101 Item item = itemControl.getItemByName(itemName); 102 103 if(item != null) { 104 var offerItemControl = Session.getModelController(OfferItemControl.class); 105 OfferItem offerItem = offerItemControl.getOfferItem(offer, item); 106 107 if(offerItem != null) { 108 var inventoryControl = Session.getModelController(InventoryControl.class); 109 String inventoryConditionName = form.getInventoryConditionName(); 110 InventoryCondition inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName); 111 112 if(inventoryCondition != null) { 113 var uomControl = Session.getModelController(UomControl.class); 114 UnitOfMeasureKind unitOfMeasureKind = item.getLastDetail().getUnitOfMeasureKind(); 115 String unitOfMeasureTypeName = form.getUnitOfMeasureTypeName(); 116 UnitOfMeasureType unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName); 117 118 if(unitOfMeasureType != null) { 119 var accountingControl = Session.getModelController(AccountingControl.class); 120 String currencyIsoName = form.getCurrencyIsoName(); 121 Currency currency = accountingControl.getCurrencyByIsoName(currencyIsoName); 122 123 if(currency != null) { 124 offerItemPrice = offerItemControl.getOfferItemPrice(offerItem, inventoryCondition, unitOfMeasureType, currency); 125 126 if(offerItemPrice == null) { 127 addExecutionError(ExecutionErrors.UnknownOfferItemPrice.name(), offer.getLastDetail().getOfferName(), 128 item.getLastDetail().getItemName(), inventoryCondition.getLastDetail().getInventoryConditionName(), 129 unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(), currency.getCurrencyIsoName()); 130 } 131 } else { 132 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 133 } 134 } else { 135 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), 136 unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName(), unitOfMeasureTypeName); 137 } 138 } else { 139 addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName); 140 } 141 } else { 142 addExecutionError(ExecutionErrors.UnknownOfferItem.name(), offer.getLastDetail().getOfferName(), 143 item.getLastDetail().getItemName()); 144 } 145 } else { 146 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 147 } 148 } else { 149 addExecutionError(ExecutionErrors.UnknownOfferName.name(), offerName); 150 } 151 152 return offerItemPrice; 153 } 154 155 @Override 156 protected BaseResult getResult(OfferItemPrice offerItemPrice) { 157 GetOfferItemPriceResult result = OfferResultFactory.getGetOfferItemPriceResult(); 158 159 if(offerItemPrice != null) { 160 var offerItemControl = Session.getModelController(OfferItemControl.class); 161 UserVisit userVisit = getUserVisit(); 162 163 result.setOfferItemPrice(offerItemControl.getOfferItemPriceTransfer(userVisit, offerItemPrice)); 164 165 if(Boolean.parseBoolean(form.getIncludeHistory())) { 166 result.setHistory(offerItemControl.getOfferItemPriceHistory(userVisit, offerItemPrice)); 167 } 168 } 169 170 return result; 171 } 172 173}