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    /** Creates a new instance of GetSubscriptionsCommand */
079    public GetSubscriptionsCommand() {
080        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
081    }
082    
083    private SubscriptionType subscriptionType;
084    private Party party;
085
086    @Override
087    protected void handleForm() {
088        var subscriptionKindName = form.getSubscriptionKindName();
089        var subscriptionTypeName = form.getSubscriptionTypeName();
090        var partyName = form.getPartyName();
091
092        if(subscriptionKindName != null && subscriptionTypeName != null && partyName == null) {
093                subscriptionType = subscriptionTypeLogic.getSubscriptionTypeByName(this, subscriptionKindName, subscriptionTypeName);
094        } else if(subscriptionKindName == null && subscriptionTypeName == null && partyName != null) {
095            party = partyLogic.getPartyByName(this, partyName);
096        } else {
097            addExecutionError(com.echothree.util.common.message.ExecutionErrors.InvalidParameterCount.name());
098        }
099    }
100
101    @Override
102    protected Long getTotalEntities() {
103        Long total = null;
104
105        if(!hasExecutionErrors()) {
106            if(subscriptionType != null) {
107                total = subscriptionControl.countSubscriptionsBySubscriptionType(subscriptionType);
108            } else if(party != null) {
109                total = subscriptionControl.countSubscriptionsByParty(party);
110            }
111        }
112
113        return total;
114    }
115
116    @Override
117    protected Collection<Subscription> getEntities() {
118        Collection<Subscription> subscriptions = null;
119
120        if(!hasExecutionErrors()) {
121            if(subscriptionType != null) {
122                subscriptions = subscriptionControl.getSubscriptionsBySubscriptionType(subscriptionType);
123            } else if(party != null) {
124                subscriptions = subscriptionControl.getSubscriptionsByParty(party);
125            }
126        }
127
128        return subscriptions;
129    }
130
131    @Override
132    protected BaseResult getResult(Collection<Subscription> entities) {
133        var result = SubscriptionResultFactory.getGetSubscriptionsResult();
134
135        if(entities != null) {
136            var userVisit = getUserVisit();
137
138            if(subscriptionType != null) {
139                result.setSubscriptionType(subscriptionControl.getSubscriptionTypeTransfer(userVisit, subscriptionType));
140            } else if(party != null) {
141                result.setParty(partyControl.getPartyTransfer(userVisit, party));
142            }
143
144            if(session.hasLimit(SubscriptionFactory.class)) {
145                result.setSubscriptionCount(getTotalEntities());
146            }
147
148            result.setSubscriptions(subscriptionControl.getSubscriptionTransfers(userVisit, entities));
149        }
150
151        return result;
152    }
153
154}