001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.Arrays;
034import java.util.Collections;
035import java.util.List;
036import javax.enterprise.context.RequestScoped;
037
038@RequestScoped
039public class GetWishlistLinesCommand
040        extends BaseSimpleCommand<GetWishlistLinesForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
046            new FieldDefinition("WishlistName", FieldType.ENTITY_NAME, false, null, null),
047            new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null)
048        ));
049    }
050    
051    /** Creates a new instance of GetWishlistLinesCommand */
052    public GetWishlistLinesCommand() {
053        super(null, FORM_FIELD_DEFINITIONS, true);
054    }
055    
056    @Override
057    protected BaseResult execute() {
058        var result = WishlistResultFactory.getGetWishlistLinesResult();
059        var wishlistName = form.getWishlistName();
060        var itemName = form.getItemName();
061        var parameterCount = (wishlistName == null ? 0 : 1) + (itemName == null ? 0 : 1);
062        
063        if(parameterCount == 1) {
064            var wishlistControl = Session.getModelController(WishlistControl.class);
065            
066            if(wishlistName != null) {
067                var orderControl = Session.getModelController(OrderControl.class);
068                var orderTypeControl = Session.getModelController(OrderTypeControl.class);
069                var order = orderControl.getOrderByName(orderTypeControl.getOrderTypeByName(OrderTypes.WISHLIST.name()), wishlistName);
070                
071                if(order != null && order.getLastDetail().getOrderType().getLastDetail().getOrderTypeName().equals(OrderTypes.WISHLIST.name())) {
072                    result.setWishlistLines(wishlistControl.getWishlistLineTransfersByOrder(getUserVisit(), order));
073                } else {
074                    addExecutionError(ExecutionErrors.UnknownWishlistName.name(), wishlistName);
075                }
076            }
077            
078            if(itemName != null) {
079                var itemControl = Session.getModelController(ItemControl.class);
080                var item = itemControl.getItemByName(itemName);
081                
082                if(item != null) {
083                    result.setWishlistLines(wishlistControl.getWishlistLineTransfersByItem(getUserVisit(), item));
084                } else {
085                    addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
086                }
087            }
088        } else {
089            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
090        }
091        
092        return result;
093    }
094    
095}