smsBackupAddress = message.getHeader("X-backup2gmail-sms-address")[0];
}
final String emailId = messageId + smsBackupId;
if (type == SmsEntryFacet.class){
return apiDataService.createOrReadModifyWrite(SmsEntryFacet.class,
new FacetQuery(
"e.apiKeyId = ? AND e.emailId = ?",
updateInfo.apiKey.getId(),
emailId),
new FacetModifier<SmsEntryFacet>() {
// Throw exception if it turns out we can't make sense of the observation's JSON
// This will abort the transaction
@Override
public SmsEntryFacet createOrModify(SmsEntryFacet facet, Long apiKeyId) {
if (facet == null) {
facet = new SmsEntryFacet(updateInfo.apiKey.getId());
facet.emailId = emailId;
facet.guestId = updateInfo.apiKey.getGuestId();
facet.api = updateInfo.apiKey.getConnector().value();
}
facet.timeUpdated = System.currentTimeMillis();
try{
InternetAddress[] senders = null;
try{
senders = (InternetAddress[]) message.getFrom();
} catch (AddressException ignored){}
InternetAddress[] recipients = null;
try{
recipients = (InternetAddress[]) message.getRecipients(MimeMessage.RecipientType.TO);
} catch (AddressException ignored){}
String fromAddress, toAddress;
boolean senderMissing = false, recipientsMissing = false;
if (senders != null && senders.length > 0){
fromAddress = senders[0].getAddress();
}
else{
fromAddress = message.getSubject().substring(9);
senderMissing = true;
}
if (recipients != null && recipients.length > 0){
toAddress = recipients[0].getAddress();
}
else{
toAddress = message.getSubject().substring(9);
recipientsMissing = true;
}
if (fromAddress.startsWith(username)) {
facet.smsType = SmsEntryFacet.SmsType.OUTGOING;
if (recipientsMissing){
facet.personName = toAddress;
facet.personNumber = smsBackupAddress;
}
else if (toAddress.indexOf("unknown.email")!=-1) {
facet.personName = recipients[0].getPersonal();
facet.personNumber = toAddress.substring(0, toAddress.indexOf("@"));
}
else {
facet.personName = recipients[0].getPersonal();
facet.personNumber = smsBackupAddress;
}
}else {
facet.smsType = SmsEntryFacet.SmsType.INCOMING;
if (senderMissing){
facet.personName = fromAddress;
facet.personNumber = smsBackupAddress;
}
else if (fromAddress.indexOf("unknown.email")!=-1) {
facet.personName = senders[0].getPersonal();
facet.personNumber = fromAddress.substring(0, fromAddress.indexOf("@"));
}
else {
facet.personName = senders[0].getPersonal();
facet.personNumber = smsBackupAddress;
}
}
facet.dateReceived = message.getSentDate();
facet.start = facet.dateReceived.getTime();
facet.end = facet.start;
Object content = message.getContent();
facet.hasAttachments = false;
if (content instanceof String)
facet.message = (String) message.getContent();
else if (content instanceof MimeMultipart) {//TODO: this is an MMS and needs to be handled properly
facet.message = "";
MimeMultipart multipart = (MimeMultipart) content;
int partCount = multipart.getCount();
for (int i = 0; i < partCount; i++){
MimeBodyPart part = (MimeBodyPart) multipart.getBodyPart(i);
String contentType = part.getContentType().split(";")[0].toLowerCase();
Object partContent = part.getContent();
if (contentType.startsWith("text/plain")){//other types of text are returned as byte streams and are attachments
if (!facet.message.equals("")){
facet.message += "\n\n";
}
facet.message = (String) partContent;
}
else{
if (!facet.hasAttachments){
facet.hasAttachments = true;
facet.attachmentMimeTypes = contentType;
facet.attachmentNames = (emailId + i).replaceAll("\\W+","");
}
else{
facet.attachmentMimeTypes += "," + contentType;
facet.attachmentNames += "," + (emailId + i).replaceAll("\\W+","");
}
File attachmentFile = getAttachmentFile(env.targetEnvironmentProps.getString("btdatastore.db.location"),updateInfo.getGuestId(),updateInfo.apiKey.getId(),(emailId + i).replaceAll("\\W+",""));
attachmentFile.getParentFile().mkdirs();
FileOutputStream fileoutput = new FileOutputStream(attachmentFile);
IOUtils.copy((BASE64DecoderStream) partContent, fileoutput);
fileoutput.close();
}
}
}
} catch(Exception e){
e.printStackTrace();
return null;
}
return facet;
}
}, updateInfo.apiKey.getId());
}
else if (type == CallLogEntryFacet.class){
return apiDataService.createOrReadModifyWrite(CallLogEntryFacet.class,
new FacetQuery(
"e.apiKeyId = ? AND e.emailId = ?",
updateInfo.apiKey.getId(),
emailId),
new FacetModifier<CallLogEntryFacet>() {
// Throw exception if it turns out we can't make sense of the observation's JSON