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.offer.server.command;
018
019import com.echothree.control.user.offer.common.form.GetOfferUsesForm;
020import com.echothree.control.user.offer.common.result.OfferResultFactory;
021import com.echothree.model.control.offer.server.control.OfferControl;
022import com.echothree.model.control.offer.server.control.OfferUseControl;
023import com.echothree.model.control.offer.server.control.UseControl;
024import com.echothree.model.control.offer.server.logic.OfferLogic;
025import com.echothree.model.control.offer.server.logic.UseLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.offer.server.entity.Offer;
030import com.echothree.model.data.offer.server.entity.OfferUse;
031import com.echothree.model.data.offer.server.entity.Use;
032import com.echothree.model.data.offer.server.factory.OfferUseFactory;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.validation.ParameterUtils;
041import java.util.Collection;
042import java.util.List;
043import javax.enterprise.context.Dependent;
044import javax.inject.Inject;
045
046@Dependent
047public class GetOfferUsesCommand
048        extends BasePaginatedMultipleEntitiesCommand<OfferUse, GetOfferUsesForm> {
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.OfferUse.name(), SecurityRoles.List.name())
058                ))
059        ));
060
061        FORM_FIELD_DEFINITIONS = List.of(
062                new FieldDefinition("OfferName", FieldType.ENTITY_NAME, false, null, 20L),
063                new FieldDefinition("UseName", FieldType.ENTITY_NAME, false, null, 20L)
064        );
065    }
066
067    @Inject
068    OfferControl offerControl;
069
070    @Inject
071    OfferUseControl offerUseControl;
072
073    @Inject
074    UseControl useControl;
075
076    @Inject
077    OfferLogic offerLogic;
078
079    @Inject
080    UseLogic useLogic;
081
082
083    /** Creates a new instance of GetOfferUsesCommand */
084    public GetOfferUsesCommand() {
085        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
086    }
087
088    private Offer offer;
089    private Use use;
090
091    @Override
092    protected void handleForm() {
093        var offerName = form.getOfferName();
094        var useName = form.getUseName();
095
096        if(ParameterUtils.getInstance().isExactlyOneBooleanTrue(this, offerName != null && useName == null,
097                offerName == null && useName != null, offerName == null && useName == null)) {
098            if(offerName != null) {
099                offer = offerLogic.getOfferByName(this, offerName);
100            } else if(useName != null) {
101                use = useLogic.getUseByName(this, useName);
102            }
103        }
104    }
105
106    @Override
107    protected Long getTotalEntities() {
108        Long total = null;
109
110        if(!hasExecutionErrors()) {
111            if(offer != null) {
112                total = offerUseControl.countOfferUsesByOffer(offer);
113            } else if(use != null) {
114                total = offerUseControl.countOfferUsesByUse(use);
115            } else {
116                total = offerUseControl.countOfferUses();
117            }
118        }
119
120        return total;
121    }
122
123    @Override
124    protected Collection<OfferUse> getEntities() {
125        Collection<OfferUse> offerUses = null;
126
127        if(!hasExecutionErrors()) {
128            if(offer != null) {
129                offerUses = offerUseControl.getOfferUsesByOffer(offer);
130            } else if(use != null) {
131                offerUses = offerUseControl.getOfferUsesByUse(use);
132            } else {
133                offerUses = offerUseControl.getOfferUses();
134            }
135        }
136
137        return offerUses;
138    }
139    
140    @Override
141    protected BaseResult getResult(Collection<OfferUse> entities) {
142        var result = OfferResultFactory.getGetOfferUsesResult();
143        
144        if(entities != null) {
145            var userVisit = getUserVisit();
146
147            if(offer != null) {
148                result.setOffer(offerControl.getOfferTransfer(userVisit, offer));
149            }
150
151            if(use != null) {
152                result.setUse(useControl.getUseTransfer(userVisit, use));
153            }
154
155            if(session.hasLimit(OfferUseFactory.class)) {
156                result.setOfferUseCount(getTotalEntities());
157            }
158
159            result.setOfferUses(offerUseControl.getOfferUseTransfers(userVisit, entities));
160        }
161        
162        return result;
163    }
164    
165}