View Javadoc

1   package com.atlassian.user.impl.hibernate.properties;
2   
3   import java.util.HashMap;
4   
5   import com.atlassian.user.Entity;
6   import com.atlassian.user.EntityException;
7   import com.atlassian.user.ExternalEntity;
8   import com.atlassian.user.User;
9   import com.atlassian.user.UserManager;
10  import com.atlassian.user.impl.hibernate.DefaultExternalEntityDAO;
11  import com.atlassian.user.impl.hibernate.DefaultHibernateUser;
12  import com.atlassian.user.impl.hibernate.ExternalEntityDAO;
13  import com.atlassian.user.impl.hibernate.HibernateUserManager;
14  import com.atlassian.user.impl.hibernate.repository.HibernateRepository;
15  import com.atlassian.user.properties.PropertySetFactory;
16  import com.opensymphony.module.propertyset.PropertySet;
17  import com.opensymphony.module.propertyset.PropertySetManager;
18  
19  public class HibernatePropertySetFactory implements PropertySetFactory
20  {
21      public static final String HIBERNATE_PROPERTY_SET = "hibernate";
22      public static final String EXTERNAL_ENTITY = DefaultExternalEntityDAO.EXTERNAL_ENTITY_TYPE;
23      public static final String LOCAL_USER = "LOC";
24  
25      protected final UserManager userManager;
26      protected final ExternalEntityDAO externalEntityDAO;
27      protected final HibernateRepository repository;
28  
29      public HibernatePropertySetFactory(UserManager userManager, ExternalEntityDAO externalEntityDAO, HibernateRepository repository)
30      {
31          this.userManager = userManager;
32          this.externalEntityDAO = externalEntityDAO;
33          this.repository = repository;
34      }
35  
36      public PropertySet getPropertySet(Entity entity) throws EntityException
37      {
38          return getPropertySet(entity.getName());
39      }
40  
41      /**
42       * Retrieve profile for User with given name.
43       */
44      protected PropertySet getPropertySet(String entityName) throws EntityException
45      {
46          PropertySet propertySet;
47  
48          HashMap<String, Object> args = new HashMap<String, Object>();
49  
50          User user = null;
51          if (userManager instanceof HibernateUserManager)
52          {
53              //attain the local user, as we need to ensure that this user is handled by this database
54              //so that we can use the 'id' as a reference to pass to get the user's propertyset
55              //this is due to propertysets needing a numeric id
56              user = userManager.getUser(entityName);
57              if (user != null)
58              {
59                  args.put("entityId", ((DefaultHibernateUser) user).getId());
60                  args.put("entityName", LOCAL_USER + "_" + user.getName());
61                  args.put("configurationProvider", repository.getHibernateConfigurationProvider());
62              }
63          }
64  
65          if (user == null)
66          {
67              //these objects will be built via the call to Hibernate
68              ExternalEntity externalEntity = externalEntityDAO.getExternalEntity(entityName);
69  
70              if (externalEntity == null)
71              {
72                  externalEntity = externalEntityDAO.createExternalEntity(entityName);
73              }
74  
75              args.put("entityId", externalEntity.getId());
76              args.put("entityName", externalEntity.getType() + "_" + externalEntity.getName());
77              args.put("configurationProvider", repository.getHibernateConfigurationProvider());
78          }
79  
80          propertySet = getPropertySet(args);
81  
82          return propertySet;
83      }
84  
85      protected PropertySet getPropertySet(HashMap args)
86      {
87          return PropertySetManager.getInstance(HIBERNATE_PROPERTY_SET, args);
88      }
89  }