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