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.edit.InventoryEditFactory;
020import com.echothree.control.user.inventory.common.edit.PartyInventoryCostingMethodEdit;
021import com.echothree.control.user.inventory.common.result.EditPartyInventoryCostingMethodResult;
022import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
023import com.echothree.control.user.party.common.spec.PartyUniversalSpec;
024import com.echothree.model.control.inventory.server.control.InventoryCostingMethodControl;
025import com.echothree.model.control.inventory.server.logic.InventoryCostingMethodLogic;
026import com.echothree.model.control.inventory.server.logic.PartyInventoryCostingMethodLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.party.server.logic.PartyLogic;
029import com.echothree.model.control.security.common.SecurityRoleGroups;
030import com.echothree.model.control.security.common.SecurityRoles;
031import com.echothree.model.data.inventory.server.entity.InventoryCostingMethod;
032import com.echothree.model.data.inventory.server.entity.PartyInventoryCostingMethod;
033import com.echothree.model.data.party.server.entity.Party;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class EditPartyInventoryCostingMethodCommand
046        extends BaseAbstractEditCommand<PartyUniversalSpec, PartyInventoryCostingMethodEdit,
047        EditPartyInventoryCostingMethodResult, PartyInventoryCostingMethod, Party> {
048
049    private static final CommandSecurityDefinition COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
050            new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
051            new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
052                    new SecurityRoleDefinition(SecurityRoleGroups.InventoryCostingMethod.name(), SecurityRoles.Edit.name())
053            ))
054    ));
055
056    private static final List<FieldDefinition> SPEC_FIELD_DEFINITIONS = List.of(
057            new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
058            new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
059            new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
060    );
061
062    private static final List<FieldDefinition> EDIT_FIELD_DEFINITIONS = List.of(
063            new FieldDefinition("InventoryCostingMethodName", FieldType.ENTITY_NAME, true, null, null)
064    );
065
066    @Inject
067    InventoryCostingMethodControl inventoryCostingMethodControl;
068
069    @Inject
070    InventoryCostingMethodLogic inventoryCostingMethodLogic;
071
072    @Inject
073    PartyInventoryCostingMethodLogic partyInventoryCostingMethodLogic;
074
075    @Inject
076    PartyLogic partyLogic;
077
078    private Party party;
079    private InventoryCostingMethod inventoryCostingMethod;
080
081    public EditPartyInventoryCostingMethodCommand() {
082        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
083    }
084
085    @Override
086    public EditPartyInventoryCostingMethodResult getResult() {
087        return InventoryResultFactory.getEditPartyInventoryCostingMethodResult();
088    }
089
090    @Override
091    public PartyInventoryCostingMethodEdit getEdit() {
092        return InventoryEditFactory.getPartyInventoryCostingMethodEdit();
093    }
094
095    @Override
096    public PartyInventoryCostingMethod getEntity(EditPartyInventoryCostingMethodResult result) {
097        PartyInventoryCostingMethod partyInventoryCostingMethod = null;
098
099        party = partyLogic.getPartyByName(this, spec.getPartyName(), spec);
100
101        if(!hasExecutionErrors()) {
102            partyInventoryCostingMethod = partyInventoryCostingMethodLogic.getPartyInventoryCostingMethod(this, party,
103                    false, getPartyPK(), editModeToEntityPermission(editMode));
104        }
105
106        return partyInventoryCostingMethod;
107    }
108
109    @Override
110    public Party getLockEntity(PartyInventoryCostingMethod partyInventoryCostingMethod) {
111        return party;
112    }
113
114    @Override
115    public void fillInResult(EditPartyInventoryCostingMethodResult result,
116            PartyInventoryCostingMethod partyInventoryCostingMethod) {
117        result.setPartyInventoryCostingMethod(inventoryCostingMethodControl.getPartyInventoryCostingMethodTransfer(
118                getUserVisit(), partyInventoryCostingMethod));
119    }
120
121    @Override
122    public void doLock(PartyInventoryCostingMethodEdit edit, PartyInventoryCostingMethod partyInventoryCostingMethod) {
123        edit.setInventoryCostingMethodName(partyInventoryCostingMethod.getInventoryCostingMethod().getLastDetail()
124                .getInventoryCostingMethodName());
125    }
126
127    @Override
128    public void canUpdate(PartyInventoryCostingMethod partyInventoryCostingMethod) {
129        inventoryCostingMethod = inventoryCostingMethodLogic.getInventoryCostingMethodByName(this,
130                edit.getInventoryCostingMethodName());
131    }
132
133    @Override
134    public void doUpdate(PartyInventoryCostingMethod partyInventoryCostingMethod) {
135        var partyInventoryCostingMethodValue = inventoryCostingMethodControl
136                .getPartyInventoryCostingMethodValue(partyInventoryCostingMethod);
137
138        partyInventoryCostingMethodValue.setInventoryCostingMethodPK(inventoryCostingMethod.getPrimaryKey());
139        partyInventoryCostingMethodLogic.updatePartyInventoryCostingMethodFromValue(partyInventoryCostingMethodValue,
140                getPartyPK());
141    }
142}