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.GetPartyInventoryCostingMethodsForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
022import com.echothree.model.control.inventory.server.control.InventoryCostingMethodControl;
023import com.echothree.model.control.inventory.server.logic.InventoryCostingMethodLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.data.inventory.server.entity.InventoryCostingMethod;
028import com.echothree.model.data.inventory.server.entity.PartyInventoryCostingMethod;
029import com.echothree.model.data.inventory.server.factory.PartyInventoryCostingMethodFactory;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import java.util.Collection;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class GetPartyInventoryCostingMethodsCommand
044        extends BasePaginatedMultipleEntitiesCommand<PartyInventoryCostingMethod, GetPartyInventoryCostingMethodsForm> {
045
046    private static final CommandSecurityDefinition COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
047            new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
048            new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
049                    new SecurityRoleDefinition(SecurityRoleGroups.InventoryCostingMethod.name(), SecurityRoles.List.name())
050            ))
051    ));
052
053    private static final List<FieldDefinition> FORM_FIELD_DEFINITIONS = List.of(
054            new FieldDefinition("InventoryCostingMethodName", FieldType.ENTITY_NAME, false, null, null),
055            new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
056            new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
057    );
058
059    @Inject
060    InventoryCostingMethodControl inventoryCostingMethodControl;
061
062    @Inject
063    InventoryCostingMethodLogic inventoryCostingMethodLogic;
064
065    @Inject
066    EntityInstanceLogic entityInstanceLogic;
067
068    private InventoryCostingMethod inventoryCostingMethod;
069
070    public GetPartyInventoryCostingMethodsCommand() {
071        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
072    }
073
074    @Override
075    protected void handleForm() {
076        var inventoryCostingMethodName = form.getInventoryCostingMethodName();
077        var parameterCount = (inventoryCostingMethodName == null ? 0 : 1)
078                + entityInstanceLogic.countPossibleEntitySpecs(form);
079
080        if(parameterCount > 0) {
081            inventoryCostingMethod = inventoryCostingMethodLogic.getInventoryCostingMethodByUniversalSpec(this, form, false);
082        }
083    }
084
085    @Override
086    protected Long getTotalEntities() {
087        return hasExecutionErrors() ? null : inventoryCostingMethod == null
088                ? inventoryCostingMethodControl.countPartyInventoryCostingMethods()
089                : inventoryCostingMethodControl.countPartyInventoryCostingMethodsByInventoryCostingMethod(inventoryCostingMethod);
090    }
091
092    @Override
093    protected Collection<PartyInventoryCostingMethod> getEntities() {
094        return hasExecutionErrors() ? null : inventoryCostingMethod == null
095                ? inventoryCostingMethodControl.getPartyInventoryCostingMethods()
096                : inventoryCostingMethodControl.getPartyInventoryCostingMethodsByInventoryCostingMethod(inventoryCostingMethod);
097    }
098
099    @Override
100    protected BaseResult getResult(Collection<PartyInventoryCostingMethod> entities) {
101        var result = InventoryResultFactory.getGetPartyInventoryCostingMethodsResult();
102
103        if(entities != null) {
104            var userVisit = getUserVisit();
105
106            if(inventoryCostingMethod != null) {
107                result.setInventoryCostingMethod(inventoryCostingMethodControl.getInventoryCostingMethodTransfer(userVisit,
108                        inventoryCostingMethod));
109            }
110
111            if(session.hasLimit(PartyInventoryCostingMethodFactory.class)) {
112                result.setPartyInventoryCostingMethodCount(getTotalEntities());
113            }
114
115            result.setPartyInventoryCostingMethods(inventoryCostingMethodControl.getPartyInventoryCostingMethodTransfers(
116                    userVisit, entities));
117        }
118
119        return result;
120    }
121}