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.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.BasePaginatedMultipleEntitiesCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.Collection;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class GetPartyAliasesCommand
046        extends BasePaginatedMultipleEntitiesCommand<PartyAlias, GetPartyAliasesForm> {
047    
048    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
049    
050    static {
051        FORM_FIELD_DEFINITIONS = List.of(
052                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
053                new FieldDefinition("PartyTypeName", FieldType.ENTITY_NAME, false, null, null),
054                new FieldDefinition("PartyAliasTypeName", FieldType.ENTITY_NAME, false, null, null)
055        );
056    }
057
058    @Inject
059    PartyControl partyControl;
060
061    @Inject
062    PartyAliasTypeLogic partyAliasTypeLogic;
063
064    @Inject
065    PartyLogic partyLogic;
066    
067    /** Creates a new instance of GetPartyAliasesCommand */
068    public GetPartyAliasesCommand() {
069        super(null, FORM_FIELD_DEFINITIONS, true);
070    }
071
072    @Override
073    protected CommandSecurityDefinition getCommandSecurityDefinition() {
074        return new CommandSecurityDefinition(List.of(
075                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
076                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
077                        new SecurityRoleDefinition(PartyAliasUtil.getInstance().getSecurityRoleGroupNameBySpecs(form, form), SecurityRoles.List.name())
078                ))
079        ));
080    }
081
082    private Party party;
083    private PartyAliasType partyAliasType;
084
085    @Override
086    protected void handleForm() {
087        var partyName = form.getPartyName();
088        var partyTypeName = form.getPartyTypeName();
089        var partyAliasTypeName = form.getPartyAliasTypeName();
090        // Must specify either PartyName or PartyTypeName + PartyAliasTypeName
091        var parameterOption1 = (partyName != null) && (partyTypeName == null) && (partyAliasTypeName == null );
092        var parameterOption2 = (partyName == null) && (partyTypeName != null) && (partyAliasTypeName != null );
093        var parameterCount = (parameterOption1 ? 1 : 0) + (parameterOption2 ? 1 : 0);
094
095        if(parameterCount == 1) {
096            if(parameterOption1) {
097                party = partyLogic.getPartyByName(this, form.getPartyName());
098            } else {
099                partyAliasType = partyAliasTypeLogic.getPartyAliasTypeByName(this, partyTypeName, partyAliasTypeName);
100            }
101        } else {
102            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
103        }
104    }
105
106    @Override
107    protected Long getTotalEntities() {
108        Long totalEntities = null;
109
110        if(!hasExecutionErrors()) {
111            if(party != null) {
112                totalEntities = partyControl.countPartyAliasesByParty(party);
113            } else {
114                totalEntities = partyControl.countPartyAliasesByPartyAliasType(partyAliasType);
115            }
116        }
117
118        return totalEntities;
119    }
120
121    @Override
122    protected Collection<PartyAlias> getEntities() {
123        Collection<PartyAlias> partyAliases = null;
124
125        if(!hasExecutionErrors()) {
126            if(party != null) {
127                partyAliases = partyControl.getPartyAliasesByParty(party);
128            } else {
129                partyAliases = partyControl.getPartyAliasesByPartyAliasType(partyAliasType);
130            }
131        }
132
133        return partyAliases;
134    }
135
136    @Override
137    protected BaseResult getResult(Collection<PartyAlias> entities) {
138        var result = PartyResultFactory.getGetPartyAliasesResult();
139
140        if(entities != null) {
141            if(session.hasLimit(PartyAliasFactory.class)) {
142                result.setPartyAliasCount(getTotalEntities());
143            }
144
145            if(party != null) {
146                result.setParty(partyControl.getPartyTransfer(getUserVisit(), party));
147            }
148
149            if(partyAliasType != null) {
150                result.setPartyAliasType(partyControl.getPartyAliasTypeTransfer(getUserVisit(), partyAliasType));
151            }
152
153            result.setPartyAliases(partyControl.getPartyAliasTransfers(getUserVisit(), entities));
154        }
155
156        return result;
157    }
158
159}