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 java.util.List;
037import javax.enterprise.context.Dependent;
038import javax.inject.Inject;
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    @Inject
056    PartyControl partyControl;
057
058    @Inject
059    PartyAliasTypeLogic partyAliasTypeLogic;
060
061    @Inject
062    PartyLogic partyLogic;
063
064    
065    /** Creates a new instance of GetPartyAliasCommand */
066    public GetPartyAliasCommand() {
067        super(null, FORM_FIELD_DEFINITIONS, false);
068    }
069
070    @Override
071    protected CommandSecurityDefinition getCommandSecurityDefinition() {
072        return new CommandSecurityDefinition(List.of(
073                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
074                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
075                        new SecurityRoleDefinition(PartyAliasUtil.getInstance().getSecurityRoleGroupNameBySpecs(form, form), SecurityRoles.Review.name())
076                ))
077        ));
078    }
079
080    @Override
081    protected PartyAlias getEntity() {
082        var partyName = form.getPartyName();
083        var partyTypeName = form.getPartyTypeName();
084        var partyAliasTypeName = form.getPartyAliasTypeName();
085        var alias = form.getAlias();
086        // Must specify either PartyName + PartyAliasTypeName or PartyTypeName + PartyAliasTypeName + Alias
087        var parameterOption1 = (partyName != null) && (partyTypeName == null) && (partyAliasTypeName != null ) && (alias == null);
088        var parameterOption2 = (partyName == null) && (partyTypeName != null) && (partyAliasTypeName != null ) && (alias != null);
089        var parameterCount = (parameterOption1 ? 1 : 0) + (parameterOption2 ? 1 : 0);
090        PartyAlias partyAlias = null;
091
092        if(parameterCount == 1) {
093            if(parameterOption1) {
094                var party = partyLogic.getPartyByName(this, partyName);
095
096                if(!hasExecutionErrors()) {
097                    var partyType = party.getLastDetail().getPartyType();
098                    var partyAliasType = partyAliasTypeLogic.getPartyAliasTypeByName(this, partyType, partyAliasTypeName);
099
100                    if(!hasExecutionErrors()) {
101                        partyAlias = partyControl.getPartyAlias(party, partyAliasType);
102
103                        if(partyAlias == null) {
104                            addExecutionError(ExecutionErrors.UnknownPartyAlias.name(), party.getLastDetail().getPartyName(),
105                                    partyAliasType.getLastDetail().getPartyAliasTypeName());
106                        }
107                    }
108                }
109            } else {
110                var partyAliasType = partyAliasTypeLogic.getPartyAliasTypeByName(this, partyTypeName, partyAliasTypeName);
111
112                if(!hasExecutionErrors()) {
113                    partyAlias = partyControl.getPartyAliasByAlias(partyAliasType, alias);
114
115                    if(partyAlias == null) {
116                        var partyAliasTypeDetail = partyAliasType.getLastDetail();
117                        addExecutionError(ExecutionErrors.UnknownPartyAliasByAlias.name(), partyAliasTypeDetail.getPartyType().getPartyTypeName(),
118                                partyAliasTypeDetail.getPartyAliasTypeName(), partyAliasType.getLastDetail().getPartyAliasTypeName(), alias);
119                    }
120                }
121            }
122        } else {
123            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
124        }
125
126        return partyAlias;
127    }
128
129    @Override
130    protected BaseResult getResult(PartyAlias partyAlias) {
131        var result = PartyResultFactory.getGetPartyAliasResult();
132
133        if(partyAlias != null) {
134            result.setPartyAlias(partyControl.getPartyAliasTransfer(getUserVisit(), partyAlias));
135        }
136
137        return result;
138    }
139
140}