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    
068    /** Creates a new instance of GetPartyAliasesCommand */
069    public GetPartyAliasesCommand() {
070        super(null, FORM_FIELD_DEFINITIONS, true);
071    }
072
073    @Override
074    protected CommandSecurityDefinition getCommandSecurityDefinition() {
075        return new CommandSecurityDefinition(List.of(
076                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
077                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
078                        new SecurityRoleDefinition(PartyAliasUtil.getInstance().getSecurityRoleGroupNameBySpecs(form, form), SecurityRoles.List.name())
079                ))
080        ));
081    }
082
083    private Party party;
084    private PartyAliasType partyAliasType;
085
086    @Override
087    protected void handleForm() {
088        var partyName = form.getPartyName();
089        var partyTypeName = form.getPartyTypeName();
090        var partyAliasTypeName = form.getPartyAliasTypeName();
091        // Must specify either PartyName or PartyTypeName + PartyAliasTypeName
092        var parameterOption1 = (partyName != null) && (partyTypeName == null) && (partyAliasTypeName == null );
093        var parameterOption2 = (partyName == null) && (partyTypeName != null) && (partyAliasTypeName != null );
094        var parameterCount = (parameterOption1 ? 1 : 0) + (parameterOption2 ? 1 : 0);
095
096        if(parameterCount == 1) {
097            if(parameterOption1) {
098                party = partyLogic.getPartyByName(this, form.getPartyName());
099            } else {
100                partyAliasType = partyAliasTypeLogic.getPartyAliasTypeByName(this, partyTypeName, partyAliasTypeName);
101            }
102        } else {
103            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
104        }
105    }
106
107    @Override
108    protected Long getTotalEntities() {
109        Long totalEntities = null;
110
111        if(!hasExecutionErrors()) {
112            if(party != null) {
113                totalEntities = partyControl.countPartyAliasesByParty(party);
114            } else {
115                totalEntities = partyControl.countPartyAliasesByPartyAliasType(partyAliasType);
116            }
117        }
118
119        return totalEntities;
120    }
121
122    @Override
123    protected Collection<PartyAlias> getEntities() {
124        Collection<PartyAlias> partyAliases = null;
125
126        if(!hasExecutionErrors()) {
127            if(party != null) {
128                partyAliases = partyControl.getPartyAliasesByParty(party);
129            } else {
130                partyAliases = partyControl.getPartyAliasesByPartyAliasType(partyAliasType);
131            }
132        }
133
134        return partyAliases;
135    }
136
137    @Override
138    protected BaseResult getResult(Collection<PartyAlias> entities) {
139        var result = PartyResultFactory.getGetPartyAliasesResult();
140
141        if(entities != null) {
142            if(session.hasLimit(PartyAliasFactory.class)) {
143                result.setPartyAliasCount(getTotalEntities());
144            }
145
146            if(party != null) {
147                result.setParty(partyControl.getPartyTransfer(getUserVisit(), party));
148            }
149
150            if(partyAliasType != null) {
151                result.setPartyAliasType(partyControl.getPartyAliasTypeTransfer(getUserVisit(), partyAliasType));
152            }
153
154            result.setPartyAliases(partyControl.getPartyAliasTransfers(getUserVisit(), entities));
155        }
156
157        return result;
158    }
159
160}