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.CampaignStatusConstants;
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.Campaign;
026import com.echothree.model.data.campaign.server.entity.CampaignDetail;
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 object")
035@GraphQLName("Campaign")
036public class CampaignObject
037        extends BaseEntityInstanceObject {
038    
039    private final Campaign campaign; // Always Present
040    
041    public CampaignObject(Campaign campaign) {
042        super(campaign.getPrimaryKey());
043        
044        this.campaign = campaign;
045    }
046
047    private CampaignDetail campaignDetail; // Optional, use getCampaignDetail()
048    
049    private CampaignDetail getCampaignDetail() {
050        if(campaignDetail == null) {
051            campaignDetail = campaign.getLastDetail();
052        }
053        
054        return campaignDetail;
055    }
056
057    @GraphQLField
058    @GraphQLDescription("campaign name")
059    @GraphQLNonNull
060    public String getCampaignName() {
061        return getCampaignDetail().getCampaignName();
062    }
063
064    @GraphQLField
065    @GraphQLDescription("value SHA1 hash")
066    @GraphQLNonNull
067    public String getValueSha1Hash() {
068        return getCampaignDetail().getValueSha1Hash();
069    }
070
071    @GraphQLField
072    @GraphQLDescription("value")
073    @GraphQLNonNull
074    public String getValue() {
075        return getCampaignDetail().getValue();
076    }
077
078    @GraphQLField
079    @GraphQLDescription("is default")
080    @GraphQLNonNull
081    public boolean getIsDefault() {
082        return getCampaignDetail().getIsDefault();
083    }
084    
085    @GraphQLField
086    @GraphQLDescription("sort order")
087    @GraphQLNonNull
088    public int getSortOrder() {
089        return getCampaignDetail().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.getBestCampaignDescription(campaign, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
100    }
101
102    @GraphQLField
103    @GraphQLDescription("campaign status")
104    public WorkflowEntityStatusObject getCampaignStatus(final DataFetchingEnvironment env) {
105        return getWorkflowEntityStatusObject(env, CampaignStatusConstants.Workflow_CAMPAIGN_STATUS);
106    }
107
108}