001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.GetCampaignMediumForm;
020import com.echothree.control.user.campaign.common.result.CampaignResultFactory;
021import com.echothree.control.user.campaign.common.result.GetCampaignMediumResult;
022import com.echothree.model.control.campaign.server.control.CampaignControl;
023import com.echothree.model.control.core.common.ComponentVendors;
024import com.echothree.model.control.core.common.EntityTypes;
025import com.echothree.model.control.core.common.EventTypes;
026import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.campaign.server.entity.CampaignMedium;
031import com.echothree.model.data.core.server.entity.EntityInstance;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.common.command.BaseResult;
037import com.echothree.util.server.control.BaseSimpleCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import com.echothree.util.server.persistence.Session;
042import java.util.Arrays;
043import java.util.Collections;
044import java.util.List;
045
046public class GetCampaignMediumCommand
047        extends BaseSimpleCommand<GetCampaignMediumForm> {
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.CampaignMedium.name(), SecurityRoles.Review.name())
057                        )))
058                )));
059        
060        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
061                new FieldDefinition("CampaignMediumName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
063                new FieldDefinition("Key", FieldType.KEY, false, null, null),
064                new FieldDefinition("Guid", FieldType.GUID, false, null, null),
065                new FieldDefinition("Ulid", FieldType.ULID, false, null, null)
066                ));
067    }
068    
069    /** Creates a new instance of GetCampaignMediumCommand */
070    public GetCampaignMediumCommand(UserVisitPK userVisitPK, GetCampaignMediumForm form) {
071        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
072    }
073    
074    @Override
075    protected BaseResult execute() {
076        GetCampaignMediumResult result = CampaignResultFactory.getGetCampaignMediumResult();
077        String campaignMediumName = form.getCampaignMediumName();
078        var parameterCount = (campaignMediumName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
079
080        if(parameterCount == 1) {
081            var campaignControl = Session.getModelController(CampaignControl.class);
082            CampaignMedium campaignMedium = null;
083
084            if(campaignMediumName == null) {
085                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
086                        EntityTypes.CampaignMedium.name());
087                
088                if(!hasExecutionErrors()) {
089                    campaignMedium = campaignControl.getCampaignMediumByEntityInstance(entityInstance);
090                }
091            } else {
092                campaignMedium = campaignControl.getCampaignMediumByName(campaignMediumName);
093
094                if(campaignMedium == null) {
095                    addExecutionError(ExecutionErrors.UnknownCampaignMediumName.name(), campaignMediumName);
096                }
097            }
098
099            if(!hasExecutionErrors()) {
100                result.setCampaignMedium(campaignControl.getCampaignMediumTransfer(getUserVisit(), campaignMedium));
101                sendEvent(campaignMedium.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
102            }
103        } else {
104            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
105        }
106        
107        return result;
108    }
109    
110}