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.form.CreateLocationNameElementForm;
020import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory;
021import com.echothree.model.control.party.common.PartyTypes;
022import com.echothree.model.control.security.common.SecurityRoleGroups;
023import com.echothree.model.control.security.common.SecurityRoles;
024import com.echothree.model.control.warehouse.server.control.WarehouseControl;
025import com.echothree.model.data.party.server.entity.Party;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.model.data.warehouse.server.entity.LocationNameElement;
028import com.echothree.model.data.warehouse.server.entity.LocationType;
029import com.echothree.model.data.warehouse.server.entity.Warehouse;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.persistence.BasePK;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseSimpleCommand;
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.Arrays;
041import java.util.Collections;
042import java.util.List;
043
044public class CreateLocationNameElementCommand
045        extends BaseSimpleCommand<CreateLocationNameElementForm> {
046
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> FORM_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.LocationNameElement.name(), SecurityRoles.Create.name())
055                ))
056        ));
057
058        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
059                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("LocationTypeName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("LocationNameElementName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("Offset", FieldType.UNSIGNED_INTEGER, true, null, null),
063                new FieldDefinition("Length", FieldType.UNSIGNED_INTEGER, true, null, null),
064                new FieldDefinition("ValidationPattern", FieldType.REGULAR_EXPRESSION, false, null, null),
065                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
066                ));
067    }
068    
069    /** Creates a new instance of CreateLocationNameElementCommand */
070    public CreateLocationNameElementCommand(UserVisitPK userVisitPK, CreateLocationNameElementForm form) {
071        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
072    }
073    
074    @Override
075    protected BaseResult execute() {
076        var result = WarehouseResultFactory.getCreateLocationNameElementResult();
077        var warehouseControl = Session.getModelController(WarehouseControl.class);
078        String warehouseName = form.getWarehouseName();
079        Warehouse warehouse = warehouseControl.getWarehouseByName(warehouseName);
080        
081        if(warehouse != null) {
082            Party warehouseParty = warehouse.getParty();
083            String locationTypeName = form.getLocationTypeName();
084            LocationType locationType = warehouseControl.getLocationTypeByName(warehouseParty, locationTypeName);
085            
086            if(locationType != null) {
087                String locationNameElementName = form.getLocationNameElementName();
088                LocationNameElement locationNameElement = warehouseControl.getLocationNameElementByName(locationType,
089                        locationNameElementName);
090                
091                if(locationNameElement == null) {
092                    BasePK createdBy = getPartyPK();
093                    Integer offset = Integer.valueOf(form.getOffset());
094                    Integer length = Integer.valueOf(form.getLength());
095                    String validationPattern = form.getValidationPattern();
096                    var description = form.getDescription();
097                    
098                    locationNameElement = warehouseControl.createLocationNameElement(locationType, locationNameElementName, offset,
099                            length, validationPattern, createdBy);
100                    
101                    if(description != null) {
102                        warehouseControl.createLocationNameElementDescription(locationNameElement, getPreferredLanguage(),
103                                description, createdBy);
104                    }
105                } else {
106                    addExecutionError(ExecutionErrors.DuplicateLocationNameElementName.name(), locationNameElementName);
107                }
108
109                if(locationNameElement != null) {
110                    result.setEntityRef(locationNameElement.getPrimaryKey().getEntityRef());
111                    result.setWarehouseName(warehouse.getWarehouseName());
112                    result.setLocationTypeName(locationType.getLastDetail().getLocationTypeName());
113                    result.setLocationNameElementName(locationNameElement.getLastDetail().getLocationNameElementName());
114                }
115            } else {
116                addExecutionError(ExecutionErrors.UnknownLocationTypeName.name(), locationTypeName);
117            }
118        } else {
119            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
120        }
121        
122        return result;
123    }
124    
125}