<commit_before>/**
 * @author alexanderweiss
 * @date 19.11.2015
 */
package com.github.alexanderwe.bananaj.model.campaign;

import java.net.URL;
import java.time.LocalDateTime;

import org.json.JSONArray;
import org.json.JSONObject;

import com.github.alexanderwe.bananaj.connection.MailChimpConnection;
import com.github.alexanderwe.bananaj.exceptions.CampaignSettingsException;
import com.github.alexanderwe.bananaj.model.ReportSummary;
import com.github.alexanderwe.bananaj.model.Tracking;
import com.github.alexanderwe.bananaj.model.report.Report;
import com.github.alexanderwe.bananaj.utils.DateConverter;

/**
 * Class for representing a mailchimp campaign
 * @author alexanderweiss
 *
 */
public class Campaign {

	private MailChimpConnection connection;
	
	private String id;
	private int webId;
	private String parentCampaignId;
	private CampaignType type;
	private LocalDateTime createTime;
	private String archiveUrl;
	private String longArchiveUrl;
	private CampaignStatus status;
	private int emailsSent;
	private LocalDateTime sendTime;
	private CampaignContentType contentType;
	private boolean needsBlockRefresh;
	private boolean hasLogoMergeTag;
	private boolean resendable;
	private CampaignRecipients recipients;
	private CampaignSettings settings;
	//private VariateSettings variate_settings;
	private Tracking tracking;
	//private RssOpts rss_opts;
	//private AbSplitOpts ab_split_opts;
	//private SocialCard social_card;
	private ReportSummary reportSummary;
	//private DeliveryStatus delivery_status;
	
	private CampaignContent content;

	public Campaign(MailChimpConnection connection, JSONObject jsonObj) throws Exception {
		parse(connection, jsonObj);
	}

	public void parse(MailChimpConnection connection, JSONObject jsonObj) {
		id = jsonObj.getString("id");
		this.connection = connection;
		this.webId = jsonObj.getInt("web_id");
		if (jsonObj.has("parent_campaign_id")) {
			this.parentCampaignId = jsonObj.getString("parent_campaign_id");
		}
		this.type = CampaignType.valueOf(jsonObj.getString("type").toUpperCase());
		this.createTime = DateConverter.getInstance().createDateFromISO8601(jsonObj.getString("create_time"));
		this.archiveUrl = jsonObj.getString("archive_url");
		this.longArchiveUrl = jsonObj.getString("long_archive_url");
		this.status = CampaignStatus.valueOf(jsonObj.getString("status").toUpperCase());
		this.emailsSent = jsonObj.getInt("emails_sent");
		if (jsonObj.has("send_time")) {
			this.sendTime = DateConverter.getInstance().createDateFromISO8601(jsonObj.getString("send_time"));
		}
		this.contentType = CampaignContentType.valueOf(jsonObj.getString("content_type").toUpperCase());
		this.needsBlockRefresh = jsonObj.getBoolean("needs_block_refresh");
		this.hasLogoMergeTag = jsonObj.getBoolean("has_logo_merge_tag");
		this.resendable = jsonObj.getBoolean("resendable");
		
		if (jsonObj.has("recipients")) {
			this.recipients = new CampaignRecipients(jsonObj.getJSONObject("recipients"));
//			if (recipients.getListId() != null) {
//				this.mailChimpList = connection.getList(recipients.getString("list_id"));
//			}
		}
		this.settings = new CampaignSettings(jsonObj.getJSONObject("settings"));
		this.tracking = new Tracking(jsonObj.getJSONObject("tracking"));
		if (jsonObj.has("report_summary")) {
			this.reportSummary = new ReportSummary(jsonObj.getJSONObject("report_summary"));
		}
	}
	
	/**
	 * Update the campaign settings given specified CampaignSettings.
	 * @param campaignSettings
	 */
	public void updateSettings(CampaignSettings campaignSettings) throws CampaignSettingsException, Exception {<commit_msg>#49 Null check for campaign has_logo_merge_tag<commit_after>/**
 * @author alexanderweiss
 * @date 19.11.2015
 */
package com.github.alexanderwe.bananaj.model.campaign;

import java.net.URL;
import java.time.LocalDateTime;

import org.json.JSONArray;
import org.json.JSONObject;

import com.github.alexanderwe.bananaj.connection.MailChimpConnection;
import com.github.alexanderwe.bananaj.exceptions.CampaignSettingsException;
import com.github.alexanderwe.bananaj.model.ReportSummary;
import com.github.alexanderwe.bananaj.model.Tracking;
import com.github.alexanderwe.bananaj.model.report.Report;
import com.github.alexanderwe.bananaj.utils.DateConverter;

/**
 * Class for representing a mailchimp campaign
 * @author alexanderweiss
 *
 */
public class Campaign {

	private MailChimpConnection connection;
	
	private String id;
	private int webId;
	private String parentCampaignId;
	private CampaignType type;
	private LocalDateTime createTime;
	private String archiveUrl;
	private String longArchiveUrl;
	private CampaignStatus status;
	private int emailsSent;
	private LocalDateTime sendTime;
	private CampaignContentType contentType;
	private boolean needsBlockRefresh;
	private boolean hasLogoMergeTag;
	private boolean resendable;
	private CampaignRecipients recipients;
	private CampaignSettings settings;
	//private VariateSettings variate_settings;
	private Tracking tracking;
	//private RssOpts rss_opts;
	//private AbSplitOpts ab_split_opts;
	//private SocialCard social_card;
	private ReportSummary reportSummary;
	//private DeliveryStatus delivery_status;
	
	private CampaignContent content;

	public Campaign(MailChimpConnection connection, JSONObject jsonObj) throws Exception {
		parse(connection, jsonObj);
	}

	public void parse(MailChimpConnection connection, JSONObject jsonObj) {
		id = jsonObj.getString("id");
		this.connection = connection;
		this.webId = jsonObj.getInt("web_id");
		if (jsonObj.has("parent_campaign_id")) {
			this.parentCampaignId = jsonObj.getString("parent_campaign_id");
		}
		this.type = CampaignType.valueOf(jsonObj.getString("type").toUpperCase());
		this.createTime = DateConverter.getInstance().createDateFromISO8601(jsonObj.getString("create_time"));
		this.archiveUrl = jsonObj.getString("archive_url");
		this.longArchiveUrl = jsonObj.getString("long_archive_url");
		this.status = CampaignStatus.valueOf(jsonObj.getString("status").toUpperCase());
		this.emailsSent = jsonObj.getInt("emails_sent");
		if (jsonObj.has("send_time")) {
			this.sendTime = DateConverter.getInstance().createDateFromISO8601(jsonObj.getString("send_time"));
		}
		this.contentType = CampaignContentType.valueOf(jsonObj.getString("content_type").toUpperCase());
		this.needsBlockRefresh = jsonObj.getBoolean("needs_block_refresh");
		if (jsonObj.has("has_logo_merge_tag")) {
			this.hasLogoMergeTag = jsonObj.getBoolean("has_logo_merge_tag");
		}
		this.resendable = jsonObj.getBoolean("resendable");
		
		if (jsonObj.has("recipients")) {
			this.recipients = new CampaignRecipients(jsonObj.getJSONObject("recipients"));
//			if (recipients.getListId() != null) {
//				this.mailChimpList = connection.getList(recipients.getString("list_id"));
//			}
		}
		this.settings = new CampaignSettings(jsonObj.getJSONObject("settings"));
		this.tracking = new Tracking(jsonObj.getJSONObject("tracking"));
		if (jsonObj.has("report_summary")) {
			this.reportSummary = new ReportSummary(jsonObj.getJSONObject("report_summary"));
		}
	}
	
	/**
	 * Update the campaign settings given specified CampaignSettings.
	 * @param campaignSettings
	 */
	public void updateSettings(CampaignSettings campaignSettings) throws CampaignSettingsException, Exception {
{'fraction_non_alphanumeric': 0.10788495940553186, 'fraction_numerical': 0.004678684464015412, 'mean_word_length': 6.153543307086614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2563411', 'n_tokens_mistral': 2282, 'n_tokens_neox': 2113, 'n_words': 405}