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 Apr 5, 2023
1 parent 0c05e45 commit b364c61
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 74 deletions.
77 changes: 69 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,26 @@
*/
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.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 +37,49 @@
*
* @see Privilege
*/
public class Role extends BaseChangeableOpenmrsMetadata {

@Entity
@Table(name="role")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
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")
)
private Set<Privilege> privileges;

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

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

// Constructors
Expand All @@ -52,6 +93,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 +127,17 @@ 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) {
this.name = 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 @@ -60,7 +60,6 @@
<mapping resource="org/openmrs/api/db/hibernate/User.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.Visit;
Expand Down Expand Up @@ -2654,6 +2655,7 @@ public void saveOrder_shouldFailIfTheJavaTypeOfThePreviousOrderDoesNotMatch() th
.addAnnotatedClass(Location.class)
.addAnnotatedClass(PersonAddress.class)
.addAnnotatedClass(PersonAttributeType.class)
.addAnnotatedClass(Role.class)
.getMetadataBuilder().build();


Expand Down

0 comments on commit b364c61

Please sign in to comment.