childCampaign.setIsRecurrent(false);
childCampaign.setLocked(true);
HibernateUtil.saveOrUpdate(childCampaign);
MailingList ml = (MailingList) InodeFactory.getChildOfClass(c, MailingList.class);
HTMLPage page = (HTMLPage) InodeFactory.getChildOfClass(c, HTMLPage.class);
Communication comm = (Communication) InodeFactory.getChildOfClass(c, Communication.class);
UserFilter userfilter = (UserFilter) InodeFactory.getChildOfClass(c, UserFilter.class);
if(InodeUtils.isSet(ml.getInode())){
relationshipAPI.addRelationship(childCampaign.getInode(), ml.getInode(), "child");
}
if(InodeUtils.isSet(page.getInode())){
relationshipAPI.addRelationship(childCampaign.getInode(), page.getInode(), "child");
}
if (InodeUtils.isSet(comm.getInode())) {
relationshipAPI.addRelationship(childCampaign.getInode(), comm.getInode(), "child");
}
if (InodeUtils.isSet(userfilter.getInode())) {
relationshipAPI.addRelationship(childCampaign.getInode(), userfilter.getInode(), "child");
}
HibernateUtil.saveOrUpdate(childCampaign);
campaigns.add(childCampaign);
c.setLocked(false);
}
catch (Exception ex) {
Logger.debug(this, ex.getMessage());
}
}
}
else {
//get a list of waiting queues
campaigns = CampaignFactory.getWaitingCampaigns();
}
StringBuffer message = null;
if (campaigns.size() == 0)
{
campaigns = null;
return;
}
try {
Logger.debug(DeliverCampaignThread.class, "GOING to deliver campaigns");
Iterator<Campaign> campIter = campaigns.iterator();
//### LOOP THE CAMPAIGNS ###
while (campIter.hasNext())
{
//Obtain the campaign
Campaign c = (Campaign) campIter.next();
Communication comm = (Communication) InodeFactory.getChildOfClass(c, Communication.class);
if ((comm == null) || (!InodeUtils.isSet(comm.getInode()))) {
Logger.info(DeliverCampaignThread.class, "I didn't find a communication for campaign inode=" + c.getInode());
c.setCompletedDate(new java.util.Date());
c.setLocked(false);
message = null;
HibernateUtil.saveOrUpdate(c);
continue;
}
Logger.debug(DeliverCampaignThread.class, "got campaign:" + c.getTitle());
//Mailing list
String campaingSendTo = c.getSendTo();
MailingList ml = null;
UserFilter uf = null;
List<UserProxy> subscribers = null;
if ((campaingSendTo != null) && campaingSendTo.equalsIgnoreCase("mailingList")) {
ml = (MailingList) InodeFactory.getChildOfClass(c,MailingList.class);
if (!InodeUtils.isSet(ml.getInode()))
{
Logger.info(DeliverCampaignThread.class, "I didn't find a mailing list for campaign inode=" + c.getInode());
c.setCompletedDate(new java.util.Date());
c.setLocked(false);
message = null;
HibernateUtil.saveOrUpdate(c);
continue;
}
else
{
Logger.debug(DeliverCampaignThread.class, "got mailingList:" + ml.getTitle());
//Get the subscribers
subscribers = MailingListFactory.getMailingListSubscribers(ml);
Logger.debug(DeliverCampaignThread.class, "Got subscribers:" + subscribers.size());
}
}
else if ((campaingSendTo != null) && campaingSendTo.equalsIgnoreCase("userFilter")) {
uf = (UserFilter) InodeFactory.getChildOfClass(c,UserFilter.class);
if (!InodeUtils.isSet(uf.getInode()))
{
Logger.info(DeliverCampaignThread.class, "I didn't find an user filter for campaign inode=" + c.getInode());
c.setCompletedDate(new java.util.Date());
c.setLocked(false);
message = null;
HibernateUtil.saveOrUpdate(c);
continue;
}
else
{
Logger.debug(DeliverCampaignThread.class, "got user filter:" + uf.getUserFilterTitle());
//Get the subscribers
try {
subscribers = UserFilterFactory.getUserProxiesFromFilter(uf);
}
catch (Exception e) {
Logger.info(DeliverCampaignThread.class, "Error getting subscriber from user filter for campaign inode=" + c.getInode());
c.setCompletedDate(new java.util.Date());
c.setLocked(false);
message = null;
HibernateUtil.saveOrUpdate(c);
continue;
}
Logger.debug(DeliverCampaignThread.class, "Got subscribers:" + subscribers.size());
}
}
//Unsubscriber mailing list
MailingList unSubscribers = MailingListFactory.getUnsubscribersMailingList();
//do we have an html page?
String alternateTextMessage = null;
HTMLPageAPI pageAPI = APILocator.getHTMLPageAPI();
HTMLPage htmlPage = (HTMLPage) pageAPI.loadWorkingPageById(comm.getHtmlPage(), APILocator.getUserAPI().getSystemUser(), false);
String serverName;
if (htmlPage != null && UtilMethods.isSet(htmlPage.getTitle())) {
html = true;
Logger.debug(DeliverCampaignThread.class, "Got htmlPage:"+ htmlPage.getTitle());
// get the newsletter and the attachments
Identifier id = APILocator.getIdentifierAPI().find(htmlPage);
serverName = APILocator.getHostAPI().find(id.getHostId(), APILocator.getUserAPI().getSystemUser(), false).getHostname();
//rewrite the urls
try {
Logger.debug(DeliverCampaignThread.class, "Retrieving page from url " + "http://"+ serverName + UtilMethods.encodeURIComponent(id.getURI()));
message = new StringBuffer(UtilMethods.escapeUnicodeCharsForHTML(UtilMethods.getURL("http://"+ serverName + UtilMethods.encodeURIComponent(id.getURI())).toString()));
Logger.debug(DeliverCampaignThread.class, "Page retrieved " + message);
message = EmailFactory.alterBodyHTML(message, serverName);
Logger.debug(DeliverCampaignThread.class, "Page altered " + message);
alternateTextMessage = "If you are having trouble reading this message, click here: "+ "http://"+ serverName + UtilMethods.encodeURIComponent(id.getURI());
}catch(Exception e){
/**
* This condition was included to avoid send
* campaigns without content
* */
Logger.info(DeliverCampaignThread.class, "Error generating message for campaign inode=" + c.getInode() + " and htmlPage inode=" + htmlPage.getInode());
c.setCompletedDate(new java.util.Date());
c.setLocked(false);
message = null;
HibernateUtil.saveOrUpdate(c);