KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > cms > factories > PublicUserFactory


1 /**
2  * Copyright (c) 2000-2004 Liferay, LLC. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */

22 package com.dotmarketing.cms.factories;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27
28 import com.dotmarketing.db.DotConnect;
29 import com.dotmarketing.exception.DotRuntimeException;
30 import com.dotmarketing.util.Logger;
31 import com.dotmarketing.util.UtilMethods;
32 import com.liferay.counter.ejb.CounterManagerUtil;
33 import com.liferay.portal.NoSuchUserException;
34 import com.liferay.portal.PortalException;
35 import com.liferay.portal.SystemException;
36 import com.liferay.portal.ejb.UserLocalManagerUtil;
37 import com.liferay.portal.ejb.UserUtil;
38 import com.liferay.portal.model.Role;
39 import com.liferay.portal.model.User;
40
41
42 /**
43  * <a HREF="AddressUtil.java.htm"><b><i>View Source</i></b></a>
44  *
45  * @author Brian Wing Shun Chan
46  * @version $Revision: 1.1 $
47  *
48  */

49 public class PublicUserFactory extends UserUtil {
50     public static User getInstance() {
51         String JavaDoc userId = null;
52         
53         try {
54             String JavaDoc companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId();
55             boolean more = false;
56             
57             do {
58                 userId = companyId + "." +
59                 Long.toString(CounterManagerUtil.increment(User.class.getName() + "." + companyId));
60                 
61                 
62                 try {
63                     User user = UserLocalManagerUtil.getUserById(companyId, userId);
64                     if(!user.isNew())
65                         more=true;
66                     else
67                         more=false;
68                     
69                 } catch (PortalException e) {
70                     more=false;
71                 }
72                 
73             }while(more);
74         } catch (SystemException e) {
75             throw new DotRuntimeException("Can't get a counter");
76         }
77         
78         return new User(userId);
79     }
80     
81     public static boolean userExists(String JavaDoc email) {
82         try {
83             String JavaDoc companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId();
84             findByC_EA(companyId, email);
85             
86             return true;
87         } catch (NoSuchUserException nsu) {
88             return false;
89         } catch (SystemException e) {
90             throw new DotRuntimeException("Can't get a counter");
91         }
92     }
93     
94     /**
95      * This method finds a User by email, if the user was not found it returns a new user instance
96      * ready to be filled and stored. This methods pulls a new userid from liferay counters.
97      * @param email
98      * @return
99      */

100     public static User getUserByEmail(String JavaDoc email) {
101         try {
102             String JavaDoc companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId();
103             return findByC_EA(companyId, email);
104         } catch (NoSuchUserException nsu) {
105             return getInstance();
106         } catch (SystemException e) {
107             throw new DotRuntimeException("Can't get a getUserByEmail");
108         }
109     }
110     
111     /**
112      * This method finds a User by userId, if the user was not found it returns a new user instance
113      * ready to be filled and stored. This methods pulls a new userid from liferay counters.
114      * @param email
115      * @return
116      */

117     public static User getUserByUserId(String JavaDoc userId) {
118         try {
119             String JavaDoc companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId();
120             return findByC_U(companyId, userId);
121         } catch (NoSuchUserException nsu) {
122             return getInstance();
123         } catch (SystemException e) {
124             throw new DotRuntimeException("Can't get a getUserByUserId");
125         }
126     }
127     
128     /**
129      * This method finds a User by userId, if the user was not found it returns a null instance.
130      * @param email
131      * @return
132      */

133     public static User findUserByUserId(String JavaDoc userId) {
134         try {
135             String JavaDoc companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId();
136             return findByC_U(companyId, userId);
137         } catch (NoSuchUserException nsu) {
138             return null;
139         } catch (SystemException e) {
140             throw new DotRuntimeException("Can't get a getUserByUserId");
141         }
142     }
143     
144     /**
145      * This method finds a User by email, if the user was not found it returns a null instance.
146      * @param email
147      * @return
148      */

149     public static User findUserByEmail(String JavaDoc userId) {
150         try {
151             String JavaDoc companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId();
152             return findByC_U(companyId, userId);
153         } catch (NoSuchUserException nsu) {
154             return null;
155         } catch (SystemException e) {
156             throw new DotRuntimeException("Can't get a getUserByUserId");
157         }
158     }
159     
160     public static void save(User u) {
161         if (u.getUserId() == null) {
162             throw new DotRuntimeException("Can't save a user without a userId");
163         }
164         
165         try {
166             u.setModified(true);
167             update(u);
168         } catch (SystemException e) {
169             throw new DotRuntimeException("saving a user failed");
170         }
171     }
172     
173     public static User getDefaultUser() {
174         try {
175             String JavaDoc companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId();
176             return findByPrimaryKey(User.getDefaultUserId(companyId));
177         } catch (Exception JavaDoc e) {
178             throw new DotRuntimeException("getting default user user failed");
179         }
180     }
181     
182     public static List JavaDoc<User> getAllUsers () {
183         try {
184             return findByCompanyId(PublicCompanyFactory.getDefaultCompanyId());
185         } catch (SystemException e) {
186             Logger.error(PublicUserFactory.class, "getAllUsers: error", e);
187             return new ArrayList JavaDoc<User> ();
188         }
189     }
190     
191     public static boolean isACMSAdmin (User user) {
192         List JavaDoc<Role> roles = com.dotmarketing.factories.RoleFactory.getAllRolesForUser (user.getUserId());
193         Role cmsAdminRole = null;
194         try {
195             cmsAdminRole = PublicRoleFactory.getCMSAdminRole();
196         } catch (Exception JavaDoc e) {
197             Logger.error(PublicUserFactory.class, "Error trying to pull the cms admin role", e);
198             return false;
199         }
200         for (Role r : roles) {
201             if (r.getRoleId().equals(cmsAdminRole.getRoleId()))
202                 return true;
203         }
204         return false;
205     }
206     
207     public static long getCountUsersByNameOrEmail(String JavaDoc filter)
208     {
209         try
210         {
211             filter = (UtilMethods.isSet(filter) ? filter.toLowerCase() : "");
212             
213             String JavaDoc sql = "select count(*) as count from user_ where firstName like '%" + filter + "%' or lastName like '%" + filter +"%' or emailAddress like '%" + filter + "%'";
214             DotConnect dotConnect = new DotConnect();
215             dotConnect.setSQL(sql);
216             
217             ArrayList JavaDoc results = dotConnect.getResults();
218             HashMap JavaDoc hash = (HashMap JavaDoc) results.get(0);
219             return Long.parseLong((String JavaDoc)hash.get("count"));
220         }
221         catch(Exception JavaDoc ex)
222         {
223             Logger.debug(PublicUserFactory.class,ex.toString());
224             return 0;
225         }
226     }
227
228     public static List JavaDoc<User> getUsersByNameOrEmail(String JavaDoc filter,int page,int pageSize)
229     {
230         try
231         {
232             ArrayList JavaDoc users = new ArrayList JavaDoc(pageSize);
233             int bottom = ((page - 1) * pageSize);
234             int top = (page * pageSize);
235             filter = (UtilMethods.isSet(filter) ? filter.toLowerCase() : "");
236                         
237             String JavaDoc sql = "select userid from user_ where firstName like '%" + filter + "%' or lastName like '%" + filter +"%' or emailAddress like '%" + filter + "%' order by firstName asc,lastname asc";
238             DotConnect dotConnect = new DotConnect();
239             dotConnect.setSQL(sql);
240             
241             dotConnect.setMaxRows(top);
242             ArrayList JavaDoc results = dotConnect.getResults();
243             
244             int lenght = results.size();
245             for(int i = 0;i < lenght;i++)
246             {
247                 if(i >= bottom)
248                 {
249                     if(i < top)
250                     {
251                         HashMap JavaDoc hash = (HashMap JavaDoc) results.get(i);
252                         String JavaDoc userId = (String JavaDoc) hash.get("userid");
253                         users.add(PublicUserFactory.findByPrimaryKey(userId));
254                     }
255                     else
256                     {
257                         break;
258                     }
259                 }
260             }
261             return users;
262         }
263         catch(Exception JavaDoc ex)
264         {
265             Logger.debug(PublicUserFactory.class,ex.toString());
266             return new ArrayList JavaDoc();
267         }
268     }
269
270 }
271
Popular Tags