0 Replies Latest reply on Feb 26, 2014 6:27 AM by Richard Clarke

    How to link external entities to Users in PicketLink IDM

    Richard Clarke Newbie

      I am fairly new to Picketlink and am trying to implement PicketLink IDM (ver 2.5.2.Final) with JPA (MySQL) as a identity store.

      I am running this under JBOSS EAP 6.2.

       

      I am using the simple schema (modified with a couple of extra properties) and have created my own 'Users' class which is correctly persisted to the database in the PicketLink schema tables (I can see the user's details in the AccountTypeEntity table and retrieve the Users object using Identity Manager).

       

      I have another table in the same database, 'Cases' which is mapped using hibernate and accessed via entity manager.

      The Cases table requires an owner which I want to define as a foreign key to a user object, however I don't have a Users table since my users are mapped using PicketLink IDM.

      I can see the users in the AccountTypeEntity table, and have tried creating a foreign key to this table, but I can't see how to retrieve this object without reverting to mapping this table using hibernate and using entity manager find call (which doesn't seem the right way to go).

       

      I have tried the following (this creates a new Case, retrieves a Users object using IdentityManager (which works fine), but the lookupIdentityById

      method doesn't take a AccountTypeEntity class as a parameter (it requires an IdentityType).

       

         

              Cases newCase = new Cases();
              newCase.setName("test");
           
              IdentityManager identityManager = this.partitionManager.createIdentityManager();
              IdentityQuery<Users> query = identityManager.<Users>createIdentityQuery(Users.class);
           
              query.setParameter(Users.LOGIN_NAME, "richyclarke2");
              List<Users> result = query.getResultList();
      
      
              // there should be only one user
      
              for (Users thisUser : result)
              {
                  AccountTypeEntity theATE;
                  theATE = identityManager.lookupIdentityById(AccountTypeEntity.class, thisUser.getId());
                  newCase.setUserid(theATE);
              }
      
      
      

       

      Am I missing something? I may be going about this the wrong way, but I can't find any examples, quickstarts or documentation which covers this use case.

      Can anyone point me in the right direction here.

       

      My 'Users' Class...

       


      package com.vesey.proequip.model;
      
      import org.picketlink.idm.model.annotation.AttributeProperty;
      import org.picketlink.idm.model.basic.Agent;
      
      
      public class Users extends Agent
      {
          private String firstName;
          private String lastName;
          private String email;
          private String telephone;
          private Boolean admin;
      
          public Users() { }
      
      
          public Users(String loginName) {
              super(loginName);
          }
      
          @AttributeProperty
          public String getFirstName()
          {
              return firstName;
          }
      
      
          public void setFirstName(String firstName)
          {
              this.firstName = firstName;
          }
      
          @AttributeProperty
          public String getLastName()
          {
              return lastName;
          }
      
          public void setLastName(String lastName)
          {
              this.lastName = lastName;
          }
      
          @AttributeProperty
          public String getEmail()
          {
              return email;
          }
      
          public void setEmail(String email)
          {
              this.email = email;
          }
      
          @AttributeProperty
          public String getTelephone()
          {
              return telephone;
          }
      
          public void setTelephone(String telephone)
          {
              this.telephone = telephone;
          }
      
          @AttributeProperty
          public Boolean getAdmin()
          {
              return admin;
          }
      
          public void setAdmin(Boolean admin)
          {
              this.admin = admin;
          }
      }
      
      
      


      and my Cases Class (auto created from database table using Netbeans) ..

       

       

      package com.vesey.proequip.generatedentity;
      
      import java.io.Serializable;
      import javax.persistence.Basic;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.NamedQueries;
      import javax.persistence.NamedQuery;
      import javax.persistence.Table;
      import javax.validation.constraints.NotNull;
      import javax.validation.constraints.Size;
      import javax.xml.bind.annotation.XmlRootElement;
      import org.picketlink.idm.jpa.model.sample.simple.AccountTypeEntity;
      
      
      /**
       *
       * @author Richard Clarke
       */
      @Entity
      @Table(name = "cases")
      @XmlRootElement
      @NamedQueries(
      {
          @NamedQuery(name = "Cases.findAll", query = "SELECT c FROM Cases c"),
          @NamedQuery(name = "Cases.findByCaseid", query = "SELECT c FROM Cases c WHERE c.caseid = :caseid"),
          @NamedQuery(name = "Cases.findByName", query = "SELECT c FROM Cases c WHERE c.name = :name")
      })
      public class Cases implements Serializable
      {
          @JoinColumn(name = "userid", referencedColumnName = "id")
          @ManyToOne(optional = false)
          private AccountTypeEntity userid;
          private static final long serialVersionUID = 1L;
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          @Basic(optional = false)
          @Column(name = "caseid")
          private Integer caseid;
          @Basic(optional = false)
          @NotNull
          @Size(min = 1, max = 45)
          @Column(name = "name")
          private String name;
      
      
          public Cases()
          {
          }
      
      
          public Cases(Integer caseid)
          {
              this.caseid = caseid;
          }
      
      
          public Cases(Integer caseid, String name)
          {
              this.caseid = caseid;
              this.name = name;
          }
      
      
          public Integer getCaseid()
          {
              return caseid;
          }
      
      
          public void setCaseid(Integer caseid)
          {
              this.caseid = caseid;
          }
      
      
          public String getName()
          {
              return name;
          }
      
      
          public void setName(String name)
          {
              this.name = name;
          }
      
      
          @Override
          public int hashCode()
          {
              int hash = 0;
              hash += (caseid != null ? caseid.hashCode() : 0);
              return hash;
          }
      
      
          @Override
          public boolean equals(Object object)
          {
              // TODO: Warning - this method won't work in the case the id fields are not set
              if (!(object instanceof Cases))
              {
                  return false;
              }
              Cases other = (Cases) object;
              if ((this.caseid == null && other.caseid != null) || (this.caseid != null && !this.caseid.equals(other.caseid)))
              {
                  return false;
              }
              return true;
          }
      
      
          @Override
          public String toString()
          {
              return "com.vesey.proequip.generatedentity.Cases[ caseid=" + caseid + " ]";
          }
      
      
          /**
           * @return the userid
           */
          public AccountTypeEntity getUserid()
          {
              return userid;
          }
      
      
          /**
           * @param userid the userid to set
           */
          public void setUserid(AccountTypeEntity userid)
          {
              this.userid = userid;
          }
      
      
      
      }