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