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.payment.server.command;
018
019import com.echothree.control.user.payment.common.form.GetPartyPaymentMethodsForm;
020import com.echothree.control.user.payment.common.result.PaymentResultFactory;
021import com.echothree.model.control.party.common.PartyTypes;
022import com.echothree.model.control.party.server.control.PartyControl;
023import com.echothree.model.control.party.server.logic.PartyLogic;
024import com.echothree.model.control.payment.server.control.PartyPaymentMethodControl;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.data.party.server.entity.Party;
028import com.echothree.model.data.payment.server.entity.PartyPaymentMethod;
029import com.echothree.model.data.payment.server.factory.PartyPaymentMethodFactory;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.Collection;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class GetPartyPaymentMethodsCommand
045        extends BasePaginatedMultipleEntitiesCommand<PartyPaymentMethod, GetPartyPaymentMethodsForm> {
046    
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
049    
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.PartyPaymentMethod.name(), SecurityRoles.List.name())
056                ))
057        ));
058
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null)
061        );
062    }
063
064    @Inject
065    PartyControl partyControl;
066
067    @Inject
068    PartyPaymentMethodControl partyPaymentMethodControl;
069
070    @Inject
071    PartyLogic partyLogic;
072
073
074    /** Creates a new instance of GetPartyPaymentMethodsCommand */
075    public GetPartyPaymentMethodsCommand() {
076        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
077    }
078
079    Party party;
080
081    @Override
082    protected void handleForm() {
083        party = getParty();
084
085        var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName();
086        var partyName = form.getPartyName();
087        // If the caller is a CUSTOMER then they're the Party. If they're not the PartyName parameter is
088        // required and we'll look them up.
089        if(partyTypeName.equals(PartyTypes.CUSTOMER.name())) {
090            if(partyName != null) {
091                addExecutionError(ExecutionErrors.InvalidParameterCount.name());
092            }
093        } else {
094            if(partyName == null) {
095                addExecutionError(ExecutionErrors.PartyNameRequired.name());
096            } else {
097                party = partyControl.getPartyByName(partyName);
098
099                if(party == null) {
100                    addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
101                }
102            }
103        }
104    }
105
106    @Override
107    protected Long getTotalEntities() {
108        return hasExecutionErrors() ? null : partyPaymentMethodControl.countPartyPaymentMethodsByParty(party);
109    }
110
111    @Override
112    protected Collection<PartyPaymentMethod> getEntities() {
113        return hasExecutionErrors() ? null : partyPaymentMethodControl.getPartyPaymentMethodsByParty(party);
114    }
115
116    @Override
117    protected BaseResult getResult(Collection<PartyPaymentMethod> entities) {
118        var result = PaymentResultFactory.getGetPartyPaymentMethodsResult();
119
120        if(entities != null) {
121            var userVisit = getUserVisit();
122
123            result.setParty(partyControl.getPartyTransfer(userVisit, party));
124
125            if(session.hasLimit(PartyPaymentMethodFactory.class)) {
126                result.setPartyPaymentMethodCount(getTotalEntities());
127            }
128
129            result.setPartyPaymentMethods(partyPaymentMethodControl.getPartyPaymentMethodTransfers(userVisit, entities));
130        }
131
132        return result;
133    }
134    
135}