KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > workflow > OwnerFactory


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23 package org.infoglue.cms.util.workflow;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.infoglue.cms.security.InfoGlueGroup;
31 import org.infoglue.cms.security.InfoGluePrincipal;
32 import org.infoglue.cms.security.InfoGlueRole;
33
34 /**
35  * A principal owner indicates that only the associated principal has access to the workflow.
36  */

37 class PrincipalOwner implements Owner
38 {
39     /**
40      * The principal.
41      */

42     private final InfoGluePrincipal principal;
43
44     /**
45      * Constructs an owner object for the specified principal.
46      *
47      * @param principal the principal.
48      */

49     PrincipalOwner(final InfoGluePrincipal principal)
50     {
51         this.principal = principal;
52     }
53     
54     /**
55      * Returns the identifier of the owner.
56      *
57      * @return the identifier of the owner.
58      */

59     public String JavaDoc getIdentifier()
60     {
61         return principal.getName();
62     }
63 }
64
65 /**
66  * A role owner indicates that all principals that are members of the role has access to the workflow.
67  * If a group is specified, then only principals that are members of both the role and the group has access to the workflow.
68  *
69  * TODO NOTE NOTE NOTE! Will not work very well if any user/role/group contains '\' characters
70  */

71 class RoleOwner implements Owner
72 {
73     /**
74      *
75      */

76     private static final String JavaDoc PREFIX = "\\";
77     
78     /**
79      * The role.
80      */

81     private final InfoGlueRole role;
82     
83     /**
84      * The group; null indicates all groups.
85      */

86     private final InfoGlueGroup group;
87     
88     /**
89      * Constructs a role owner for the specified role.
90      *
91      * @param role the role.
92      */

93     RoleOwner(final InfoGlueRole role)
94     {
95         this(role, null);
96     }
97     
98     /**
99      * Constructs a role owner for the specified role and group.
100      *
101      * @param role the role.
102      * @param group the group.
103      */

104     RoleOwner(final InfoGlueRole role, final InfoGlueGroup group)
105     {
106         this.role = role;
107         this.group = group;
108     }
109     
110     /**
111      * Returns the identifier of the owner.
112      *
113      * @return the identifier of the owner.
114      */

115     public String JavaDoc getIdentifier()
116     {
117         if(group == null)
118         {
119             return PREFIX + role.getName();
120         }
121         else
122         {
123             return PREFIX + role.getName() + PREFIX + group.getName();
124         }
125     }
126 }
127
128 /**
129  * Factory for creating workflow owners.
130  */

131 public class OwnerFactory
132 {
133     /**
134      * Static class; don't allow instantiation.
135      */

136     private OwnerFactory()
137     {
138     }
139
140     /**
141      * Creates a principal owner.
142      *
143      * @param principal the principal.
144      * @return the owner.
145      */

146     public static final Owner create(final InfoGluePrincipal principal)
147     {
148         return new PrincipalOwner(principal);
149     }
150
151     /**
152      * Creates a role owner for all groups.
153      *
154      * @param role the role.
155      * @return the owner.
156      */

157     public static final Owner create(final InfoGlueRole role)
158     {
159         return new RoleOwner(role);
160     }
161
162     /**
163      * Creates a role owner for the specified group.
164      *
165      * @param role the role.
166      * @param group the group.
167      * @return the owner.
168      */

169     public static final Owner create(final InfoGlueRole role, final InfoGlueGroup group)
170     {
171         return new RoleOwner(role, group);
172     }
173
174     /**
175      * Creates all possible owner objects for the specified principal.
176      *
177      * @param principal the principal.
178      * @return all owners.
179      */

180     public static final Collection JavaDoc createAll(final InfoGluePrincipal principal)
181     {
182         final List JavaDoc owners = new ArrayList JavaDoc();
183         owners.add(OwnerFactory.create(principal));
184         for(final Iterator JavaDoc roleIterator = principal.getRoles().iterator(); roleIterator.hasNext(); )
185         {
186             final InfoGlueRole role = (InfoGlueRole) roleIterator.next();
187             owners.add(create(role));
188             for(final Iterator JavaDoc groupIterator = principal.getGroups().iterator(); groupIterator.hasNext(); )
189             {
190                 final InfoGlueGroup group = (InfoGlueGroup) groupIterator.next();
191                 owners.add(create(role, group));
192             }
193         }
194         return owners;
195     }
196     
197 }
198
Popular Tags