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    /** Creates a new instance of EditLocationNameElementCommand */
076    public EditLocationNameElementCommand() {
077        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079
080    @Override
081    public EditLocationNameElementResult getResult() {
082        return WarehouseResultFactory.getEditLocationNameElementResult();
083    }
084
085    @Override
086    public LocationNameElementEdit getEdit() {
087        return WarehouseEditFactory.getLocationNameElementEdit();
088    }
089
090    @Override
091    public LocationNameElement getEntity(EditLocationNameElementResult result) {
092        LocationNameElement locationNameElement = null;
093        var warehouseName = spec.getWarehouseName();
094        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
095
096        if(warehouse != null) {
097            var warehouseParty = warehouse.getParty();
098            var locationTypeName = spec.getLocationTypeName();
099            var locationType = warehouseControl.getLocationTypeByName(warehouseParty, locationTypeName);
100
101            if(locationType != null) {
102                var locationNameElementName = spec.getLocationNameElementName();
103
104                locationNameElement = warehouseControl.getLocationNameElementByName(locationType, locationNameElementName,
105                        editModeToEntityPermission(editMode));
106
107                if(locationNameElement == null) {
108                    addExecutionError(ExecutionErrors.UnknownLocationNameElementName.name(),
109                            warehouse.getWarehouseName(), locationType.getLastDetail().getLocationTypeName(), locationNameElementName);
110                }
111            } else {
112                addExecutionError(ExecutionErrors.UnknownLocationTypeName.name(), warehouse.getWarehouseName(), locationTypeName);
113            }
114        } else {
115            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
116        }
117
118        return locationNameElement;
119    }
120
121    @Override
122    public LocationNameElement getLockEntity(LocationNameElement locationNameElement) {
123        return locationNameElement;
124    }
125
126    @Override
127    public void fillInResult(EditLocationNameElementResult result, LocationNameElement locationNameElement) {
128        result.setLocationNameElement(warehouseControl.getLocationNameElementTransfer(getUserVisit(), locationNameElement));
129    }
130
131    @Override
132    public void doLock(LocationNameElementEdit edit, LocationNameElement locationNameElement) {
133        var locationNameElementDescription = warehouseControl.getLocationNameElementDescription(locationNameElement, getPreferredLanguage());
134        var locationNameElementDetail = locationNameElement.getLastDetail();
135
136        edit.setLocationNameElementName(locationNameElementDetail.getLocationNameElementName());
137        edit.setOffset(locationNameElementDetail.getOffset().toString());
138        edit.setLength(locationNameElementDetail.getLength().toString());
139        edit.setValidationPattern(locationNameElementDetail.getValidationPattern());
140
141        if(locationNameElementDescription != null) {
142            edit.setDescription(locationNameElementDescription.getDescription());
143        }
144    }
145
146    @Override
147    public void canUpdate(LocationNameElement locationNameElement) {
148        var locationType = locationNameElement.getLastDetail().getLocationType();
149        var locationNameElementName = edit.getLocationNameElementName();
150        var duplicateLocationNameElement = warehouseControl.getLocationNameElementByName(locationType, locationNameElementName);
151
152        if(duplicateLocationNameElement != null && !locationNameElement.equals(duplicateLocationNameElement)) {
153            addExecutionError(ExecutionErrors.DuplicateLocationNameElementName.name(), locationNameElementName);
154        }
155    }
156
157    @Override
158    public void doUpdate(LocationNameElement locationNameElement) {
159        var partyPK = getPartyPK();
160        var locationNameElementDetailValue = warehouseControl.getLocationNameElementDetailValueForUpdate(locationNameElement);
161        var locationNameElementDescription = warehouseControl.getLocationNameElementDescriptionForUpdate(locationNameElement, getPreferredLanguage());
162        var description = edit.getDescription();
163
164        locationNameElementDetailValue.setLocationNameElementName(edit.getLocationNameElementName());
165        locationNameElementDetailValue.setOffset(Integer.valueOf(edit.getOffset()));
166        locationNameElementDetailValue.setLength(Integer.valueOf(edit.getLength()));
167        locationNameElementDetailValue.setValidationPattern(edit.getValidationPattern());
168
169        warehouseControl.updateLocationNameElementFromValue(locationNameElementDetailValue, partyPK);
170
171        if(locationNameElementDescription == null && description != null) {
172            warehouseControl.createLocationNameElementDescription(locationNameElement, getPreferredLanguage(), description, partyPK);
173        } else if(locationNameElementDescription != null && description == null) {
174            warehouseControl.deleteLocationNameElementDescription(locationNameElementDescription, partyPK);
175        } else if(locationNameElementDescription != null && description != null) {
176            var locationNameElementDescriptionValue = warehouseControl.getLocationNameElementDescriptionValue(locationNameElementDescription);
177
178            locationNameElementDescriptionValue.setDescription(description);
179            warehouseControl.updateLocationNameElementDescriptionFromValue(locationNameElementDescriptionValue, partyPK);
180        }
181    }
182
183}