From b4f5fe99778e4fbc00c6d9ed45db1e394ee8c48d Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Mon, 30 Jun 2014 13:12:42 -0400 Subject: [PATCH] model fix for caching --- .../org/keycloak/models/jpa/ApplicationAdapter.java | 10 +++++----- .../java/org/keycloak/models/jpa/ClientAdapter.java | 11 +++++++---- .../java/org/keycloak/models/jpa/RealmAdapter.java | 5 +++-- .../java/org/keycloak/models/jpa/RoleAdapter.java | 11 +++++++++-- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/ApplicationAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/ApplicationAdapter.java index 11ee6f07b9..0ebe0170be 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/ApplicationAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/ApplicationAdapter.java @@ -130,15 +130,14 @@ public class ApplicationAdapter extends ClientAdapter implements ApplicationMode @Override public boolean removeRole(RoleModel roleModel) { - RoleAdapter roleAdapter = (RoleAdapter)roleModel; if (roleModel == null) { return false; } - if (!roleAdapter.getContainer().equals(this)) return false; + if (!roleModel.getContainer().equals(this)) return false; - if (!roleAdapter.getRole().isApplicationRole()) return false; + RoleEntity role = RoleAdapter.toRoleEntity(roleModel, em); + if (!role.isApplicationRole()) return false; - RoleEntity role = roleAdapter.getRole(); applicationEntity.getRoles().remove(role); applicationEntity.getDefaultRoles().remove(role); @@ -209,7 +208,8 @@ public class ApplicationAdapter extends ClientAdapter implements ApplicationMode return; } } - entities.add(((RoleAdapter) role).getRole()); + RoleEntity roleEntity = RoleAdapter.toRoleEntity(role, em); + entities.add(roleEntity); em.flush(); } diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/ClientAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/ClientAdapter.java index 4360882cc8..2e996580a5 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/ClientAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/ClientAdapter.java @@ -7,6 +7,7 @@ import org.keycloak.models.RoleModel; import org.keycloak.models.UserSessionModel; import org.keycloak.models.jpa.entities.ClientEntity; import org.keycloak.models.jpa.entities.ClientUserSessionAssociationEntity; +import org.keycloak.models.jpa.entities.RoleEntity; import org.keycloak.models.jpa.entities.ScopeMappingEntity; import javax.persistence.EntityManager; @@ -215,7 +216,8 @@ public abstract class ClientAdapter implements ClientModel { if (hasScope(role)) return; ScopeMappingEntity entity = new ScopeMappingEntity(); entity.setClient(getEntity()); - entity.setRole(((RoleAdapter) role).getRole()); + RoleEntity roleEntity = RoleAdapter.toRoleEntity(role, em); + entity.setRole(roleEntity); em.persist(entity); em.flush(); em.detach(entity); @@ -223,7 +225,7 @@ public abstract class ClientAdapter implements ClientModel { @Override public void deleteScopeMapping(RoleModel role) { - TypedQuery query = getRealmScopeMappingQuery((RoleAdapter) role); + TypedQuery query = getRealmScopeMappingQuery(role); List results = query.getResultList(); if (results.size() == 0) return; for (ScopeMappingEntity entity : results) { @@ -231,10 +233,11 @@ public abstract class ClientAdapter implements ClientModel { } } - protected TypedQuery getRealmScopeMappingQuery(RoleAdapter role) { + protected TypedQuery getRealmScopeMappingQuery(RoleModel role) { TypedQuery query = em.createNamedQuery("hasScope", ScopeMappingEntity.class); query.setParameter("client", getEntity()); - query.setParameter("role", ((RoleAdapter) role).getRole()); + RoleEntity roleEntity = RoleAdapter.toRoleEntity(role, em); + query.setParameter("role", roleEntity); return query; } diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java index 18fc1db8ff..4ac868d874 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java @@ -527,7 +527,8 @@ public class RealmAdapter implements RealmModel { return; } } - entities.add(((RoleAdapter) role).getRole()); + RoleEntity roleEntity = RoleAdapter.toRoleEntity(role, em); + entities.add(roleEntity); em.flush(); } @@ -924,7 +925,7 @@ public class RealmAdapter implements RealmModel { } if (!role.getContainer().equals(this)) return false; - RoleEntity roleEntity = ((RoleAdapter) role).getRole(); + RoleEntity roleEntity = RoleAdapter.toRoleEntity(role, em); realm.getRoles().remove(role); realm.getDefaultRoles().remove(role); diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/RoleAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/RoleAdapter.java index 2b12a1b207..28a1f47d02 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/RoleAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/RoleAdapter.java @@ -66,7 +66,7 @@ public class RoleAdapter implements RoleModel { @Override public void addCompositeRole(RoleModel role) { - RoleEntity entity = ((RoleAdapter)role).getRole(); + RoleEntity entity = RoleAdapter.toRoleEntity(role, em); for (RoleEntity composite : getRole().getCompositeRoles()) { if (composite.equals(entity)) return; } @@ -76,7 +76,7 @@ public class RoleAdapter implements RoleModel { @Override public void removeCompositeRole(RoleModel role) { - RoleEntity entity = ((RoleAdapter)role).getRole(); + RoleEntity entity = RoleAdapter.toRoleEntity(role, em); Iterator it = getRole().getCompositeRoles().iterator(); while (it.hasNext()) { if (it.next().equals(entity)) it.remove(); @@ -125,4 +125,11 @@ public class RoleAdapter implements RoleModel { public int hashCode() { return getId().hashCode(); } + + public static RoleEntity toRoleEntity(RoleModel model, EntityManager em) { + if (model instanceof RoleAdapter) { + return ((RoleAdapter)model).getRole(); + } + return em.getReference(RoleEntity.class, model.getId()); + } }