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.InventoryConditionEdit;
021import com.echothree.control.user.inventory.common.form.EditInventoryConditionForm;
022import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
023import com.echothree.control.user.inventory.common.result.EditInventoryConditionResult;
024import com.echothree.control.user.inventory.common.spec.InventoryConditionUniversalSpec;
025import com.echothree.model.control.inventory.server.control.InventoryConditionControl;
026import com.echothree.model.control.inventory.server.logic.InventoryConditionLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.inventory.server.entity.InventoryCondition;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseAbstractEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.List;
040import javax.inject.Inject;
041import javax.enterprise.context.Dependent;
042
043@Dependent
044public class EditInventoryConditionCommand
045        extends BaseAbstractEditCommand<InventoryConditionUniversalSpec, InventoryConditionEdit, EditInventoryConditionResult, InventoryCondition, InventoryCondition> {
046
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.InventoryCondition.name(), SecurityRoles.Edit.name())
056                ))
057        ));
058
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
062                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
063        );
064
065        EDIT_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
068                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
069                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
070        );
071    }
072
073    @Inject
074    InventoryConditionControl inventoryConditionControl;
075
076    @Inject
077    InventoryConditionLogic inventoryConditionLogic;
078
079    /** Creates a new instance of EditInventoryConditionCommand */
080    public EditInventoryConditionCommand() {
081        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
082    }
083
084    @Override
085    public EditInventoryConditionResult getResult() {
086        return InventoryResultFactory.getEditInventoryConditionResult();
087    }
088
089    @Override
090    public InventoryConditionEdit getEdit() {
091        return InventoryEditFactory.getInventoryConditionEdit();
092    }
093
094    @Override
095    public InventoryCondition getEntity(EditInventoryConditionResult result) {
096        return inventoryConditionLogic.getInventoryConditionByUniversalSpec(this, spec, false, editModeToEntityPermission(editMode));
097    }
098
099    @Override
100    public InventoryCondition getLockEntity(InventoryCondition inventoryCondition) {
101        return inventoryCondition;
102    }
103
104    @Override
105    public void fillInResult(EditInventoryConditionResult result, InventoryCondition inventoryCondition) {
106        result.setInventoryCondition(inventoryConditionControl.getInventoryConditionTransfer(getUserVisit(), inventoryCondition));
107    }
108
109    @Override
110    public void doLock(InventoryConditionEdit edit, InventoryCondition inventoryCondition) {
111        var inventoryConditionDescription = inventoryConditionControl.getInventoryConditionDescription(inventoryCondition, getPreferredLanguage());
112        var inventoryConditionDetail = inventoryCondition.getLastDetail();
113
114        edit.setInventoryConditionName(inventoryConditionDetail.getInventoryConditionName());
115        edit.setIsDefault(inventoryConditionDetail.getIsDefault().toString());
116        edit.setSortOrder(inventoryConditionDetail.getSortOrder().toString());
117
118        if(inventoryConditionDescription != null) {
119            edit.setDescription(inventoryConditionDescription.getDescription());
120        }
121    }
122
123    @Override
124    public void canUpdate(InventoryCondition inventoryCondition) {
125        var inventoryConditionName = edit.getInventoryConditionName();
126        var duplicateInventoryCondition = inventoryConditionControl.getInventoryConditionByName(inventoryConditionName);
127
128        if(duplicateInventoryCondition != null && !inventoryCondition.equals(duplicateInventoryCondition)) {
129            addExecutionError(ExecutionErrors.DuplicateInventoryConditionName.name(), inventoryConditionName);
130        }
131    }
132
133    @Override
134    public void doUpdate(InventoryCondition inventoryCondition) {
135        var partyPK = getPartyPK();
136        var inventoryConditionDetailValue = inventoryConditionControl.getInventoryConditionDetailValueForUpdate(inventoryCondition);
137        var inventoryConditionDescription = inventoryConditionControl.getInventoryConditionDescriptionForUpdate(inventoryCondition, getPreferredLanguage());
138        var description = edit.getDescription();
139
140        inventoryConditionDetailValue.setInventoryConditionName(edit.getInventoryConditionName());
141        inventoryConditionDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
142        inventoryConditionDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
143
144        inventoryConditionControl.updateInventoryConditionFromValue(inventoryConditionDetailValue, partyPK);
145
146        if(inventoryConditionDescription == null && description != null) {
147            inventoryConditionControl.createInventoryConditionDescription(inventoryCondition, getPreferredLanguage(), description, partyPK);
148        } else if(inventoryConditionDescription != null && description == null) {
149            inventoryConditionControl.deleteInventoryConditionDescription(inventoryConditionDescription, partyPK);
150        } else if(inventoryConditionDescription != null && description != null) {
151            var inventoryConditionDescriptionValue = inventoryConditionControl.getInventoryConditionDescriptionValue(inventoryConditionDescription);
152
153            inventoryConditionDescriptionValue.setDescription(description);
154            inventoryConditionControl.updateInventoryConditionDescriptionFromValue(inventoryConditionDescriptionValue, partyPK);
155        }
156    }
157
158}