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