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