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.term.server.command;
018
019import com.echothree.control.user.term.common.form.GetPartyCreditLimitsForm;
020import com.echothree.control.user.term.common.result.TermResultFactory;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.control.party.server.logic.PartyLogic;
023import com.echothree.model.control.term.server.control.TermControl;
024import com.echothree.model.data.party.server.entity.Party;
025import com.echothree.model.data.term.server.entity.PartyCreditLimit;
026import com.echothree.model.data.term.server.factory.PartyCreditLimitFactory;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
031import java.util.Collection;
032import java.util.List;
033import javax.enterprise.context.Dependent;
034import javax.inject.Inject;
035
036@Dependent
037public class GetPartyCreditLimitsCommand
038        extends BasePaginatedMultipleEntitiesCommand<PartyCreditLimit, GetPartyCreditLimitsForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = List.of(
044                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null)
045        );
046    }
047
048    @Inject
049    PartyControl partyControl;
050
051    @Inject
052    TermControl termControl;
053
054    @Inject
055    PartyLogic partyLogic;
056    
057    /** Creates a new instance of GetPartyCreditLimitsCommand */
058    public GetPartyCreditLimitsCommand() {
059        super(null, FORM_FIELD_DEFINITIONS, true);
060    }
061
062    Party party;
063
064    @Override
065    protected void handleForm() {
066        party = partyLogic.getPartyByName(this, form.getPartyName());
067    }
068
069    @Override
070    protected Long getTotalEntities() {
071        return hasExecutionErrors() ? null : termControl.countPartyCreditLimitsByParty(party);
072    }
073
074    @Override
075    protected Collection<PartyCreditLimit> getEntities() {
076        return hasExecutionErrors() ? null : termControl.getPartyCreditLimitsByParty(party);
077    }
078
079    @Override
080    protected BaseResult getResult(Collection<PartyCreditLimit> entities) {
081        var result = TermResultFactory.getGetPartyCreditLimitsResult();
082
083        if(entities != null) {
084            var userVisit = getUserVisit();
085
086            result.setParty(partyControl.getPartyTransfer(userVisit, party));
087
088            if(session.hasLimit(PartyCreditLimitFactory.class)) {
089                result.setPartyCreditLimitCount(getTotalEntities());
090            }
091
092            result.setPartyCreditLimits(termControl.getPartyCreditLimitTransfers(userVisit, entities));
093        }
094
095        return result;
096    }
097    
098}