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.party.server.command;
018
019import com.echothree.control.user.party.common.form.GetPartyAliasForm;
020import com.echothree.control.user.party.common.result.PartyResultFactory;
021import com.echothree.control.user.party.server.command.util.PartyAliasUtil;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.control.party.server.logic.PartyAliasTypeLogic;
025import com.echothree.model.control.party.server.logic.PartyLogic;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.data.party.server.entity.PartyAlias;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.server.control.BaseSingleEntityCommand;
033import com.echothree.util.server.control.CommandSecurityDefinition;
034import com.echothree.util.server.control.PartyTypeDefinition;
035import com.echothree.util.server.control.SecurityRoleDefinition;
036import com.echothree.util.server.persistence.Session;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039
040@Dependent
041public class GetPartyAliasCommand
042        extends BaseSingleEntityCommand<PartyAlias, GetPartyAliasForm> {
043    
044    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
045    
046    static {
047        FORM_FIELD_DEFINITIONS = List.of(
048                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
049                new FieldDefinition("PartyTypeName", FieldType.ENTITY_NAME, false, null, null),
050                new FieldDefinition("PartyAliasTypeName", FieldType.ENTITY_NAME, false, null, null),
051                new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null)
052        );
053    }
054    
055    /** Creates a new instance of GetPartyAliasCommand */
056    public GetPartyAliasCommand() {
057        super(null, FORM_FIELD_DEFINITIONS, false);
058    }
059
060    @Override
061    protected CommandSecurityDefinition getCommandSecurityDefinition() {
062        return new CommandSecurityDefinition(List.of(
063                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
064                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
065                        new SecurityRoleDefinition(PartyAliasUtil.getInstance().getSecurityRoleGroupNameBySpecs(form, form), SecurityRoles.Review.name())
066                ))
067        ));
068    }
069
070    @Override
071    protected PartyAlias getEntity() {
072        var partyName = form.getPartyName();
073        var partyTypeName = form.getPartyTypeName();
074        var partyAliasTypeName = form.getPartyAliasTypeName();
075        var alias = form.getAlias();
076        // Must specify either PartyName + PartyAliasTypeName or PartyTypeName + PartyAliasTypeName + Alias
077        var parameterOption1 = (partyName != null) && (partyTypeName == null) && (partyAliasTypeName != null ) && (alias == null);
078        var parameterOption2 = (partyName == null) && (partyTypeName != null) && (partyAliasTypeName != null ) && (alias != null);
079        var parameterCount = (parameterOption1 ? 1 : 0) + (parameterOption2 ? 1 : 0);
080        PartyAlias partyAlias = null;
081
082        if(parameterCount == 1) {
083            var partyControl = Session.getModelController(PartyControl.class);
084
085            if(parameterOption1) {
086                var party = PartyLogic.getInstance().getPartyByName(this, partyName);
087
088                if(!hasExecutionErrors()) {
089                    var partyType = party.getLastDetail().getPartyType();
090                    var partyAliasType = PartyAliasTypeLogic.getInstance().getPartyAliasTypeByName(this, partyType, partyAliasTypeName);
091
092                    if(!hasExecutionErrors()) {
093                        partyAlias = partyControl.getPartyAlias(party, partyAliasType);
094
095                        if(partyAlias == null) {
096                            addExecutionError(ExecutionErrors.UnknownPartyAlias.name(), party.getLastDetail().getPartyName(),
097                                    partyAliasType.getLastDetail().getPartyAliasTypeName());
098                        }
099                    }
100                }
101            } else {
102                var partyAliasType = PartyAliasTypeLogic.getInstance().getPartyAliasTypeByName(this, partyTypeName, partyAliasTypeName);
103
104                if(!hasExecutionErrors()) {
105                    partyAlias = partyControl.getPartyAliasByAlias(partyAliasType, alias);
106
107                    if(partyAlias == null) {
108                        var partyAliasTypeDetail = partyAliasType.getLastDetail();
109                        addExecutionError(ExecutionErrors.UnknownPartyAliasByAlias.name(), partyAliasTypeDetail.getPartyType().getPartyTypeName(),
110                                partyAliasTypeDetail.getPartyAliasTypeName(), partyAliasType.getLastDetail().getPartyAliasTypeName(), alias);
111                    }
112                }
113            }
114        } else {
115            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
116        }
117
118        return partyAlias;
119    }
120
121    @Override
122    protected BaseResult getResult(PartyAlias partyAlias) {
123        var result = PartyResultFactory.getGetPartyAliasResult();
124
125        if(partyAlias != null) {
126            var partyControl = Session.getModelController(PartyControl.class);
127
128            result.setPartyAlias(partyControl.getPartyAliasTransfer(getUserVisit(), partyAlias));
129        }
130
131        return result;
132    }
133
134}