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.LocationNameElementDescriptionEdit;
020import com.echothree.control.user.warehouse.common.edit.WarehouseEditFactory;
021import com.echothree.control.user.warehouse.common.form.EditLocationNameElementDescriptionForm;
022import com.echothree.control.user.warehouse.common.result.EditLocationNameElementDescriptionResult;
023import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory;
024import com.echothree.control.user.warehouse.common.spec.LocationNameElementDescriptionSpec;
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.LocationNameElement;
034import com.echothree.model.data.warehouse.server.entity.LocationNameElementDescription;
035import com.echothree.model.data.warehouse.server.entity.LocationType;
036import com.echothree.model.data.warehouse.server.entity.Warehouse;
037import com.echothree.model.data.warehouse.server.value.LocationNameElementDescriptionValue;
038import com.echothree.util.common.command.BaseResult;
039import com.echothree.util.common.command.EditMode;
040import com.echothree.util.common.message.ExecutionErrors;
041import com.echothree.util.common.validation.FieldDefinition;
042import com.echothree.util.common.validation.FieldType;
043import com.echothree.util.server.control.BaseEditCommand;
044import com.echothree.util.server.control.CommandSecurityDefinition;
045import com.echothree.util.server.control.PartyTypeDefinition;
046import com.echothree.util.server.control.SecurityRoleDefinition;
047import com.echothree.util.server.persistence.Session;
048import java.util.ArrayList;
049import java.util.Collections;
050import java.util.List;
051
052public class EditLocationNameElementDescriptionCommand
053        extends BaseEditCommand<LocationNameElementDescriptionSpec, LocationNameElementDescriptionEdit> {
054
055    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
056    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
057    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
058    
059    static {
060        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
061                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
062                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
063                        new SecurityRoleDefinition(SecurityRoleGroups.LocationNameElement.name(), SecurityRoles.Description.name())
064                ))
065        ));
066
067        List<FieldDefinition> temp = new ArrayList<>(4);
068        temp.add(new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null));
069        temp.add(new FieldDefinition("LocationTypeName", FieldType.ENTITY_NAME, true, null, null));
070        temp.add(new FieldDefinition("LocationNameElementName", FieldType.ENTITY_NAME, true, null, null));
071        temp.add(new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null));
072        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(temp);
073        
074        temp = new ArrayList<>(1);
075        temp.add(new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L));
076        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(temp);
077    }
078    
079    /** Creates a new instance of EditLocationNameElementDescriptionCommand */
080    public EditLocationNameElementDescriptionCommand(UserVisitPK userVisitPK, EditLocationNameElementDescriptionForm form) {
081        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
082    }
083    
084    @Override
085    protected BaseResult execute() {
086        var warehouseControl = Session.getModelController(WarehouseControl.class);
087        EditLocationNameElementDescriptionResult result = WarehouseResultFactory.getEditLocationNameElementDescriptionResult();
088        String warehouseName = spec.getWarehouseName();
089        Warehouse warehouse = warehouseControl.getWarehouseByName(warehouseName);
090        
091        if(warehouse != null) {
092            Party warehouseParty = warehouse.getParty();
093            String locationTypeName = spec.getLocationTypeName();
094            LocationType locationType = warehouseControl.getLocationTypeByName(warehouseParty, locationTypeName);
095            
096            if(locationType != null) {
097                String locationNameElementName = spec.getLocationNameElementName();
098                LocationNameElement locationNameElement = warehouseControl.getLocationNameElementByName(locationType, locationNameElementName);
099                
100                if(locationNameElement != null) {
101                    var partyControl = Session.getModelController(PartyControl.class);
102                    String languageIsoName = spec.getLanguageIsoName();
103                    Language language = partyControl.getLanguageByIsoName(languageIsoName);
104                    
105                    if(language != null) {
106                        if(editMode.equals(EditMode.LOCK)) {
107                            LocationNameElementDescription locationTypeDescription = warehouseControl.getLocationNameElementDescription(locationNameElement, language);
108                            
109                            if(locationTypeDescription != null) {
110                                result.setLocationNameElementDescription(warehouseControl.getLocationNameElementDescriptionTransfer(getUserVisit(), locationTypeDescription));
111                                
112                                if(lockEntity(locationType)) {
113                                    LocationNameElementDescriptionEdit edit = WarehouseEditFactory.getLocationNameElementDescriptionEdit();
114                                    
115                                    result.setEdit(edit);
116                                    edit.setDescription(locationTypeDescription.getDescription());
117                                } else {
118                                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
119                                }
120                                
121                                result.setEntityLock(getEntityLockTransfer(locationType));
122                            } else {
123                                addExecutionError(ExecutionErrors.UnknownLocationNameElementDescription.name());
124                            }
125                        } else if(editMode.equals(EditMode.UPDATE)) {
126                            LocationNameElementDescriptionValue locationTypeDescriptionValue = warehouseControl.getLocationNameElementDescriptionValueForUpdate(locationNameElement, language);
127                            
128                            if(locationTypeDescriptionValue != null) {
129                                if(lockEntityForUpdate(locationType)) {
130                                    try {
131                                        String description = edit.getDescription();
132                                        
133                                        locationTypeDescriptionValue.setDescription(description);
134                                        
135                                        warehouseControl.updateLocationNameElementDescriptionFromValue(locationTypeDescriptionValue, getPartyPK());
136                                    } finally {
137                                        unlockEntity(locationType);
138                                    }
139                                } else {
140                                    addExecutionError(ExecutionErrors.EntityLockStale.name());
141                                }
142                            } else {
143                                addExecutionError(ExecutionErrors.UnknownLocationNameElementDescription.name());
144                            }
145                        }
146                    } else {
147                        addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
148                    }
149                } else {
150                    addExecutionError(ExecutionErrors.UnknownLocationNameElementName.name(), locationNameElementName);
151                }
152            } else {
153                addExecutionError(ExecutionErrors.UnknownLocationTypeName.name(), locationTypeName);
154            }
155        } else {
156            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
157        }
158        
159        return result;
160    }
161    
162}