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.subscription.server.command;
018
019import com.echothree.control.user.subscription.common.form.GetSubscriptionsForm;
020import com.echothree.control.user.subscription.common.result.SubscriptionResultFactory;
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.security.common.SecurityRoleGroups;
025import com.echothree.model.control.security.common.SecurityRoles;
026import com.echothree.model.control.subscription.server.control.SubscriptionControl;
027import com.echothree.model.control.subscription.server.logic.SubscriptionTypeLogic;
028import com.echothree.model.data.party.server.entity.Party;
029import com.echothree.model.data.subscription.server.entity.Subscription;
030import com.echothree.model.data.subscription.server.entity.SubscriptionType;
031import com.echothree.model.data.subscription.server.factory.SubscriptionFactory;
032import com.echothree.util.common.command.BaseResult;
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 GetSubscriptionsCommand
046        extends BasePaginatedMultipleEntitiesCommand<Subscription, GetSubscriptionsForm> {
047
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.Subscription.name(), SecurityRoles.List.name())
056                ))
057        ));
058
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("SubscriptionKindName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("SubscriptionTypeName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null)
063        );
064    }
065
066    @Inject
067    PartyControl partyControl;
068
069    @Inject
070    SubscriptionControl subscriptionControl;
071
072    @Inject
073    PartyLogic partyLogic;
074
075    @Inject
076    SubscriptionTypeLogic subscriptionTypeLogic;
077
078
079    
080    /** Creates a new instance of GetSubscriptionsCommand */
081    public GetSubscriptionsCommand() {
082        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
083    }
084    
085    private SubscriptionType subscriptionType;
086    private Party party;
087
088    @Override
089    protected void handleForm() {
090        var subscriptionKindName = form.getSubscriptionKindName();
091        var subscriptionTypeName = form.getSubscriptionTypeName();
092        var partyName = form.getPartyName();
093
094        if(subscriptionKindName != null && subscriptionTypeName != null && partyName == null) {
095                subscriptionType = subscriptionTypeLogic.getSubscriptionTypeByName(this, subscriptionKindName, subscriptionTypeName);
096        } else if(subscriptionKindName == null && subscriptionTypeName == null && partyName != null) {
097            party = partyLogic.getPartyByName(this, partyName);
098        } else {
099            addExecutionError(com.echothree.util.common.message.ExecutionErrors.InvalidParameterCount.name());
100        }
101    }
102
103    @Override
104    protected Long getTotalEntities() {
105        Long total = null;
106
107        if(!hasExecutionErrors()) {
108            if(subscriptionType != null) {
109                total = subscriptionControl.countSubscriptionsBySubscriptionType(subscriptionType);
110            } else if(party != null) {
111                total = subscriptionControl.countSubscriptionsByParty(party);
112            }
113        }
114
115        return total;
116    }
117
118    @Override
119    protected Collection<Subscription> getEntities() {
120        Collection<Subscription> subscriptions = null;
121
122        if(!hasExecutionErrors()) {
123            if(subscriptionType != null) {
124                subscriptions = subscriptionControl.getSubscriptionsBySubscriptionType(subscriptionType);
125            } else if(party != null) {
126                subscriptions = subscriptionControl.getSubscriptionsByParty(party);
127            }
128        }
129
130        return subscriptions;
131    }
132
133    @Override
134    protected BaseResult getResult(Collection<Subscription> entities) {
135        var result = SubscriptionResultFactory.getGetSubscriptionsResult();
136
137        if(entities != null) {
138            var userVisit = getUserVisit();
139
140            if(subscriptionType != null) {
141                result.setSubscriptionType(subscriptionControl.getSubscriptionTypeTransfer(userVisit, subscriptionType));
142            } else if(party != null) {
143                result.setParty(partyControl.getPartyTransfer(userVisit, party));
144            }
145
146            if(session.hasLimit(SubscriptionFactory.class)) {
147                result.setSubscriptionCount(getTotalEntities());
148            }
149
150            result.setSubscriptions(subscriptionControl.getSubscriptionTransfers(userVisit, entities));
151        }
152
153        return result;
154    }
155
156}