package net.spatula.tally_ho.service; import java.util.Date; import javax.persistence.EntityManager; import javax.persistence.Query; import net.spatula.tally_ho.jpa.ResourceManager; import net.spatula.tally_ho.model.Message; import net.spatula.tally_ho.model.MessageRoot; import net.spatula.tally_ho.service.beans.MessageBean; import net.spatula.tally_ho.service.beans.MessageRootBean; public class MessageService { private static MessageService instance = null; public synchronized static MessageService getInstance() { if (instance == null) { instance = new MessageService(); } return instance; } public ServiceResult createMessageRoot(MessageRootBean root) { EntityManager em = ResourceManager.getInstance().getEM(); try { em.getTransaction().begin(); MessageRoot messageRoot = new MessageRoot(); copyInRoot(root, messageRoot); em.persist(messageRoot); em.getTransaction().commit(); root.setId(messageRoot.getObjectId()); return new ServiceResult(ServiceResult.STATUS.OK, "Root was created successfully. Id is " + messageRoot.getObjectId()); } catch (Exception e) { return new ServiceResult(ServiceResult.STATUS.FAIL_HARD, "Exception during creation", e); } finally { em.close(); } } private void copyInRoot(MessageRootBean rootBean, MessageRoot root) { root.setLastPost(null); root.setPostCount(0); root.setPostCount24hr(0); root.setPostingPermitted(rootBean.getPostingPermitted()); root.setSourceId(rootBean.getSourceId()); root.setSourceType(rootBean.getSourceType()); } public ServiceResult destroyMessageRoot(MessageRootBean rootBean) { EntityManager em = ResourceManager.getInstance().getEM(); em.getTransaction().begin(); MessageRoot root = getRootFromBean(em, rootBean); em.remove(root); em.getTransaction().commit(); return new ServiceResult(ServiceResult.STATUS.OK, "Root was deleted successfully."); } private MessageRoot getRootFromBean(EntityManager em, MessageRootBean rootBean) { Query q = em.createQuery("Select x from MessageRoot x where x.sourceId = ?1 and x.sourceType = ?2"); q.setParameter(1, rootBean.getSourceId()); q.setParameter(2, rootBean.getSourceType()); MessageRoot messageRoot = (MessageRoot) q.getSingleResult(); return messageRoot; } private void copyInMessage(MessageBean messageBean, Message message) { message.setAlias(messageBean.getAlias()); message.setClosed(messageBean.isClosed()); message.setFlags(messageBean.getFlags()); message.setIp(messageBean.getIp()); message.setLocation(messageBean.getLocation()); message.setMessageText(messageBean.getText()); message.setPending(messageBean.isPending()); message.setRegistered(messageBean.isRegistered()); message.setUid(messageBean.getUid()); } public ServiceResult createMessage(MessageBean messageBean, MessageRootBean rootBean) { EntityManager em = ResourceManager.getInstance().getEM(); try { Message message = new Message(); copyInMessage(messageBean, message); message.setChanger(UserService.getInstance().getSystemAccount(em)); message.setChangeSummary("Initial object creation"); message.setCreateDate(new Date()); message.setVersion(1); message.setRoot(getRootFromBean(em, rootBean)); if (messageBean.getParent() != null) { message.setParent(getMessageFromBean(em, messageBean.getParent())); } em.getTransaction().begin(); em.persist(message); em.getTransaction().commit(); messageBean.setId(message.getObjectId()); return new ServiceResult(ServiceResult.STATUS.OK, "Message was created successfully. Id is " + message.getObjectId()); } catch (Exception e) { return new ServiceResult(ServiceResult.STATUS.FAIL_HARD, "Exception during creation", e); } finally { em.close(); } } private Message getMessageFromBean(EntityManager em, MessageBean parent) { Query q = em.createQuery("Select x from Message x where x.objectId = ?1"); q.setParameter(1, parent.getId()); Message message = (Message) q.getSingleResult(); return message; } public ServiceResult destroyMessage(MessageBean message) { return null; } }