Skip to content

Commit

Permalink
Revert "TRUNK-5492 Replace Person and Patient hbm mapping file with a…
Browse files Browse the repository at this point in the history
…nnotations (#2900)"

This reverts commit 9c3a95f.
  • Loading branch information
Ruhanga committed Jan 23, 2020
1 parent c2fea2f commit 49e79b6
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 182 deletions.
61 changes: 0 additions & 61 deletions api/src/main/java/org/openmrs/Patient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,87 +11,30 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.SortNatural;
import org.hibernate.search.annotations.ContainedIn;
import org.hibernate.search.annotations.Field;

/**
* Defines a Patient in the system. A patient is simply an extension of a person and all that that
* implies.
*
* @version 2.0
*/
@Entity
@Table(name = "patient")
@PrimaryKeyJoinColumn(name = "patient_id")
public class Patient extends Person {

public static final long serialVersionUID = 93123L;

@Column(name = "patient_id", nullable = false, updatable = false, insertable = false)
private Integer patientId;

@Column(name = "allergy_status", length = 50)
private String allergyStatus = Allergies.UNKNOWN;

@OneToMany(mappedBy = "patient", cascade = CascadeType.ALL, orphanRemoval = true)
@SortNatural
@ContainedIn
private Set<PatientIdentifier> identifiers;

@ManyToOne
@JoinColumn(name = "creator", updatable = false)
@Access(AccessType.PROPERTY)
private User creator;

@Column(name = "date_created", nullable = false, updatable = false, length = 19)
@Access(AccessType.PROPERTY)
private Date dateCreated;

@Column(name = "voided", nullable = false)
@Access(AccessType.PROPERTY)
@Field
private Boolean voided = Boolean.FALSE;

@Column(name = "date_voided", length = 19)
@Access(AccessType.PROPERTY)
private Date dateVoided;

@ManyToOne
@JoinColumn(name = "voided_by")
@Access(AccessType.PROPERTY)
private User voidedBy;

@Column(name = "void_reason")
@Access(AccessType.PROPERTY)
private String voidReason;

@ManyToOne
@JoinColumn(name = "changed_by")
@Access(AccessType.PROPERTY)
private User changedBy;

@Column(name = "date_changed", length = 19)
@Access(AccessType.PROPERTY)
private Date dateChanged;

// Constructors

/** default constructor */
Expand Down Expand Up @@ -156,9 +99,6 @@ public Patient(Patient patient) {
* @return internal identifier for patient
*/
public Integer getPatientId() {
if (this.patientId == null) {
this.patientId = getPersonId();
}
return this.patientId;
}

Expand Down Expand Up @@ -460,5 +400,4 @@ public void setId(Integer id) {
public Person getPerson() {
return this;
}

}
73 changes: 0 additions & 73 deletions api/src/main/java/org/openmrs/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,7 @@
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.SortNatural;
import org.hibernate.search.annotations.ContainedIn;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
Expand All @@ -62,106 +37,60 @@
*
* @see org.openmrs.Patient
*/
@Entity
@Table(name = "person")
@Inheritance(strategy = InheritanceType.JOINED)
public class Person extends BaseChangeableOpenmrsData {

public static final long serialVersionUID = 2L;

private static final Logger log = LoggerFactory.getLogger(Person.class);

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "person_id")
@DocumentId
protected Integer personId;

@OneToMany(mappedBy = "person", cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
@SortNatural
@OrderBy("voided asc, preferred desc, date_created desc")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@BatchSize(size = 1000)
private Set<PersonAddress> addresses = null;

@OneToMany(mappedBy = "person", cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
@SortNatural
@OrderBy("voided asc, preferred desc, date_created desc")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@BatchSize(size = 1000)
@ContainedIn
private Set<PersonName> names = null;

@OneToMany(mappedBy = "person", cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
@SortNatural
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@BatchSize(size = 1000)
@ContainedIn
private Set<PersonAttribute> attributes = null;

@Field
@Column(length = 50)
private String gender;

@Column(name = "birthdate", length = 10)
private Date birthdate;

@Basic
@Temporal(TemporalType.TIME)
private Date birthtime;

@Column(name = "birthdate_estimated")
private Boolean birthdateEstimated = false;

@Column(name = "deathdate_estimated")
private Boolean deathdateEstimated = false;

@Field
@Column(nullable = false)
private Boolean dead = false;

@Column(name = "death_date", length = 19)
private Date deathDate;

@ManyToOne
@JoinColumn(name = "cause_of_death")
private Concept causeOfDeath;

@Column(name = "cause_of_death_non_coded")
private String causeOfDeathNonCoded;

@ManyToOne
@JoinColumn(name = "creator", updatable = false, insertable = false)
private User personCreator;

@Column(name = "date_created", updatable = false, insertable = false, nullable = false, length = 19)
private Date personDateCreated;

@ManyToOne
@JoinColumn(name = "changed_by", updatable = false, insertable = false)
private User personChangedBy;

@Column(name = "date_changed", updatable = false, insertable = false, length = 19)
private Date personDateChanged;

@Column(name = "voided", updatable = false, insertable = false, nullable = false)
private Boolean personVoided = false;

@ManyToOne
@JoinColumn(name = "voided_by", updatable = false, insertable = false)
private User personVoidedBy;

@Column(name = "date_voided", updatable = false, insertable = false, length = 19)
private Date personDateVoided;

@Column(name = "void_reason", updatable = false, insertable = false)
private String personVoidReason;

@Field
@Formula("case when exists (select * from patient p where p.patient_id = person_id) then 1 else 0 end")
private boolean isPatient;

/**
Expand All @@ -170,10 +99,8 @@ public class Person extends BaseChangeableOpenmrsData {
* This is "cached" for each user upon first load. When an attribute is changed, the cache is
* cleared and rebuilt on next access.
*/
@Transient
Map<String, PersonAttribute> attributeMap = null;

@Transient
private Map<String, PersonAttribute> allAttributeMap = null;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public boolean isIdentifierInUseByAnotherPatient(PatientIdentifier patientIdenti
&& patientIdentifier.getIdentifierType().getUniquenessBehavior() == UniquenessBehavior.LOCATION;

// switched this to an hql query so the hibernate cache can be considered as well as the database
String hql = "select count(*) from PatientIdentifier pi, Patient p where pi.patient.patientId = p.patientId "
String hql = "select count(*) from PatientIdentifier pi, Patient p where pi.patient.patientId = p.patient.patientId "
+ "and p.voided = false and pi.voided = false and pi.identifier = :identifier and pi.identifierType = :idType";

if (checkPatient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.openmrs.User;
import org.openmrs.annotation.Handler;
import org.openmrs.aop.RequiredDataAdvice;
import org.openmrs.api.context.Context;

/**
* This class deals with {@link Patient} objects when they are saved via a save* method in an
Expand Down Expand Up @@ -46,10 +45,5 @@ public void handle(Patient patient, User creator, Date dateCreated, String other
}
}
}

if(patient.getPatientId() == null){
patient.setCreator(Context.getAuthenticatedUser());
patient.setDateCreated(new Date());
}
}
}
10 changes: 2 additions & 8 deletions api/src/main/java/org/openmrs/api/handler/PersonSaveHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
package org.openmrs.api.handler;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.openmrs.Address;
import org.openmrs.Person;
import org.openmrs.PersonAddress;
import org.openmrs.PersonAttribute;
Expand Down Expand Up @@ -45,16 +42,13 @@ public void handle(Person person, User creator, Date dateCreated, String other)

// address collection
if (person.getAddresses() != null && !person.getAddresses().isEmpty()) {
Set<Address> blankAddresses = new HashSet<>();
for (PersonAddress pAddress : person.getAddresses()) {
if (pAddress.isBlank()) {
blankAddresses.add(pAddress);
if (pAddress.isBlank()){
person.removeAddress(pAddress);
continue;
}
pAddress.setPerson(person);
}

person.getAddresses().removeAll(blankAddresses);
}

// name collection
Expand Down
2 changes: 2 additions & 0 deletions api/src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<mapping resource="org/openmrs/api/db/hibernate/FormResource.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/GlobalProperty.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/Obs.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/Person.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/PersonAttribute.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/PersonAttributeType.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/PersonAddress.hbm.xml" />
Expand All @@ -61,6 +62,7 @@
<mapping resource="org/openmrs/api/db/hibernate/LoginCredential.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/Privilege.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/Role.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/Patient.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/PatientIdentifier.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/PatientIdentifierType.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/PatientProgram.hbm.xml" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0"?>
<!--
This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
graphic logo is a trademark of OpenMRS Inc.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.openmrs">

<joined-subclass name="Patient" table="patient" extends="Person">

<key column="patient_id" not-null="true" on-delete="cascade" />

<property name="patientId" type="int" not-null="true"
column="patient_id" update="false" insert="false" />

<many-to-one name="creator" class="User" lazy="no-proxy" />

<property name="dateCreated" type="java.util.Date"
column="date_created" not-null="true" length="19" />

<many-to-one name="changedBy" class="User" column="changed_by" />

<property name="dateChanged" type="java.util.Date"
column="date_changed" length="19" />

<property name="voided" type="boolean" not-null="true" />

<many-to-one name="voidedBy" class="User" column="voided_by" />

<property name="dateVoided" type="java.util.Date"
column="date_voided" length="19" />

<property name="voidReason" type="java.lang.String"
column="void_reason" length="255" />

<property name="allergyStatus" type="java.lang.String"
column="allergy_status" not-null="false" length="50" />

<!-- Associations -->

<!-- bi-directional one-to-many association to PatientIdentifier -->
<!-- list is unsorted due so that its put into a LinkedHashSet which uses .hashcode() instead of .compareTo() -->
<set name="identifiers" lazy="true" cascade="all-delete-orphan"
table="patient_identifier" inverse="true" sort="natural">
<key not-null="true" column="patient_id" />
<one-to-many class="PatientIdentifier" />
</set>

</joined-subclass>

</hibernate-mapping>
Loading

0 comments on commit 49e79b6

Please sign in to comment.