1 5 6 package org.exoplatform.services.portletregistery.impl.hibernate; 7 8 import java.util.Collection ; 9 import java.util.Date ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import net.sf.hibernate.Hibernate; 13 import net.sf.hibernate.Session; 14 import net.sf.hibernate.type.Type; 15 import org.apache.commons.logging.Log; 16 import org.exoplatform.Constants; 17 import org.exoplatform.commons.utils.IdentifierUtil; 18 import org.exoplatform.services.database.DatabaseService; 19 import org.exoplatform.services.database.HibernateService; 20 import org.exoplatform.services.log.LogService; 21 import org.exoplatform.services.portletcontainer.monitor.PortletRuntimeData; 22 import org.exoplatform.services.portletregistery.*; 23 24 29 public class PortletRegisteryServiceImpl implements PortletRegisteryService { 30 31 private static String [] MAPPING = 32 { 33 "org/exoplatform/services/portletregistery/impl/hibernate/PortletCategoryImpl.hbm.xml", 34 "org/exoplatform/services/portletregistery/impl/hibernate/PortletImpl.hbm.xml", 35 "org/exoplatform/services/portletregistery/impl/hibernate/PortletRoleImpl.hbm.xml" 36 }; 37 38 private static final String queryFindAllPortletCategories = 39 "from u in class org.exoplatform.services.portletregistery.impl.hibernate.PortletCategoryImpl"; 40 private static final String queryPortletCategoryById = 41 "from portletCategory in class org.exoplatform.services.portletregistery.impl.hibernate.PortletCategoryImpl " + 42 "where portletCategory.id = ?"; 43 private static final String queryPortletCategoryByName = 44 "from portletCategory in class org.exoplatform.services.portletregistery.impl.hibernate.PortletCategoryImpl " + 45 "where portletCategory.portletCategoryName = ?"; 46 47 private static final String queryPortletByCategory = 48 "from portlet in class org.exoplatform.services.portletregistery.impl.hibernate.PortletImpl " + 49 "where portlet.portletCategoryId = ?"; 50 private static final String queryPortletById = 51 "from portlet in class org.exoplatform.services.portletregistery.impl.hibernate.PortletImpl " + 52 "where portlet.id = ?"; 53 private static final String queryPortletByDisplayName = 54 "from p in class org.exoplatform.services.portletregistery.impl.hibernate.PortletImpl " + 55 "where p.portletCategoryId = ? and p.displayName = ?"; 56 57 private static final String queryRoleByPortlet = 58 "from pr in class org.exoplatform.services.portletregistery.impl.hibernate.PortletRoleImpl " + 59 "where pr.portletId = ?"; 60 private static final String queryRoleById = 61 "from pr in class org.exoplatform.services.portletregistery.impl.hibernate.PortletRoleImpl " + 62 "where pr.id = ?"; 63 64 private HibernateService hibernateService_; 65 private Log log; 66 67 public PortletRegisteryServiceImpl(HibernateService hibernateService_, 68 DatabaseService dbService, LogService logService) { 69 this.hibernateService_ = hibernateService_; 70 this.log = logService.getLog("org.exoplatform.services.portletregistery"); 71 hibernateService_.addMappingFiles(MAPPING); 72 } 74 75 public PortletCategory createPortletCategoryInstance() { 76 return new PortletCategoryImpl(); 77 } 78 79 public List getPortletCategories() throws Exception { 80 Session session = hibernateService_.openSession(); 81 return getPortletCategories(session) ; 82 } 83 84 private List getPortletCategories(Session session) throws Exception { 85 return session.find(queryFindAllPortletCategories); 86 } 87 88 public PortletCategory getPortletCategory(String id) throws Exception { 89 Session session = hibernateService_.openSession(); 90 PortletCategory category = getPortletCategory(id, session); 91 return category; 92 } 93 94 private PortletCategory getPortletCategory(String id, Session session) throws Exception { 95 List l = session.find(queryPortletCategoryById, id, Hibernate.STRING); 96 if (l.size() == 0) { 97 throw new PortletRegisteryException("the portlet category " + id + " does not exist", 98 PortletRegisteryException.PORTLET_CATEGORY_NOT_FOUND); 99 } 100 return (PortletCategory) l.get(0); 101 } 102 103 public PortletCategory addPortletCategory(PortletCategory portletCategory) throws Exception { 104 Session session = hibernateService_.openSession(); 105 portletCategory = addPortletCategory(portletCategory, session); 106 session.flush(); 107 return portletCategory; 108 } 109 110 private PortletCategory addPortletCategory(PortletCategory portletCategory, Session session) throws Exception { 111 PortletCategoryImpl impl = (PortletCategoryImpl) portletCategory; 112 Date now = new Date (); 113 impl.setId(IdentifierUtil.generateUUID(impl)); 114 impl.setCreatedDate(now); 115 impl.setModifiedDate(now); 116 session.save(impl); 117 return impl; 118 } 119 120 public PortletCategory updatePortletCategory(PortletCategory portletCategory) throws Exception { 121 Session session = hibernateService_.openSession(); 122 portletCategory = updatePortletCategory(portletCategory, session); 123 session.flush(); 124 return portletCategory; 125 } 126 127 private PortletCategory updatePortletCategory(PortletCategory portletCategory, Session session) throws Exception { 128 Date now = new Date (); 129 PortletCategoryImpl impl = (PortletCategoryImpl) portletCategory; 130 impl.setModifiedDate(now); 131 session.update(impl); 132 return impl; 133 } 134 135 public PortletCategory removePortletCategory(String id) throws Exception { 136 Session session = hibernateService_.openSession(); 137 PortletCategory category = removePortletCategory(id, session); 138 session.flush(); 139 return category; 140 } 141 142 private PortletCategory removePortletCategory(String id, Session session) throws Exception { 143 List l = session.find(queryPortletCategoryById, id, Hibernate.STRING); 144 if (l.size() == 0) { 145 throw new PortletRegisteryException("the portlet category " + id + " does not exist", 146 PortletRegisteryException.PORTLET_CATEGORY_NOT_FOUND); 147 } 148 PortletCategory category = (PortletCategory) l.get(0); 149 List portlets = getPortlets(category.getId(), session) ; 150 for(int i = 0; i < portlets.size(); i++) { 151 PortletImpl portlet = (PortletImpl) portlets.get(i) ; 152 removePortlet(portlet.getId(), session) ; 153 } 154 session.delete(category); 155 return category; 156 } 157 158 public PortletCategory removePortletCategoryByName(String name) throws Exception { 159 Session session = hibernateService_.openSession(); 160 PortletCategory category = removePortletCategoryByName(name, session); 161 session.flush(); 162 return category; 163 } 164 165 private PortletCategory removePortletCategoryByName(String name, Session session) throws Exception { 166 List l = session.find(queryPortletCategoryByName, name, Hibernate.STRING); 167 if (l.size() == 0) { 168 throw new PortletRegisteryException("the portlet category " + name + " does not exist", 169 PortletRegisteryException.PORTLET_CATEGORY_NOT_FOUND); 170 } 171 PortletCategory category = (PortletCategory) l.get(0) ; 172 List portlets = getPortlets(category.getId(), session) ; 173 for(int i = 0; i < portlets.size(); i++) { 174 PortletImpl portlet = (PortletImpl) portlets.get(i) ; 175 removePortlet(portlet.getId(), session) ; 176 } 177 session.delete(category); 178 return category; 179 } 180 181 public PortletCategory findPortletCategoryByName(String portletCategoryName) throws Exception { 182 Session session = hibernateService_.openSession(); 183 PortletCategory category = findPortletCategoryByName(portletCategoryName, session); 184 return category; 185 } 186 187 private PortletCategory findPortletCategoryByName(String portletCategoryName, Session session) throws Exception { 188 List l = session.find(queryPortletCategoryByName, portletCategoryName, Hibernate.STRING); 189 if (l.size() == 0) { 190 throw new PortletRegisteryException("the portlet category " + portletCategoryName + " does not exist", 191 PortletRegisteryException.PORTLET_CATEGORY_NOT_FOUND); 192 } 193 return (PortletCategory) l.get(0); 194 } 195 196 public List getPortlets(String portletCategoryId) throws Exception { 197 Session session = hibernateService_.openSession(); 198 List portlets = getPortlets(portletCategoryId, session); 199 return portlets; 200 } 201 202 private List getPortlets(String portletCategoryId, Session session) throws Exception { 203 return session.find(queryPortletByCategory, portletCategoryId, Hibernate.STRING); 204 } 205 206 public Portlet getPortlet(String id) throws Exception { 207 Session session = hibernateService_.openSession(); 208 Portlet portlet = getPortlet(id, session); 209 return portlet; 210 } 211 212 private Portlet getPortlet(String id, Session session) throws Exception { 213 List l = session.find(queryPortletById, id, Hibernate.STRING); 214 if (l.size() == 0) { 215 throw new PortletRegisteryException("the portlet " + id + " does not exist", 216 PortletRegisteryException.PORTLET_NOT_FOUND); 217 } 218 return (Portlet) l.get(0); 219 } 220 221 public Portlet addPortlet(PortletCategory category, Portlet portlet) throws Exception { 222 Session session = hibernateService_.openSession(); 223 portlet = addPortlet(category, portlet, session); 224 session.flush(); 225 return portlet; 226 } 227 228 private Portlet addPortlet(PortletCategory category, Portlet portlet, Session session) throws Exception { 229 Date now = new Date (); 230 PortletImpl impl = (PortletImpl) portlet; 231 impl.setId(IdentifierUtil.generateUUID(impl)); 232 impl.setCreatedDate(now); 233 impl.setModifiedDate(now); 234 impl.setPortletCategoryId(category.getId()); 235 session.save(impl); 236 return impl; 237 } 238 239 public Portlet removePortlet(String id) throws Exception { 240 Session session = hibernateService_.openSession(); 241 Portlet portlet = removePortlet(id, session); 242 session.flush(); 243 return portlet; 244 } 245 246 private Portlet removePortlet(String id, Session session) throws Exception { 247 List l = session.find(queryPortletById, id, Hibernate.STRING); 248 if (l.size() == 0) { 249 throw new PortletRegisteryException("the portlet " + id + " does not exist", 250 PortletRegisteryException.PORTLET_NOT_FOUND); 251 } 252 Portlet portlet = (Portlet) l.get(0); 253 session.delete(queryRoleByPortlet, portlet.getId(), Hibernate.STRING); 254 session.delete(portlet); 255 return portlet; 256 } 257 258 public Portlet updatePortlet(Portlet portlet) throws Exception { 259 Session session = hibernateService_.openSession(); 260 portlet = updatePortlet(portlet, session); 261 session.flush(); 262 return portlet; 263 } 264 265 private Portlet updatePortlet(Portlet portlet, Session session) throws Exception { 266 Date now = new Date (); 267 PortletImpl impl = (PortletImpl) portlet; 268 impl.setModifiedDate(now); 269 session.update(impl); 270 return impl; 271 } 272 273 public void findPortletByDisplayName(String portletCategory, String displayName, Session session) 274 throws Exception { 275 Object [] args = new Object []{portletCategory, displayName}; 276 Type[] types = new Type[]{Hibernate.STRING, Hibernate.STRING}; 277 278 List l = session.find(queryPortletByDisplayName, args, types); 279 if (l.size() == 0) { 280 throw new PortletRegisteryException("Portlet not found", PortletRegisteryException.PORTLET_NOT_FOUND); 281 } 282 } 283 284 public Portlet createPortletInstance() { 285 return new PortletImpl(); 286 } 287 288 public List getPortletRoles(String portletId) throws Exception { 289 Session session = hibernateService_.openSession(); 290 return getPortletRoles(portletId, session); 291 } 292 293 private List getPortletRoles(String portletId, Session session) throws Exception { 294 return session.find(queryRoleByPortlet, portletId, Hibernate.STRING); 295 } 296 297 public PortletRole getPortletRole(String id) throws Exception { 298 Session session = hibernateService_.openSession(); 299 return getPortletRole(id, session); 300 } 301 302 private PortletRole getPortletRole(String id, Session session) throws Exception { 303 List l = session.find(queryRoleById, id, Hibernate.STRING); 304 if (l.size() == 0) { 305 throw new PortletRegisteryException("the portlet role " + id + " does not exist", 306 PortletRegisteryException.PORTLET_ROLE_NOT_FOUND); 307 } 308 return (PortletRole) l.get(0); 309 } 310 311 public PortletRole addPortletRole(Portlet portlet, PortletRole portletRole) throws Exception { 312 Session session = hibernateService_.openSession(); 313 portletRole = addPortletRole(portlet, portletRole, session); 314 session.flush(); 315 return portletRole; 316 } 317 318 private PortletRole addPortletRole(Portlet portlet, PortletRole portletRole, Session session) throws Exception { 319 PortletRoleImpl impl = (PortletRoleImpl) portletRole; 320 if (portletRole.getId() == null) 321 impl.setId(IdentifierUtil.generateUUID(impl)); 322 impl.setPortletId(portlet.getId()); 323 session.save(impl); 324 return impl; 325 } 326 327 public PortletRole removePortletRole(String id) throws Exception { 328 Session session = hibernateService_.openSession(); 329 PortletRole portletRole = removePortletRole(id, session); 330 session.flush(); 331 return portletRole; 332 } 333 334 private PortletRole removePortletRole(String id, Session session) throws Exception { 335 List l = session.find(queryRoleById, id, Hibernate.STRING); 336 if (l.size() == 0) { 337 throw new PortletRegisteryException("the portlet role " + id + " does not exist", 338 PortletRegisteryException.PORTLET_ROLE_NOT_FOUND); 339 } 340 PortletRole portletRole = (PortletRole) l.get(0); 341 session.delete(portletRole); 342 return portletRole; 343 } 344 345 public PortletRole updatePortletRole(PortletRole portletRole) throws Exception { 346 Session session = hibernateService_.openSession(); 347 portletRole = updatePortletRole(portletRole, session); 348 session.flush(); 349 return portletRole; 350 } 351 352 private PortletRole updatePortletRole(PortletRole portletRole, Session session) throws Exception { 353 session.save(portletRole); 354 return portletRole; 355 } 356 357 public void clearPortletRoles(String portletId) throws Exception { 358 Session session = hibernateService_.openSession(); 359 clearPortletRoles(portletId, session); 360 session.flush(); 361 } 362 363 private void clearPortletRoles(String portletId, Session session) throws Exception { 364 List currentRoles = getPortletRoles(portletId, session); 365 for (Iterator iterator = currentRoles.iterator(); iterator.hasNext();) { 366 PortletRole portletRole = (PortletRole) iterator.next(); 367 removePortletRole(portletRole.getId(), session); 368 } 369 } 370 371 public PortletRole createPortletRoleInstance() { 372 return new PortletRoleImpl(); 373 } 374 375 public void updatePortletRoles(String portletId, Collection currentRoles) throws Exception { 376 Session session = hibernateService_.openSession(); 377 clearPortletRoles(portletId, session); 378 Portlet portlet = getPortlet(portletId, session); 379 for (Iterator iterator = currentRoles.iterator(); iterator.hasNext();) { 380 String role = (String ) iterator.next(); 381 PortletRole portletRole = createPortletRoleInstance(); 382 portletRole.setPortletRoleName(role); 383 addPortletRole(portlet, portletRole, session); 384 } 385 session.flush(); 386 } 387 388 public void importPortlets(Collection portletDatas) throws Exception { 389 Session session = hibernateService_.openSession(); 390 for (Iterator iterator = portletDatas.iterator(); iterator.hasNext();) { 391 PortletRuntimeData portletRuntimeData = (PortletRuntimeData) iterator.next(); 392 String portletCategoryName = portletRuntimeData.getPortletAppName(); 393 String portletName = portletRuntimeData.getPortletName(); 394 PortletCategory portletCategory = null; 395 try { 396 portletCategory = findPortletCategoryByName(portletCategoryName, session); 397 } catch (Exception e) { 398 portletCategory = createPortletCategoryInstance(); 399 portletCategory.setPortletCategoryName(portletCategoryName); 400 portletCategory = addPortletCategory(portletCategory, session); 401 } 402 try { 403 findPortletByDisplayName(portletCategory.getId(), portletName, session); 404 } catch (Exception e) { 405 Portlet portlet = createPortletInstance(); 406 portlet.setDisplayName(portletName); 407 portlet.setPortletApplicationName(portletCategoryName); 408 portlet.setPortletName(portletName); 409 addPortlet(portletCategory, portlet, session); 410 PortletRole portletRole = createPortletRoleInstance(); 411 portletRole.setPortletRoleName(Constants.USER_ROLE); 412 addPortletRole(portlet, portletRole, session); 413 } 414 } 415 session.flush() ; 416 } 417 418 public void clearRepository() throws Exception { 419 Session session = hibernateService_.openSession(); 420 session.delete("from pr in class org.exoplatform.services.portletregistery.impl.hibernate.PortletRoleImpl") ; 421 session.delete("from pr in class org.exoplatform.services.portletregistery.impl.hibernate.PortletImpl") ; 422 session.delete("from pr in class org.exoplatform.services.portletregistery.impl.hibernate.PortletCategoryImpl") ; 423 431 } 432 } | Popular Tags |