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