Skip to content

Commit

Permalink
switching from hibernate mappings to annotations-Role
Browse files Browse the repository at this point in the history
  • Loading branch information
mherman22 committed Oct 15, 2023
1 parent 2aa881f commit 3bd5c51
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 74 deletions.
79 changes: 71 additions & 8 deletions api/src/main/java/org/openmrs/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,27 @@
*/
package org.openmrs;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.search.annotations.Field;
import org.openmrs.util.RoleConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
* A Role is just an aggregater of {@link Privilege}s. {@link User}s contain a number of roles
* (Users DO NOT contain any privileges directly) Roles can be grouped by inheriting other roles. If
Expand All @@ -25,20 +38,51 @@
*
* @see Privilege
*/
public class Role extends BaseChangeableOpenmrsMetadata {

@Entity
@Table(name="role")
public class Role extends BaseOpenmrsObject {

public static final long serialVersionUID = 1234233L;

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

// Fields

@Id
@Column(name = "role")
private String role;

@Transient
@Field
private String name;

@Column(name = "description", length = 255)
private String description;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "role_privilege",
joinColumns = @JoinColumn(name = "role"),
inverseJoinColumns = @JoinColumn(name = "privilege")
)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private Set<Privilege> privileges;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "role_role",
joinColumns = @JoinColumn(name = "child_role"),
inverseJoinColumns = @JoinColumn(name = "parent_role")
)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private Set<Role> inheritedRoles;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "role_role",
joinColumns = @JoinColumn(name = "parent_role"),
inverseJoinColumns = @JoinColumn(name = "child_role")
)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private Set<Role> childRoles;

// Constructors
Expand All @@ -52,6 +96,20 @@ public Role(String role) {
this.role = role;
}

/**
* @return the description
*/
public String getDescription() {
return this.description;
}

/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}

/** constructor with all database required properties */
public Role(String role, String description) {
this.role = role;
Expand All @@ -72,11 +130,16 @@ public void setPrivileges(Set<Privilege> privileges) {
this.privileges = privileges;
}

@Override
public String getName() {
return this.getRole();
}

/**
* @param name the name to set
*/
public void setName(String name) {
}

/**
* Adds the given Privilege to the list of privileges
*
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 @@ -59,7 +59,6 @@
<mapping resource="org/openmrs/api/db/hibernate/PersonName.hbm.xml" />
<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" />
Expand Down
65 changes: 0 additions & 65 deletions api/src/main/resources/org/openmrs/api/db/hibernate/Role.hbm.xml

This file was deleted.

2 changes: 2 additions & 0 deletions api/src/test/java/org/openmrs/api/OrderServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.openmrs.PersonAttributeType;
import org.openmrs.Provider;
import org.openmrs.ProviderAttributeType;
import org.openmrs.Role;
import org.openmrs.SimpleDosingInstructions;
import org.openmrs.TestOrder;
import org.openmrs.User;
Expand Down Expand Up @@ -2656,6 +2657,7 @@ public void saveOrder_shouldFailIfTheJavaTypeOfThePreviousOrderDoesNotMatch() th
.addAnnotatedClass(PersonAddress.class)
.addAnnotatedClass(PersonAttributeType.class)
.addAnnotatedClass(User.class)
.addAnnotatedClass(Role.class)
.getMetadataBuilder().build();


Expand Down

0 comments on commit 3bd5c51

Please sign in to comment.