Skip to content

Commit

Permalink
TRUNK-6279: Migrate OrderType from Hibernate Mapping XML to JPA annot…
Browse files Browse the repository at this point in the history
…ations. (#4800)
  • Loading branch information
wikumChamith authored Oct 27, 2024
1 parent b2bd587 commit 335c2b5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 80 deletions.
70 changes: 53 additions & 17 deletions api/src/main/java/org/openmrs/OrderType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<ConceptClass> 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<ConceptClass> conceptClasses;

/**
* default constructor
Expand All @@ -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;
Expand Down Expand Up @@ -87,15 +123,15 @@ public void setOrderTypeId(Integer orderTypeId) {
}

/**
* @see org.openmrs.OpenmrsObject#getId()
* @see OpenmrsObject#getId()
*/
@Override
public Integer getId() {
return getOrderTypeId();
}

/**
* @see org.openmrs.OpenmrsObject#setId(java.lang.Integer)
* @see OpenmrsObject#setId(Integer)
*/
@Override
public void setId(Integer id) {
Expand All @@ -118,41 +154,41 @@ 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<ConceptClass> getConceptClasses() {
public Set<ConceptClass> getConceptClasses() {
if (conceptClasses == null) {
conceptClasses = new LinkedHashSet<>();
}
return conceptClasses;
}

/**
* @param conceptClasses the collection containing the {@link org.openmrs.ConceptClass}es
* @param conceptClasses the collection containing the {@link ConceptClass}es
*/
public void setConceptClasses(Collection<ConceptClass> conceptClasses) {
public void setConceptClasses(Set<ConceptClass> 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() {
Expand All @@ -177,7 +213,7 @@ public void addConceptClass(ConceptClass conceptClass) {
}

/**
* @see org.openmrs.BaseOpenmrsObject#toString()
* @see BaseOpenmrsObject#toString()
*/
@Override
public String toString() {
Expand Down
1 change: 0 additions & 1 deletion api/src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
<mapping resource="org/openmrs/api/db/hibernate/PatientProgram.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/PatientProgramAttribute.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/RelationshipType.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/OrderType.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/Order.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/OrderAttribute.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/OrderAttributeType.hbm.xml" />
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions api/src/test/java/org/openmrs/api/OrderServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2739,6 +2739,7 @@ public void saveOrder_shouldFailIfTheJavaTypeOfThePreviousOrderDoesNotMatch() th
.addAnnotatedClass(PatientIdentifierType.class)
.addAnnotatedClass(ProgramAttributeType.class)
.addAnnotatedClass(HL7InError.class)
.addAnnotatedClass(OrderType.class)
.getMetadataBuilder().build();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -159,7 +160,7 @@ public void validate_shouldPassIfAllFieldsAreCorrectForANewOrderType() {
OrderType orderType = new OrderType();
orderType.setName("unique name");
orderType.setJavaClassName("org.openmrs.TestDrugOrder");
Collection<ConceptClass> col = new HashSet<>();
Set<ConceptClass> col = new HashSet<>();
col.add(Context.getConceptService().getConceptClass(2));
orderType.setConceptClasses(col);
Errors errors = new BindException(orderType, "orderType");
Expand Down Expand Up @@ -201,7 +202,7 @@ public void validate_shouldPassValidationIfFieldLengthsAreCorrect() {
OrderType orderType = new OrderType();
orderType.setName("unique name");
orderType.setJavaClassName("org.openmrs.TestDrugOrder");
Collection<ConceptClass> col = new HashSet<>();
Set<ConceptClass> col = new HashSet<>();
col.add(Context.getConceptService().getConceptClass(2));
orderType.setConceptClasses(col);

Expand All @@ -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<ConceptClass> col = new HashSet<>();
Set<ConceptClass> col = new HashSet<>();
col.add(Context.getConceptService().getConceptClass(2));
orderType.setConceptClasses(col);

Expand Down

0 comments on commit 335c2b5

Please sign in to comment.