001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.GetPartyAliasesForm;
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.Party;
028import com.echothree.model.data.party.server.entity.PartyAlias;
029import com.echothree.model.data.party.server.entity.PartyAliasType;
030import com.echothree.model.data.party.server.factory.PartyAliasFactory;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseMultipleEntitiesCommand;
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.Collection;
042import java.util.Collections;
043import java.util.List;
044import javax.enterprise.context.RequestScoped;
045
046@RequestScoped
047public class GetPartyAliasesCommand
048        extends BaseMultipleEntitiesCommand<PartyAlias, GetPartyAliasesForm> {
049    
050    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
051    
052    static {
053        FORM_FIELD_DEFINITIONS = List.of(
054                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
055                new FieldDefinition("PartyTypeName", FieldType.ENTITY_NAME, false, null, null),
056                new FieldDefinition("PartyAliasTypeName", FieldType.ENTITY_NAME, false, null, null)
057        );
058    }
059    
060    /** Creates a new instance of GetPartyAliasesCommand */
061    public GetPartyAliasesCommand() {
062        super(null, FORM_FIELD_DEFINITIONS, false);
063    }
064
065    @Override
066    protected CommandSecurityDefinition getCommandSecurityDefinition() {
067        return new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
068                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
069                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
070                        new SecurityRoleDefinition(PartyAliasUtil.getInstance().getSecurityRoleGroupNameBySpecs(form, form), SecurityRoles.List.name())
071                )))
072        )));
073    }
074
075    Party party;
076    PartyAliasType partyAliasType;
077
078    @Override
079    protected Collection<PartyAlias> getEntities() {
080        var partyName = form.getPartyName();
081        var partyTypeName = form.getPartyTypeName();
082        var partyAliasTypeName = form.getPartyAliasTypeName();
083        // Must specify either PartyName or PartyTypeName + PartyAliasTypeName
084        var parameterOption1 = (partyName != null) && (partyTypeName == null) && (partyAliasTypeName == null );
085        var parameterOption2 = (partyName == null) && (partyTypeName != null) && (partyAliasTypeName != null );
086        var parameterCount = (parameterOption1 ? 1 : 0) + (parameterOption2 ? 1 : 0);
087        Collection<PartyAlias> partyAliases = null;
088
089        if(parameterCount == 1) {
090            var partyControl = Session.getModelController(PartyControl.class);
091
092            if(parameterOption1) {
093                party = PartyLogic.getInstance().getPartyByName(this, form.getPartyName());
094
095                if(!hasExecutionErrors()) {
096                    partyAliases = partyControl.getPartyAliasesByParty(party);
097                }
098            } else {
099                partyAliasType = PartyAliasTypeLogic.getInstance().getPartyAliasTypeByName(this, partyTypeName, partyAliasTypeName);
100
101                if(!hasExecutionErrors()) {
102                    partyAliases = partyControl.getPartyAliasesByPartyAliasType(partyAliasType);
103                }
104            }
105        } else {
106            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
107        }
108
109        return partyAliases;
110    }
111
112    @Override
113    protected BaseResult getResult(Collection<PartyAlias> entities) {
114        var result = PartyResultFactory.getGetPartyAliasesResult();
115
116        if(entities != null) {
117            var partyControl = Session.getModelController(PartyControl.class);
118
119            if(session.hasLimit(PartyAliasFactory.class)) {
120                if(party != null) {
121                    result.setPartyAliasCount(partyControl.countPartyAliasesByParty(party));
122                } else {
123                    result.setPartyAliasCount(partyControl.countPartyAliasesByPartyAliasType(partyAliasType));
124                }
125            }
126
127            if(party != null) {
128                result.setParty(partyControl.getPartyTransfer(getUserVisit(), party));
129            }
130
131            if(partyAliasType != null) {
132                result.setPartyAliasType(partyControl.getPartyAliasTypeTransfer(getUserVisit(), partyAliasType));
133            }
134
135            result.setPartyAliases(partyControl.getPartyAliasTransfers(getUserVisit(), entities));
136        }
137
138        return result;
139    }
140
141}