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