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.model.control.campaign.server.graphql;
018
019import com.echothree.model.control.campaign.common.workflow.CampaignSourceStatusConstants;
020import com.echothree.model.control.campaign.server.control.CampaignControl;
021import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
022import com.echothree.model.control.graphql.server.util.BaseGraphQl;
023import com.echothree.model.control.user.server.control.UserControl;
024import com.echothree.model.control.workflow.server.graphql.WorkflowEntityStatusObject;
025import com.echothree.model.data.campaign.server.entity.CampaignSource;
026import com.echothree.model.data.campaign.server.entity.CampaignSourceDetail;
027import com.echothree.util.server.persistence.Session;
028import graphql.annotations.annotationTypes.GraphQLDescription;
029import graphql.annotations.annotationTypes.GraphQLField;
030import graphql.annotations.annotationTypes.GraphQLName;
031import graphql.annotations.annotationTypes.GraphQLNonNull;
032import graphql.schema.DataFetchingEnvironment;
033
034@GraphQLDescription("campaign source object")
035@GraphQLName("CampaignSource")
036public class CampaignSourceObject
037        extends BaseEntityInstanceObject {
038    
039    private final CampaignSource campaignSource; // Always Present
040    
041    public CampaignSourceObject(CampaignSource campaignSource) {
042        super(campaignSource.getPrimaryKey());
043        
044        this.campaignSource = campaignSource;
045    }
046
047    private CampaignSourceDetail campaignSourceDetail; // Optional, use getCampaignSourceDetail()
048    
049    private CampaignSourceDetail getCampaignSourceDetail() {
050        if(campaignSourceDetail == null) {
051            campaignSourceDetail = campaignSource.getLastDetail();
052        }
053        
054        return campaignSourceDetail;
055    }
056
057    @GraphQLField
058    @GraphQLDescription("campaign source name")
059    @GraphQLNonNull
060    public String getCampaignSourceName() {
061        return getCampaignSourceDetail().getCampaignSourceName();
062    }
063
064    @GraphQLField
065    @GraphQLDescription("value SHA1 hash")
066    @GraphQLNonNull
067    public String getValueSha1Hash() {
068        return getCampaignSourceDetail().getValueSha1Hash();
069    }
070
071    @GraphQLField
072    @GraphQLDescription("value")
073    @GraphQLNonNull
074    public String getValue() {
075        return getCampaignSourceDetail().getValue();
076    }
077
078    @GraphQLField
079    @GraphQLDescription("is default")
080    @GraphQLNonNull
081    public boolean getIsDefault() {
082        return getCampaignSourceDetail().getIsDefault();
083    }
084    
085    @GraphQLField
086    @GraphQLDescription("sort order")
087    @GraphQLNonNull
088    public int getSortOrder() {
089        return getCampaignSourceDetail().getSortOrder();
090    }
091    
092    @GraphQLField
093    @GraphQLDescription("description")
094    @GraphQLNonNull
095    public String getDescription(final DataFetchingEnvironment env) {
096        var campaignControl = Session.getModelController(CampaignControl.class);
097        var userControl = Session.getModelController(UserControl.class);
098
099        return campaignControl.getBestCampaignSourceDescription(campaignSource, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
100    }
101
102    @GraphQLField
103    @GraphQLDescription("campaign source status")
104    public WorkflowEntityStatusObject getCampaignSourceStatus(final DataFetchingEnvironment env) {
105        return getWorkflowEntityStatusObject(env, CampaignSourceStatusConstants.Workflow_CAMPAIGN_SOURCE_STATUS);
106    }
107
108}