1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.Contact;
33  import com.liferay.portal.model.impl.ContactImpl;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  import com.liferay.portal.spring.hibernate.FinderCache;
36  import com.liferay.portal.spring.hibernate.HibernateUtil;
37  
38  import com.liferay.util.dao.hibernate.QueryUtil;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  import org.hibernate.Query;
44  import org.hibernate.Session;
45  
46  import java.util.Collections;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  /**
51   * <a href="ContactPersistenceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class ContactPersistenceImpl extends BasePersistence
57      implements ContactPersistence {
58      public Contact create(long contactId) {
59          Contact contact = new ContactImpl();
60          contact.setNew(true);
61          contact.setPrimaryKey(contactId);
62  
63          return contact;
64      }
65  
66      public Contact remove(long contactId)
67          throws NoSuchContactException, SystemException {
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              Contact contact = (Contact)session.get(ContactImpl.class,
74                      new Long(contactId));
75  
76              if (contact == null) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn("No Contact exists with the primary key " +
79                          contactId);
80                  }
81  
82                  throw new NoSuchContactException(
83                      "No Contact exists with the primary key " + contactId);
84              }
85  
86              return remove(contact);
87          }
88          catch (NoSuchContactException nsee) {
89              throw nsee;
90          }
91          catch (Exception e) {
92              throw HibernateUtil.processException(e);
93          }
94          finally {
95              closeSession(session);
96          }
97      }
98  
99      public Contact remove(Contact contact) throws SystemException {
100         Session session = null;
101 
102         try {
103             session = openSession();
104             session.delete(contact);
105             session.flush();
106 
107             return contact;
108         }
109         catch (Exception e) {
110             throw HibernateUtil.processException(e);
111         }
112         finally {
113             closeSession(session);
114             FinderCache.clearCache(Contact.class.getName());
115         }
116     }
117 
118     public Contact update(com.liferay.portal.model.Contact contact)
119         throws SystemException {
120         return update(contact, false);
121     }
122 
123     public Contact update(com.liferay.portal.model.Contact contact,
124         boolean merge) throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             if (merge) {
131                 session.merge(contact);
132             }
133             else {
134                 if (contact.isNew()) {
135                     session.save(contact);
136                 }
137             }
138 
139             session.flush();
140             contact.setNew(false);
141 
142             return contact;
143         }
144         catch (Exception e) {
145             throw HibernateUtil.processException(e);
146         }
147         finally {
148             closeSession(session);
149             FinderCache.clearCache(Contact.class.getName());
150         }
151     }
152 
153     public Contact findByPrimaryKey(long contactId)
154         throws NoSuchContactException, SystemException {
155         Contact contact = fetchByPrimaryKey(contactId);
156 
157         if (contact == null) {
158             if (_log.isWarnEnabled()) {
159                 _log.warn("No Contact exists with the primary key " +
160                     contactId);
161             }
162 
163             throw new NoSuchContactException(
164                 "No Contact exists with the primary key " + contactId);
165         }
166 
167         return contact;
168     }
169 
170     public Contact fetchByPrimaryKey(long contactId) throws SystemException {
171         Session session = null;
172 
173         try {
174             session = openSession();
175 
176             return (Contact)session.get(ContactImpl.class, new Long(contactId));
177         }
178         catch (Exception e) {
179             throw HibernateUtil.processException(e);
180         }
181         finally {
182             closeSession(session);
183         }
184     }
185 
186     public List findByCompanyId(long companyId) throws SystemException {
187         String finderClassName = Contact.class.getName();
188         String finderMethodName = "findByCompanyId";
189         String[] finderParams = new String[] { Long.class.getName() };
190         Object[] finderArgs = new Object[] { new Long(companyId) };
191         Object result = FinderCache.getResult(finderClassName,
192                 finderMethodName, finderParams, finderArgs, getSessionFactory());
193 
194         if (result == null) {
195             Session session = null;
196 
197             try {
198                 session = openSession();
199 
200                 StringMaker query = new StringMaker();
201                 query.append("FROM com.liferay.portal.model.Contact WHERE ");
202                 query.append("companyId = ?");
203                 query.append(" ");
204 
205                 Query q = session.createQuery(query.toString());
206                 int queryPos = 0;
207                 q.setLong(queryPos++, companyId);
208 
209                 List list = q.list();
210                 FinderCache.putResult(finderClassName, finderMethodName,
211                     finderParams, finderArgs, list);
212 
213                 return list;
214             }
215             catch (Exception e) {
216                 throw HibernateUtil.processException(e);
217             }
218             finally {
219                 closeSession(session);
220             }
221         }
222         else {
223             return (List)result;
224         }
225     }
226 
227     public List findByCompanyId(long companyId, int begin, int end)
228         throws SystemException {
229         return findByCompanyId(companyId, begin, end, null);
230     }
231 
232     public List findByCompanyId(long companyId, int begin, int end,
233         OrderByComparator obc) throws SystemException {
234         String finderClassName = Contact.class.getName();
235         String finderMethodName = "findByCompanyId";
236         String[] finderParams = new String[] {
237                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
238                 "com.liferay.portal.kernel.util.OrderByComparator"
239             };
240         Object[] finderArgs = new Object[] {
241                 new Long(companyId), String.valueOf(begin), String.valueOf(end),
242                 String.valueOf(obc)
243             };
244         Object result = FinderCache.getResult(finderClassName,
245                 finderMethodName, finderParams, finderArgs, getSessionFactory());
246 
247         if (result == null) {
248             Session session = null;
249 
250             try {
251                 session = openSession();
252 
253                 StringMaker query = new StringMaker();
254                 query.append("FROM com.liferay.portal.model.Contact WHERE ");
255                 query.append("companyId = ?");
256                 query.append(" ");
257 
258                 if (obc != null) {
259                     query.append("ORDER BY ");
260                     query.append(obc.getOrderBy());
261                 }
262 
263                 Query q = session.createQuery(query.toString());
264                 int queryPos = 0;
265                 q.setLong(queryPos++, companyId);
266 
267                 List list = QueryUtil.list(q, getDialect(), begin, end);
268                 FinderCache.putResult(finderClassName, finderMethodName,
269                     finderParams, finderArgs, list);
270 
271                 return list;
272             }
273             catch (Exception e) {
274                 throw HibernateUtil.processException(e);
275             }
276             finally {
277                 closeSession(session);
278             }
279         }
280         else {
281             return (List)result;
282         }
283     }
284 
285     public Contact findByCompanyId_First(long companyId, OrderByComparator obc)
286         throws NoSuchContactException, SystemException {
287         List list = findByCompanyId(companyId, 0, 1, obc);
288 
289         if (list.size() == 0) {
290             StringMaker msg = new StringMaker();
291             msg.append("No Contact exists with the key ");
292             msg.append(StringPool.OPEN_CURLY_BRACE);
293             msg.append("companyId=");
294             msg.append(companyId);
295             msg.append(StringPool.CLOSE_CURLY_BRACE);
296             throw new NoSuchContactException(msg.toString());
297         }
298         else {
299             return (Contact)list.get(0);
300         }
301     }
302 
303     public Contact findByCompanyId_Last(long companyId, OrderByComparator obc)
304         throws NoSuchContactException, SystemException {
305         int count = countByCompanyId(companyId);
306         List list = findByCompanyId(companyId, count - 1, count, obc);
307 
308         if (list.size() == 0) {
309             StringMaker msg = new StringMaker();
310             msg.append("No Contact exists with the key ");
311             msg.append(StringPool.OPEN_CURLY_BRACE);
312             msg.append("companyId=");
313             msg.append(companyId);
314             msg.append(StringPool.CLOSE_CURLY_BRACE);
315             throw new NoSuchContactException(msg.toString());
316         }
317         else {
318             return (Contact)list.get(0);
319         }
320     }
321 
322     public Contact[] findByCompanyId_PrevAndNext(long contactId,
323         long companyId, OrderByComparator obc)
324         throws NoSuchContactException, SystemException {
325         Contact contact = findByPrimaryKey(contactId);
326         int count = countByCompanyId(companyId);
327         Session session = null;
328 
329         try {
330             session = openSession();
331 
332             StringMaker query = new StringMaker();
333             query.append("FROM com.liferay.portal.model.Contact WHERE ");
334             query.append("companyId = ?");
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             int queryPos = 0;
344             q.setLong(queryPos++, companyId);
345 
346             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, contact);
347             Contact[] array = new ContactImpl[3];
348             array[0] = (Contact)objArray[0];
349             array[1] = (Contact)objArray[1];
350             array[2] = (Contact)objArray[2];
351 
352             return array;
353         }
354         catch (Exception e) {
355             throw HibernateUtil.processException(e);
356         }
357         finally {
358             closeSession(session);
359         }
360     }
361 
362     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
363         throws SystemException {
364         Session session = null;
365 
366         try {
367             session = openSession();
368 
369             DynamicQuery query = queryInitializer.initialize(session);
370 
371             return query.list();
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         int begin, int end) throws SystemException {
383         Session session = null;
384 
385         try {
386             session = openSession();
387 
388             DynamicQuery query = queryInitializer.initialize(session);
389             query.setLimit(begin, end);
390 
391             return query.list();
392         }
393         catch (Exception e) {
394             throw HibernateUtil.processException(e);
395         }
396         finally {
397             closeSession(session);
398         }
399     }
400 
401     public List findAll() throws SystemException {
402         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
403     }
404 
405     public List findAll(int begin, int end) throws SystemException {
406         return findAll(begin, end, null);
407     }
408 
409     public List findAll(int begin, int end, OrderByComparator obc)
410         throws SystemException {
411         String finderClassName = Contact.class.getName();
412         String finderMethodName = "findAll";
413         String[] finderParams = new String[] {
414                 "java.lang.Integer", "java.lang.Integer",
415                 "com.liferay.portal.kernel.util.OrderByComparator"
416             };
417         Object[] finderArgs = new Object[] {
418                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
419             };
420         Object result = FinderCache.getResult(finderClassName,
421                 finderMethodName, finderParams, finderArgs, getSessionFactory());
422 
423         if (result == null) {
424             Session session = null;
425 
426             try {
427                 session = openSession();
428 
429                 StringMaker query = new StringMaker();
430                 query.append("FROM com.liferay.portal.model.Contact ");
431 
432                 if (obc != null) {
433                     query.append("ORDER BY ");
434                     query.append(obc.getOrderBy());
435                 }
436 
437                 Query q = session.createQuery(query.toString());
438                 List list = QueryUtil.list(q, getDialect(), begin, end);
439 
440                 if (obc == null) {
441                     Collections.sort(list);
442                 }
443 
444                 FinderCache.putResult(finderClassName, finderMethodName,
445                     finderParams, finderArgs, list);
446 
447                 return list;
448             }
449             catch (Exception e) {
450                 throw HibernateUtil.processException(e);
451             }
452             finally {
453                 closeSession(session);
454             }
455         }
456         else {
457             return (List)result;
458         }
459     }
460 
461     public void removeByCompanyId(long companyId) throws SystemException {
462         Iterator itr = findByCompanyId(companyId).iterator();
463 
464         while (itr.hasNext()) {
465             Contact contact = (Contact)itr.next();
466             remove(contact);
467         }
468     }
469 
470     public void removeAll() throws SystemException {
471         Iterator itr = findAll().iterator();
472 
473         while (itr.hasNext()) {
474             remove((Contact)itr.next());
475         }
476     }
477 
478     public int countByCompanyId(long companyId) throws SystemException {
479         String finderClassName = Contact.class.getName();
480         String finderMethodName = "countByCompanyId";
481         String[] finderParams = new String[] { Long.class.getName() };
482         Object[] finderArgs = new Object[] { new Long(companyId) };
483         Object result = FinderCache.getResult(finderClassName,
484                 finderMethodName, finderParams, finderArgs, getSessionFactory());
485 
486         if (result == null) {
487             Session session = null;
488 
489             try {
490                 session = openSession();
491 
492                 StringMaker query = new StringMaker();
493                 query.append("SELECT COUNT(*) ");
494                 query.append("FROM com.liferay.portal.model.Contact WHERE ");
495                 query.append("companyId = ?");
496                 query.append(" ");
497 
498                 Query q = session.createQuery(query.toString());
499                 int queryPos = 0;
500                 q.setLong(queryPos++, companyId);
501 
502                 Long count = null;
503                 Iterator itr = q.list().iterator();
504 
505                 if (itr.hasNext()) {
506                     count = (Long)itr.next();
507                 }
508 
509                 if (count == null) {
510                     count = new Long(0);
511                 }
512 
513                 FinderCache.putResult(finderClassName, finderMethodName,
514                     finderParams, finderArgs, count);
515 
516                 return count.intValue();
517             }
518             catch (Exception e) {
519                 throw HibernateUtil.processException(e);
520             }
521             finally {
522                 closeSession(session);
523             }
524         }
525         else {
526             return ((Long)result).intValue();
527         }
528     }
529 
530     public int countAll() throws SystemException {
531         String finderClassName = Contact.class.getName();
532         String finderMethodName = "countAll";
533         String[] finderParams = new String[] {  };
534         Object[] finderArgs = new Object[] {  };
535         Object result = FinderCache.getResult(finderClassName,
536                 finderMethodName, finderParams, finderArgs, getSessionFactory());
537 
538         if (result == null) {
539             Session session = null;
540 
541             try {
542                 session = openSession();
543 
544                 StringMaker query = new StringMaker();
545                 query.append("SELECT COUNT(*) ");
546                 query.append("FROM com.liferay.portal.model.Contact");
547 
548                 Query q = session.createQuery(query.toString());
549                 Long count = null;
550                 Iterator itr = q.list().iterator();
551 
552                 if (itr.hasNext()) {
553                     count = (Long)itr.next();
554                 }
555 
556                 if (count == null) {
557                     count = new Long(0);
558                 }
559 
560                 FinderCache.putResult(finderClassName, finderMethodName,
561                     finderParams, finderArgs, count);
562 
563                 return count.intValue();
564             }
565             catch (Exception e) {
566                 throw HibernateUtil.processException(e);
567             }
568             finally {
569                 closeSession(session);
570             }
571         }
572         else {
573             return ((Long)result).intValue();
574         }
575     }
576 
577     protected void initDao() {
578     }
579 
580     private static Log _log = LogFactory.getLog(ContactPersistenceImpl.class);
581 }