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.cancellationpolicy.server.command;
018
019import com.echothree.control.user.cancellationpolicy.common.form.GetPartyCancellationPoliciesForm;
020import com.echothree.control.user.cancellationpolicy.common.result.CancellationPolicyResultFactory;
021import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl;
022import com.echothree.model.control.cancellationpolicy.server.logic.CancellationKindLogic;
023import com.echothree.model.control.cancellationpolicy.server.logic.CancellationPolicyLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.server.control.PartyControl;
026import com.echothree.model.control.party.server.logic.PartyLogic;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.cancellationpolicy.server.entity.CancellationPolicy;
030import com.echothree.model.data.cancellationpolicy.server.entity.PartyCancellationPolicy;
031import com.echothree.model.data.cancellationpolicy.server.factory.PartyCancellationPolicyFactory;
032import com.echothree.model.data.party.server.entity.Party;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import java.util.Collection;
042import java.util.List;
043import javax.enterprise.context.Dependent;
044import javax.inject.Inject;
045
046@Dependent
047public class GetPartyCancellationPoliciesCommand
048        extends BasePaginatedMultipleEntitiesCommand<PartyCancellationPolicy, GetPartyCancellationPoliciesForm> {
049
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
052
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
057                        new SecurityRoleDefinition(SecurityRoleGroups.PartyCancellationPolicy.name(), SecurityRoles.List.name())
058                ))
059        ));
060        
061        FORM_FIELD_DEFINITIONS = List.of(
062                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("CancellationKindName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("CancellationPolicyName", FieldType.ENTITY_NAME, false, null, null)
065        );
066    }
067
068    @Inject
069    CancellationPolicyControl cancellationPolicyControl;
070
071    @Inject
072    PartyControl partyControl;
073
074    @Inject
075    CancellationKindLogic cancellationKindLogic;
076
077    @Inject
078    CancellationPolicyLogic cancellationPolicyLogic;
079
080    @Inject
081    PartyLogic partyLogic;
082
083    /** Creates a new instance of GetPartyCancellationPoliciesCommand */
084    public GetPartyCancellationPoliciesCommand() {
085        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
086    }
087
088    private Party party;
089    private CancellationPolicy cancellationPolicy;
090
091    @Override
092    protected void handleForm() {
093        var partyName = form.getPartyName();
094        var cancellationKindName = form.getCancellationKindName();
095        var cancellationPolicyName = form.getCancellationPolicyName();
096        var parameterCount = (partyName != null ? 1 : 0) + (cancellationKindName != null && cancellationPolicyName != null ? 1 : 0);
097
098        if(parameterCount == 1) {
099            if(partyName != null) {
100                party = partyLogic.getPartyByName(this, partyName);
101            } else {
102                var cancellationKind = cancellationKindLogic.getCancellationKindByName(this, cancellationKindName);
103
104                if(!hasExecutionErrors()) {
105                    cancellationPolicy = cancellationPolicyLogic.getCancellationPolicyByName(this, cancellationKind, cancellationPolicyName);
106                }
107            }
108        } else {
109            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
110        }
111    }
112
113    @Override
114    protected Long getTotalEntities() {
115        Long total = null;
116
117        if(!hasExecutionErrors()) {
118            if(party != null) {
119                total = cancellationPolicyControl.countPartyCancellationPoliciesByParty(party);
120            } else {
121                total = cancellationPolicyControl.countPartyCancellationPoliciesByCancellationPolicy(cancellationPolicy);
122            }
123        }
124
125        return total;
126    }
127
128    @Override
129    protected Collection<PartyCancellationPolicy> getEntities() {
130        Collection<PartyCancellationPolicy> entities = null;
131
132        if(!hasExecutionErrors()) {
133            if(party != null) {
134                entities = cancellationPolicyControl.getPartyCancellationPoliciesByParty(party);
135            } else {
136                entities = cancellationPolicyControl.getPartyCancellationPoliciesByCancellationPolicy(cancellationPolicy);
137            }
138        }
139
140        return entities;
141    }
142
143    @Override
144    protected BaseResult getResult(Collection<PartyCancellationPolicy> entities) {
145        var result = CancellationPolicyResultFactory.getGetPartyCancellationPoliciesResult();
146
147        if(entities != null) {
148            var userVisit = getUserVisit();
149
150            if(party != null) {
151                result.setParty(partyControl.getPartyTransfer(userVisit, party));
152            }
153
154            if(cancellationPolicy != null) {
155                result.setCancellationPolicy(cancellationPolicyControl.getCancellationPolicyTransfer(userVisit, cancellationPolicy));
156            }
157
158            if(session.hasLimit(PartyCancellationPolicyFactory.class)) {
159                result.setPartyCancellationPolicyCount(getTotalEntities());
160            }
161
162            result.setPartyCancellationPolicies(cancellationPolicyControl.getPartyCancellationPolicyTransfers(userVisit, entities));
163        }
164
165        return result;
166    }
167
168}