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.vendor.server.command;
018
019import com.echothree.control.user.vendor.common.form.GetVendorItemsForm;
020import com.echothree.control.user.vendor.common.result.VendorResultFactory;
021import com.echothree.model.control.item.server.control.ItemControl;
022import com.echothree.model.control.item.server.logic.ItemLogic;
023import com.echothree.model.control.party.common.PartyTypes;
024import com.echothree.model.control.security.common.SecurityRoleGroups;
025import com.echothree.model.control.security.common.SecurityRoles;
026import com.echothree.model.control.vendor.server.control.VendorControl;
027import com.echothree.model.control.vendor.server.logic.VendorLogic;
028import com.echothree.model.data.item.server.entity.Item;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.model.data.vendor.server.entity.Vendor;
031import com.echothree.model.data.vendor.server.entity.VendorItem;
032import com.echothree.model.data.vendor.server.factory.VendorItemFactory;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.server.control.BaseMultipleEntitiesCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import com.echothree.util.server.persistence.Session;
042import java.util.Arrays;
043import java.util.Collection;
044import java.util.Collections;
045import java.util.List;
046import javax.enterprise.context.RequestScoped;
047
048@RequestScoped
049public class GetVendorItemsCommand
050        extends BaseMultipleEntitiesCommand<VendorItem, GetVendorItemsForm> {
051
052    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
053    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
054    
055    static {
056        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
057                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
058                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
059                        new SecurityRoleDefinition(SecurityRoleGroups.VendorItem.name(), SecurityRoles.List.name())
060                        )))
061                )));
062        
063        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
064                new FieldDefinition("VendorName", FieldType.ENTITY_NAME, false, null, null),
065                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null)
066                ));
067    }
068    
069    /** Creates a new instance of GetVendorItemsCommand */
070    public GetVendorItemsCommand() {
071        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
072    }
073
074    Vendor vendor;
075    Item item;
076
077    @Override
078    protected Collection<VendorItem> getEntities() {
079        var vendorControl = Session.getModelController(VendorControl.class);
080        var vendorName = form.getVendorName();
081        var itemName = form.getItemName();
082        var parameterCount = (vendorName == null ? 0 : 1) + (itemName == null ? 0 : 1);
083        Collection<VendorItem> entities = null;
084
085        if(parameterCount == 1) {
086            if(vendorName != null) {
087                vendor = VendorLogic.getInstance().getVendorByName(this, vendorName, null, null);
088
089                if(!hasExecutionErrors()) {
090                    var vendorParty = vendor.getParty();
091
092                    entities = vendorControl.getVendorItemsByVendorParty(vendorParty);
093                }
094            } else if(itemName != null) {
095                item = ItemLogic.getInstance().getItemByNameThenAlias(this, itemName);
096
097                if(!hasExecutionErrors()) {
098                    entities = vendorControl.getVendorItemsByItem(item);
099                }
100            }
101        } else {
102            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
103        }
104
105        return entities;
106    }
107
108    @Override
109    protected BaseResult getResult(Collection<VendorItem> entities) {
110        var result = VendorResultFactory.getGetVendorItemsResult();
111
112        if(entities != null) {
113            var vendorControl = Session.getModelController(VendorControl.class);
114            var userVisit = getUserVisit();
115
116            result.setVendorItems(vendorControl.getVendorItemTransfers(userVisit, entities));
117
118            if(vendor != null) {
119                var vendorParty = vendor.getParty();
120
121                result.setVendor(vendorControl.getVendorTransfer(userVisit, vendor));
122
123                if(session.hasLimit(VendorItemFactory.class)) {
124                    result.setVendorItemCount(vendorControl.countVendorItemsByVendorParty(vendorParty));
125                }
126            } else if(item != null) {
127                var itemControl = Session.getModelController(ItemControl.class);
128
129                result.setItem(itemControl.getItemTransfer(userVisit, item));
130
131                if(session.hasLimit(VendorItemFactory.class)) {
132                    result.setVendorItemCount(vendorControl.countVendorItemsByItem(item));
133                }
134            }
135        }
136
137        return result;
138    }
139
140}