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.NoSuchCompanyException;
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.Company;
33  import com.liferay.portal.model.impl.CompanyImpl;
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="CompanyPersistenceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class CompanyPersistenceImpl extends BasePersistence
57      implements CompanyPersistence {
58      public Company create(long companyId) {
59          Company company = new CompanyImpl();
60          company.setNew(true);
61          company.setPrimaryKey(companyId);
62  
63          return company;
64      }
65  
66      public Company remove(long companyId)
67          throws NoSuchCompanyException, SystemException {
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              Company company = (Company)session.get(CompanyImpl.class,
74                      new Long(companyId));
75  
76              if (company == null) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn("No Company exists with the primary key " +
79                          companyId);
80                  }
81  
82                  throw new NoSuchCompanyException(
83                      "No Company exists with the primary key " + companyId);
84              }
85  
86              return remove(company);
87          }
88          catch (NoSuchCompanyException 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 Company remove(Company company) throws SystemException {
100         Session session = null;
101 
102         try {
103             session = openSession();
104             session.delete(company);
105             session.flush();
106 
107             return company;
108         }
109         catch (Exception e) {
110             throw HibernateUtil.processException(e);
111         }
112         finally {
113             closeSession(session);
114             FinderCache.clearCache(Company.class.getName());
115         }
116     }
117 
118     public Company update(com.liferay.portal.model.Company company)
119         throws SystemException {
120         return update(company, false);
121     }
122 
123     public Company update(com.liferay.portal.model.Company company,
124         boolean merge) throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             if (merge) {
131                 session.merge(company);
132             }
133             else {
134                 if (company.isNew()) {
135                     session.save(company);
136                 }
137             }
138 
139             session.flush();
140             company.setNew(false);
141 
142             return company;
143         }
144         catch (Exception e) {
145             throw HibernateUtil.processException(e);
146         }
147         finally {
148             closeSession(session);
149             FinderCache.clearCache(Company.class.getName());
150         }
151     }
152 
153     public Company findByPrimaryKey(long companyId)
154         throws NoSuchCompanyException, SystemException {
155         Company company = fetchByPrimaryKey(companyId);
156 
157         if (company == null) {
158             if (_log.isWarnEnabled()) {
159                 _log.warn("No Company exists with the primary key " +
160                     companyId);
161             }
162 
163             throw new NoSuchCompanyException(
164                 "No Company exists with the primary key " + companyId);
165         }
166 
167         return company;
168     }
169 
170     public Company fetchByPrimaryKey(long companyId) throws SystemException {
171         Session session = null;
172 
173         try {
174             session = openSession();
175 
176             return (Company)session.get(CompanyImpl.class, new Long(companyId));
177         }
178         catch (Exception e) {
179             throw HibernateUtil.processException(e);
180         }
181         finally {
182             closeSession(session);
183         }
184     }
185 
186     public Company findByWebId(String webId)
187         throws NoSuchCompanyException, SystemException {
188         Company company = fetchByWebId(webId);
189 
190         if (company == null) {
191             StringMaker msg = new StringMaker();
192             msg.append("No Company exists with the key ");
193             msg.append(StringPool.OPEN_CURLY_BRACE);
194             msg.append("webId=");
195             msg.append(webId);
196             msg.append(StringPool.CLOSE_CURLY_BRACE);
197 
198             if (_log.isWarnEnabled()) {
199                 _log.warn(msg.toString());
200             }
201 
202             throw new NoSuchCompanyException(msg.toString());
203         }
204 
205         return company;
206     }
207 
208     public Company fetchByWebId(String webId) throws SystemException {
209         String finderClassName = Company.class.getName();
210         String finderMethodName = "fetchByWebId";
211         String[] finderParams = new String[] { String.class.getName() };
212         Object[] finderArgs = new Object[] { webId };
213         Object result = FinderCache.getResult(finderClassName,
214                 finderMethodName, finderParams, finderArgs, getSessionFactory());
215 
216         if (result == null) {
217             Session session = null;
218 
219             try {
220                 session = openSession();
221 
222                 StringMaker query = new StringMaker();
223                 query.append("FROM com.liferay.portal.model.Company WHERE ");
224 
225                 if (webId == null) {
226                     query.append("webId IS NULL");
227                 }
228                 else {
229                     query.append("webId = ?");
230                 }
231 
232                 query.append(" ");
233 
234                 Query q = session.createQuery(query.toString());
235                 int queryPos = 0;
236 
237                 if (webId != null) {
238                     q.setString(queryPos++, webId);
239                 }
240 
241                 List list = q.list();
242                 FinderCache.putResult(finderClassName, finderMethodName,
243                     finderParams, finderArgs, list);
244 
245                 if (list.size() == 0) {
246                     return null;
247                 }
248                 else {
249                     return (Company)list.get(0);
250                 }
251             }
252             catch (Exception e) {
253                 throw HibernateUtil.processException(e);
254             }
255             finally {
256                 closeSession(session);
257             }
258         }
259         else {
260             List list = (List)result;
261 
262             if (list.size() == 0) {
263                 return null;
264             }
265             else {
266                 return (Company)list.get(0);
267             }
268         }
269     }
270 
271     public Company findByVirtualHost(String virtualHost)
272         throws NoSuchCompanyException, SystemException {
273         Company company = fetchByVirtualHost(virtualHost);
274 
275         if (company == null) {
276             StringMaker msg = new StringMaker();
277             msg.append("No Company exists with the key ");
278             msg.append(StringPool.OPEN_CURLY_BRACE);
279             msg.append("virtualHost=");
280             msg.append(virtualHost);
281             msg.append(StringPool.CLOSE_CURLY_BRACE);
282 
283             if (_log.isWarnEnabled()) {
284                 _log.warn(msg.toString());
285             }
286 
287             throw new NoSuchCompanyException(msg.toString());
288         }
289 
290         return company;
291     }
292 
293     public Company fetchByVirtualHost(String virtualHost)
294         throws SystemException {
295         String finderClassName = Company.class.getName();
296         String finderMethodName = "fetchByVirtualHost";
297         String[] finderParams = new String[] { String.class.getName() };
298         Object[] finderArgs = new Object[] { virtualHost };
299         Object result = FinderCache.getResult(finderClassName,
300                 finderMethodName, finderParams, finderArgs, getSessionFactory());
301 
302         if (result == null) {
303             Session session = null;
304 
305             try {
306                 session = openSession();
307 
308                 StringMaker query = new StringMaker();
309                 query.append("FROM com.liferay.portal.model.Company WHERE ");
310 
311                 if (virtualHost == null) {
312                     query.append("virtualHost IS NULL");
313                 }
314                 else {
315                     query.append("virtualHost = ?");
316                 }
317 
318                 query.append(" ");
319 
320                 Query q = session.createQuery(query.toString());
321                 int queryPos = 0;
322 
323                 if (virtualHost != null) {
324                     q.setString(queryPos++, virtualHost);
325                 }
326 
327                 List list = q.list();
328                 FinderCache.putResult(finderClassName, finderMethodName,
329                     finderParams, finderArgs, list);
330 
331                 if (list.size() == 0) {
332                     return null;
333                 }
334                 else {
335                     return (Company)list.get(0);
336                 }
337             }
338             catch (Exception e) {
339                 throw HibernateUtil.processException(e);
340             }
341             finally {
342                 closeSession(session);
343             }
344         }
345         else {
346             List list = (List)result;
347 
348             if (list.size() == 0) {
349                 return null;
350             }
351             else {
352                 return (Company)list.get(0);
353             }
354         }
355     }
356 
357     public Company findByMx(String mx)
358         throws NoSuchCompanyException, SystemException {
359         Company company = fetchByMx(mx);
360 
361         if (company == null) {
362             StringMaker msg = new StringMaker();
363             msg.append("No Company exists with the key ");
364             msg.append(StringPool.OPEN_CURLY_BRACE);
365             msg.append("mx=");
366             msg.append(mx);
367             msg.append(StringPool.CLOSE_CURLY_BRACE);
368 
369             if (_log.isWarnEnabled()) {
370                 _log.warn(msg.toString());
371             }
372 
373             throw new NoSuchCompanyException(msg.toString());
374         }
375 
376         return company;
377     }
378 
379     public Company fetchByMx(String mx) throws SystemException {
380         String finderClassName = Company.class.getName();
381         String finderMethodName = "fetchByMx";
382         String[] finderParams = new String[] { String.class.getName() };
383         Object[] finderArgs = new Object[] { mx };
384         Object result = FinderCache.getResult(finderClassName,
385                 finderMethodName, finderParams, finderArgs, getSessionFactory());
386 
387         if (result == null) {
388             Session session = null;
389 
390             try {
391                 session = openSession();
392 
393                 StringMaker query = new StringMaker();
394                 query.append("FROM com.liferay.portal.model.Company WHERE ");
395 
396                 if (mx == null) {
397                     query.append("mx IS NULL");
398                 }
399                 else {
400                     query.append("mx = ?");
401                 }
402 
403                 query.append(" ");
404 
405                 Query q = session.createQuery(query.toString());
406                 int queryPos = 0;
407 
408                 if (mx != null) {
409                     q.setString(queryPos++, mx);
410                 }
411 
412                 List list = q.list();
413                 FinderCache.putResult(finderClassName, finderMethodName,
414                     finderParams, finderArgs, list);
415 
416                 if (list.size() == 0) {
417                     return null;
418                 }
419                 else {
420                     return (Company)list.get(0);
421                 }
422             }
423             catch (Exception e) {
424                 throw HibernateUtil.processException(e);
425             }
426             finally {
427                 closeSession(session);
428             }
429         }
430         else {
431             List list = (List)result;
432 
433             if (list.size() == 0) {
434                 return null;
435             }
436             else {
437                 return (Company)list.get(0);
438             }
439         }
440     }
441 
442     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
443         throws SystemException {
444         Session session = null;
445 
446         try {
447             session = openSession();
448 
449             DynamicQuery query = queryInitializer.initialize(session);
450 
451             return query.list();
452         }
453         catch (Exception e) {
454             throw HibernateUtil.processException(e);
455         }
456         finally {
457             closeSession(session);
458         }
459     }
460 
461     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
462         int begin, int end) throws SystemException {
463         Session session = null;
464 
465         try {
466             session = openSession();
467 
468             DynamicQuery query = queryInitializer.initialize(session);
469             query.setLimit(begin, end);
470 
471             return query.list();
472         }
473         catch (Exception e) {
474             throw HibernateUtil.processException(e);
475         }
476         finally {
477             closeSession(session);
478         }
479     }
480 
481     public List findAll() throws SystemException {
482         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
483     }
484 
485     public List findAll(int begin, int end) throws SystemException {
486         return findAll(begin, end, null);
487     }
488 
489     public List findAll(int begin, int end, OrderByComparator obc)
490         throws SystemException {
491         String finderClassName = Company.class.getName();
492         String finderMethodName = "findAll";
493         String[] finderParams = new String[] {
494                 "java.lang.Integer", "java.lang.Integer",
495                 "com.liferay.portal.kernel.util.OrderByComparator"
496             };
497         Object[] finderArgs = new Object[] {
498                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
499             };
500         Object result = FinderCache.getResult(finderClassName,
501                 finderMethodName, finderParams, finderArgs, getSessionFactory());
502 
503         if (result == null) {
504             Session session = null;
505 
506             try {
507                 session = openSession();
508 
509                 StringMaker query = new StringMaker();
510                 query.append("FROM com.liferay.portal.model.Company ");
511 
512                 if (obc != null) {
513                     query.append("ORDER BY ");
514                     query.append(obc.getOrderBy());
515                 }
516 
517                 Query q = session.createQuery(query.toString());
518                 List list = QueryUtil.list(q, getDialect(), begin, end);
519 
520                 if (obc == null) {
521                     Collections.sort(list);
522                 }
523 
524                 FinderCache.putResult(finderClassName, finderMethodName,
525                     finderParams, finderArgs, list);
526 
527                 return list;
528             }
529             catch (Exception e) {
530                 throw HibernateUtil.processException(e);
531             }
532             finally {
533                 closeSession(session);
534             }
535         }
536         else {
537             return (List)result;
538         }
539     }
540 
541     public void removeByWebId(String webId)
542         throws NoSuchCompanyException, SystemException {
543         Company company = findByWebId(webId);
544         remove(company);
545     }
546 
547     public void removeByVirtualHost(String virtualHost)
548         throws NoSuchCompanyException, SystemException {
549         Company company = findByVirtualHost(virtualHost);
550         remove(company);
551     }
552 
553     public void removeByMx(String mx)
554         throws NoSuchCompanyException, SystemException {
555         Company company = findByMx(mx);
556         remove(company);
557     }
558 
559     public void removeAll() throws SystemException {
560         Iterator itr = findAll().iterator();
561 
562         while (itr.hasNext()) {
563             remove((Company)itr.next());
564         }
565     }
566 
567     public int countByWebId(String webId) throws SystemException {
568         String finderClassName = Company.class.getName();
569         String finderMethodName = "countByWebId";
570         String[] finderParams = new String[] { String.class.getName() };
571         Object[] finderArgs = new Object[] { webId };
572         Object result = FinderCache.getResult(finderClassName,
573                 finderMethodName, finderParams, finderArgs, getSessionFactory());
574 
575         if (result == null) {
576             Session session = null;
577 
578             try {
579                 session = openSession();
580 
581                 StringMaker query = new StringMaker();
582                 query.append("SELECT COUNT(*) ");
583                 query.append("FROM com.liferay.portal.model.Company WHERE ");
584 
585                 if (webId == null) {
586                     query.append("webId IS NULL");
587                 }
588                 else {
589                     query.append("webId = ?");
590                 }
591 
592                 query.append(" ");
593 
594                 Query q = session.createQuery(query.toString());
595                 int queryPos = 0;
596 
597                 if (webId != null) {
598                     q.setString(queryPos++, webId);
599                 }
600 
601                 Long count = null;
602                 Iterator itr = q.list().iterator();
603 
604                 if (itr.hasNext()) {
605                     count = (Long)itr.next();
606                 }
607 
608                 if (count == null) {
609                     count = new Long(0);
610                 }
611 
612                 FinderCache.putResult(finderClassName, finderMethodName,
613                     finderParams, finderArgs, count);
614 
615                 return count.intValue();
616             }
617             catch (Exception e) {
618                 throw HibernateUtil.processException(e);
619             }
620             finally {
621                 closeSession(session);
622             }
623         }
624         else {
625             return ((Long)result).intValue();
626         }
627     }
628 
629     public int countByVirtualHost(String virtualHost) throws SystemException {
630         String finderClassName = Company.class.getName();
631         String finderMethodName = "countByVirtualHost";
632         String[] finderParams = new String[] { String.class.getName() };
633         Object[] finderArgs = new Object[] { virtualHost };
634         Object result = FinderCache.getResult(finderClassName,
635                 finderMethodName, finderParams, finderArgs, getSessionFactory());
636 
637         if (result == null) {
638             Session session = null;
639 
640             try {
641                 session = openSession();
642 
643                 StringMaker query = new StringMaker();
644                 query.append("SELECT COUNT(*) ");
645                 query.append("FROM com.liferay.portal.model.Company WHERE ");
646 
647                 if (virtualHost == null) {
648                     query.append("virtualHost IS NULL");
649                 }
650                 else {
651                     query.append("virtualHost = ?");
652                 }
653 
654                 query.append(" ");
655 
656                 Query q = session.createQuery(query.toString());
657                 int queryPos = 0;
658 
659                 if (virtualHost != null) {
660                     q.setString(queryPos++, virtualHost);
661                 }
662 
663                 Long count = null;
664                 Iterator itr = q.list().iterator();
665 
666                 if (itr.hasNext()) {
667                     count = (Long)itr.next();
668                 }
669 
670                 if (count == null) {
671                     count = new Long(0);
672                 }
673 
674                 FinderCache.putResult(finderClassName, finderMethodName,
675                     finderParams, finderArgs, count);
676 
677                 return count.intValue();
678             }
679             catch (Exception e) {
680                 throw HibernateUtil.processException(e);
681             }
682             finally {
683                 closeSession(session);
684             }
685         }
686         else {
687             return ((Long)result).intValue();
688         }
689     }
690 
691     public int countByMx(String mx) throws SystemException {
692         String finderClassName = Company.class.getName();
693         String finderMethodName = "countByMx";
694         String[] finderParams = new String[] { String.class.getName() };
695         Object[] finderArgs = new Object[] { mx };
696         Object result = FinderCache.getResult(finderClassName,
697                 finderMethodName, finderParams, finderArgs, getSessionFactory());
698 
699         if (result == null) {
700             Session session = null;
701 
702             try {
703                 session = openSession();
704 
705                 StringMaker query = new StringMaker();
706                 query.append("SELECT COUNT(*) ");
707                 query.append("FROM com.liferay.portal.model.Company WHERE ");
708 
709                 if (mx == null) {
710                     query.append("mx IS NULL");
711                 }
712                 else {
713                     query.append("mx = ?");
714                 }
715 
716                 query.append(" ");
717 
718                 Query q = session.createQuery(query.toString());
719                 int queryPos = 0;
720 
721                 if (mx != null) {
722                     q.setString(queryPos++, mx);
723                 }
724 
725                 Long count = null;
726                 Iterator itr = q.list().iterator();
727 
728                 if (itr.hasNext()) {
729                     count = (Long)itr.next();
730                 }
731 
732                 if (count == null) {
733                     count = new Long(0);
734                 }
735 
736                 FinderCache.putResult(finderClassName, finderMethodName,
737                     finderParams, finderArgs, count);
738 
739                 return count.intValue();
740             }
741             catch (Exception e) {
742                 throw HibernateUtil.processException(e);
743             }
744             finally {
745                 closeSession(session);
746             }
747         }
748         else {
749             return ((Long)result).intValue();
750         }
751     }
752 
753     public int countAll() throws SystemException {
754         String finderClassName = Company.class.getName();
755         String finderMethodName = "countAll";
756         String[] finderParams = new String[] {  };
757         Object[] finderArgs = new Object[] {  };
758         Object result = FinderCache.getResult(finderClassName,
759                 finderMethodName, finderParams, finderArgs, getSessionFactory());
760 
761         if (result == null) {
762             Session session = null;
763 
764             try {
765                 session = openSession();
766 
767                 StringMaker query = new StringMaker();
768                 query.append("SELECT COUNT(*) ");
769                 query.append("FROM com.liferay.portal.model.Company");
770 
771                 Query q = session.createQuery(query.toString());
772                 Long count = null;
773                 Iterator itr = q.list().iterator();
774 
775                 if (itr.hasNext()) {
776                     count = (Long)itr.next();
777                 }
778 
779                 if (count == null) {
780                     count = new Long(0);
781                 }
782 
783                 FinderCache.putResult(finderClassName, finderMethodName,
784                     finderParams, finderArgs, count);
785 
786                 return count.intValue();
787             }
788             catch (Exception e) {
789                 throw HibernateUtil.processException(e);
790             }
791             finally {
792                 closeSession(session);
793             }
794         }
795         else {
796             return ((Long)result).intValue();
797         }
798     }
799 
800     protected void initDao() {
801     }
802 
803     private static Log _log = LogFactory.getLog(CompanyPersistenceImpl.class);
804 }