package net.ihe.xcpd.init.model; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EntityManager; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Query; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.UniqueConstraint; import net.ihe.gazelle.simulator.common.model.Message; import net.ihe.gazelle.simulator.common.model.TestInstance; import net.ihe.gazelle.simulator.common.model.TestInstanceParticipants; import net.ihe.gazelle.simulator.common.tf.model.Transaction; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; import org.hibernate.validator.NotNull; import org.jboss.seam.Component; import org.jboss.seam.annotations.Name; /** * Class Description : XCPD Message

* XCPD Message * * @class XCPD Message * @author Abderrazek Boufahja / INRIA Rennes IHE development Project * @see > epoiseau@irisa.fr - http://www.ihe-europe.org * @version 1.0 - 2010, October 11th * */ @Entity @Name("XCPDMessage") @Table(name="xcpd_message_gui", schema="public", uniqueConstraints=@UniqueConstraint(columnNames="id")) @SequenceGenerator(name="xcpd_message_gui_sequence", sequenceName="xcpd_message_gui_id_seq", allocationSize=1) public class XCPDMessage implements Serializable{ /** * */ private static final long serialVersionUID = 1L; @Id @GeneratedValue(generator="xcpd_message_gui_sequence", strategy=GenerationType.SEQUENCE) @NotNull @Column(name="id") private Integer id; @Temporal(TemporalType.TIMESTAMP) @Column(name="time_stamp") private Date timeStamp; @Column(name="message_response_content") private byte[] messageResponseContent; @Column(name="message_sent_content") private byte[] messageSentContent; @Column(name="message_type") private String messageType; @ManyToOne @JoinColumn(name="transaction_id") private Transaction transaction; @ManyToOne @JoinColumn(name="responder_id") private Responder configuration; @Column(name="msg_context") private String context; /** * Constructor */ public XCPDMessage(){ } public XCPDMessage(XCPDMessage message){ this.messageType = message.getMessageType() + "Response"; this.transaction = message.getTransaction(); this.configuration = message.getConfiguration(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public void setContext(String context) { this.context = context; } public String getContext() { return context; } public Date getTimeStamp() { return timeStamp; } public void setTimeStamp(Date timeStamp) { this.timeStamp = timeStamp; } public byte[] getMessageSentContent() { return messageSentContent; } public void setMessageSentContent(byte[] messageSentContent) { this.messageSentContent = messageSentContent; } public byte[] getMessageResponseContent() { return messageResponseContent; } public void setMessageResponseContent(byte[] messageResponseContent) { this.messageResponseContent = messageResponseContent; } public String getMessageType() { return messageType; } public void setMessageType(String messageType) { this.messageType = messageType; } public Transaction getTransaction() { return transaction; } public void setTransaction(Transaction transaction) { this.transaction = transaction; } public Responder getConfiguration() { return configuration; } public void setConfiguration(Responder configuration) { this.configuration = configuration; } /** * @return */ public static List getAllSentMessages() { EntityManager em = (EntityManager) Component.getInstance("entityManager"); Session session = (Session) em.getDelegate(); Criteria criteria = session.createCriteria(XCPDMessage.class); criteria.add(Restrictions.isNotNull("relativeMessage")); List messages = criteria.list(); if (messages != null && messages.size() > 0) return messages; else return null; } /** * @param inRequest * @return */ public static XCPDMessage getResponseRelativeToGivenRequest(XCPDMessage inRequest) { if (inRequest == null) return null; EntityManager em = (EntityManager) Component.getInstance("entityManager"); Session session = (Session) em.getDelegate(); Criteria criteria = session.createCriteria(XCPDMessage.class); criteria.add(Restrictions.ilike("relativeMessage", inRequest.getId())); List messages = criteria.list(); if (messages != null && messages.size() > 0) return messages.get(0); else return null; } public static List getAllMessages() { EntityManager em = (EntityManager) Component.getInstance("entityManager"); Session session = (Session) em.getDelegate(); Criteria criteria = session.createCriteria(XCPDMessage.class); criteria.addOrder(Order.desc("timeStamp")); List messages = criteria.list(); if (messages != null && messages.size() > 0) return messages; else return null; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((configuration == null) ? 0 : configuration.hashCode()); result = prime * result + ((context == null) ? 0 : context.hashCode()); result = prime * result + Arrays.hashCode(messageResponseContent); result = prime * result + Arrays.hashCode(messageSentContent); result = prime * result + ((messageType == null) ? 0 : messageType.hashCode()); result = prime * result + ((timeStamp == null) ? 0 : timeStamp.hashCode()); result = prime * result + ((transaction == null) ? 0 : transaction.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; XCPDMessage other = (XCPDMessage) obj; if (configuration == null) { if (other.configuration != null) return false; } else if (!configuration.equals(other.configuration)) return false; if (context == null) { if (other.context != null) return false; } else if (!context.equals(other.context)) return false; if (!Arrays .equals(messageResponseContent, other.messageResponseContent)) return false; if (!Arrays.equals(messageSentContent, other.messageSentContent)) return false; if (messageType == null) { if (other.messageType != null) return false; } else if (!messageType.equals(other.messageType)) return false; if (timeStamp == null) { if (other.timeStamp != null) return false; } else if (!timeStamp.equals(other.timeStamp)) return false; if (transaction == null) { if (other.transaction != null) return false; } else if (!transaction.equals(other.transaction)) return false; return true; } @Override public String toString() { return "XCPDMessage [id=" + id + ", timeStamp=" + timeStamp + ", messageResponseContent=" + Arrays.toString(messageResponseContent) + ", messageSentContent=" + Arrays.toString(messageSentContent) + ", messageType=" + messageType + ", transaction=" + transaction + ", configuration=" + configuration + ", context=" + context + "]"; } public String getSentStringValue(){ if (this.messageSentContent == null) return null; String msgHL7 = new String(this.messageSentContent); return msgHL7; } public String getRespStringValue(){ if (this.messageResponseContent == null) return null; String msgHL7 = new String(this.messageResponseContent); return msgHL7; } public static XCPDMessage persistXCPDMessage(XCPDMessage res){ EntityManager entityManager=(EntityManager)Component.getInstance("entityManager"); res = entityManager.merge(res); entityManager.flush(); return res; } public static List getMessageFiltred(String context){ EntityManager em = (EntityManager) Component.getInstance("entityManager"); Query query ; StringBuffer queryString = new StringBuffer() ; HashSet < String> prefixes = new HashSet(); HashSet < String> prefixesUsed = new HashSet(); HashMap mapOfJoin = new HashMap() ; HashMap mapOfParameters = new HashMap() ; if (context != null){ addANDorWHERE(queryString) ; prefixes.add("XCPDMessage mess"); queryString.append( "mess.context = :context" ); mapOfParameters.put("context", context); } prefixes.add("XCPDMessage mess"); List listOfPrefixes = new ArrayList( prefixes ) ; StringBuffer firstPartOfQuery = new StringBuffer() ; for ( int i= 0 ; i< listOfPrefixes.size() ; i++ ) { if ( i == 0 ) { firstPartOfQuery.append("SELECT distinct mess FROM " ) ; } if ( !prefixesUsed.contains(listOfPrefixes.get(i)) ) { if ( prefixesUsed.size() > 0 ) firstPartOfQuery.append(" , " ) ; firstPartOfQuery.append( listOfPrefixes.get(i) ) ; if (mapOfJoin.containsKey(listOfPrefixes.get(i))) firstPartOfQuery.append(" " + mapOfJoin.get(listOfPrefixes.get(i)) +" " ) ; prefixesUsed.add( listOfPrefixes.get(i) ) ; } } queryString.insert(0, firstPartOfQuery ) ; if ( queryString.toString().trim().length() == 0 ) return null ; query = em.createQuery(queryString.toString()) ; List listOfParameters = new ArrayList( mapOfParameters.keySet() ) ; for ( String param : listOfParameters ){ query.setParameter( param , mapOfParameters.get(param) ) ; } List listOfObjectFile = ( List )query.getResultList(); return listOfObjectFile ; } public static StringBuffer addANDorWHERE( StringBuffer currentQueryString ) { if ( currentQueryString.indexOf("WHERE") > 0 ) return currentQueryString.append(" AND " ) ; else return currentQueryString.append(" WHERE " ) ; } }