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.wishlist.server.command; 018 019import com.echothree.control.user.wishlist.common.form.CreateWishlistLineForm; 020import com.echothree.model.control.accounting.server.control.AccountingControl; 021import com.echothree.model.control.inventory.common.InventoryConstants; 022import com.echothree.model.control.inventory.server.control.InventoryControl; 023import com.echothree.model.control.item.server.control.ItemControl; 024import com.echothree.model.control.offer.server.control.OfferItemControl; 025import com.echothree.model.control.offer.server.control.SourceControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.party.server.control.PartyControl; 028import com.echothree.model.control.uom.server.control.UomControl; 029import com.echothree.model.control.wishlist.server.control.WishlistControl; 030import com.echothree.model.control.wishlist.server.logic.WishlistLogic; 031import com.echothree.model.data.inventory.server.entity.InventoryCondition; 032import com.echothree.model.data.user.common.pk.UserVisitPK; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.server.control.BaseSimpleCommand; 038import com.echothree.util.server.persistence.Session; 039import java.util.List; 040import javax.enterprise.context.Dependent; 041 042@Dependent 043public class CreateWishlistLineCommand 044 extends BaseSimpleCommand<CreateWishlistLineForm> { 045 046 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 047 048 static { 049 FORM_FIELD_DEFINITIONS = List.of( 050 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 051 new FieldDefinition("WishlistTypeName", FieldType.ENTITY_NAME, false, null, null), 052 new FieldDefinition("WishlistPriorityName", FieldType.ENTITY_NAME, false, null, null), 053 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null), 054 new FieldDefinition("SourceName", FieldType.ENTITY_NAME, false, null, null), 055 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 056 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null), 057 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null), 058 new FieldDefinition("Quantity", FieldType.UNSIGNED_LONG, false, null, null), 059 new FieldDefinition("Comment", FieldType.STRING, false, 1L, 80L) 060 ); 061 } 062 063 /** Creates a new instance of CreateWishlistLineCommand */ 064 public CreateWishlistLineCommand() { 065 super(null, FORM_FIELD_DEFINITIONS, false); 066 } 067 068 @Override 069 protected BaseResult execute() { 070 var partyControl = Session.getModelController(PartyControl.class); 071 var partyName = form.getPartyName(); 072 var party = partyName == null? getParty(): partyControl.getPartyByName(partyName); 073 074 if(party != null) { 075 var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName(); 076 077 if(partyTypeName.equals(PartyTypes.CUSTOMER.name())) { 078 var wishlistControl = Session.getModelController(WishlistControl.class); 079 var wishlistTypeName = form.getWishlistTypeName(); 080 var wishlistType = wishlistTypeName == null? wishlistControl.getDefaultWishlistType(): 081 wishlistControl.getWishlistTypeByName(wishlistTypeName); 082 083 if(wishlistType != null) { 084 var wishlistPriorityName = form.getWishlistPriorityName(); 085 var wishlistPriority = wishlistPriorityName == null? wishlistControl.getDefaultWishlistPriority(wishlistType): 086 wishlistControl.getWishlistPriorityByName(wishlistType, wishlistPriorityName); 087 088 if(wishlistPriority != null) { 089 var accountingControl = Session.getModelController(AccountingControl.class); 090 var currencyIsoName = form.getCurrencyIsoName(); 091 var currency = currencyIsoName == null? accountingControl.getDefaultCurrency(): 092 accountingControl.getCurrencyByIsoName(currencyIsoName); 093 094 if(currency != null) { 095 var sourceControl = Session.getModelController(SourceControl.class); 096 var sourceName = form.getSourceName(); 097 var source = sourceName == null? sourceControl.getDefaultSource(): sourceControl.getSourceByName(sourceName); 098 099 if(source != null) { 100 var itemControl = Session.getModelController(ItemControl.class); 101 var itemName = form.getItemName(); 102 var item = itemControl.getItemByName(itemName); 103 104 if(item != null) { 105 var offerItemControl = Session.getModelController(OfferItemControl.class); 106 var offer = source.getLastDetail().getOfferUse().getLastDetail().getOffer(); 107 var offerItem = offerItemControl.getOfferItem(offer, item); 108 109 if(offerItem != null) { 110 var inventoryControl = Session.getModelController(InventoryControl.class); 111 var inventoryConditionName = form.getInventoryConditionName(); 112 var inventoryConditionUseType = inventoryControl.getInventoryConditionUseTypeByName(InventoryConstants.InventoryConditionUseType_SALES_ORDER); 113 InventoryCondition inventoryCondition = null; 114 115 if(inventoryConditionName == null) { 116 var inventoryConditionUse = inventoryControl.getDefaultInventoryConditionUse(inventoryConditionUseType); 117 118 if(inventoryConditionUse == null) { 119 addExecutionError(ExecutionErrors.MissingDefaultInventoryConditionUse.name()); 120 } else { 121 inventoryCondition = inventoryConditionUse.getInventoryCondition(); 122 } 123 } else { 124 inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName); 125 126 if(inventoryControl.getInventoryConditionUse(inventoryConditionUseType, inventoryCondition) == null) { 127 addExecutionError(ExecutionErrors.InvalidInventoryCondition.name()); 128 } 129 } 130 131 if(!hasExecutionErrors()) { 132 var uomControl = Session.getModelController(UomControl.class); 133 var unitOfMeasureKind = item.getLastDetail().getUnitOfMeasureKind(); 134 var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName(); 135 var unitOfMeasureType = unitOfMeasureTypeName == null? uomControl.getDefaultUnitOfMeasureType(unitOfMeasureKind): 136 uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName); 137 138 if(unitOfMeasureType != null) { 139 var offerItemPrice = offerItemControl.getOfferItemPrice(offerItem, inventoryCondition, unitOfMeasureType, currency); 140 141 if(offerItemPrice != null) { 142 var strQuantity = form.getQuantity(); 143 Long quantity = strQuantity == null ? 1L : Long.valueOf(strQuantity); 144 var comment = form.getComment(); 145 146 WishlistLogic.getInstance().createWishlistLine(session, this, getUserVisit(), party, source, offerItemPrice, 147 wishlistType, wishlistPriority, quantity, comment, getPartyPK()); 148 } else { 149 addExecutionError(ExecutionErrors.UnknownOfferItemPrice.name(), 150 offerItem.getOffer().getLastDetail().getOfferName(), 151 item.getLastDetail().getItemName(), 152 inventoryCondition.getLastDetail().getInventoryConditionName(), 153 unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(), 154 currency.getCurrencyIsoName()); 155 } 156 } else { 157 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName); 158 } 159 } 160 } else { 161 addExecutionError(ExecutionErrors.UnknownOfferItem.name(), offer.getLastDetail().getOfferName(), itemName); 162 } 163 } else { 164 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 165 } 166 } else { 167 addExecutionError(ExecutionErrors.UnknownSourceName.name(), sourceName); 168 } 169 } else { 170 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 171 } 172 } else { 173 addExecutionError(ExecutionErrors.UnknownWishlistPriorityName.name(), wishlistPriorityName); 174 } 175 } else { 176 addExecutionError(ExecutionErrors.UnknownWishlistTypeName.name(), wishlistTypeName); 177 } 178 } else { 179 addExecutionError(ExecutionErrors.InvalidPartyType.name(), partyTypeName); 180 } 181 } else { 182 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 183 } 184 185 return null; 186 } 187 188}