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.LocationDescriptionEdit;
020import com.echothree.control.user.warehouse.common.edit.WarehouseEditFactory;
021import com.echothree.control.user.warehouse.common.result.EditLocationDescriptionResult;
022import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory;
023import com.echothree.control.user.warehouse.common.spec.LocationDescriptionSpec;
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.Location;
030import com.echothree.model.data.warehouse.server.entity.LocationDescription;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseAbstractEditCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class EditLocationDescriptionCommand
044        extends BaseAbstractEditCommand<LocationDescriptionSpec, LocationDescriptionEdit, EditLocationDescriptionResult, LocationDescription, Location> {
045
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
048    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
049    
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.Location.name(), SecurityRoles.Description.name())
055                ))
056        ));
057
058        SPEC_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("LocationName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
062        );
063        
064        EDIT_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
066        );
067    }
068
069    @Inject
070    PartyControl partyControl;
071
072    @Inject
073    WarehouseControl warehouseControl;
074
075    /** Creates a new instance of EditLocationDescriptionCommand */
076    public EditLocationDescriptionCommand() {
077        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079
080    @Override
081    public EditLocationDescriptionResult getResult() {
082        return WarehouseResultFactory.getEditLocationDescriptionResult();
083    }
084
085    @Override
086    public LocationDescriptionEdit getEdit() {
087        return WarehouseEditFactory.getLocationDescriptionEdit();
088    }
089
090    @Override
091    public LocationDescription getEntity(EditLocationDescriptionResult result) {
092        LocationDescription locationDescription = null;
093        var warehouseName = spec.getWarehouseName();
094        var warehouse = warehouseControl.getWarehouseByName(warehouseName);
095
096        if(warehouse != null) {
097            var warehouseParty = warehouse.getParty();
098            var locationName = spec.getLocationName();
099            var location = warehouseControl.getLocationByName(warehouseParty, locationName);
100
101            if(location != null) {
102                var languageIsoName = spec.getLanguageIsoName();
103                var language = partyControl.getLanguageByIsoName(languageIsoName);
104
105                if(language != null) {
106                    locationDescription = warehouseControl.getLocationDescription(location, language, editModeToEntityPermission(editMode));
107
108                    if(locationDescription == null) {
109                        addExecutionError(ExecutionErrors.UnknownLocationDescription.name(),
110                                warehouse.getWarehouseName(), location.getLastDetail().getLocationName(),
111                                language.getLanguageIsoName());
112                    }
113                } else {
114                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
115                }
116            } else {
117                addExecutionError(ExecutionErrors.UnknownLocationName.name(), warehouse.getWarehouseName(), locationName);
118            }
119        } else {
120            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
121        }
122
123        return locationDescription;
124    }
125
126    @Override
127    public Location getLockEntity(LocationDescription locationDescription) {
128        return locationDescription.getLocation();
129    }
130
131    @Override
132    public void fillInResult(EditLocationDescriptionResult result, LocationDescription locationDescription) {
133        result.setLocationDescription(warehouseControl.getLocationDescriptionTransfer(getUserVisit(), locationDescription));
134    }
135
136    @Override
137    public void doLock(LocationDescriptionEdit edit, LocationDescription locationDescription) {
138        edit.setDescription(locationDescription.getDescription());
139    }
140
141    @Override
142    public void doUpdate(LocationDescription locationDescription) {
143        var locationDescriptionValue = warehouseControl.getLocationDescriptionValue(locationDescription);
144
145        locationDescriptionValue.setDescription(edit.getDescription());
146
147        warehouseControl.updateLocationDescriptionFromValue(locationDescriptionValue, getPartyPK());
148    }
149    
150}