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.shipment.server.command;
018
019import com.echothree.control.user.shipment.common.form.CreateShipmentAliasForm;
020import com.echothree.control.user.shipment.server.command.util.ShipmentAliasUtil;
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.shipment.server.ShipmentControl;
025import com.echothree.model.data.shipment.server.entity.Shipment;
026import com.echothree.model.data.shipment.server.entity.ShipmentAliasType;
027import com.echothree.model.data.shipment.server.entity.ShipmentAliasTypeDetail;
028import com.echothree.model.data.shipment.server.entity.ShipmentType;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import com.echothree.util.server.persistence.Session;
039import java.util.Arrays;
040import java.util.Collections;
041import java.util.List;
042import java.util.regex.Matcher;
043import java.util.regex.Pattern;
044
045public class CreateShipmentAliasCommand
046        extends BaseSimpleCommand<CreateShipmentAliasForm> {
047    
048    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
049    
050    static {
051        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
052                new FieldDefinition("ShipmentTypeName", FieldType.ENTITY_NAME, true, null, null),
053                new FieldDefinition("ShipmentName", FieldType.ENTITY_NAME, true, null, null),
054                new FieldDefinition("ShipmentAliasTypeName", FieldType.ENTITY_NAME, true, null, null),
055                new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null)
056                ));
057    }
058    
059    /** Creates a new instance of CreateShipmentAliasCommand */
060    public CreateShipmentAliasCommand(UserVisitPK userVisitPK, CreateShipmentAliasForm form) {
061        super(userVisitPK, form, new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
062                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
063                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
064                        new SecurityRoleDefinition(ShipmentAliasUtil.getInstance().getSecurityRoleGroupNameByShipmentTypeSpec(form), SecurityRoles.Create.name())
065                        )))
066                ))), FORM_FIELD_DEFINITIONS, false);
067    }
068
069    @Override
070    protected BaseResult execute() {
071        var shipmentControl = Session.getModelController(ShipmentControl.class);
072        String shipmentTypeName = form.getShipmentTypeName();
073        ShipmentType shipmentType = shipmentControl.getShipmentTypeByName(shipmentTypeName);
074
075        if(shipmentType != null) {
076            String shipmentName = form.getShipmentName();
077            Shipment shipment = shipmentControl.getShipmentByName(shipmentType, shipmentName);
078
079            if(shipment != null) {
080                String shipmentAliasTypeName = form.getShipmentAliasTypeName();
081                ShipmentAliasType shipmentAliasType = shipmentControl.getShipmentAliasTypeByName(shipmentType, shipmentAliasTypeName);
082
083                if(shipmentAliasType != null) {
084                    ShipmentAliasTypeDetail shipmentAliasTypeDetail = shipmentAliasType.getLastDetail();
085                    String validationPattern = shipmentAliasTypeDetail.getValidationPattern();
086                    String alias = form.getAlias();
087
088                    if(validationPattern != null) {
089                        Pattern pattern = Pattern.compile(validationPattern);
090                        Matcher m = pattern.matcher(alias);
091
092                        if(!m.matches()) {
093                            addExecutionError(ExecutionErrors.InvalidAlias.name(), alias);
094                        }
095                    }
096
097                    if(!hasExecutionErrors()) {
098                        if(shipmentControl.getShipmentAlias(shipment, shipmentAliasType) == null && shipmentControl.getShipmentAliasByAlias(shipmentAliasType, alias) == null) {
099                            shipmentControl.createShipmentAlias(shipment, shipmentAliasType, alias, getPartyPK());
100                        } else {
101                            addExecutionError(ExecutionErrors.DuplicateShipmentAlias.name(), shipmentTypeName, shipmentName, shipmentAliasTypeName, alias);
102                        }
103                    }
104                } else {
105                    addExecutionError(ExecutionErrors.UnknownShipmentAliasTypeName.name(), shipmentTypeName, shipmentAliasTypeName);
106                }
107            } else {
108                addExecutionError(ExecutionErrors.UnknownShipmentName.name(), shipmentTypeName, shipmentName);
109            }
110        } else {
111            addExecutionError(ExecutionErrors.UnknownShipmentTypeName.name(), shipmentTypeName);
112        }
113
114        return null;
115    }
116    
117}