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