<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Home</title>
	<atom:link href="http://www.othenos.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.othenos.com</link>
	<description>developers of rich internet applications</description>
	<lastBuildDate>Mon, 18 Jan 2010 02:05:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MailChimp and Java Integration &#8211; Part 2</title>
		<link>http://www.othenos.com/2010/01/17/mailchimp-and-java-integration-part-2/</link>
		<comments>http://www.othenos.com/2010/01/17/mailchimp-and-java-integration-part-2/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 19:18:49 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.othenos.com/?p=321</guid>
		<description><![CDATA[
In the previous article, we showed how to connection your Java application to MailChimp using the MailChimp api and the XML-RPC library provided by Zviki Cohen at nWire Software. The code in the article also provided a method to initialize the MailChimp mailing list by using the batch upload function in the api. This article [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.othenos.com/wp-content/uploads/2010/01/smallMailChimp.png"><img alt="" class="alignleft size-full wp-image-311" height="93" src="http://www.othenos.com/wp-content/uploads/2010/01/smallMailChimp.png" title="smallMailChimp" width="87" /></a></p>
<p>In the <a href="http://www.othenos.com/2010/01/15/mailchimp-and-java-integration-part-1/">previous article</a>, we showed how to connection your Java application to MailChimp using the MailChimp api and the XML-RPC library provided by Zviki Cohen at <a href="http://www.nwiresoftware.com" target="_blank">nWire Software</a>. The code in the article also provided a method to initialize the MailChimp mailing list by using the batch upload function in the api. This article will finish off the MailChimpHandler class by adding functionality to add, remove and update records to an existing list.</p>
<p>We are going to make use of two additional MailChimp api calls, <strong>listSubscribe </strong>and <strong>listUnsubscribe</strong>. Here is the completed <strong>MailChimpHandler </strong>class that adds the functionality of adding, removing and updating a record in the MailChimp list.</p>
<p>In a future, I&#39;ll post another article (or two) about how to get MailChimp to notify your Java application when changes are made to the mailing list (such as bounced emails, or a user unsubscribing from the list) so you can keep your contacts database in sync with changes made in the mailing list.</p>
<h5>MailChimpHander Class (Version 2)</h5>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MailChimpHandler <span style="color: #000000; font-weight: bold;">implements</span> IMailChimpHandler <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Log log <span style="color: #339933;">=</span> LogFactory.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// API Key - see http://admin.mailchimp.com/account/api or run login() once</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> apiKey <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;put your api key info here&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// A List Id to run examples against. use lists() to view all</span>
    <span style="color: #666666; font-style: italic;">// Also, login to MC account, go to List, then List Tools, and look for the</span>
    <span style="color: #666666; font-style: italic;">// List ID entry</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> listId <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;put your list ID here&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Instance of the MailChimpServices</span>
    <span style="color: #000000; font-weight: bold;">private</span> IMailChimpServices mcServices <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Flag indicating status of our connection to MailChimp</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> connectionStatus<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Class to get the attributes associated with a company from database</span>
    <span style="color: #000000; font-weight: bold;">private</span> ICompanyAttributesDao companyAttributesDao<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> MailChimpHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        connectionStatus <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * prepare an instance of the MailChimpServices and determine
    * if we can connect using our apiKey.
    */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> initialize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// OMITTED FOR BREVITY. SEE ARTICLE 1</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * test the connection to see if it is still good
    * @return result
    */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> testConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// OMITTED FOR BREVITY. SEE ARTICLE 1</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Performs the initialization of the mail chimp list by adding
     * all the appropriate customer contacts to the list
     * @param companyId (this is optional and only here because my application needs it. Yours almost certainly won&amp;#39;t need it)
     * @return number of successful insertions
     */</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> initializeMailChimpList<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> companyId<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// OMITTED FOR BREVITY. SEE ARTICLE 1</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * add a new contact to the MailChimp List
    * @param contactId - I need this to get the contact info in question. You will use something else.
    * @return result
    */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> addUser<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> contactId<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">boolean</span> listSubscribe <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// This will hold the contact data we are going to add to the mailing list</span>
	<span style="color: #000000; font-weight: bold;">final</span> Map<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span> merges <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Get the new contact from the database. You will use something else to get your contact</span>
	CustomerContact contact <span style="color: #339933;">=</span> customerContactDao.<span style="color: #006633;">getMailChimpCustomerContact</span><span style="color: #009900;">&#40;</span>contactId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// make sure we got it, else return</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>contact <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">return</span> listSubscribe<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// make sure we have had a good connection, otherwise try it again	</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>testConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Attempting to re-connect to MailChimp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		initialize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>         <span style="color: #666666; font-style: italic;">// reinstantiate the MailChimpServices and test connection</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// assuming we could connect, proceed</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>connectedStatus<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// Add the new contact information into the Map</span>
		merges.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;FNAME&quot;</span>, contact.<span style="color: #006633;">getFirstName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		merges.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;LNAME&quot;</span>, contact.<span style="color: #006633;">getLastName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		merges.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;EMAIL&quot;</span>, contact.<span style="color: #006633;">getEmail</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		merges.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;EMAIL_TYPE&quot;</span>, <span style="color: #0000ff;">&quot;html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">// Attempt to add the new contact using the listSubscribe api</span>
                <span style="color: #666666; font-style: italic;">// 5th parameter is normally HTML. We set it here so we can specify 6th parameter</span>
                <span style="color: #666666; font-style: italic;">// 6th parameter is whether double opt-in message is sent (defaults to true)</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
		        listSubscribe <span style="color: #339933;">=</span> mcServices.<span style="color: #006633;">listSubscribe</span><span style="color: #009900;">&#40;</span>apiKey, listId, contact.<span style="color: #006633;">getEmail</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, merges,
					IMailChimpServices.<span style="color: #006633;">EMAIL_TYPE_HTML</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>MailChimpServiceException mcse<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error in adding user to list. &quot;</span> <span style="color: #339933;">+</span> mcse.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			mcse.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;listSubscribe: &quot;</span> <span style="color: #339933;">+</span> listSubscribe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span>
		log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not add user because could not connect to MailChimp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// return the result of our attempt to the caller 	</span>
	<span style="color: #000000; font-weight: bold;">return</span> listSubscribe<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * update an existing contact to MailChimp. This is essentially like the addUser method
    * except we are unsubscribing the original email from the list before adding the new one.
    * In my circumstances, it is not really know what has changed on the contact&amp;#39;s information.
    * It could be the email address. So this is why we unsubscribe the user from the list first
    * using their old email address (which could be the same as the new email address), then we
    * add the user back in. Regardless of what data had changed in the contact, the result is it
    * gets updated in the MailChimp list without creating a duplicate.
    * @param contactId - I need this to get the contact in question
    * @param oldEmail - this tells what the previous email was before the change
    * @return result
    */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> updateUser<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> contactId, <span style="color: #003399;">String</span> oldEmail<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">boolean</span> listSubscribe <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">final</span> Map<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span> merges <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	CustomerContact contact <span style="color: #339933;">=</span> customerContactDao.<span style="color: #006633;">getMailChimpCustomerContact</span><span style="color: #009900;">&#40;</span>contactId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>contact <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000000; font-weight: bold;">return</span> listSubscribe<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// make sure we have had a good connection, otherwise try it again			</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>testConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Attempting to re-connect to MailChimp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		initialize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// make sure we have a connection</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>connectedStatus<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// first UNSUBSCRIBE the original email address</span>
                <span style="color: #666666; font-style: italic;">// the 4th parameter is to delete the existing MailChimp record (defaults false)</span>
                <span style="color: #666666; font-style: italic;">// the 5th parameter is whether to send a goodbye message or not (defaults true)</span>
                <span style="color: #666666; font-style: italic;">// the 6th parameter is whether to send a notification message or not (defaults true)</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			listSubscribe <span style="color: #339933;">=</span> mcServices.<span style="color: #006633;">listUnsubscribe</span><span style="color: #009900;">&#40;</span>apiKey, listId, oldEmail, <span style="color: #000066; font-weight: bold;">true</span>, <span style="color: #000066; font-weight: bold;">false</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>MailChimpServiceException mcse<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error in unsubscribing user from list. &quot;</span> <span style="color: #339933;">+</span> mcse.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			mcse.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #666666; font-style: italic;">// then SUBSCRIBE the new email address</span>
		merges.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;FNAME&quot;</span>, contact.<span style="color: #006633;">getFirstName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		merges.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;LNAME&quot;</span>, contact.<span style="color: #006633;">getLastName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		merges.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;EMAIL&quot;</span>, contact.<span style="color: #006633;">getEmail</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		merges.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;EMAIL_TYPE&quot;</span>, <span style="color: #0000ff;">&quot;html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			listSubscribe <span style="color: #339933;">=</span> mcServices.<span style="color: #006633;">listSubscribe</span><span style="color: #009900;">&#40;</span>apiKey, listId, contact.<span style="color: #006633;">getEmail</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, merges,
					IMailChimpServices.<span style="color: #006633;">EMAIL_TYPE_HTML</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>MailChimpServiceException mcse<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error in adding user to list. &quot;</span> <span style="color: #339933;">+</span> mcse.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			mcse.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;listSubscribe: &quot;</span> <span style="color: #339933;">+</span> listSubscribe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span>
		log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not add user because could not connect to MailChimp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">return</span> listSubscribe<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * delete a contact from MailChimp. 
    * @param oldEmail - previous email address
    * @return result
    */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> deleteUser<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> oldEmail<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">boolean</span> listSubscribe <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>testConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Attempting to re-connect to MailChimp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		initialize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #666666; font-style: italic;">// make sure we have a connection</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>connectedStatus<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// UNSUBSCRIBE the original email address</span>
                <span style="color: #666666; font-style: italic;">// the 4th parameter is to delete the existing MailChimp record (defaults false)</span>
                <span style="color: #666666; font-style: italic;">// the 5th parameter is whether to send a goodbye message or not (defaults true)</span>
                <span style="color: #666666; font-style: italic;">// the 6th parameter is whether to send a notification message or not (defaults true)</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			listSubscribe <span style="color: #339933;">=</span> mcServices.<span style="color: #006633;">listUnsubscribe</span><span style="color: #009900;">&#40;</span>apiKey, listId, oldEmail, <span style="color: #000066; font-weight: bold;">true</span>, <span style="color: #000066; font-weight: bold;">false</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>MailChimpServiceException mcse<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error in unsubscribing user from list. &quot;</span> <span style="color: #339933;">+</span> mcse.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			mcse.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;listSubscribe: &quot;</span> <span style="color: #339933;">+</span> listSubscribe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span>
		log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not remove user because could not connect to MailChimp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">return</span> listSubscribe<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.othenos.com/2010/01/17/mailchimp-and-java-integration-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MailChimp and Java Integration &#8211; Part 1</title>
		<link>http://www.othenos.com/2010/01/15/mailchimp-and-java-integration-part-1/</link>
		<comments>http://www.othenos.com/2010/01/15/mailchimp-and-java-integration-part-1/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 19:21:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[MailChimp]]></category>

		<guid isPermaLink="false">http://www.othenos.com/?p=283</guid>
		<description><![CDATA[MailChimp&#8482; is a great online email marketing site. It is extremely easy to use and provides tools to manage your email lists, build attractive HTML email campaigns, and track how those campaigns performed. On top of that, it offers a free account for smaller companies. How can you beat that? Those of you who are [...]]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://www.othenos.com/wp-content/uploads/2010/01/smallMailChimp.png"><img alt="" class="alignleft size-full wp-image-311" height="93" src="http://www.othenos.com/wp-content/uploads/2010/01/smallMailChimp.png" title="smallMailChimp" width="87" /></a>MailChimp</strong>&trade; is a great online email marketing site. It is extremely easy to use and provides tools to manage your email lists, build attractive HTML email campaigns, and track how those campaigns performed. On top of that, it offers a free account for smaller companies. How can you beat that? Those of you who are Java programmers might want to integrate your application with MailChimp. If so, then this is for you. These two articles are going to show you how to automatically create and update your MailChimp list using Java.</p>
<h4>Assumptions</h4>
<p>It is assumed that you have some kind of an existing list of customers, contacts or other names with email addresses that you have in a database and manage with a Java application. And, of course, you want to be able to populate a MailChimp list with these contact email addresses, as well as keep the list updated when contacts are added, modified or deleted. If either assumption is wrong, then probably these articles are not what you&#39;re looking for! But if their right, then read on. Article 1, will focus on getting everything setup and perform the initial upload of your existing contact information. Article 2, will finish up by creating the methods that will perform the continual additions, modifications and deletions to the list as your contacts change. So let&#39;s get started.</p>
<h3>MailChimp Basics</h3>
<p>OK. MailChimp is pretty straightforward. I am going to let you get familiar with setting up an account and creating a list. They have plenty of help to get you started. So, if you do not already have a MailChimp account, go and create one now, I&#39;ll wait. <em>(Note that this article does not include any details on setting up the MailChimp account or creating your mailing lists, as there are sufficient resources at MailChimp to help with that.)</em> If you have not already done so, review the MailChimp tutorials on setting up an account and creating your first mailing list. Then do both.</p>
<h4>API Key</h4>
<p>Once you have your account, go to the account page (button on upper left of page after sign-in). Click the <strong>API Keys and Info</strong> button. You are going to want to create an API Key for use by your Java application. This key essentially identifies your account and provides access to it. Add a key and then make a mental note of where this information is located, as we will be coming back here later to retrieve it.</p>
<h4>List ID</h4>
<p>You need to create a mailing list to hold the email information you are going to upload. Go ahead and create one using the default values. A mailing list that you create in MailChimp will have a unique alphanumeric identifier. This can be found by selecting a list, then clicking the <strong>list settings</strong> link. Towards the bottom of that page you will find the ID that has been assigned to the list in question. Again, we don&#39;t need it now but will later so make a note of where to find it.</p>
<h3>Java Connections</h3>
<p>The MailChimp api (1.2) provides several protocols for connecting to the MailChimp servers &ndash; XML-RPC, JSON, PHP. We are going to use XML-RPC. Zviki Cohen at <a href="http://www.nwiresoftware.com" target="_blank">nWire Software</a> has provided a nice Java xml-rpc library as a starting point in our integration attempt with MailChimp. Download it from <a href="http://www.mailchimp.com/mint/pepper/orderedlist/downloads/download.php?file=http%3A//www.mailchimp.com/api/downloads/mcapi_java_1.2.zip" target="_blank">here</a>. Read over the MailChimp api to become familiar with the operations that are possible. There are three basic groups of functions &ndash; list management, campaign creation and sending, and campaign statistics. We are only going to be using the list management portion of the api. In this article, we are only interested in testing the connection and doing a batch upload of our data into the MailChimp list. Zviki does not define all the list management functions in the IMailChimpServices interface but he defines enough for our purposes. You can easily add additional functions to the interface as needed.</p>
<h4>Connecting to MailChimp</h4>
<p>The first step in our integration process will be connecting to our account on the MailChimp server. To do so, we need to have our API Key handy.Using the XML-RPC library you just downloaded, we are going to create a class called <strong>MailChimpHandler </strong>which will be our entry point for all interactions between our Java application and the MailChimp server. This class will implement the methods we have defined in the <strong>IMailChimpHandler </strong>interface. Eventually, we are going to be able to initialize the mailing list with our current set of contacts, add records to the mailing list as we add more contacts to our database, update a record in MailChimp when a contact changes in our database, and delete a record from MailChimp when deleting a contact from the database. But for right now, we just want to insure we can create the MailChimpServices and connect to MailChimp.</p>
<h5>MailChimpHandler interface</h5>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.othenos.mailchimp</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> IMailChimpHandler <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> initializeMailChimpList<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> companyId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> addUser<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> contactId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> updateUser<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> contactId, <span style="color: #003399;">String</span> oldEmail<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> deleteUser<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> oldEmail<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h5>MailChimpHandler class (version 1).</h5>
<p>This version simply tests to insure that we can make a connection to MailChimp using the provided API key and List ID. After instantiating an instance of MailChimpHandler, we can call the <strong><em>initializeMailChimpList </em></strong>method to upload our list of contacts. The method will insure that the mcServices is created and will test our connection before uploading. The code is well commented, so please read though it carefully, as this is all the explanation that is provided. I hope it is enough. Once you get this working OK, then you can move on to the <a href="http://www.othenos.com/2010/01/17/mailchimp-and-java-integration-part-2/">next article</a> to see how to add to, delete from and modify the list.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MailChimpHandler <span style="color: #000000; font-weight: bold;">implements</span> IMailChimpHandler <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Log log <span style="color: #339933;">=</span> LogFactory.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// API Key - see http://admin.mailchimp.com/account/api or run login() once</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> apiKey <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;put your api key info here&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// A List Id to run examples against. use lists() to view all</span>
    <span style="color: #666666; font-style: italic;">// Also, login to MC account, go to List, then List Tools, and look for the</span>
    <span style="color: #666666; font-style: italic;">// List ID entry</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> listId <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;put your list ID here&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Instance of the MailChimpServices</span>
    <span style="color: #000000; font-weight: bold;">private</span> IMailChimpServices mcServices <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Flag indicating status of our connection to MailChimp</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> connectionStatus<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Class to get the attributes associated with a company from database</span>
    <span style="color: #000000; font-weight: bold;">private</span> ICompanyAttributesDao companyAttributesDao<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> MailChimpHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        connectionStatus <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * prepare an instance of the MailChimpServices and determine
    * if we can connect using our apiKey.
    */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> initialize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        mcServices <span style="color: #339933;">=</span> MailChimpServiceFactory.<span style="color: #006633;">getMailChimpServices</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> ping <span style="color: #339933;">=</span> mcServices.<span style="color: #006633;">ping</span><span style="color: #009900;">&#40;</span>apiKey<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>IMailChimpServices.<span style="color: #006633;">PING_SUCCESS</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>ping<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MailChimp connection pinged successfully&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            connectionStatus <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Failed to ping MailChimp, response: &quot;</span> <span style="color: #339933;">+</span> ping<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * test the connection to see if it is still good
    * @return result
    */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> testConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>mcServices <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">return</span> IMailChimpServices.<span style="color: #006633;">PING_SUCCESS</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>mcServices.<span style="color: #006633;">ping</span><span style="color: #009900;">&#40;</span>apiKey<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Performs the initialization of the mail chimp list by adding
     * all the appropriate customer contacts to the list
     * @param companyId (this is optional and only here because my application needs it. Yours almost certainly won&amp;#39;t need it)
     * @return number of successful insertions
     */</span>
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> initializeMailChimpList<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> companyId<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #000066; font-weight: bold;">int</span> listSubscribe <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
         <span style="color: #000066; font-weight: bold;">int</span> listFailure <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #666666; font-style: italic;">// insure we can connect to the server, if not then re-initialize</span>
         <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>testConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
             log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Attempting to re-connect to MailChimp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
             initialize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #009900;">&#125;</span>
&nbsp;
         <span style="color: #666666; font-style: italic;">// if we have confirmed we can connect OK, proceed</span>
         <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>connectedStatus<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
             <span style="color: #666666; font-style: italic;">// This is my way of getting all the contacts I want to add to the list</span>
             <span style="color: #666666; font-style: italic;">// You will come up with your own way of getting your contacts</span>
             List<span style="color: #339933;">&lt;</span>CustomerContact<span style="color: #339933;">&gt;</span> contacts <span style="color: #339933;">=</span> customerContactDao.<span style="color: #006633;">getMailChimpCustomerContacts</span><span style="color: #009900;">&#40;</span>companyId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
             <span style="color: #666666; font-style: italic;">// Instantiate an ArrayList to hold all the contact info to send</span>
             <span style="color: #666666; font-style: italic;">// ArrayList seems to be the preferred data structure for sending and receiving array information</span>
             List<span style="color: #339933;">&lt;</span>Map<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;&gt;</span> chimps <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Map<span style="color: #339933;">&lt;</span>String,String<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
             <span style="color: #666666; font-style: italic;">// For each of our contacts, build a HashMap of MailChimp tags and add it to the list</span>
             <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>CustomerContact contact <span style="color: #339933;">:</span> contacts<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                 Map<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span> chimp <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 chimp.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;FNAME&quot;</span>, contact.<span style="color: #006633;">getFirstName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 chimp.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;LNAME&quot;</span>, contact.<span style="color: #006633;">getLastName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 chimp.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;EMAIL&quot;</span>, contact.<span style="color: #006633;">getEmail</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 chimp.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;EMAIL_TYPE&quot;</span>, <span style="color: #0000ff;">&quot;html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>             <span style="color: #666666; font-style: italic;">// define the mail type to be HTML</span>
                 chimps.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>chimp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
             <span style="color: #009900;">&#125;</span>
             <span style="color: #666666; font-style: italic;">// Now that we have the ArrayList populated, attempt to send it to MailChimp</span>
             <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                 <span style="color: #666666; font-style: italic;">// attempt a batch update of the list using mcServices listBatchSubscribe method</span>
                 <span style="color: #666666; font-style: italic;">// the last 3 arguments have to do with double-optin, updating existing and replacing interests. All false for us.</span>
                 HashMap<span style="color: #339933;">&lt;</span>String, Object<span style="color: #339933;">&gt;</span> results <span style="color: #339933;">=</span> mcServices.<span style="color: #006633;">listBatchSubscribe</span><span style="color: #009900;">&#40;</span>apiKey, listId, chimps, <span style="color: #000066; font-weight: bold;">false</span>, <span style="color: #000066; font-weight: bold;">false</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 <span style="color: #666666; font-style: italic;">// the results come back as a HashMap with three entries, success_count, error_count and errors</span>
                 listSubscribe <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span>results.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;success_count&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 listFailure <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span>results.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error_count&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Mail Chimp results : &quot;</span> <span style="color: #339933;">+</span> listSubscribe <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; inserted and &quot;</span> <span style="color: #339933;">+</span> listFailure <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; errors.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 <span style="color: #666666; font-style: italic;">// interpret the results to see what went good and bad</span>
                 <span style="color: #666666; font-style: italic;">// The errors comes back as an array of Objects</span>
                 <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> errors <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>results.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;errors&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 createLogFile<span style="color: #009900;">&#40;</span>errors, companyId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 <span style="color: #666666; font-style: italic;">// and update the good contacts to indicate they have been subscribed</span>
                 <span style="color: #666666; font-style: italic;">// We have opted to update our contact list to indicate that the contacts got subscribe OK</span>
                 <span style="color: #666666; font-style: italic;">// This is certainly optional</span>
                 customerContactDao.<span style="color: #006633;">markContactsSubscribed</span><span style="color: #009900;">&#40;</span>companyId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 <span style="color: #666666; font-style: italic;">// be sure and mark contacts that have errors as not subscribed</span>
                 <span style="color: #666666; font-style: italic;">// Again, we have opted to mark our contacts that failed to get subscribed</span>
                 <span style="color: #666666; font-style: italic;">// This is optional as well</span>
                 <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> item <span style="color: #339933;">:</span> errors<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                     HashMap<span style="color: #339933;">&lt;</span>String,Object<span style="color: #339933;">&gt;</span> result <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HashMap<span style="color: #339933;">&lt;</span>String,Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span>item<span style="color: #339933;">;</span>
                     HashMap<span style="color: #339933;">&lt;</span>String,Object<span style="color: #339933;">&gt;</span> row <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>HashMap<span style="color: #339933;">&lt;</span>String,Object<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span>result.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;row&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                     <span style="color: #003399;">String</span> fName <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>row.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;FNAME&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                     <span style="color: #003399;">String</span> lName <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>row.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;LNAME&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                     <span style="color: #003399;">String</span> email <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>row.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;EMAIL&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                     customerContactDao.<span style="color: #006633;">markContactsNotSubscribed</span><span style="color: #009900;">&#40;</span>fName, lName, email<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 <span style="color: #009900;">&#125;</span>
&nbsp;
             <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>MailChimpServiceException mcse<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                 log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error in adding user to list. &quot;</span> <span style="color: #339933;">+</span> mcse.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 mcse.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
             <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                 log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error in adding user to list. &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              <span style="color: #009900;">&#125;</span>
         <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">else</span>
             log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not add user because could not connect to MailChimp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
         <span style="color: #000000; font-weight: bold;">return</span> listSubscribe<span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * add a new contact to MailChimp
    * @param contactId
    * return result
    */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> addUser<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> contactId<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// TODO Complete</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * update an existing contact to MailChimp
    * @param contactId
    * @param oldEmail
    * return result
    */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> updateUser<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> contactId, <span style="color: #003399;">String</span> oldEmail<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// TODO Complete</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
    * delete a contact from MailChimp
    * @param oldEmail
    * return result
    */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> deleteUser<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> oldEmail<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// TODO Complete</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.othenos.com/2010/01/15/mailchimp-and-java-integration-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Styling DataGrid rows with an Item Renderer</title>
		<link>http://www.othenos.com/2009/09/06/styling-datagrid-rows-with-item-renderer/</link>
		<comments>http://www.othenos.com/2009/09/06/styling-datagrid-rows-with-item-renderer/#comments</comments>
		<pubDate>Sun, 06 Sep 2009 23:55:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Adobe Flex]]></category>

		<guid isPermaLink="false">http://www.othenos.com/?p=208</guid>
		<description><![CDATA[I had a need to bold the data in only one row of a Flex DataGrid. There were some solutions posted on-line (especially ninjacaptain) but honestly I could not get them to work the way I wanted. So, using their examples as a starting place, I created an extremely simple ItemRenderer which would bold the [...]]]></description>
			<content:encoded><![CDATA[<p>I had a need to <strong>bold</strong> the data in only one row of a Flex DataGrid. There were some solutions posted on-line (especially <a href="http://blog.ninjacaptain.com/2008/06/make-a-datagrid-row-bold/" target="_blank">ninjacaptain</a>) but honestly I could not get them to work the way I wanted. So, using their examples as a starting place, I created an extremely simple ItemRenderer which would <strong>bold </strong>the row desired.</p>
<p>The key to getting it to work is to have some property in your dataProvider which you can use to determine whether to <strong>bold</strong> or not. In the example, I actually added an additional property called &#8216;fontWeight&#8217; and set the value to either &#8216;bold&#8217; or &#8216;normal&#8217; depending on the results I wanted.</p>
<p>You can potentially key off of any existing data element from your dataProvider, such as a property having a value greater or equal to some other value. Basically, whatever that is available to make the determination to <strong>bold </strong>or not.</p>
<p>Lastly, instead of fontWeight, you could set any of the additional style properties available for the DataGridItemRender class.</p>
<p>Here is the <a href="http://www.othenos.com/StyledDataGridRow.html" target="_blank">example </a>with the source code.</p>
<p>Here is the code for the itemRenderer.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com.othenos.renderer <span style="color: #000000;">&#123;</span>
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.dataGridClasses.DataGridColumn;
  <span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.dataGridClasses.DataGridItemRenderer;
&nbsp;
  <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> MyDataGridItemRenderer extends DataGridItemRenderer <span style="color: #000000;">&#123;</span>
   <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> MyDataGridItemRenderer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
&nbsp;
    override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">set</span> <span style="color: #004993;">data</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0033ff; font-weight: bold;">super</span>.<span style="color: #004993;">data</span>=<span style="color: #004993;">value</span>;
      <span style="color: #3f5fbf;">/**
       * First insure the value is non-null and not a DataGridItem type but of the type that you are expecting
       * as the value. In this case, it is an Object.
       **/</span>
       <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">!</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span> <span style="color: #0033ff; font-weight: bold;">is</span> DataGridColumn<span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #004993;">value</span> <span style="color: #0033ff; font-weight: bold;">is</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span>
         <span style="color: #3f5fbf;">/**
          * In the example, there is a property on each value that is
          * named fontWeight and has the value of either 'normal' or 'bold'.
          *
          * Of course, you could do this in many different ways. For example,
          * you may want to bold the line only when a certain property equals
          * a certain value. Or when the property exceeds a certain value. In these
          * cases, you may or may not have multiple lines of the grid which are 'bold'.
          *
          * This example sets the fontWeight style. You could, of course, set one or many of the style
          * properties that are available to the DataGridItemRenderer. The result would apply to the
          * DataGrid row associated with this value.
          **/</span>
          <span style="color: #004993;">setStyle</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'fontWeight'</span>, <span style="color: #004993;">value</span>.fontWeight<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
      <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.othenos.com/2009/09/06/styling-datagrid-rows-with-item-renderer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DataGrid Footer</title>
		<link>http://www.othenos.com/2009/08/27/datagrid-footer/</link>
		<comments>http://www.othenos.com/2009/08/27/datagrid-footer/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 16:28:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Adobe Flex]]></category>

		<guid isPermaLink="false">http://www.othenos.com/?p=204</guid>
		<description><![CDATA[I was enjoying the enhancements made to the DataGrid control I found on Doug Marttila&#8217;s site, who in turn was working from an original update made by Alex Harui. However, I found that the footer did not work correctly when you make columns visible/invisible, especially horizontal scrolling. There was also a problem of vertical scrolling [...]]]></description>
			<content:encoded><![CDATA[<p>I was enjoying the enhancements made to the DataGrid control I found on <a href="http://www.forestandthetrees.com/2008/06/16/data-grid-and-adg-footers/" target="_blank">Doug Marttila&#8217;s site</a>, who in turn was working from an original update made by <a href="http://blogs.adobe.com/aharui/2008/03/flex_3_datagrid_footers.html" target="_blank">Alex Harui</a>. However, I found that the footer did not work correctly when you make columns visible/invisible, especially horizontal scrolling. There was also a problem of vertical scrolling and the regular row data partially showing up in the footer area.</p>
<p>So the changes I made were to address those issues. Included are the ability to dynamically turn the footer on or off (make visible or invisible) and to change the background color of the footer on the fly.</p>
<p>If anyone comes up with a way to change the footer&#8217;s font color, I&#8217;d like to know how.</p>
<p>Here is a <a href="http://www.othenos.com/testdgfooter.html" target="_blank">link </a>to the demo. You can right-click and get the source.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.othenos.com/2009/08/27/datagrid-footer/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
