[seam-commits] Seam SVN: r13397 - modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Wed Jul 14 21:31:24 EDT 2010


Author: shane.bryzak at jboss.com
Date: 2010-07-14 21:31:24 -0400 (Wed, 14 Jul 2010)
New Revision: 13397

Added:
   modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectRelationshipTypeImpl.java
Modified:
   modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectImpl.java
   modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectRelationshipImpl.java
   modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectTypeImpl.java
   modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java
Log:
implement resolveRelationships


Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectImpl.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectImpl.java	2010-07-15 01:30:40 UTC (rev 13396)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectImpl.java	2010-07-15 01:31:24 UTC (rev 13397)
@@ -21,6 +21,9 @@
    
    public IdentityObjectImpl(String id, String name, IdentityObjectType type)
    {
+      if (name == null) throw new IllegalArgumentException("IdentityObject.name cannot be null");
+      if (type == null) throw new IllegalArgumentException("IdentityObject.identityType cannot be null");
+      
       this.id = id;
       this.name = name;
       this.type = type;
@@ -45,4 +48,24 @@
    {
 
    }
+   
+   @Override
+   public boolean equals(Object value)
+   {
+      if (!(value instanceof IdentityObject)) return false;
+      IdentityObject other = (IdentityObject) value;
+      
+      return (id != null ? id.equals(other.getId()) : other.getId() == null) && 
+             name.equals(other.getName()) &&
+             type.equals(other.getIdentityType());
+   }
+   
+   @Override
+   public int hashCode()
+   {
+      int hash = 0;
+      if (id != null) hash ^= (id.hashCode() * 17);
+      hash ^= (name.hashCode() * 29) ^ (type.hashCode() * 37);      
+      return hash;
+   }
 }

Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectRelationshipImpl.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectRelationshipImpl.java	2010-07-15 01:30:40 UTC (rev 13396)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectRelationshipImpl.java	2010-07-15 01:31:24 UTC (rev 13397)
@@ -23,6 +23,10 @@
          IdentityObject toIdentityObject, String name, 
          IdentityObjectRelationshipType type)
    {
+      if (fromIdentityObject == null) throw new IllegalArgumentException("IdentityObjectRelationship.fromIdentityObject cannot be null.");
+      if (toIdentityObject == null) throw new IllegalArgumentException("IdentityObjectRelationship.toIdentityObject cannot be null.");
+      if (type == null) throw new IllegalArgumentException("IdentityObjectRelationship.type cannot be null.");
+      
       this.fromIdentityObject = fromIdentityObject;
       this.toIdentityObject = toIdentityObject;
       this.name = name;
@@ -48,4 +52,37 @@
    {
       return type;
    }
+   
+   @Override
+   public boolean equals(Object value)
+   {
+      if (!(value instanceof IdentityObjectRelationship)) return false;
+      IdentityObjectRelationship other = (IdentityObjectRelationship) value;
+      
+      if (!fromIdentityObject.equals(other.getFromIdentityObject())) return false;
+      if (!toIdentityObject.equals(other.getToIdentityObject())) return false;
+      if (!type.equals(other.getType())) return false;
+      if (name == null)
+      {
+         if (other.getName() != null) return false;
+      }
+      else
+      {
+         if (!name.equals(other.getName())) return false;
+      }
+      
+      return true;
+   }
+   
+   @Override
+   public int hashCode()
+   {
+      int hash = (fromIdentityObject.hashCode() * 11) ^
+             (toIdentityObject.hashCode() * 17) ^
+             (type.hashCode() * 23);
+      
+      if (name != null) hash ^= (name.hashCode() * 29);
+      
+      return hash;
+   }
 }

Added: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectRelationshipTypeImpl.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectRelationshipTypeImpl.java	                        (rev 0)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectRelationshipTypeImpl.java	2010-07-15 01:31:24 UTC (rev 13397)
@@ -0,0 +1,45 @@
+package org.jboss.seam.security.management;
+
+import java.io.Serializable;
+
+import org.picketlink.idm.spi.model.IdentityObjectRelationshipType;
+
+/**
+ * Simple implementation of IdentityObjectRelationshipType
+ * 
+ * @author Shane Bryzak
+ */
+public class IdentityObjectRelationshipTypeImpl implements IdentityObjectRelationshipType, Serializable
+{
+   private static final long serialVersionUID = 6389479876202629001L;
+
+   private String name;
+   
+   public IdentityObjectRelationshipTypeImpl(String name)
+   {
+      if (name == null) throw new IllegalArgumentException("IdentityObjectRelationshipType.name cannot be null.");
+      
+      this.name = name;
+   }
+   
+   public String getName()
+   {
+      return name;
+   }
+   
+   @Override
+   public boolean equals(Object value)
+   {
+      if (!(value instanceof IdentityObjectRelationshipType)) return false;
+      IdentityObjectRelationshipType other = (IdentityObjectRelationshipType) value;
+      
+      return name.equals(other.getName());
+   }
+   
+   @Override
+   public int hashCode()
+   {
+      return name.hashCode();
+   }
+
+}

Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectTypeImpl.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectTypeImpl.java	2010-07-15 01:30:40 UTC (rev 13396)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/IdentityObjectTypeImpl.java	2010-07-15 01:31:24 UTC (rev 13397)
@@ -17,6 +17,7 @@
    
    public IdentityObjectTypeImpl(String name)
    {
+      if (name == null) throw new IllegalArgumentException("IdentityObjectType name cannot be null");      
       this.name = name;
    }
    
@@ -24,4 +25,18 @@
    {
       return name;
    }
+   
+   @Override
+   public boolean equals(Object value)
+   {
+      if (!(value instanceof IdentityObjectType)) return false;
+      IdentityObjectType other = (IdentityObjectType) value;
+      return name.equals(other.getName());
+   }
+   
+   @Override
+   public int hashCode()
+   {
+      return name.hashCode();
+   }
 }

Modified: modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java
===================================================================
--- modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java	2010-07-15 01:30:40 UTC (rev 13396)
+++ modules/security/trunk/impl/src/main/java/org/jboss/seam/security/management/JpaIdentityStore.java	2010-07-15 01:31:24 UTC (rev 13397)
@@ -6,6 +6,7 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -86,6 +87,87 @@
    private static final String PROPERTY_ATTRIBUTE_VALUE = "ATTRIBUTE_VALUE";
    private static final String PROPERTY_ROLE_TYPE_NAME = "ROLE_TYPE_NAME";
    
+   
+   private class EntityToSpiConverter
+   {
+      private static final String IDENTITY_TYPE_CACHE_PREFIX = "identity_type:";
+      private static final String RELATIONSHIP_TYPE_CACHE_PREFIX = "relationship_type:";
+      
+      private Map<Object,Object> cache = new HashMap<Object,Object>();
+      
+      private Property<?> identityIdProperty = modelProperties.get(PROPERTY_IDENTITY_ID);
+      private Property<?> identityNameProperty = modelProperties.get(PROPERTY_IDENTITY_NAME);
+      private Property<?> identityTypeProperty = modelProperties.get(PROPERTY_IDENTITY_TYPE);
+      private Property<?> identityTypeNameProperty = modelProperties.get(PROPERTY_IDENTITY_TYPE_NAME);
+      private Property<?> relationshipTypeNameProperty = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE_NAME);
+      
+      public IdentityObject convertToIdentityObject(Object entity)
+      {
+         if (!identityClass.isAssignableFrom(entity.getClass())) 
+         {
+            throw new IllegalArgumentException("Invalid identity entity");
+         }
+         
+         if (cache.containsKey(entity))
+         {
+            return (IdentityObject) cache.get(entity);
+         }
+         else
+         {         
+            IdentityObject obj = new IdentityObjectImpl(
+               (String) identityIdProperty.getValue(entity),
+               (String) identityNameProperty.getValue(entity),
+               convertToIdentityObjectType(identityTypeProperty.getValue(entity)));
+            cache.put(entity, obj);
+            
+            return obj;            
+         }
+      }
+      
+      public IdentityObjectType convertToIdentityObjectType(Object value)
+      {
+         if (value instanceof String)
+         {
+            String key = IDENTITY_TYPE_CACHE_PREFIX + (String) value; 
+            if (cache.containsKey(key)) return (IdentityObjectType) cache.get(key);
+            
+            IdentityObjectType type = new IdentityObjectTypeImpl((String) value);
+            cache.put(key, type);
+            return type;
+         }
+         else
+         {
+            if (cache.containsKey(value)) return (IdentityObjectType) cache.get(value);
+            IdentityObjectType type = new IdentityObjectTypeImpl(
+                  (String) identityTypeNameProperty.getValue(value));
+            cache.put(value, type);
+            return type;
+         }
+      }
+      
+      public IdentityObjectRelationshipType convertToRelationshipType(Object value)
+      {
+         if (value instanceof String)
+         {
+            String key = RELATIONSHIP_TYPE_CACHE_PREFIX + (String) value;
+            if (cache.containsKey(key)) return (IdentityObjectRelationshipType) cache.get(key);
+            
+            IdentityObjectRelationshipType type = new IdentityObjectRelationshipTypeImpl((String) value);
+            cache.put(key, type);
+            return type;
+         }
+         else
+         {
+            if (cache.containsKey(value)) return (IdentityObjectRelationshipType) cache.get(value);
+            IdentityObjectRelationshipType type = new IdentityObjectRelationshipTypeImpl(
+                  (String) relationshipTypeNameProperty.getValue(value));
+            cache.put(value, type);
+            return type;
+         }
+      }
+   }
+   
+   
    private String id;
       
    // Entity classes   
@@ -1083,12 +1165,6 @@
          return null;
       }
    }
-   
-   protected IdentityObjectType convertType(Object obj)
-   {
-      // TODO implement
-      return null;
-   }
 
    public IdentityObject findIdentityObject(
          IdentityStoreInvocationContext invocationContext, String name,
@@ -1317,13 +1393,63 @@
    }
 
    public Set<IdentityObjectRelationship> resolveRelationships(
-         IdentityStoreInvocationContext invocationCxt,
+         IdentityStoreInvocationContext ctx,
          IdentityObject fromIdentity, IdentityObject toIdentity,
          IdentityObjectRelationshipType relationshipType)
          throws IdentityException
    {
-      // TODO Auto-generated method stub
-      return null;
+      Set<IdentityObjectRelationship> relationships = new HashSet<IdentityObjectRelationship>();
+      
+      EntityManager em = getEntityManager(ctx);
+      
+      CriteriaBuilder builder = em.getCriteriaBuilder();
+      CriteriaQuery<?> criteria = builder.createQuery(relationshipClass);
+      Root<?> root = criteria.from(relationshipClass);
+      
+      Property<?> relationshipFromProp = modelProperties.get(PROPERTY_RELATIONSHIP_FROM);
+      Property<?> relationshipToProp = modelProperties.get(PROPERTY_RELATIONSHIP_TO);
+      Property<?> relationshipTypeProp = modelProperties.get(PROPERTY_RELATIONSHIP_TYPE);
+      Property<?> relationshipNameProp = modelProperties.get(PROPERTY_RELATIONSHIP_NAME);
+      
+      List<Predicate> predicates = new ArrayList<Predicate>();
+      
+      if (fromIdentity != null)
+      {
+         predicates.add(builder.equal(root.get(relationshipFromProp.getName()), 
+            lookupIdentity(fromIdentity, em)));
+      }
+      
+      if (toIdentity != null)
+      {
+         predicates.add(builder.equal(root.get(relationshipToProp.getName()),
+            lookupIdentity(toIdentity, em)));
+      }
+      
+      if (relationshipType != null)
+      {
+         predicates.add(builder.equal(root.get(relationshipTypeProp.getName()),
+               lookupRelationshipType(relationshipType, em)));
+      }
+      
+      criteria.where(predicates.toArray(new Predicate[0]));
+      
+      List<?> results = em.createQuery(criteria).getResultList();
+      
+      EntityToSpiConverter converter = new EntityToSpiConverter();
+      
+      for (Object result : results)
+      {
+         IdentityObjectRelationship relationship = new IdentityObjectRelationshipImpl(
+               converter.convertToIdentityObject(relationshipFromProp.getValue(result)),
+               converter.convertToIdentityObject(relationshipToProp.getValue(result)),
+               (String) relationshipNameProp.getValue(result),
+               converter.convertToRelationshipType(relationshipTypeProp.getValue(result))         
+         );
+         
+         relationships.add(relationship);
+      }
+      
+      return relationships;
    }
 
    public Set<IdentityObjectRelationship> resolveRelationships(



More information about the seam-commits mailing list