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    PartyPaymentMethodControl partyPaymentMethodControl;
066
067    @Inject
068    PartyControl partyControl;
069
070    @Inject
071    PartyLogic partyLogic;
072
073    /** Creates a new instance of GetPartyPaymentMethodsCommand */
074    public GetPartyPaymentMethodsCommand() {
075        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
076    }
077
078    Party party;
079
080    @Override
081    protected void handleForm() {
082        party = getParty();
083
084        var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName();
085        var partyName = form.getPartyName();
086        // If the caller is a CUSTOMER then they're the Party. If they're not the PartyName parameter is
087        // required and we'll look them up.
088        if(partyTypeName.equals(PartyTypes.CUSTOMER.name())) {
089            if(partyName != null) {
090                addExecutionError(ExecutionErrors.InvalidParameterCount.name());
091            }
092        } else {
093            if(partyName == null) {
094                addExecutionError(ExecutionErrors.PartyNameRequired.name());
095            } else {
096                party = partyControl.getPartyByName(partyName);
097
098                if(party == null) {
099                    addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
100                }
101            }
102        }
103    }
104
105    @Override
106    protected Long getTotalEntities() {
107        return hasExecutionErrors() ? null : partyPaymentMethodControl.countPartyPaymentMethodsByParty(party);
108    }
109
110    @Override
111    protected Collection<PartyPaymentMethod> getEntities() {
112        return hasExecutionErrors() ? null : partyPaymentMethodControl.getPartyPaymentMethodsByParty(party);
113    }
114
115    @Override
116    protected BaseResult getResult(Collection<PartyPaymentMethod> entities) {
117        var result = PaymentResultFactory.getGetPartyPaymentMethodsResult();
118
119        if(entities != null) {
120            var userVisit = getUserVisit();
121
122            result.setParty(partyControl.getPartyTransfer(userVisit, party));
123
124            if(session.hasLimit(PartyPaymentMethodFactory.class)) {
125                result.setPartyPaymentMethodCount(getTotalEntities());
126            }
127
128            result.setPartyPaymentMethods(partyPaymentMethodControl.getPartyPaymentMethodTransfers(userVisit, entities));
129        }
130
131        return result;
132    }
133    
134}