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.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.form.EditLocationDescriptionForm;
022import com.echothree.control.user.warehouse.common.result.EditLocationDescriptionResult;
023import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory;
024import com.echothree.control.user.warehouse.common.spec.LocationDescriptionSpec;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.party.server.control.PartyControl;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.control.warehouse.server.control.WarehouseControl;
030import com.echothree.model.data.party.server.entity.Language;
031import com.echothree.model.data.party.server.entity.Party;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.model.data.warehouse.server.entity.Location;
034import com.echothree.model.data.warehouse.server.entity.LocationDescription;
035import com.echothree.model.data.warehouse.server.entity.Warehouse;
036import com.echothree.model.data.warehouse.server.value.LocationDescriptionValue;
037import com.echothree.util.common.command.BaseResult;
038import com.echothree.util.common.command.EditMode;
039import com.echothree.util.common.message.ExecutionErrors;
040import com.echothree.util.common.validation.FieldDefinition;
041import com.echothree.util.common.validation.FieldType;
042import com.echothree.util.server.control.BaseEditCommand;
043import com.echothree.util.server.control.CommandSecurityDefinition;
044import com.echothree.util.server.control.PartyTypeDefinition;
045import com.echothree.util.server.control.SecurityRoleDefinition;
046import com.echothree.util.server.persistence.Session;
047import java.util.ArrayList;
048import java.util.Collections;
049import java.util.List;
050
051public class EditLocationDescriptionCommand
052        extends BaseEditCommand<LocationDescriptionSpec, LocationDescriptionEdit> {
053
054    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
055    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
056    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
057    
058    static {
059        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
060                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
061                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
062                        new SecurityRoleDefinition(SecurityRoleGroups.Location.name(), SecurityRoles.Description.name())
063                ))
064        ));
065
066        List<FieldDefinition> temp = new ArrayList<>(3);
067        temp.add(new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null));
068        temp.add(new FieldDefinition("LocationName", FieldType.ENTITY_NAME, true, null, null));
069        temp.add(new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null));
070        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(temp);
071        
072        temp = new ArrayList<>(1);
073        temp.add(new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L));
074        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(temp);
075    }
076    
077    /** Creates a new instance of EditLocationDescriptionCommand */
078    public EditLocationDescriptionCommand(UserVisitPK userVisitPK, EditLocationDescriptionForm form) {
079        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081    
082    @Override
083    protected BaseResult execute() {
084        var warehouseControl = Session.getModelController(WarehouseControl.class);
085        EditLocationDescriptionResult result = WarehouseResultFactory.getEditLocationDescriptionResult();
086        String warehouseName = spec.getWarehouseName();
087        Warehouse warehouse = warehouseControl.getWarehouseByName(warehouseName);
088        
089        if(warehouse != null) {
090            Party warehouseParty = warehouse.getParty();
091            String locationName = spec.getLocationName();
092            Location location = warehouseControl.getLocationByName(warehouseParty, locationName);
093            
094            if(location != null) {
095                var partyControl = Session.getModelController(PartyControl.class);
096                String languageIsoName = spec.getLanguageIsoName();
097                Language language = partyControl.getLanguageByIsoName(languageIsoName);
098                
099                if(language != null) {
100                    if(editMode.equals(EditMode.LOCK)) {
101                        LocationDescription locationDescription = warehouseControl.getLocationDescription(location, language);
102                        
103                        if(locationDescription != null) {
104                            result.setLocationDescription(warehouseControl.getLocationDescriptionTransfer(getUserVisit(), locationDescription));
105                            
106                            if(lockEntity(location)) {
107                                LocationDescriptionEdit edit = WarehouseEditFactory.getLocationDescriptionEdit();
108                                
109                                result.setEdit(edit);
110                                edit.setDescription(locationDescription.getDescription());
111                            } else {
112                                addExecutionError(ExecutionErrors.EntityLockFailed.name());
113                            }
114                            
115                            result.setEntityLock(getEntityLockTransfer(location));
116                        } else {
117                            addExecutionError(ExecutionErrors.UnknownLocationDescription.name());
118                        }
119                    } else if(editMode.equals(EditMode.UPDATE)) {
120                        LocationDescriptionValue locationDescriptionValue = warehouseControl.getLocationDescriptionValueForUpdate(location, language);
121                        
122                        if(locationDescriptionValue != null) {
123                            if(lockEntityForUpdate(location)) {
124                                try {
125                                    String description = edit.getDescription();
126                                    
127                                    locationDescriptionValue.setDescription(description);
128                                    
129                                    warehouseControl.updateLocationDescriptionFromValue(locationDescriptionValue, getPartyPK());
130                                } finally {
131                                    unlockEntity(location);
132                                }
133                            } else {
134                                addExecutionError(ExecutionErrors.EntityLockStale.name());
135                            }
136                        } else {
137                            addExecutionError(ExecutionErrors.UnknownLocationDescription.name());
138                        }
139                    }
140                } else {
141                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
142                }
143            } else {
144                addExecutionError(ExecutionErrors.UnknownLocationName.name(), locationName);
145            }
146        } else {
147            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
148        }
149        
150        return result;
151    }
152    
153}