KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > security > UserAssistant


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on 21.06.2004
33  */

34 package com.nightlabs.ipanema.security;
35
36 import java.io.UnsupportedEncodingException JavaDoc;
37 import java.security.MessageDigest JavaDoc;
38 import java.security.NoSuchAlgorithmException JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Collection JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43
44 import javax.jdo.PersistenceManager;
45 import javax.jdo.Query;
46
47 import sun.misc.BASE64Encoder;
48
49 /**
50  * @author marco
51  */

52 public class UserAssistant
53 {
54     public static String JavaDoc encryptPassword(String JavaDoc password)
55     throws SecurityException JavaDoc
56     {
57         MessageDigest JavaDoc md = null;
58         try {
59             md = MessageDigest.getInstance("SHA");
60             md.update(password.getBytes("UTF-8"));
61         } catch(NoSuchAlgorithmException JavaDoc e) {
62             throw new SecurityException JavaDoc(e);
63         } catch(UnsupportedEncodingException JavaDoc e) {
64             throw new SecurityException JavaDoc(e);
65         }
66         byte raw[] = md.digest();
67         return (new BASE64Encoder()).encode(raw);
68     }
69     
70     public static UserSearchResult searchUsers (
71             PersistenceManager pm,
72             String JavaDoc userType,
73             String JavaDoc searchStr, boolean exact, int itemsPerPage, int pageIndex, int userIncludeMask)
74         throws SecurityException JavaDoc
75     {
76         try {
77             if ("".equals(searchStr))
78                 searchStr = null;
79             
80             if (itemsPerPage <= 0) {
81                 itemsPerPage = Integer.MAX_VALUE;
82                 pageIndex = 0;
83             }
84             
85             if (pageIndex < 0)
86                 pageIndex = 0;
87             
88             Query query = pm.newQuery(pm.getExtent(User.class, true));
89             query.declareImports("import java.lang.String");
90             query.declareParameters("String userType, String searchStr");
91             StringBuffer JavaDoc filter = new StringBuffer JavaDoc();
92             if (userType != null)
93                 filter.append("this.userType == userType");
94
95             if (userType != null && searchStr != null)
96                 filter.append(" && ");
97             
98             if (searchStr != null) {
99                 searchStr = searchStr.toLowerCase();
100                 if (exact)
101                     filter.append("this.userID.toLowerCase() == searchStr");
102                 else
103                     filter.append("this.userID.toLowerCase().indexOf(searchStr) >= 0");
104             }
105             query.setFilter(filter.toString());
106             query.setOrdering("this.organisationID ascending, this.userID ascending");
107             Collection JavaDoc c = (Collection JavaDoc)query.execute(userType, searchStr);
108             int itemsFound = c.size();
109             Iterator JavaDoc it = c.iterator();
110             List JavaDoc items = new ArrayList JavaDoc();
111             int idx = 0;
112             int firstIdx = 0; int lastIdx = Integer.MAX_VALUE;
113             if (pageIndex >= 0)
114                 firstIdx = itemsPerPage * pageIndex;
115             lastIdx = firstIdx + itemsPerPage - 1;
116
117             while (it.hasNext()) {
118                 User user = (User)it.next();
119                 if (idx >= firstIdx)
120                     items.add(user);
121
122                 ++idx;
123                 if (idx > lastIdx)
124                     break;
125             } // while (it.hasNext()) {
126
return new UserSearchResult(itemsFound, itemsPerPage, pageIndex, items);
127         } catch (Exception JavaDoc x) {
128             throw new SecurityException JavaDoc(x);
129         }
130     }
131 }
132
Popular Tags