if (activity == null || !activity.hasId()) {
throw new InvalidActivityException();
}
// Create a message for the recipient
ActivityMessage message = new PersistentActivityMessage();
message.setSender(remoteJID);
message.setRecipient(localJID);
//in case of an activity update we keep the received date as the date of
//original publish, otherwise the updated posts will start showing on top of
// the inbox..which we agreed we don't want...
message.setReceived(activity.getPublished());
// Search if the activity exists in the database
final EntityManager em = OswPlugin.getEmFactory().createEntityManager();
PersistentActivityEntry previousActivity = em.find(PersistentActivityEntry.class, activity.getId());
// Assign the activity to the existing one if it exists
if (previousActivity != null) {
message.setActivity(previousActivity);
} else {
message.setActivity(activity);
}
//in case of an update the message will already exist in the DB
Query query = em.createQuery("SELECT x FROM Messages x WHERE x.activity.id = ?1");
query.setParameter(1, activity.getId());