View Javadoc
1   package edu.internet2.middleware.grouper.pspng;
2   
3   import java.util.*;
4   
5   import edu.internet2.middleware.grouper.Group;
6   import edu.internet2.middleware.grouper.Member;
7   import edu.internet2.middleware.grouper.pit.PITGroup;
8   import edu.internet2.middleware.grouper.util.GrouperUtil;
9   import org.apache.commons.lang.StringUtils;
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  /**
14   * This is a simple class to hold name and attribute information of a group.
15   * This is useful because it provides common data whether the group is an
16   * existing group or has been deleted and the information is coming from a
17   * PITGroup
18   * 
19   * @author bert
20   *
21   */
22  public class GrouperGroupInfo {
23    private static final Logger LOG = LoggerFactory.getLogger(GrouperGroupInfo.class);
24  
25    private final Group group;
26    private final PITGroup pitGroup;
27  
28    public final String name;
29    public Long idIndex;
30  
31    Map<String, Object> groupAttributes;
32    Map<String, Object> stemAttributes;
33  
34    public GrouperGroupInfo(Group g) {
35      if ( g != null )
36        name = g.getName();
37      else
38        name = "null group";
39      
40      this.group = g;
41      this.idIndex = g.getIdIndex();
42      this.pitGroup = null;
43    }
44  
45    public GrouperGroupInfo(PITGroup g) {
46      if ( g != null )
47        name = g.getName();
48      else
49        name = "null group";
50      
51      this.pitGroup = g;
52      this.group = null;
53    }
54  
55    // Create a barebones group info when neither Group nor PITGroup can be found
56    // This is used when groups are deleted, but PIT information is not available.
57    public GrouperGroupInfo(String name, Long idIndex) {
58      this.pitGroup = null;
59      this.group = null;
60  
61      this.name = name;
62      this.idIndex = idIndex;
63    }
64  
65  
66    public String getName() {
67      return name;
68    }
69  
70    @Override
71    public String toString() {
72      if (pitGroup != null)
73        return String.format("%s/#%d(PIT)", getName(), idIndex);
74      else if ( group != null )
75        return String.format("%s/#%d(Existing)", getName(), idIndex);
76      else
77        return String.format("%s/#%d(Raw)", getName(), idIndex);
78    }
79  
80    @Override
81    public int hashCode() {
82      final int prime = 31;
83      int result = 1;
84      result = prime * result + ((name == null) ? 0 : name.hashCode());
85      return result;
86    }
87  
88    @Override
89    public boolean equals(Object obj) {
90      if (this == obj)
91        return true;
92      if (obj == null)
93        return false;
94      if (getClass() != obj.getClass())
95        return false;
96      GrouperGroupInfo../../edu/internet2/middleware/grouper/pspng/GrouperGroupInfo.html#GrouperGroupInfo">GrouperGroupInfo other = (GrouperGroupInfo) obj;
97      if (name == null) {
98        if (other.name != null)
99          return false;
100     }
101     else if (!name.equals(other.name))
102       return false;
103     return true;
104   }
105 
106   public Set<Member> getMembers() {
107     if ( group != null ) {
108       return group.getMembers();
109     }
110     else {
111       return new HashSet<>();
112     }
113   }
114 
115   public Map<String, Object> getJexlMap() {
116     Map<String, Object> result = new HashMap<String, Object>();
117 
118     if ( group != null ) {
119       result.put("group", group);
120       result.put("name", group.getName());
121       result.put("displayName", group.getDisplayName());
122 
123       result.put("extension", group.getExtension());
124       result.put("displayExtension", group.getDisplayExtension());
125 
126       result.put("description", group.getDescription());
127 
128       if ( group.getIdIndex() != null )
129         result.put("idIndex", idIndex);
130 
131       result.put("groupId", group.getId());
132       
133     }
134     else if ( pitGroup != null ) {
135       result.put("pitGroup", pitGroup);
136       result.put("name", pitGroup.getName());
137 
138       result.put("idIndex", idIndex);
139       result.put("groupId", pitGroup.getSourceId());
140 
141       result.put("extension", GrouperUtil.extensionFromName(pitGroup.getName()));
142 
143       // Make display properties the same... since the group has been deleted
144       result.put("displayName", result.get("name"));
145       result.put("displayExtension", result.get("extension"));
146 
147 
148       Map<String, Object> groupAttributes = PspUtils.getGroupAttributes(pitGroup);
149       if ( groupAttributes != null )
150         result.put("groupAttributes", groupAttributes);
151       else
152         result.put("groupAttributes", Collections.EMPTY_MAP);
153       
154       // Old stem attributes probably don't matter since group has been deleted
155       result.put("stemAttributes", Collections.EMPTY_MAP);
156       
157     }
158     else if ( name != null ) {
159       // This is used when groups are deleted, but PIT information is not available.
160 
161       result.put("name", name);
162       result.put("extension",GrouperUtil.extensionFromName(name) );
163 
164       // Make display properties the same... since that is all we have
165       result.put("displayName", result.get("name"));
166       result.put("displayExtension", result.get("extension"));
167 
168 
169       // Maintain compatibility with expressions with some bogus values
170       result.put("idIndex", -999999);
171       result.put("groupId", "-9a9a9a9a9a9a");
172       result.put("group", null);
173       result.put("pitGroup", null);
174 
175       result.put("stemAttributes", Collections.EMPTY_MAP);
176       result.put("groupAttributes", Collections.EMPTY_MAP);
177     }
178     return result;
179   }
180 
181   public boolean hasGroupBeenDeleted() {
182     return pitGroup != null;
183   }
184 
185   public Group getGrouperGroup() {
186     return group;
187   }
188 
189   /**
190    * This method rereads the Grouper objects from the database in order to
191    * avoid L2 caching when database objects change.
192    */
193   public void hibernateRefresh() {
194     final Object objectToHibernateRefresh;
195 
196     if ( group != null ) {
197       objectToHibernateRefresh = group;
198     } else {
199       objectToHibernateRefresh = pitGroup;
200     }
201 
202     if ( objectToHibernateRefresh != null ) {
203       PspUtils.hibernateRefresh(objectToHibernateRefresh);
204     }
205   }
206 
207 }