From 3bd5c51803b2c39dcc83a615793b941ae9564821 Mon Sep 17 00:00:00 2001 From: herman Date: Sun, 2 Apr 2023 03:11:13 +0300 Subject: [PATCH] switching from hibernate mappings to annotations-Role --- api/src/main/java/org/openmrs/Role.java | 79 +++++++++++++++++-- api/src/main/resources/hibernate.cfg.xml | 1 - .../org/openmrs/api/db/hibernate/Role.hbm.xml | 65 --------------- .../org/openmrs/api/OrderServiceTest.java | 2 + 4 files changed, 73 insertions(+), 74 deletions(-) delete mode 100644 api/src/main/resources/org/openmrs/api/db/hibernate/Role.hbm.xml diff --git a/api/src/main/java/org/openmrs/Role.java b/api/src/main/java/org/openmrs/Role.java index 6158ada4591e..2785c44abf54 100644 --- a/api/src/main/java/org/openmrs/Role.java +++ b/api/src/main/java/org/openmrs/Role.java @@ -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 @@ -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 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 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 childRoles; // Constructors @@ -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; @@ -72,11 +130,16 @@ public void setPrivileges(Set 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 * diff --git a/api/src/main/resources/hibernate.cfg.xml b/api/src/main/resources/hibernate.cfg.xml index 745c7de7372e..14e81badde20 100644 --- a/api/src/main/resources/hibernate.cfg.xml +++ b/api/src/main/resources/hibernate.cfg.xml @@ -59,7 +59,6 @@ - diff --git a/api/src/main/resources/org/openmrs/api/db/hibernate/Role.hbm.xml b/api/src/main/resources/org/openmrs/api/db/hibernate/Role.hbm.xml deleted file mode 100644 index 7bcdfe236b79..000000000000 --- a/api/src/main/resources/org/openmrs/api/db/hibernate/Role.hbm.xml +++ /dev/null @@ -1,65 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/api/src/test/java/org/openmrs/api/OrderServiceTest.java b/api/src/test/java/org/openmrs/api/OrderServiceTest.java index 3f35d098d2a1..7dc8b2348ba5 100644 --- a/api/src/test/java/org/openmrs/api/OrderServiceTest.java +++ b/api/src/test/java/org/openmrs/api/OrderServiceTest.java @@ -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; @@ -2656,6 +2657,7 @@ public void saveOrder_shouldFailIfTheJavaTypeOfThePreviousOrderDoesNotMatch() th .addAnnotatedClass(PersonAddress.class) .addAnnotatedClass(PersonAttributeType.class) .addAnnotatedClass(User.class) + .addAnnotatedClass(Role.class) .getMetadataBuilder().build();