001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.security;
034:
035: import com.flexive.shared.FxArrayUtils;
036:
037: import java.io.Serializable;
038: import java.util.ArrayList;
039: import java.util.List;
040: import java.util.StringTokenizer;
041: import java.util.Arrays;
042:
043: /**
044: * A class for transporting and processing a list of roles.
045: *
046: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
047: */
048: public class RoleList implements Serializable {
049: private static final long serialVersionUID = 9101501258624365441L;
050:
051: private Role[] list = null;
052:
053: /**
054: * Constructor
055: *
056: * @param roles the roles in the list
057: */
058: public RoleList(Role[] roles) {
059: this .list = FxArrayUtils.clone(roles);
060: }
061:
062: /**
063: * Constructor
064: *
065: * @param roles the roles in the list
066: */
067: public RoleList(byte[] roles) {
068: if (roles == null)
069: roles = new byte[0];
070: list = new Role[roles.length];
071: for (int i = 0; i < roles.length; i++) {
072: list[i] = Role.getById(roles[i]);
073: }
074: }
075:
076: /**
077: * Constructor for a empts role list
078: */
079: public RoleList() {
080: list = new Role[0];
081: }
082:
083: /**
084: * Constructor
085: *
086: * @param roles the roles in the list, as Integer id objects in a array list
087: */
088: public RoleList(ArrayList roles) {
089: if (roles == null)
090: roles = new ArrayList(0);
091: list = new Role[roles.size()];
092: for (int i = 0; i < roles.size(); i++) {
093: Integer id = (Integer) roles.get(i);
094: list[i] = Role.getById(id);
095: }
096: }
097:
098: /**
099: * Constructs a role list from a string with the comma separated role id's.
100: *
101: * @param roles a list of comma separated role id's.
102: */
103: public RoleList(String roles) {
104: // Handle empty strings
105: if (roles == null || roles.length() == 0) {
106: list = new Role[0];
107: return;
108: }
109: // Build the list
110: List<Role> tmp1 = new java.util.ArrayList<Role>(20);
111: StringTokenizer st = new java.util.StringTokenizer(roles, ",",
112: false);
113: while (st.hasMoreTokens()) {
114: String value = st.nextToken();
115: Role aRole = Role.getById(Integer.valueOf(value));
116: tmp1.add(aRole);
117: }
118: list = tmp1.toArray(new Role[tmp1.size()]);
119: }
120:
121: /**
122: * Returns the amount of roles in the list.
123: *
124: * @return the amount of roles in the list
125: */
126: public int size() {
127: return list.length;
128: }
129:
130: /**
131: * Returns all roles in the list.
132: *
133: * @return all roles in the list
134: */
135: public Role[] getRoles() {
136: return FxArrayUtils.clone(list);
137: }
138:
139: /**
140: * Returns all roles as array list.
141: *
142: * @return the list
143: */
144: public ArrayList<Role> getList() {
145: if (list == null || list.length == 0) {
146: return new ArrayList<Role>(0);
147: }
148: ArrayList<Role> result = new ArrayList<Role>(list.length);
149: result.addAll(Arrays.asList(list));
150: return result;
151: }
152:
153: /**
154: * Returns the role at the given position.
155: *
156: * @param pos the position
157: * @return the role at the given position
158: */
159: public Role get(int pos) {
160: return this .list[pos];
161: }
162:
163: /**
164: * Adds a role to the end of the list, but only if its not already part of it
165: *
166: * @param roleId the role to add
167: */
168: public void add(byte roleId) {
169: // Check if the role is already part of the list
170: for (Role aList : list)
171: if (aList.getId() == roleId)
172: return;
173: // Add the role
174: Role[] tmp = new Role[list.length + 1];
175: System.arraycopy(list, 0, tmp, 0, list.length);
176: tmp[tmp.length - 1] = Role.getById(roleId);
177: list = tmp;
178: }
179:
180: /**
181: * Returns a array holding the id's of all roles in the list.
182: *
183: * @return a array holding the id's of all roles in the list
184: */
185: public long[] toIdArray() {
186: long idArr[] = new long[list.length];
187: for (int i = 0; i < list.length; i++)
188: idArr[i] = list[i].getId();
189: return idArr;
190: }
191:
192: /**
193: * Returns a array holding the names of all roles in the list.
194: *
195: * @return a array holding the names of all roles in the list
196: */
197: public String[] toNameArray() {
198: if (list == null)
199: return new String[0];
200: String idArr[] = new String[list.length];
201: for (int i = 0; i < list.length; i++)
202: idArr[i] = list[i].getName();
203: return idArr;
204: }
205:
206: /**
207: * Removes a role from the list.
208: *
209: * @param role the roles to remove from the list
210: */
211: public void remove(Role role) {
212: remove(role.getId());
213: }
214:
215: /**
216: * Removes groups from the list.
217: *
218: * @param role the groups to remove from the list
219: */
220: public void remove(Role role[]) {
221: for (Role aRole : role)
222: remove(aRole.getId());
223: }
224:
225: /**
226: * Removes role identified by the unique id from the list.
227: *
228: * @param roleId the role to remove from the list
229: */
230: public void remove(long roleId[]) {
231: for (long aRoleId : roleId)
232: remove(aRoleId);
233: }
234:
235: /**
236: * Removes a role identified by its unique id from the list.
237: *
238: * @param roleId the role to remove from the list
239: */
240: public void remove(long roleId) {
241: if (list != null) {
242: int elementFound = 0;
243: while (elementFound != -1) {
244: // Look for the elemenet
245: elementFound = -1;
246: for (int i = 0; i < list.length; i++) {
247: if (list[i].getId() == roleId) {
248: elementFound = i;
249: break;
250: }
251: }
252: // Delete the element
253: if (elementFound != -1) {
254: Role tmp[] = new Role[list.length - 1];
255: int pos = 0;
256: for (int i = 0; i < list.length; i++) {
257: if (i != elementFound)
258: tmp[pos++] = list[i];
259: }
260: list = tmp;
261: }
262: }
263: }
264: }
265:
266: /**
267: * Returns a string representation of the roles.
268: *
269: * @return a string representation of the roles
270: */
271: @Override
272: public String toString() {
273: return RoleList.class + "@[" + toNameString() + "]";
274: }
275:
276: /**
277: * Returns a comma seperated list of the role names.
278: *
279: * @return a comma seperated list of the role names.
280: */
281: public String toNameString() {
282: StringBuilder result = new StringBuilder(50);
283: for (Role entry : list) {
284: if (result.length() > 0)
285: result.append(',');
286: result.append(entry.getName());
287: }
288: return result.toString();
289: }
290:
291: }
|