1
22
23 package com.liferay.portal.service.persistence;
24
25 import com.liferay.portal.NoSuchClassNameException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
28 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29 import com.liferay.portal.kernel.dao.orm.Query;
30 import com.liferay.portal.kernel.dao.orm.QueryPos;
31 import com.liferay.portal.kernel.dao.orm.QueryUtil;
32 import com.liferay.portal.kernel.dao.orm.Session;
33 import com.liferay.portal.kernel.util.GetterUtil;
34 import com.liferay.portal.kernel.util.ListUtil;
35 import com.liferay.portal.kernel.util.OrderByComparator;
36 import com.liferay.portal.kernel.util.StringPool;
37 import com.liferay.portal.kernel.util.StringUtil;
38 import com.liferay.portal.model.ClassName;
39 import com.liferay.portal.model.ModelListener;
40 import com.liferay.portal.model.impl.ClassNameImpl;
41 import com.liferay.portal.model.impl.ClassNameModelImpl;
42 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
43
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46
47 import java.util.ArrayList;
48 import java.util.Collections;
49 import java.util.Iterator;
50 import java.util.List;
51
52
58 public class ClassNamePersistenceImpl extends BasePersistenceImpl
59 implements ClassNamePersistence {
60 public ClassName create(long classNameId) {
61 ClassName className = new ClassNameImpl();
62
63 className.setNew(true);
64 className.setPrimaryKey(classNameId);
65
66 return className;
67 }
68
69 public ClassName remove(long classNameId)
70 throws NoSuchClassNameException, SystemException {
71 Session session = null;
72
73 try {
74 session = openSession();
75
76 ClassName className = (ClassName)session.get(ClassNameImpl.class,
77 new Long(classNameId));
78
79 if (className == null) {
80 if (_log.isWarnEnabled()) {
81 _log.warn("No ClassName exists with the primary key " +
82 classNameId);
83 }
84
85 throw new NoSuchClassNameException(
86 "No ClassName exists with the primary key " + classNameId);
87 }
88
89 return remove(className);
90 }
91 catch (NoSuchClassNameException nsee) {
92 throw nsee;
93 }
94 catch (Exception e) {
95 throw processException(e);
96 }
97 finally {
98 closeSession(session);
99 }
100 }
101
102 public ClassName remove(ClassName className) throws SystemException {
103 if (_listeners.length > 0) {
104 for (ModelListener listener : _listeners) {
105 listener.onBeforeRemove(className);
106 }
107 }
108
109 className = removeImpl(className);
110
111 if (_listeners.length > 0) {
112 for (ModelListener listener : _listeners) {
113 listener.onAfterRemove(className);
114 }
115 }
116
117 return className;
118 }
119
120 protected ClassName removeImpl(ClassName className)
121 throws SystemException {
122 Session session = null;
123
124 try {
125 session = openSession();
126
127 if (BatchSessionUtil.isEnabled()) {
128 Object staleObject = session.get(ClassNameImpl.class,
129 className.getPrimaryKeyObj());
130
131 if (staleObject != null) {
132 session.evict(staleObject);
133 }
134 }
135
136 session.delete(className);
137
138 session.flush();
139
140 return className;
141 }
142 catch (Exception e) {
143 throw processException(e);
144 }
145 finally {
146 closeSession(session);
147
148 FinderCacheUtil.clearCache(ClassName.class.getName());
149 }
150 }
151
152
155 public ClassName update(ClassName className) throws SystemException {
156 if (_log.isWarnEnabled()) {
157 _log.warn(
158 "Using the deprecated update(ClassName className) method. Use update(ClassName className, boolean merge) instead.");
159 }
160
161 return update(className, false);
162 }
163
164
177 public ClassName update(ClassName className, boolean merge)
178 throws SystemException {
179 boolean isNew = className.isNew();
180
181 if (_listeners.length > 0) {
182 for (ModelListener listener : _listeners) {
183 if (isNew) {
184 listener.onBeforeCreate(className);
185 }
186 else {
187 listener.onBeforeUpdate(className);
188 }
189 }
190 }
191
192 className = updateImpl(className, merge);
193
194 if (_listeners.length > 0) {
195 for (ModelListener listener : _listeners) {
196 if (isNew) {
197 listener.onAfterCreate(className);
198 }
199 else {
200 listener.onAfterUpdate(className);
201 }
202 }
203 }
204
205 return className;
206 }
207
208 public ClassName updateImpl(com.liferay.portal.model.ClassName className,
209 boolean merge) throws SystemException {
210 Session session = null;
211
212 try {
213 session = openSession();
214
215 BatchSessionUtil.update(session, className, merge);
216
217 className.setNew(false);
218
219 return className;
220 }
221 catch (Exception e) {
222 throw processException(e);
223 }
224 finally {
225 closeSession(session);
226
227 FinderCacheUtil.clearCache(ClassName.class.getName());
228 }
229 }
230
231 public ClassName findByPrimaryKey(long classNameId)
232 throws NoSuchClassNameException, SystemException {
233 ClassName className = fetchByPrimaryKey(classNameId);
234
235 if (className == null) {
236 if (_log.isWarnEnabled()) {
237 _log.warn("No ClassName exists with the primary key " +
238 classNameId);
239 }
240
241 throw new NoSuchClassNameException(
242 "No ClassName exists with the primary key " + classNameId);
243 }
244
245 return className;
246 }
247
248 public ClassName fetchByPrimaryKey(long classNameId)
249 throws SystemException {
250 Session session = null;
251
252 try {
253 session = openSession();
254
255 return (ClassName)session.get(ClassNameImpl.class,
256 new Long(classNameId));
257 }
258 catch (Exception e) {
259 throw processException(e);
260 }
261 finally {
262 closeSession(session);
263 }
264 }
265
266 public ClassName findByValue(String value)
267 throws NoSuchClassNameException, SystemException {
268 ClassName className = fetchByValue(value);
269
270 if (className == null) {
271 StringBuilder msg = new StringBuilder();
272
273 msg.append("No ClassName exists with the key {");
274
275 msg.append("value=" + value);
276
277 msg.append(StringPool.CLOSE_CURLY_BRACE);
278
279 if (_log.isWarnEnabled()) {
280 _log.warn(msg.toString());
281 }
282
283 throw new NoSuchClassNameException(msg.toString());
284 }
285
286 return className;
287 }
288
289 public ClassName fetchByValue(String value) throws SystemException {
290 boolean finderClassNameCacheEnabled = ClassNameModelImpl.CACHE_ENABLED;
291 String finderClassName = ClassName.class.getName();
292 String finderMethodName = "fetchByValue";
293 String[] finderParams = new String[] { String.class.getName() };
294 Object[] finderArgs = new Object[] { value };
295
296 Object result = null;
297
298 if (finderClassNameCacheEnabled) {
299 result = FinderCacheUtil.getResult(finderClassName,
300 finderMethodName, finderParams, finderArgs, this);
301 }
302
303 if (result == null) {
304 Session session = null;
305
306 try {
307 session = openSession();
308
309 StringBuilder query = new StringBuilder();
310
311 query.append("FROM com.liferay.portal.model.ClassName WHERE ");
312
313 if (value == null) {
314 query.append("value IS NULL");
315 }
316 else {
317 query.append("value = ?");
318 }
319
320 query.append(" ");
321
322 Query q = session.createQuery(query.toString());
323
324 QueryPos qPos = QueryPos.getInstance(q);
325
326 if (value != null) {
327 qPos.add(value);
328 }
329
330 List<ClassName> list = q.list();
331
332 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
333 finderClassName, finderMethodName, finderParams,
334 finderArgs, list);
335
336 if (list.size() == 0) {
337 return null;
338 }
339 else {
340 return list.get(0);
341 }
342 }
343 catch (Exception e) {
344 throw processException(e);
345 }
346 finally {
347 closeSession(session);
348 }
349 }
350 else {
351 List<ClassName> list = (List<ClassName>)result;
352
353 if (list.size() == 0) {
354 return null;
355 }
356 else {
357 return list.get(0);
358 }
359 }
360 }
361
362 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
363 throws SystemException {
364 Session session = null;
365
366 try {
367 session = openSession();
368
369 dynamicQuery.compile(session);
370
371 return dynamicQuery.list();
372 }
373 catch (Exception e) {
374 throw processException(e);
375 }
376 finally {
377 closeSession(session);
378 }
379 }
380
381 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
382 int start, int end) throws SystemException {
383 Session session = null;
384
385 try {
386 session = openSession();
387
388 dynamicQuery.setLimit(start, end);
389
390 dynamicQuery.compile(session);
391
392 return dynamicQuery.list();
393 }
394 catch (Exception e) {
395 throw processException(e);
396 }
397 finally {
398 closeSession(session);
399 }
400 }
401
402 public List<ClassName> findAll() throws SystemException {
403 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
404 }
405
406 public List<ClassName> findAll(int start, int end)
407 throws SystemException {
408 return findAll(start, end, null);
409 }
410
411 public List<ClassName> findAll(int start, int end, OrderByComparator obc)
412 throws SystemException {
413 boolean finderClassNameCacheEnabled = ClassNameModelImpl.CACHE_ENABLED;
414 String finderClassName = ClassName.class.getName();
415 String finderMethodName = "findAll";
416 String[] finderParams = new String[] {
417 "java.lang.Integer", "java.lang.Integer",
418 "com.liferay.portal.kernel.util.OrderByComparator"
419 };
420 Object[] finderArgs = new Object[] {
421 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
422 };
423
424 Object result = null;
425
426 if (finderClassNameCacheEnabled) {
427 result = FinderCacheUtil.getResult(finderClassName,
428 finderMethodName, finderParams, finderArgs, this);
429 }
430
431 if (result == null) {
432 Session session = null;
433
434 try {
435 session = openSession();
436
437 StringBuilder query = new StringBuilder();
438
439 query.append("FROM com.liferay.portal.model.ClassName ");
440
441 if (obc != null) {
442 query.append("ORDER BY ");
443 query.append(obc.getOrderBy());
444 }
445
446 Query q = session.createQuery(query.toString());
447
448 List<ClassName> list = null;
449
450 if (obc == null) {
451 list = (List<ClassName>)QueryUtil.list(q, getDialect(),
452 start, end, false);
453
454 Collections.sort(list);
455 }
456 else {
457 list = (List<ClassName>)QueryUtil.list(q, getDialect(),
458 start, end);
459 }
460
461 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
462 finderClassName, finderMethodName, finderParams,
463 finderArgs, list);
464
465 return list;
466 }
467 catch (Exception e) {
468 throw processException(e);
469 }
470 finally {
471 closeSession(session);
472 }
473 }
474 else {
475 return (List<ClassName>)result;
476 }
477 }
478
479 public void removeByValue(String value)
480 throws NoSuchClassNameException, SystemException {
481 ClassName className = findByValue(value);
482
483 remove(className);
484 }
485
486 public void removeAll() throws SystemException {
487 for (ClassName className : findAll()) {
488 remove(className);
489 }
490 }
491
492 public int countByValue(String value) throws SystemException {
493 boolean finderClassNameCacheEnabled = ClassNameModelImpl.CACHE_ENABLED;
494 String finderClassName = ClassName.class.getName();
495 String finderMethodName = "countByValue";
496 String[] finderParams = new String[] { String.class.getName() };
497 Object[] finderArgs = new Object[] { value };
498
499 Object result = null;
500
501 if (finderClassNameCacheEnabled) {
502 result = FinderCacheUtil.getResult(finderClassName,
503 finderMethodName, finderParams, finderArgs, this);
504 }
505
506 if (result == null) {
507 Session session = null;
508
509 try {
510 session = openSession();
511
512 StringBuilder query = new StringBuilder();
513
514 query.append("SELECT COUNT(*) ");
515 query.append("FROM com.liferay.portal.model.ClassName WHERE ");
516
517 if (value == null) {
518 query.append("value IS NULL");
519 }
520 else {
521 query.append("value = ?");
522 }
523
524 query.append(" ");
525
526 Query q = session.createQuery(query.toString());
527
528 QueryPos qPos = QueryPos.getInstance(q);
529
530 if (value != null) {
531 qPos.add(value);
532 }
533
534 Long count = null;
535
536 Iterator<Long> itr = q.list().iterator();
537
538 if (itr.hasNext()) {
539 count = itr.next();
540 }
541
542 if (count == null) {
543 count = new Long(0);
544 }
545
546 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
547 finderClassName, finderMethodName, finderParams,
548 finderArgs, count);
549
550 return count.intValue();
551 }
552 catch (Exception e) {
553 throw processException(e);
554 }
555 finally {
556 closeSession(session);
557 }
558 }
559 else {
560 return ((Long)result).intValue();
561 }
562 }
563
564 public int countAll() throws SystemException {
565 boolean finderClassNameCacheEnabled = ClassNameModelImpl.CACHE_ENABLED;
566 String finderClassName = ClassName.class.getName();
567 String finderMethodName = "countAll";
568 String[] finderParams = new String[] { };
569 Object[] finderArgs = new Object[] { };
570
571 Object result = null;
572
573 if (finderClassNameCacheEnabled) {
574 result = FinderCacheUtil.getResult(finderClassName,
575 finderMethodName, finderParams, finderArgs, this);
576 }
577
578 if (result == null) {
579 Session session = null;
580
581 try {
582 session = openSession();
583
584 Query q = session.createQuery(
585 "SELECT COUNT(*) FROM com.liferay.portal.model.ClassName");
586
587 Long count = null;
588
589 Iterator<Long> itr = q.list().iterator();
590
591 if (itr.hasNext()) {
592 count = itr.next();
593 }
594
595 if (count == null) {
596 count = new Long(0);
597 }
598
599 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
600 finderClassName, finderMethodName, finderParams,
601 finderArgs, count);
602
603 return count.intValue();
604 }
605 catch (Exception e) {
606 throw processException(e);
607 }
608 finally {
609 closeSession(session);
610 }
611 }
612 else {
613 return ((Long)result).intValue();
614 }
615 }
616
617 public void registerListener(ModelListener listener) {
618 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
619
620 listeners.add(listener);
621
622 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
623 }
624
625 public void unregisterListener(ModelListener listener) {
626 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
627
628 listeners.remove(listener);
629
630 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
631 }
632
633 public void afterPropertiesSet() {
634 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
635 com.liferay.portal.util.PropsUtil.get(
636 "value.object.listener.com.liferay.portal.model.ClassName")));
637
638 if (listenerClassNames.length > 0) {
639 try {
640 List<ModelListener> listeners = new ArrayList<ModelListener>();
641
642 for (String listenerClassName : listenerClassNames) {
643 listeners.add((ModelListener)Class.forName(
644 listenerClassName).newInstance());
645 }
646
647 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
648 }
649 catch (Exception e) {
650 _log.error(e);
651 }
652 }
653 }
654
655 private static Log _log = LogFactory.getLog(ClassNamePersistenceImpl.class);
656 private ModelListener[] _listeners = new ModelListener[0];
657 }