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