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.warehouse.server.command;
018
019import com.echothree.control.user.warehouse.common.edit.LocationNameElementEdit;
020import com.echothree.control.user.warehouse.common.edit.WarehouseEditFactory;
021import com.echothree.control.user.warehouse.common.result.EditLocationNameElementResult;
022import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory;
023import com.echothree.control.user.warehouse.common.spec.LocationNameElementSpec;
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.control.warehouse.server.control.WarehouseControl;
028import com.echothree.model.data.warehouse.server.entity.LocationNameElement;
029import com.echothree.util.common.command.EditMode;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.server.control.BaseAbstractEditCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039import javax.inject.Inject;
040
041@Dependent
042public class EditLocationNameElementCommand
043        extends BaseAbstractEditCommand<LocationNameElementSpec, LocationNameElementEdit, EditLocationNameElementResult, LocationNameElement, LocationNameElement> {
044
045    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
046    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
047    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
048    
049    static {
050        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.LocationNameElement.name(), SecurityRoles.Edit.name())
054                ))
055        ));
056
057        SPEC_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("LocationTypeName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("LocationNameElementName", FieldType.ENTITY_NAME, true, null, null)
061        );
062        
063        EDIT_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("LocationNameElementName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("Offset", FieldType.UNSIGNED_INTEGER, true, null, null),
066                new FieldDefinition("Length", FieldType.UNSIGNED_INTEGER, true, null, null),
067                new FieldDefinition("ValidationPattern", FieldType.REGULAR_EXPRESSION, false, null, null),
068                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
069        );
070    }
071
072    @Inject
073    WarehouseControl warehouseControl;
074
075    
076    /** Creates a new instance of EditLocationNameElementCommand */
077    public EditLocationNameElementCommand() {
078        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
079    }
080
081    @Override
082    public EditLocationNameElementResult getResult() {
083        return WarehouseResultFactory.getEditLocationNameElementResult();
084    }
085
086    @Override
087    public LocationNameElementEdit getEdit() {
088        return WarehouseEditFactory.getLocationNameElementEdit();
089    }
090
091    @Override
092    public LocationNameElement getEntity(EditLocationNameElementResult result) {
093        LocationNameElement locationNameElement = null;
094        var warehouseName = spec.getWarehouseName();
095        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
096
097        if(warehouse != null) {
098            var warehouseParty = warehouse.getParty();
099            var locationTypeName = spec.getLocationTypeName();
100            var locationType = warehouseControl.getLocationTypeByName(warehouseParty, locationTypeName);
101
102            if(locationType != null) {
103                var locationNameElementName = spec.getLocationNameElementName();
104
105                locationNameElement = warehouseControl.getLocationNameElementByName(locationType, locationNameElementName,
106                        editModeToEntityPermission(editMode));
107
108                if(locationNameElement == null) {
109                    addExecutionError(ExecutionErrors.UnknownLocationNameElementName.name(),
110                            warehouse.getWarehouseName(), locationType.getLastDetail().getLocationTypeName(), locationNameElementName);
111                }
112            } else {
113                addExecutionError(ExecutionErrors.UnknownLocationTypeName.name(), warehouse.getWarehouseName(), locationTypeName);
114            }
115        } else {
116            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
117        }
118
119        return locationNameElement;
120    }
121
122    @Override
123    public LocationNameElement getLockEntity(LocationNameElement locationNameElement) {
124        return locationNameElement;
125    }
126
127    @Override
128    public void fillInResult(EditLocationNameElementResult result, LocationNameElement locationNameElement) {
129        result.setLocationNameElement(warehouseControl.getLocationNameElementTransfer(getUserVisit(), locationNameElement));
130    }
131
132    @Override
133    public void doLock(LocationNameElementEdit edit, LocationNameElement locationNameElement) {
134        var locationNameElementDescription = warehouseControl.getLocationNameElementDescription(locationNameElement, getPreferredLanguage());
135        var locationNameElementDetail = locationNameElement.getLastDetail();
136
137        edit.setLocationNameElementName(locationNameElementDetail.getLocationNameElementName());
138        edit.setOffset(locationNameElementDetail.getOffset().toString());
139        edit.setLength(locationNameElementDetail.getLength().toString());
140        edit.setValidationPattern(locationNameElementDetail.getValidationPattern());
141
142        if(locationNameElementDescription != null) {
143            edit.setDescription(locationNameElementDescription.getDescription());
144        }
145    }
146
147    @Override
148    public void canUpdate(LocationNameElement locationNameElement) {
149        var locationType = locationNameElement.getLastDetail().getLocationType();
150        var locationNameElementName = edit.getLocationNameElementName();
151        var duplicateLocationNameElement = warehouseControl.getLocationNameElementByName(locationType, locationNameElementName);
152
153        if(duplicateLocationNameElement != null && !locationNameElement.equals(duplicateLocationNameElement)) {
154            addExecutionError(ExecutionErrors.DuplicateLocationNameElementName.name(), locationNameElementName);
155        }
156    }
157
158    @Override
159    public void doUpdate(LocationNameElement locationNameElement) {
160        var partyPK = getPartyPK();
161        var locationNameElementDetailValue = warehouseControl.getLocationNameElementDetailValueForUpdate(locationNameElement);
162        var locationNameElementDescription = warehouseControl.getLocationNameElementDescriptionForUpdate(locationNameElement, getPreferredLanguage());
163        var description = edit.getDescription();
164
165        locationNameElementDetailValue.setLocationNameElementName(edit.getLocationNameElementName());
166        locationNameElementDetailValue.setOffset(Integer.valueOf(edit.getOffset()));
167        locationNameElementDetailValue.setLength(Integer.valueOf(edit.getLength()));
168        locationNameElementDetailValue.setValidationPattern(edit.getValidationPattern());
169
170        warehouseControl.updateLocationNameElementFromValue(locationNameElementDetailValue, partyPK);
171
172        if(locationNameElementDescription == null && description != null) {
173            warehouseControl.createLocationNameElementDescription(locationNameElement, getPreferredLanguage(), description, partyPK);
174        } else if(locationNameElementDescription != null && description == null) {
175            warehouseControl.deleteLocationNameElementDescription(locationNameElementDescription, partyPK);
176        } else if(locationNameElementDescription != null && description != null) {
177            var locationNameElementDescriptionValue = warehouseControl.getLocationNameElementDescriptionValue(locationNameElementDescription);
178
179            locationNameElementDescriptionValue.setDescription(description);
180            warehouseControl.updateLocationNameElementDescriptionFromValue(locationNameElementDescriptionValue, partyPK);
181        }
182    }
183
184}