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.campaign.server.command;
018
019import com.echothree.control.user.campaign.common.form.GetCampaignForm;
020import com.echothree.control.user.campaign.common.result.CampaignResultFactory;
021import com.echothree.model.control.campaign.server.control.CampaignControl;
022import com.echothree.model.control.core.common.ComponentVendors;
023import com.echothree.model.control.core.common.EntityTypes;
024import com.echothree.model.control.core.common.EventTypes;
025import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
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.campaign.server.entity.Campaign;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.common.command.BaseResult;
035import com.echothree.util.server.control.BaseSimpleCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import com.echothree.util.server.persistence.Session;
040import java.util.Arrays;
041import java.util.Collections;
042import java.util.List;
043import javax.enterprise.context.RequestScoped;
044
045@RequestScoped
046public class GetCampaignCommand
047        extends BaseSimpleCommand<GetCampaignForm> {
048    
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
056                        new SecurityRoleDefinition(SecurityRoleGroups.Campaign.name(), SecurityRoles.Review.name())
057                        )))
058                )));
059        
060        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
061                new FieldDefinition("CampaignName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
063                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
064                ));
065    }
066    
067    /** Creates a new instance of GetCampaignCommand */
068    public GetCampaignCommand() {
069        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
070    }
071    
072    @Override
073    protected BaseResult execute() {
074        var result = CampaignResultFactory.getGetCampaignResult();
075        var campaignName = form.getCampaignName();
076        var parameterCount = (campaignName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
077
078        if(parameterCount == 1) {
079            var campaignControl = Session.getModelController(CampaignControl.class);
080            Campaign campaign = null;
081
082            if(campaignName == null) {
083                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
084                        EntityTypes.Campaign.name());
085                
086                if(!hasExecutionErrors()) {
087                    campaign = campaignControl.getCampaignByEntityInstance(entityInstance);
088                }
089            } else {
090                campaign = campaignControl.getCampaignByName(campaignName);
091
092                if(campaign == null) {
093                    addExecutionError(ExecutionErrors.UnknownCampaignName.name(), campaignName);
094                }
095            }
096
097            if(!hasExecutionErrors()) {
098                result.setCampaign(campaignControl.getCampaignTransfer(getUserVisit(), campaign));
099                sendEvent(campaign.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
100            }
101        } else {
102            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
103        }
104        
105        return result;
106    }
107    
108}