{
try {
InitialContext ic = CVUtility.getInitialContext();
TicketLocalHome ticketHome = (TicketLocalHome)ic.lookup("local/Ticket");
TicketLocal ticketRemote = (TicketLocal) ticketHome.create();
ticketRemote.setDataSource(this.dataSource);
// Used for getting the Default Owner ID.
AppSettingsLocalHome appHome=(AppSettingsLocalHome)ic.lookup("local/AppSettings");
AppSettingsLocal appRemote=(AppSettingsLocal)appHome.create();
appRemote.setDataSource(this.dataSource);
String strAssignedID = appRemote.getApplicationSettings("DEFAULTOWNER");
appRemote = null;
appHome = null;
// Used to get the Entity information when passing the EmailAddress
ContactHelperLocalHome contactHelperHome = (ContactHelperLocalHome)ic.lookup("local/ContactHelper");
ContactHelperLocal contactHelperRemote = contactHelperHome.create();
contactHelperRemote.setDataSource(this.dataSource);
// Setting the Record Permission for newly Created record.
AuthorizationLocalHome authHome=(AuthorizationLocalHome)ic.lookup("local/Authorization");
AuthorizationLocal authRemote=(AuthorizationLocal)authHome.create();
authRemote.setDataSource(this.dataSource);
// get the Default Error Message Template for the Support replying emails
EmailSettingsLocalHome emailSettingsHome=(EmailSettingsLocalHome)ic.lookup("local/EmailSettings");
EmailSettingsLocal emailSettingsRemote=(EmailSettingsLocal)emailSettingsHome.create();
emailSettingsRemote.setDataSource(this.dataSource);
// Its a predefined Template for the replying message for the newly created Ticket
EmailTemplateForm ticketTemplateForm = emailSettingsRemote.getEmailTemplate(AdministrationConstantKeys.EMAIL_TEMPLATE_SUPPORTTICKET);
// Its a predefined Template for the replying message for the new created Thread
EmailTemplateForm threadTemplateForm = emailSettingsRemote.getEmailTemplate(AdministrationConstantKeys.EMAIL_TEMPLATE_SUPPORTTHREAD);
// Its a predefined Template for the replying for Error Message
EmailTemplateForm errorTemplateForm = emailSettingsRemote.getEmailTemplate(AdministrationConstantKeys.EMAIL_TEMPLATE_SUPPORTERROR);
emailSettingsRemote = null;
emailSettingsHome = null;
String defaultMessageBody = "";
for (int i=0; i < messageIDs.size() ; i++) {
int messageID = ((Integer) messageIDs.get(i)).intValue();
if (messageID != -1) {
MailMessageVO mailMessageVO = this.getEmailMessageVO(ownerID, messageID);
String messageFrom = mailMessageVO.getFromAddress();
InternetAddress emailAddress = new InternetAddress(messageFrom);
messageFrom = emailAddress.getAddress();
String messageSubject = mailMessageVO.getSubject();
String messageBody = mailMessageVO.getBody();
Html2Text plainMessageBody = new Html2Text(messageBody);
int accountID = mailMessageVO.getEmailAccountID();
int ticketID = 0;
int startIndex = messageSubject.indexOf("TICKET#");
int endIndex = -1;
if (startIndex >0) {
endIndex = messageSubject.indexOf("#",(startIndex + 7));
}
if (startIndex >0 && endIndex > startIndex) {
startIndex = startIndex + 7;
String tempTicketID =messageSubject.substring(startIndex,endIndex);
if (tempTicketID != null && !tempTicketID.equals("")) {
ticketID = Integer.parseInt(tempTicketID);
}
}
if (ticketID == 0) {
TicketVO ticketVO = new TicketVO();
ticketVO.setTitle(messageSubject);
ticketVO.setDetail(plainMessageBody.getPlainText());
ticketVO.setPriorityId(1);
ticketVO.setStatusId(1);
ticketVO.setCreatedBy(ownerID);
HashMap contactInfo = contactHelperRemote.getContactInfoForEmailAddress(messageFrom);
int assignedId = 0;
int entityId = 0;
int individualId = 0;
if (strAssignedID != null && !strAssignedID.equals("")) {
try {
assignedId = Integer.parseInt(strAssignedID);
} catch(NumberFormatException nfe){
// already set a default value
}
}
if (contactInfo != null && contactInfo.size() != 0) {
entityId = ((Number) contactInfo.get("EntityID")).intValue();
individualId = ((Number) contactInfo.get("IndividualID")).intValue();
}else{
// initialize thread vo
ThreadVO tVO = new ThreadVO();
tVO.setTitle(messageFrom+": "+messageSubject);
tVO.setDetail("");
tVO.setPriorityId(1);
tVO.setCreatedBy(ownerID);
tVO.setOwner(ownerID);
ticketVO.setThreadVO(tVO);
} // end of else if (contactInfo != null && contactInfo.size() != 0)
ticketVO.setAssignedToId(assignedId);
ticketVO.setRefEntityId(entityId);
ticketVO.setRefIndividualId(individualId);
defaultMessageBody = ticketTemplateForm.getBody();
String ticketSubject = ticketTemplateForm.getSubject();
try {
ticketID = ticketRemote.addTicket(ownerID, ticketVO);
}catch(Exception e){
defaultMessageBody = errorTemplateForm.getBody();
}// end of catch block
messageSubject = ticketSubject+"-["+messageSubject+"] TICKET#"+ticketID+"#";
int arrBlank[] = {};
authRemote.saveRecordPermission(ownerID,-1,"Ticket",ticketID,arrBlank,arrBlank,arrBlank);
}else{
// initialize thread vo
ThreadVO tVO = new ThreadVO();
tVO.setTitle(messageSubject);
tVO.setDetail(plainMessageBody.getPlainText());
tVO.setTicketId(ticketID);
tVO.setPriorityId(1);
tVO.setCreatedBy(ownerID);
tVO.setOwner(ownerID);
defaultMessageBody = threadTemplateForm.getBody();
try {
ticketRemote.addThread(ownerID, tVO);
}catch(Exception e){
defaultMessageBody = errorTemplateForm.getBody();
}// end of catch block
messageSubject = "RE: "+messageSubject;