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.GetWishlistLinesForm; 020import com.echothree.control.user.wishlist.common.result.WishlistResultFactory; 021import com.echothree.model.control.item.server.control.ItemControl; 022import com.echothree.model.control.order.common.OrderTypes; 023import com.echothree.model.control.order.server.control.OrderControl; 024import com.echothree.model.control.order.server.control.OrderTypeControl; 025import com.echothree.model.control.wishlist.server.control.WishlistControl; 026import com.echothree.model.data.user.common.pk.UserVisitPK; 027import com.echothree.util.common.command.BaseResult; 028import com.echothree.util.common.message.ExecutionErrors; 029import com.echothree.util.common.validation.FieldDefinition; 030import com.echothree.util.common.validation.FieldType; 031import com.echothree.util.server.control.BaseSimpleCommand; 032import com.echothree.util.server.persistence.Session; 033import java.util.List; 034import javax.enterprise.context.Dependent; 035 036@Dependent 037public class GetWishlistLinesCommand 038 extends BaseSimpleCommand<GetWishlistLinesForm> { 039 040 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 041 042 static { 043 FORM_FIELD_DEFINITIONS = List.of( 044 new FieldDefinition("WishlistName", FieldType.ENTITY_NAME, false, null, null), 045 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null) 046 ); 047 } 048 049 /** Creates a new instance of GetWishlistLinesCommand */ 050 public GetWishlistLinesCommand() { 051 super(null, FORM_FIELD_DEFINITIONS, true); 052 } 053 054 @Override 055 protected BaseResult execute() { 056 var result = WishlistResultFactory.getGetWishlistLinesResult(); 057 var wishlistName = form.getWishlistName(); 058 var itemName = form.getItemName(); 059 var parameterCount = (wishlistName == null ? 0 : 1) + (itemName == null ? 0 : 1); 060 061 if(parameterCount == 1) { 062 var wishlistControl = Session.getModelController(WishlistControl.class); 063 064 if(wishlistName != null) { 065 var orderControl = Session.getModelController(OrderControl.class); 066 var orderTypeControl = Session.getModelController(OrderTypeControl.class); 067 var order = orderControl.getOrderByName(orderTypeControl.getOrderTypeByName(OrderTypes.WISHLIST.name()), wishlistName); 068 069 if(order != null && order.getLastDetail().getOrderType().getLastDetail().getOrderTypeName().equals(OrderTypes.WISHLIST.name())) { 070 result.setWishlistLines(wishlistControl.getWishlistLineTransfersByOrder(getUserVisit(), order)); 071 } else { 072 addExecutionError(ExecutionErrors.UnknownWishlistName.name(), wishlistName); 073 } 074 } 075 076 if(itemName != null) { 077 var itemControl = Session.getModelController(ItemControl.class); 078 var item = itemControl.getItemByName(itemName); 079 080 if(item != null) { 081 result.setWishlistLines(wishlistControl.getWishlistLineTransfersByItem(getUserVisit(), item)); 082 } else { 083 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 084 } 085 } 086 } else { 087 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 088 } 089 090 return result; 091 } 092 093}