diff --git a/api/src/main/java/org/openmrs/OrderType.java b/api/src/main/java/org/openmrs/OrderType.java index 610a857d65d..d24a573c0ef 100644 --- a/api/src/main/java/org/openmrs/OrderType.java +++ b/api/src/main/java/org/openmrs/OrderType.java @@ -9,20 +9,38 @@ */ package org.openmrs; -import java.util.Collection; -import java.util.LinkedHashSet; - import org.apache.commons.lang3.StringUtils; +import org.hibernate.annotations.GenericGenerator; +import org.hibernate.annotations.Parameter; import org.hibernate.envers.Audited; import org.openmrs.annotation.Independent; import org.openmrs.api.APIException; import org.openmrs.api.context.Context; +import javax.persistence.AttributeOverride; +import javax.persistence.AttributeOverrides; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import javax.persistence.UniqueConstraint; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Set; + /** * OrderTypes are used to classify different types of Orders e.g to distinguish between Serology and * Radiology TestOrders * */ +@Entity +@Table(name = "order_type") @Audited public class OrderType extends BaseChangeableOpenmrsMetadata { @@ -33,15 +51,33 @@ public class OrderType extends BaseChangeableOpenmrsMetadata { public static final String TEST_ORDER_TYPE_UUID = "52a447d3-a64a-11e3-9aeb-50e549534c5e"; public static final String REFERRAL_ORDER_TYPE_UUID = "f1b63696-2b6c-11ec-8d3d-0242ac130003"; - + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "order_type_id_seq") + @GenericGenerator( + name = "order_type_id_seq", + strategy = "native", + parameters = @Parameter(name = "sequence", value = "order_type_order_type_id_seq") + ) + @Column(name = "order_type_id", nullable = false) private Integer orderTypeId; + @Column(name = "java_class_name", nullable = false) private String javaClassName; + @ManyToOne + @JoinColumn(name = "parent") private OrderType parent; @Independent - private Collection conceptClasses; + @ManyToMany + @JoinTable( + name = "order_type_class_map", + joinColumns = @JoinColumn(name = "order_type_id"), + inverseJoinColumns = @JoinColumn(name = "concept_class_id"), + uniqueConstraints = @UniqueConstraint(columnNames = {"order_type_id", "concept_class_id"}) + ) + private Set conceptClasses; /** * default constructor @@ -52,7 +88,7 @@ public OrderType() { /** * Constructor with ID * - * @param orderTypeId the ID of the {@link org.openmrs.OrderType} + * @param orderTypeId the ID of the {@link OrderType} */ public OrderType(Integer orderTypeId) { this.orderTypeId = orderTypeId; @@ -87,7 +123,7 @@ public void setOrderTypeId(Integer orderTypeId) { } /** - * @see org.openmrs.OpenmrsObject#getId() + * @see OpenmrsObject#getId() */ @Override public Integer getId() { @@ -95,7 +131,7 @@ public Integer getId() { } /** - * @see org.openmrs.OpenmrsObject#setId(java.lang.Integer) + * @see OpenmrsObject#setId(Integer) */ @Override public void setId(Integer id) { @@ -118,23 +154,23 @@ public void setJavaClassName(String javaClassName) { } /** - * @return Returns the {@link org.openmrs.OrderType} + * @return Returns the {@link OrderType} */ public OrderType getParent() { return parent; } /** - * @param parent The {@link org.openmrs.OrderType} to set + * @param parent The {@link OrderType} to set */ public void setParent(OrderType parent) { this.parent = parent; } /** - * @return Get the {@link org.openmrs.ConceptClass}es + * @return Get the {@link ConceptClass}es */ - public Collection getConceptClasses() { + public Set getConceptClasses() { if (conceptClasses == null) { conceptClasses = new LinkedHashSet<>(); } @@ -142,17 +178,17 @@ public Collection getConceptClasses() { } /** - * @param conceptClasses the collection containing the {@link org.openmrs.ConceptClass}es + * @param conceptClasses the collection containing the {@link ConceptClass}es */ - public void setConceptClasses(Collection conceptClasses) { + public void setConceptClasses(Set conceptClasses) { this.conceptClasses = conceptClasses; } /** - * Convenience method that returns a {@link java.lang.Class} object for the associated + * Convenience method that returns a {@link Class} object for the associated * javaClassName * - * @return The Java class as {@link java.lang.Class} + * @return The Java class as {@link Class} * @throws APIException */ public Class getJavaClass() { @@ -177,7 +213,7 @@ public void addConceptClass(ConceptClass conceptClass) { } /** - * @see org.openmrs.BaseOpenmrsObject#toString() + * @see BaseOpenmrsObject#toString() */ @Override public String toString() { diff --git a/api/src/main/resources/hibernate.cfg.xml b/api/src/main/resources/hibernate.cfg.xml index 05e1299293e..2bd3c6ad8dc 100644 --- a/api/src/main/resources/hibernate.cfg.xml +++ b/api/src/main/resources/hibernate.cfg.xml @@ -63,7 +63,6 @@ - diff --git a/api/src/main/resources/org/openmrs/api/db/hibernate/OrderType.hbm.xml b/api/src/main/resources/org/openmrs/api/db/hibernate/OrderType.hbm.xml deleted file mode 100644 index 92217966fb4..00000000000 --- a/api/src/main/resources/org/openmrs/api/db/hibernate/OrderType.hbm.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - - - - - order_type_order_type_id_seq - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/api/src/test/java/org/openmrs/api/OrderServiceTest.java b/api/src/test/java/org/openmrs/api/OrderServiceTest.java index 60b6ad9516c..6d553c03825 100644 --- a/api/src/test/java/org/openmrs/api/OrderServiceTest.java +++ b/api/src/test/java/org/openmrs/api/OrderServiceTest.java @@ -2739,6 +2739,7 @@ public void saveOrder_shouldFailIfTheJavaTypeOfThePreviousOrderDoesNotMatch() th .addAnnotatedClass(PatientIdentifierType.class) .addAnnotatedClass(ProgramAttributeType.class) .addAnnotatedClass(HL7InError.class) + .addAnnotatedClass(OrderType.class) .getMetadataBuilder().build(); diff --git a/api/src/test/java/org/openmrs/validator/OrderTypeValidatorTest.java b/api/src/test/java/org/openmrs/validator/OrderTypeValidatorTest.java index 87fb1f3cfc0..24826fe7a46 100644 --- a/api/src/test/java/org/openmrs/validator/OrderTypeValidatorTest.java +++ b/api/src/test/java/org/openmrs/validator/OrderTypeValidatorTest.java @@ -19,6 +19,7 @@ import java.util.Collection; import java.util.HashSet; +import java.util.Set; import org.junit.jupiter.api.Test; import org.openmrs.ConceptClass; @@ -159,7 +160,7 @@ public void validate_shouldPassIfAllFieldsAreCorrectForANewOrderType() { OrderType orderType = new OrderType(); orderType.setName("unique name"); orderType.setJavaClassName("org.openmrs.TestDrugOrder"); - Collection col = new HashSet<>(); + Set col = new HashSet<>(); col.add(Context.getConceptService().getConceptClass(2)); orderType.setConceptClasses(col); Errors errors = new BindException(orderType, "orderType"); @@ -201,7 +202,7 @@ public void validate_shouldPassValidationIfFieldLengthsAreCorrect() { OrderType orderType = new OrderType(); orderType.setName("unique name"); orderType.setJavaClassName("org.openmrs.TestDrugOrder"); - Collection col = new HashSet<>(); + Set col = new HashSet<>(); col.add(Context.getConceptService().getConceptClass(2)); orderType.setConceptClasses(col); @@ -224,7 +225,7 @@ public void validate_shouldFailValidationIfFieldLengthsAreNotCorrect() { .setName("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text"); orderType .setJavaClassName("too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text too long text"); - Collection col = new HashSet<>(); + Set col = new HashSet<>(); col.add(Context.getConceptService().getConceptClass(2)); orderType.setConceptClasses(col);