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