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.inventory.server.command;
018
019import com.echothree.control.user.inventory.common.form.GetInventoryLayerBucketsForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.server.control.BucketControl;
022import com.echothree.model.control.inventory.server.control.InventoryBucketTypeControl;
023import com.echothree.model.control.inventory.server.control.InventoryLayerControl;
024import com.echothree.model.control.inventory.server.logic.InventoryBucketTypeLogic;
025import com.echothree.model.control.inventory.server.logic.InventoryLayerLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.inventory.server.entity.InventoryBucketType;
030import com.echothree.model.data.inventory.server.entity.InventoryLayer;
031import com.echothree.model.data.inventory.server.entity.InventoryLayerBucket;
032import com.echothree.model.data.inventory.server.factory.InventoryLayerBucketFactory;
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.BasePaginatedMultipleEntitiesCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import java.util.Collection;
042import java.util.List;
043import javax.enterprise.context.Dependent;
044import javax.inject.Inject;
045
046@Dependent
047public class GetInventoryLayerBucketsCommand
048        extends BasePaginatedMultipleEntitiesCommand<InventoryLayerBucket, GetInventoryLayerBucketsForm> {
049
050    private static final CommandSecurityDefinition COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
051            new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052            new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
053                    new SecurityRoleDefinition(SecurityRoleGroups.InventoryLayerBucket.name(), SecurityRoles.List.name())
054            ))
055    ));
056
057    private static final List<FieldDefinition> FORM_FIELD_DEFINITIONS = List.of(
058            new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
059            new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null),
060            new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null),
061            new FieldDefinition("InventoryLayerSequence", FieldType.UNSIGNED_INTEGER, false, null, null),
062            new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null),
063            new FieldDefinition("InventoryBucketTypeName", FieldType.ENTITY_NAME, false, null, null)
064    );
065
066    @Inject
067    BucketControl bucketControl;
068
069    @Inject
070    InventoryBucketTypeControl inventoryBucketTypeControl;
071
072    @Inject
073    InventoryLayerControl inventoryLayerControl;
074
075    @Inject
076    InventoryBucketTypeLogic inventoryBucketTypeLogic;
077
078    @Inject
079    InventoryLayerLogic inventoryLayerLogic;
080
081    private InventoryLayer inventoryLayer;
082    private InventoryBucketType inventoryBucketType;
083
084    public GetInventoryLayerBucketsCommand() {
085        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
086    }
087
088    @Override
089    protected void handleForm() {
090        var partyName = form.getPartyName();
091        var companyName = form.getCompanyName();
092        var itemName = form.getItemName();
093        var inventoryConditionName = form.getInventoryConditionName();
094        var inventoryLayerSequence = form.getInventoryLayerSequence();
095        var inventoryBucketTypeName = form.getInventoryBucketTypeName();
096        var companyParameterCount = (partyName == null ? 0 : 1) + (companyName == null ? 0 : 1);
097        var hasAnyInventoryLayerParameter = companyParameterCount != 0 || itemName != null
098                || inventoryConditionName != null || inventoryLayerSequence != null;
099        var hasCompleteInventoryLayer = companyParameterCount == 1 && itemName != null
100                && inventoryConditionName != null && inventoryLayerSequence != null;
101        var selectorCount = (hasCompleteInventoryLayer ? 1 : 0) + (inventoryBucketTypeName == null ? 0 : 1);
102
103        if((hasAnyInventoryLayerParameter && !hasCompleteInventoryLayer) || selectorCount != 1) {
104            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
105        } else if(hasCompleteInventoryLayer) {
106            inventoryLayer = inventoryLayerLogic.getInventoryLayerByName(this, companyName, partyName,
107                    itemName, inventoryConditionName, Integer.valueOf(inventoryLayerSequence));
108        } else {
109            inventoryBucketType = inventoryBucketTypeLogic.getInventoryBucketTypeByName(this,
110                    inventoryBucketTypeName);
111        }
112    }
113
114    @Override
115    protected Long getTotalEntities() {
116        return hasExecutionErrors() ? null : inventoryLayer != null
117                ? bucketControl.countInventoryLayerBucketsByInventoryLayer(inventoryLayer)
118                : bucketControl.countInventoryLayerBucketsByInventoryBucketType(inventoryBucketType);
119    }
120
121    @Override
122    protected Collection<InventoryLayerBucket> getEntities() {
123        return hasExecutionErrors() ? null : inventoryLayer != null
124                ? bucketControl.getInventoryLayerBucketsByInventoryLayer(inventoryLayer)
125                : bucketControl.getInventoryLayerBucketsByInventoryBucketType(inventoryBucketType);
126    }
127
128    @Override
129    protected BaseResult getResult(Collection<InventoryLayerBucket> entities) {
130        var result = InventoryResultFactory.getGetInventoryLayerBucketsResult();
131
132        if(entities != null) {
133            var userVisit = getUserVisit();
134
135            if(inventoryLayer != null) {
136                result.setInventoryLayer(inventoryLayerControl.getInventoryLayerTransfer(userVisit,
137                        inventoryLayer));
138            } else {
139                result.setInventoryBucketType(inventoryBucketTypeControl.getInventoryBucketTypeTransfer(userVisit,
140                        inventoryBucketType));
141            }
142
143            if(session.hasLimit(InventoryLayerBucketFactory.class)) {
144                result.setInventoryLayerBucketCount(getTotalEntities());
145            }
146
147            result.setInventoryLayerBuckets(bucketControl.getInventoryLayerBucketTransfers(userVisit, entities));
148        }
149
150        return result;
151    }
152
153}