Skip to content
Permalink
e721e0e84f
Go to file
 
 
Cannot retrieve contributors at this time
343 lines (296 sloc) 12.9 KB
package com.constantcontact.services.activities;
import com.constantcontact.components.Component;
import com.constantcontact.components.activities.contacts.request.AddContactsRequest;
import com.constantcontact.components.activities.contacts.request.ClearListsRequest;
import com.constantcontact.components.activities.contacts.request.ExportContactsRequest;
import com.constantcontact.components.activities.contacts.request.RemoveContactsRequest;
import com.constantcontact.components.activities.contacts.response.ContactsResponse;
import com.constantcontact.components.activities.contacts.response.DetailedStatusReport;
import com.constantcontact.components.activities.contacts.response.SummaryReport;
import com.constantcontact.components.activities.contacts.types.BulkActivityStatus;
import com.constantcontact.components.activities.contacts.types.BulkActivityType;
import com.constantcontact.exceptions.service.ConstantContactServiceException;
import com.constantcontact.services.base.BaseService;
import com.constantcontact.util.RawApiResponse;
import com.constantcontact.util.Config;
import com.constantcontact.util.ConstantContactExceptionFactory;
import com.constantcontact.util.http.MultipartBody;
import java.util.List;
/**
* Service Layer Implementation for the Bulk Activities in Constant Contact.
*
* @author ConstantContact
*
*/
public class BulkActivitiesService extends BaseService implements IBulkActivitiesService {
private String accessToken;
private String apiKey;
/**
* @return the accessToken
*/
public String getAccessToken() {
return accessToken;
}
/**
* @param accessToken the accessToken to set
*/
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
/**
* @return the apiKey
*/
public String getApiKey() {
return apiKey;
}
/**
* @param apiKey the apiKey to set
*/
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
/**
* Add a large number of contacts in a single batch operation.<br>
* Implements the bulk add Contacts operation by calling the ConstantContact server side.
*
* @param request The request
* @return A response containing the values returned from the server for the requested operation on success; <br/>
* An exception is thrown otherwise.
* @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server.
*/
public ContactsResponse addContacts(AddContactsRequest request) throws ConstantContactServiceException {
System.out.println("Finish");
if (request == null) {
throw new IllegalArgumentException(Config.instance().getErrorBulkContactsRequestNull());
}
ContactsResponse contactsResponse = null;
try {
String url = Config.instance().getBaseUrl() + Config.instance().getActivitiesAddContacts();
String json = request.toJSON();
RawApiResponse response = getRestClient().post(url, json);
if (response.hasData()) {
contactsResponse = Component.fromJSON(response.getBody(), ContactsResponse.class);
}
if (response.isError()) {
throw ConstantContactExceptionFactory.createServiceException(response, url);
}
} catch (ConstantContactServiceException e) {
throw new ConstantContactServiceException(e);
} catch (Exception e) {
throw new ConstantContactServiceException(e);
}
return contactsResponse;
}
public ContactsResponse addContacts(MultipartBody multipartRequest)
throws ConstantContactServiceException {
if (multipartRequest == null) {
throw new IllegalArgumentException(Config.instance().getErrorBulkContactsRequestNull());
}
ContactsResponse contactsResponse = null;
try {
String url = Config.instance().getBaseUrl() + Config.instance().getActivitiesAddContacts();
RawApiResponse response = getRestClient().postMultipart(url, multipartRequest);
if (response.hasData()) {
contactsResponse = Component.fromJSON(response.getBody(), ContactsResponse.class);
}
if (response.isError()) {
throw ConstantContactExceptionFactory.createServiceException(response, url);
}
} catch (Exception e) {
throw new ConstantContactServiceException(e);
}
return contactsResponse;
}
/**
* Implements the bulk remove Contacts From Lists operation by calling the ConstantContact server side.
*
* @param request The request
* @return A response containing the values returned from the server for the requested operation on success; <br/>
* An exception is thrown otherwise.
* @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server.
*/
public ContactsResponse removeContactsFromLists(RemoveContactsRequest request) throws ConstantContactServiceException {
if (request == null) {
throw new IllegalArgumentException(Config.instance().getErrorBulkContactsRequestNull());
}
ContactsResponse contactsResponse = null;
try {
String url = Config.instance().getBaseUrl() + Config.instance().getActivitiesRemoveFromLists();
String json = request.toJSON();
RawApiResponse response = getRestClient().post(url, json);
if (response.hasData()) {
contactsResponse = Component.fromJSON(response.getBody(), ContactsResponse.class);
}
if (response.isError()) {
throw ConstantContactExceptionFactory.createServiceException(response, url);
}
} catch (ConstantContactServiceException e) {
throw new ConstantContactServiceException(e);
} catch (Exception e) {
throw new ConstantContactServiceException(e);
}
return contactsResponse;
}
public ContactsResponse removeContactsFromLists(MultipartBody multipartRequest)
throws ConstantContactServiceException {
if (multipartRequest == null) {
throw new IllegalArgumentException(Config.instance().getErrorBulkContactsRequestNull());
}
ContactsResponse contactsResponse = null;
try {
String url = Config.instance().getBaseUrl() + Config.instance().getActivitiesRemoveFromLists();
RawApiResponse response = getRestClient().postMultipart(url, multipartRequest);
if (response.hasData()) {
contactsResponse = Component.fromJSON(response.getBody(), ContactsResponse.class);
}
if (response.isError()) {
throw ConstantContactExceptionFactory.createServiceException(response, url);
}
}
catch (ConstantContactServiceException e) {
throw new ConstantContactServiceException(e);
}
catch (Exception e) {
throw new ConstantContactServiceException(e);
}
return contactsResponse;
}
/**
* Implements the bulk clear Lists operation by calling the ConstantContact server side.
*
* @param request The request
* @return A response containing the values returned from the server for the requested operation on success; <br/>
* An exception is thrown otherwise.
* @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server.
*/
public ContactsResponse clearLists(ClearListsRequest request) throws ConstantContactServiceException {
if (request == null) {
throw new IllegalArgumentException(Config.instance().getErrorBulkContactsRequestNull());
}
ContactsResponse contactsResponse = null;
try {
String url = Config.instance().getBaseUrl() + Config.instance().getActivitiesClearLists();
String json = request.toJSON();
RawApiResponse response = getRestClient().post(url, json);
if (response.hasData()) {
contactsResponse = Component.fromJSON(response.getBody(), ContactsResponse.class);
}
if (response.isError()) {
throw ConstantContactExceptionFactory.createServiceException(response, url);
}
} catch (ConstantContactServiceException e) {
throw new ConstantContactServiceException(e);
} catch (Exception e) {
throw new ConstantContactServiceException(e);
}
return contactsResponse;
}
/**
* Implements the bulk export Contacts operation by calling the ConstantContact server side.
*
* @param request The request
* @return A response containing the values returned from the server for the requested operation on success; <br/>
* An exception is thrown otherwise.
* @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server.
*/
public ContactsResponse exportContacts(ExportContactsRequest request) throws ConstantContactServiceException {
if (request == null) {
throw new IllegalArgumentException(Config.instance().getErrorBulkContactsRequestNull());
}
ContactsResponse contactsResponse = null;
try {
String url = Config.instance().getBaseUrl() + Config.instance().getActivitiesExportContacts();
String json = request.toJSON();
RawApiResponse response = getRestClient().post(url, json);
if (response.hasData()) {
contactsResponse = Component.fromJSON(response.getBody(), ContactsResponse.class);
}
if (response.isError()) {
throw ConstantContactExceptionFactory.createServiceException(response, url);
}
} catch (ConstantContactServiceException e) {
throw new ConstantContactServiceException(e);
} catch (Exception e) {
throw new ConstantContactServiceException(e);
}
return contactsResponse;
}
/**
* Implements the bulk get Summary Report operation by calling the ConstantContact server side.
*
* @return A response containing the values returned from the server for the requested operation on success; <br/>
* An exception is thrown otherwise.
* @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server.
*/
public List<SummaryReport> getSummaryReport() throws ConstantContactServiceException {
return this.getSummaryReport(null,null);
}
/**
* Implements the bulk get Summary Report operation by calling the ConstantContact server side.
*
* @param status The status, as seen in {@link com.constantcontact.components.activities.contacts.types.BulkActivityStatus}
* @param type The type, as seen in {@link com.constantcontact.components.activities.contacts.types.BulkActivityType}
* @return A response containing the values returned from the server for the requested operation on success; <br/>
* An exception is thrown otherwise.
* @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server.
*/
public List<SummaryReport> getSummaryReport(String status, String type) throws ConstantContactServiceException {
List<SummaryReport> activitiesResponse = null;
try {
String url = Config.instance().getBaseUrl() + Config.instance().getActivities();
if (status != null && status.length() > 0) {
url = appendParam(url, "status", status);
}
if (type != null && type.length() > 0) {
url = appendParam(url, "type", type);
}
RawApiResponse response = getRestClient().get(url);
if (response.hasData()) {
activitiesResponse = Component.listFromJSON(response.getBody(), SummaryReport.class);
}
if (response.isError()) {
throw ConstantContactExceptionFactory.createServiceException(response, url);
}
} catch (ConstantContactServiceException e) {
throw new ConstantContactServiceException(e);
} catch (Exception e) {
throw new ConstantContactServiceException(e);
}
return activitiesResponse;
}
/**
* Implements the bulk get Detailed Status Report operation by calling the ConstantContact server side.
*
* @param id The id
* @return A response containing the values returned from the server for the requested operation on success; <br/>
* An exception is thrown otherwise.
* @throws ConstantContactServiceException When something went wrong in the Constant Contact flow or an error is returned from server.
*/
public DetailedStatusReport getDetailedStatusReport(String id) throws
ConstantContactServiceException {
DetailedStatusReport statusReport = null;
String url = Config.instance().getBaseUrl() + String.format(Config.instance().getActivity(),id);
try {
RawApiResponse response = getRestClient().get(url);
if (response.hasData()) {
statusReport = Component.fromJSON(response.getBody(), DetailedStatusReport.class);
}
if (response.isError()) {
throw ConstantContactExceptionFactory.createServiceException(response, url);
}
} catch (ConstantContactServiceException e) {
throw new ConstantContactServiceException(e);
} catch (Exception e) {
throw new ConstantContactServiceException(e);
}
return statusReport;
}
/**
* Default constructor.
*/
public BulkActivitiesService(String accessToken, String apiKey) {
super(accessToken, apiKey);
this.setAccessToken(accessToken);
this.setApiKey(apiKey);
}
}
You can’t perform that action at this time.