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.LocationNameElementDescriptionEdit;
020import com.echothree.control.user.warehouse.common.edit.WarehouseEditFactory;
021import com.echothree.control.user.warehouse.common.result.EditLocationNameElementDescriptionResult;
022import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory;
023import com.echothree.control.user.warehouse.common.spec.LocationNameElementDescriptionSpec;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.server.control.PartyControl;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.warehouse.server.control.WarehouseControl;
029import com.echothree.model.data.warehouse.server.entity.LocationNameElementDescription;
030import com.echothree.model.data.warehouse.server.entity.LocationType;
031import com.echothree.util.common.command.EditMode;
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 com.echothree.util.server.persistence.Session;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class EditLocationNameElementDescriptionCommand
046        extends BaseAbstractEditCommand<LocationNameElementDescriptionSpec, LocationNameElementDescriptionEdit, EditLocationNameElementDescriptionResult, LocationNameElementDescription, LocationType> {
047
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.LocationNameElement.name(), SecurityRoles.Description.name())
057                ))
058        ));
059
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("LocationTypeName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("LocationNameElementName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
065        );
066        
067        EDIT_FIELD_DEFINITIONS = List.of(
068                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
069        );
070    }
071    
072    /** Creates a new instance of EditLocationNameElementDescriptionCommand */
073    public EditLocationNameElementDescriptionCommand() {
074        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076
077    @Inject
078    PartyControl partyControl;
079
080    @Inject
081    WarehouseControl warehouseControl;
082
083    @Override
084    public EditLocationNameElementDescriptionResult getResult() {
085        return WarehouseResultFactory.getEditLocationNameElementDescriptionResult();
086    }
087
088    @Override
089    public LocationNameElementDescriptionEdit getEdit() {
090        return WarehouseEditFactory.getLocationNameElementDescriptionEdit();
091    }
092
093    @Override
094    public LocationNameElementDescription getEntity(EditLocationNameElementDescriptionResult result) {
095        LocationNameElementDescription locationNameElementDescription = null;
096        var warehouseName = spec.getWarehouseName();
097        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
098
099        if(warehouse != null) {
100            var warehouseParty = warehouse.getParty();
101            var locationTypeName = spec.getLocationTypeName();
102            var locationType = warehouseControl.getLocationTypeByName(warehouseParty, locationTypeName);
103
104            if(locationType != null) {
105                var locationNameElementName = spec.getLocationNameElementName();
106                var locationNameElement = warehouseControl.getLocationNameElementByName(locationType, locationNameElementName);
107
108                if(locationNameElement != null) {
109                    var languageIsoName = spec.getLanguageIsoName();
110                    var language = partyControl.getLanguageByIsoName(languageIsoName);
111
112                    if(language != null) {
113                        locationNameElementDescription = warehouseControl.getLocationNameElementDescription(locationNameElement, language,
114                                editModeToEntityPermission(editMode));
115
116                        if(locationNameElementDescription == null) {
117                            addExecutionError(ExecutionErrors.UnknownLocationNameElementDescription.name(),
118                                    warehouse.getWarehouseName(), locationType.getLastDetail().getLocationTypeName(),
119                                    locationNameElement.getLastDetail().getLocationNameElementName(),
120                                    language.getLanguageIsoName());
121                        }
122                    } else {
123                        addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
124                    }
125                } else {
126                    addExecutionError(ExecutionErrors.UnknownLocationNameElementName.name(),
127                            warehouse.getWarehouseName(), locationType.getLastDetail().getLocationTypeName(),
128                            locationNameElementName);
129                }
130            } else {
131                addExecutionError(ExecutionErrors.UnknownLocationTypeName.name(), warehouse.getWarehouseName(), locationTypeName);
132            }
133        } else {
134            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
135        }
136
137        return locationNameElementDescription;
138    }
139
140    @Override
141    public LocationType getLockEntity(LocationNameElementDescription locationNameElementDescription) {
142        return locationNameElementDescription.getLocationNameElement().getLastDetail().getLocationType();
143    }
144
145    @Override
146    public void fillInResult(EditLocationNameElementDescriptionResult result, LocationNameElementDescription locationNameElementDescription) {
147        result.setLocationNameElementDescription(warehouseControl.getLocationNameElementDescriptionTransfer(getUserVisit(), locationNameElementDescription));
148    }
149
150    @Override
151    public void doLock(LocationNameElementDescriptionEdit edit, LocationNameElementDescription locationNameElementDescription) {
152        edit.setDescription(locationNameElementDescription.getDescription());
153    }
154
155    @Override
156    public void doUpdate(LocationNameElementDescription locationNameElementDescription) {
157        var locationNameElementDescriptionValue = warehouseControl.getLocationNameElementDescriptionValue(locationNameElementDescription);
158
159        locationNameElementDescriptionValue.setDescription(edit.getDescription());
160
161        warehouseControl.updateLocationNameElementDescriptionFromValue(locationNameElementDescriptionValue, getPartyPK());
162    }
163
164}