1 22 package com.dotmarketing.cms.factories; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.List ; 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 49 public class PublicUserFactory extends UserUtil { 50 public static User getInstance() { 51 String userId = null; 52 53 try { 54 String 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 email) { 82 try { 83 String 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 100 public static User getUserByEmail(String email) { 101 try { 102 String 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 117 public static User getUserByUserId(String userId) { 118 try { 119 String 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 133 public static User findUserByUserId(String userId) { 134 try { 135 String 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 149 public static User findUserByEmail(String userId) { 150 try { 151 String 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 companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId(); 176 return findByPrimaryKey(User.getDefaultUserId(companyId)); 177 } catch (Exception e) { 178 throw new DotRuntimeException("getting default user user failed"); 179 } 180 } 181 182 public static List <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 <User> (); 188 } 189 } 190 191 public static boolean isACMSAdmin (User user) { 192 List <Role> roles = com.dotmarketing.factories.RoleFactory.getAllRolesForUser (user.getUserId()); 193 Role cmsAdminRole = null; 194 try { 195 cmsAdminRole = PublicRoleFactory.getCMSAdminRole(); 196 } catch (Exception 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 filter) 208 { 209 try 210 { 211 filter = (UtilMethods.isSet(filter) ? filter.toLowerCase() : ""); 212 213 String 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 results = dotConnect.getResults(); 218 HashMap hash = (HashMap ) results.get(0); 219 return Long.parseLong((String )hash.get("count")); 220 } 221 catch(Exception ex) 222 { 223 Logger.debug(PublicUserFactory.class,ex.toString()); 224 return 0; 225 } 226 } 227 228 public static List <User> getUsersByNameOrEmail(String filter,int page,int pageSize) 229 { 230 try 231 { 232 ArrayList users = new ArrayList (pageSize); 233 int bottom = ((page - 1) * pageSize); 234 int top = (page * pageSize); 235 filter = (UtilMethods.isSet(filter) ? filter.toLowerCase() : ""); 236 237 String 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 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 hash = (HashMap ) results.get(i); 252 String userId = (String ) 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 ex) 264 { 265 Logger.debug(PublicUserFactory.class,ex.toString()); 266 return new ArrayList (); 267 } 268 } 269 270 } 271 | Popular Tags |