001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.user.common.pk.UserVisitPK;
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.BaseMultipleEntitiesCommand;
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.persistence.Session;
041import com.echothree.util.server.validation.ParameterUtils;
042import java.util.Collection;
043import java.util.List;
044import javax.enterprise.context.RequestScoped;
045
046@RequestScoped
047public class GetOfferUsesCommand
048        extends BaseMultipleEntitiesCommand<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    /** Creates a new instance of GetOfferUsesCommand */
068    public GetOfferUsesCommand() {
069        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
070    }
071
072    private Offer offer;
073    private Use use;
074
075    @Override
076    protected Collection<OfferUse> getEntities() {
077        Collection<OfferUse> offerUses = null;
078        var offerName = form.getOfferName();
079        var useName = form.getUseName();
080
081        if(ParameterUtils.getInstance().isExactlyOneBooleanTrue(this, offerName != null && useName == null,
082                offerName == null && useName != null, offerName == null && useName == null)) {
083            var offerUseControl = Session.getModelController(OfferUseControl.class);
084
085            if(offerName != null) {
086                offer = OfferLogic.getInstance().getOfferByName(this, offerName);
087
088                if(!hasExecutionErrors()) {
089                    offerUses = offerUseControl.getOfferUsesByOffer(offer);
090                }
091            } if(useName != null) {
092                use = UseLogic.getInstance().getUseByName(this, useName);
093
094                if(!hasExecutionErrors()) {
095                    offerUses = offerUseControl.getOfferUsesByUse(use);
096                }
097            } else {
098                offerUses = offerUseControl.getOfferUses();
099            }
100        }
101
102        return offerUses;
103    }
104    
105    @Override
106    protected BaseResult getResult(Collection<OfferUse> entities) {
107        var result = OfferResultFactory.getGetOfferUsesResult();
108        
109        if(entities != null) {
110            var offerUseControl = Session.getModelController(OfferUseControl.class);
111            var userVisit = getUserVisit();
112
113            if(offer != null) {
114                var offerControl = Session.getModelController(OfferControl.class);
115
116                result.setOffer(offerControl.getOfferTransfer(userVisit, offer));
117            }
118
119            if(use != null) {
120                var useControl = Session.getModelController(UseControl.class);
121
122                result.setUse(useControl.getUseTransfer(userVisit, use));
123            }
124
125            result.setOfferUses(offerUseControl.getOfferUseTransfers(userVisit, entities));
126        }
127        
128        return result;
129    }
130    
131}