KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > commonimpl > user > RolesUtil


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.commonimpl.user;
17
18 import org.outerj.daisy.repository.user.Role;
19 import org.outerj.daisy.repository.user.Roles;
20
21 /**
22  * A set of utility methods for working with Roles objects.
23  */

24 public class RolesUtil {
25     private static Role[] roles;
26     /**
27      * returns a specific role from a roles object.
28      *
29      * @param roles the roles object to search the role in
30      * @param roleName the name of the role object to search in the roles object
31      * @return the found Role, or null if the role with the specified name
32      * was not found in the Roles object.
33      */

34     public static Role getRole(Roles rolesToSearch, String JavaDoc roleName) {
35         if (rolesToSearch == null || roleName == null)
36             throw new IllegalArgumentException JavaDoc("One of the arguments was null!");
37
38         roles = rolesToSearch.getArray();
39         if (roles.length==0) return null;
40         else {
41             int i=0;
42             Role r = null;
43             boolean nameFound = false;
44             
45             while (i<roles.length && !nameFound) {
46                 r=roles[i];
47                 if (r.getName().equals(roleName))
48                     nameFound=true;
49                 i++;
50             }
51             
52             if(!nameFound) return null;
53             else return r;
54         }
55
56     }
57
58     /**
59      * returns a specific role from a roles object.
60      *
61      * @param roles the roles object to search the role in
62      * @param roleId the data store id of the role object to search in the roles object
63      * @return the found Role, or null if the role with the specified id
64      * was not found in the Roles object.
65      */

66     public static Role getRole(Roles rolesToSearch, long roleId) {
67         if (rolesToSearch == null)
68             throw new IllegalArgumentException JavaDoc("Roles argument was null!");
69
70         roles = rolesToSearch.getArray();
71         if (roles.length==0) return null;
72         else {
73             int i=0;
74             Role r = null;
75             boolean roleFound = false;
76             
77             while (i<roles.length && !roleFound) {
78                 r=roles[i];
79                 if (r.getId() == roleId)
80                     roleFound=true;
81                 i++;
82             }
83             
84             if(!roleFound) return null;
85             else return r;
86         }
87
88     }
89         
90 }
91
Popular Tags