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.item.server.logic.ItemLogic; 023import com.echothree.model.control.order.common.OrderTypes; 024import com.echothree.model.control.order.server.control.OrderControl; 025import com.echothree.model.control.order.server.control.OrderTypeControl; 026import com.echothree.model.control.wishlist.server.control.WishlistControl; 027import com.echothree.model.data.item.server.entity.Item; 028import com.echothree.model.data.order.server.entity.Order; 029import com.echothree.model.data.order.server.entity.OrderLine; 030import com.echothree.model.data.order.server.factory.OrderLineFactory; 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.BasePaginatedMultipleEntitiesCommand; 036import java.util.Collection; 037import java.util.List; 038import javax.enterprise.context.Dependent; 039import javax.inject.Inject; 040 041@Dependent 042public class GetWishlistLinesCommand 043 extends BasePaginatedMultipleEntitiesCommand<OrderLine, GetWishlistLinesForm> { 044 045 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 046 047 static { 048 FORM_FIELD_DEFINITIONS = List.of( 049 new FieldDefinition("WishlistName", FieldType.ENTITY_NAME, false, null, null), 050 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null) 051 ); 052 } 053 054 @Inject 055 ItemControl itemControl; 056 057 @Inject 058 OrderControl orderControl; 059 060 @Inject 061 OrderTypeControl orderTypeControl; 062 063 @Inject 064 WishlistControl wishlistControl; 065 066 @Inject 067 ItemLogic itemLogic; 068 069 /** Creates a new instance of GetWishlistLinesCommand */ 070 public GetWishlistLinesCommand() { 071 super(null, FORM_FIELD_DEFINITIONS, true); 072 } 073 074 private Order order; 075 private Item item; 076 077 @Override 078 protected void handleForm() { 079 var wishlistName = form.getWishlistName(); 080 var itemName = form.getItemName(); 081 var parameterCount = (wishlistName == null ? 0 : 1) + (itemName == null ? 0 : 1); 082 083 if(parameterCount == 1) { 084 if(wishlistName != null) { 085 var orderType = orderTypeControl.getOrderTypeByName(OrderTypes.WISHLIST.name()); 086 087 if(orderType != null) { 088 order = orderControl.getOrderByName(orderType, wishlistName); 089 090 if(order == null || !order.getLastDetail().getOrderType().getLastDetail().getOrderTypeName().equals(OrderTypes.WISHLIST.name())) { 091 addExecutionError(ExecutionErrors.UnknownWishlistName.name(), wishlistName); 092 } 093 } else { 094 addExecutionError(ExecutionErrors.UnknownOrderTypeName.name(), OrderTypes.WISHLIST.name()); 095 } 096 } else { 097 item = itemLogic.getItemByName(this, itemName); 098 } 099 } else { 100 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 101 } 102 } 103 104 @Override 105 protected Long getTotalEntities() { 106 Long total = null; 107 108 if(!hasExecutionErrors()) { 109 if(order != null) { 110 total = wishlistControl.countWishlistLinesByOrder(order); 111 } else { 112 total = wishlistControl.countWishlistLinesByItem(item); 113 } 114 } 115 116 return total; 117 } 118 119 @Override 120 protected Collection<OrderLine> getEntities() { 121 Collection<OrderLine> entities = null; 122 123 if(!hasExecutionErrors()) { 124 if(order != null) { 125 entities = wishlistControl.getWishlistLinesByOrder(order); 126 } else { 127 entities = wishlistControl.getWishlistLinesByItem(item); 128 } 129 } 130 131 return entities; 132 } 133 134 @Override 135 protected BaseResult getResult(Collection<OrderLine> entities) { 136 var result = WishlistResultFactory.getGetWishlistLinesResult(); 137 138 if(entities != null) { 139 var userVisit = getUserVisit(); 140 141 if(order != null) { 142 result.setWishlist(wishlistControl.getWishlistTransfer(userVisit, order)); 143 } else { 144 result.setItem(itemControl.getItemTransfer(userVisit, item)); 145 } 146 147 if(session.hasLimit(OrderLineFactory.class)) { 148 result.setWishlistLineCount(getTotalEntities()); 149 } 150 151 result.setWishlistLines(wishlistControl.getWishlistLineTransfers(userVisit, entities)); 152 } 153 154 return result; 155 } 156 157}