1   /**
2    * Copyright (c) 2000-2008 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.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.Company;
39  import com.liferay.portal.model.ModelListener;
40  import com.liferay.portal.model.impl.CompanyImpl;
41  import com.liferay.portal.model.impl.CompanyModelImpl;
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  /**
53   * <a href="CompanyPersistenceImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class CompanyPersistenceImpl extends BasePersistenceImpl
59      implements CompanyPersistence {
60      public Company create(long companyId) {
61          Company company = new CompanyImpl();
62  
63          company.setNew(true);
64          company.setPrimaryKey(companyId);
65  
66          return company;
67      }
68  
69      public Company remove(long companyId)
70          throws NoSuchCompanyException, SystemException {
71          Session session = null;
72  
73          try {
74              session = openSession();
75  
76              Company company = (Company)session.get(CompanyImpl.class,
77                      new Long(companyId));
78  
79              if (company == null) {
80                  if (_log.isWarnEnabled()) {
81                      _log.warn("No Company exists with the primary key " +
82                          companyId);
83                  }
84  
85                  throw new NoSuchCompanyException(
86                      "No Company exists with the primary key " + companyId);
87              }
88  
89              return remove(company);
90          }
91          catch (NoSuchCompanyException nsee) {
92              throw nsee;
93          }
94          catch (Exception e) {
95              throw processException(e);
96          }
97          finally {
98              closeSession(session);
99          }
100     }
101 
102     public Company remove(Company company) throws SystemException {
103         if (_listeners.length > 0) {
104             for (ModelListener listener : _listeners) {
105                 listener.onBeforeRemove(company);
106             }
107         }
108 
109         company = removeImpl(company);
110 
111         if (_listeners.length > 0) {
112             for (ModelListener listener : _listeners) {
113                 listener.onAfterRemove(company);
114             }
115         }
116 
117         return company;
118     }
119 
120     protected Company removeImpl(Company company) throws SystemException {
121         Session session = null;
122 
123         try {
124             session = openSession();
125 
126             if (BatchSessionUtil.isEnabled()) {
127                 Object staleObject = session.get(CompanyImpl.class,
128                         company.getPrimaryKeyObj());
129 
130                 if (staleObject != null) {
131                     session.evict(staleObject);
132                 }
133             }
134 
135             session.delete(company);
136 
137             session.flush();
138 
139             return company;
140         }
141         catch (Exception e) {
142             throw processException(e);
143         }
144         finally {
145             closeSession(session);
146 
147             FinderCacheUtil.clearCache(Company.class.getName());
148         }
149     }
150 
151     /**
152      * @deprecated Use <code>update(Company company, boolean merge)</code>.
153      */
154     public Company update(Company company) throws SystemException {
155         if (_log.isWarnEnabled()) {
156             _log.warn(
157                 "Using the deprecated update(Company company) method. Use update(Company company, boolean merge) instead.");
158         }
159 
160         return update(company, false);
161     }
162 
163     /**
164      * Add, update, or merge, the entity. This method also calls the model
165      * listeners to trigger the proper events associated with adding, deleting,
166      * or updating an entity.
167      *
168      * @param        company the entity to add, update, or merge
169      * @param        merge boolean value for whether to merge the entity. The
170      *                default value is false. Setting merge to true is more
171      *                expensive and should only be true when company is
172      *                transient. See LEP-5473 for a detailed discussion of this
173      *                method.
174      * @return        true if the portlet can be displayed via Ajax
175      */
176     public Company update(Company company, boolean merge)
177         throws SystemException {
178         boolean isNew = company.isNew();
179 
180         if (_listeners.length > 0) {
181             for (ModelListener listener : _listeners) {
182                 if (isNew) {
183                     listener.onBeforeCreate(company);
184                 }
185                 else {
186                     listener.onBeforeUpdate(company);
187                 }
188             }
189         }
190 
191         company = updateImpl(company, merge);
192 
193         if (_listeners.length > 0) {
194             for (ModelListener listener : _listeners) {
195                 if (isNew) {
196                     listener.onAfterCreate(company);
197                 }
198                 else {
199                     listener.onAfterUpdate(company);
200                 }
201             }
202         }
203 
204         return company;
205     }
206 
207     public Company updateImpl(com.liferay.portal.model.Company company,
208         boolean merge) throws SystemException {
209         Session session = null;
210 
211         try {
212             session = openSession();
213 
214             BatchSessionUtil.update(session, company, merge);
215 
216             company.setNew(false);
217 
218             return company;
219         }
220         catch (Exception e) {
221             throw processException(e);
222         }
223         finally {
224             closeSession(session);
225 
226             FinderCacheUtil.clearCache(Company.class.getName());
227         }
228     }
229 
230     public Company findByPrimaryKey(long companyId)
231         throws NoSuchCompanyException, SystemException {
232         Company company = fetchByPrimaryKey(companyId);
233 
234         if (company == null) {
235             if (_log.isWarnEnabled()) {
236                 _log.warn("No Company exists with the primary key " +
237                     companyId);
238             }
239 
240             throw new NoSuchCompanyException(
241                 "No Company exists with the primary key " + companyId);
242         }
243 
244         return company;
245     }
246 
247     public Company fetchByPrimaryKey(long companyId) throws SystemException {
248         Session session = null;
249 
250         try {
251             session = openSession();
252 
253             return (Company)session.get(CompanyImpl.class, new Long(companyId));
254         }
255         catch (Exception e) {
256             throw processException(e);
257         }
258         finally {
259             closeSession(session);
260         }
261     }
262 
263     public Company findByWebId(String webId)
264         throws NoSuchCompanyException, SystemException {
265         Company company = fetchByWebId(webId);
266 
267         if (company == null) {
268             StringBuilder msg = new StringBuilder();
269 
270             msg.append("No Company exists with the key {");
271 
272             msg.append("webId=" + webId);
273 
274             msg.append(StringPool.CLOSE_CURLY_BRACE);
275 
276             if (_log.isWarnEnabled()) {
277                 _log.warn(msg.toString());
278             }
279 
280             throw new NoSuchCompanyException(msg.toString());
281         }
282 
283         return company;
284     }
285 
286     public Company fetchByWebId(String webId) throws SystemException {
287         boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
288         String finderClassName = Company.class.getName();
289         String finderMethodName = "fetchByWebId";
290         String[] finderParams = new String[] { String.class.getName() };
291         Object[] finderArgs = new Object[] { webId };
292 
293         Object result = null;
294 
295         if (finderClassNameCacheEnabled) {
296             result = FinderCacheUtil.getResult(finderClassName,
297                     finderMethodName, finderParams, finderArgs, this);
298         }
299 
300         if (result == null) {
301             Session session = null;
302 
303             try {
304                 session = openSession();
305 
306                 StringBuilder query = new StringBuilder();
307 
308                 query.append("FROM com.liferay.portal.model.Company WHERE ");
309 
310                 if (webId == null) {
311                     query.append("webId IS NULL");
312                 }
313                 else {
314                     query.append("webId = ?");
315                 }
316 
317                 query.append(" ");
318 
319                 Query q = session.createQuery(query.toString());
320 
321                 QueryPos qPos = QueryPos.getInstance(q);
322 
323                 if (webId != null) {
324                     qPos.add(webId);
325                 }
326 
327                 List<Company> list = q.list();
328 
329                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
330                     finderClassName, finderMethodName, finderParams,
331                     finderArgs, list);
332 
333                 if (list.size() == 0) {
334                     return null;
335                 }
336                 else {
337                     return list.get(0);
338                 }
339             }
340             catch (Exception e) {
341                 throw processException(e);
342             }
343             finally {
344                 closeSession(session);
345             }
346         }
347         else {
348             List<Company> list = (List<Company>)result;
349 
350             if (list.size() == 0) {
351                 return null;
352             }
353             else {
354                 return list.get(0);
355             }
356         }
357     }
358 
359     public Company findByVirtualHost(String virtualHost)
360         throws NoSuchCompanyException, SystemException {
361         Company company = fetchByVirtualHost(virtualHost);
362 
363         if (company == null) {
364             StringBuilder msg = new StringBuilder();
365 
366             msg.append("No Company exists with the key {");
367 
368             msg.append("virtualHost=" + virtualHost);
369 
370             msg.append(StringPool.CLOSE_CURLY_BRACE);
371 
372             if (_log.isWarnEnabled()) {
373                 _log.warn(msg.toString());
374             }
375 
376             throw new NoSuchCompanyException(msg.toString());
377         }
378 
379         return company;
380     }
381 
382     public Company fetchByVirtualHost(String virtualHost)
383         throws SystemException {
384         boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
385         String finderClassName = Company.class.getName();
386         String finderMethodName = "fetchByVirtualHost";
387         String[] finderParams = new String[] { String.class.getName() };
388         Object[] finderArgs = new Object[] { virtualHost };
389 
390         Object result = null;
391 
392         if (finderClassNameCacheEnabled) {
393             result = FinderCacheUtil.getResult(finderClassName,
394                     finderMethodName, finderParams, finderArgs, this);
395         }
396 
397         if (result == null) {
398             Session session = null;
399 
400             try {
401                 session = openSession();
402 
403                 StringBuilder query = new StringBuilder();
404 
405                 query.append("FROM com.liferay.portal.model.Company WHERE ");
406 
407                 if (virtualHost == null) {
408                     query.append("virtualHost IS NULL");
409                 }
410                 else {
411                     query.append("virtualHost = ?");
412                 }
413 
414                 query.append(" ");
415 
416                 Query q = session.createQuery(query.toString());
417 
418                 QueryPos qPos = QueryPos.getInstance(q);
419 
420                 if (virtualHost != null) {
421                     qPos.add(virtualHost);
422                 }
423 
424                 List<Company> list = q.list();
425 
426                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
427                     finderClassName, finderMethodName, finderParams,
428                     finderArgs, list);
429 
430                 if (list.size() == 0) {
431                     return null;
432                 }
433                 else {
434                     return list.get(0);
435                 }
436             }
437             catch (Exception e) {
438                 throw processException(e);
439             }
440             finally {
441                 closeSession(session);
442             }
443         }
444         else {
445             List<Company> list = (List<Company>)result;
446 
447             if (list.size() == 0) {
448                 return null;
449             }
450             else {
451                 return list.get(0);
452             }
453         }
454     }
455 
456     public Company findByMx(String mx)
457         throws NoSuchCompanyException, SystemException {
458         Company company = fetchByMx(mx);
459 
460         if (company == null) {
461             StringBuilder msg = new StringBuilder();
462 
463             msg.append("No Company exists with the key {");
464 
465             msg.append("mx=" + mx);
466 
467             msg.append(StringPool.CLOSE_CURLY_BRACE);
468 
469             if (_log.isWarnEnabled()) {
470                 _log.warn(msg.toString());
471             }
472 
473             throw new NoSuchCompanyException(msg.toString());
474         }
475 
476         return company;
477     }
478 
479     public Company fetchByMx(String mx) throws SystemException {
480         boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
481         String finderClassName = Company.class.getName();
482         String finderMethodName = "fetchByMx";
483         String[] finderParams = new String[] { String.class.getName() };
484         Object[] finderArgs = new Object[] { mx };
485 
486         Object result = null;
487 
488         if (finderClassNameCacheEnabled) {
489             result = FinderCacheUtil.getResult(finderClassName,
490                     finderMethodName, finderParams, finderArgs, this);
491         }
492 
493         if (result == null) {
494             Session session = null;
495 
496             try {
497                 session = openSession();
498 
499                 StringBuilder query = new StringBuilder();
500 
501                 query.append("FROM com.liferay.portal.model.Company WHERE ");
502 
503                 if (mx == null) {
504                     query.append("mx IS NULL");
505                 }
506                 else {
507                     query.append("mx = ?");
508                 }
509 
510                 query.append(" ");
511 
512                 Query q = session.createQuery(query.toString());
513 
514                 QueryPos qPos = QueryPos.getInstance(q);
515 
516                 if (mx != null) {
517                     qPos.add(mx);
518                 }
519 
520                 List<Company> list = q.list();
521 
522                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
523                     finderClassName, finderMethodName, finderParams,
524                     finderArgs, list);
525 
526                 if (list.size() == 0) {
527                     return null;
528                 }
529                 else {
530                     return list.get(0);
531                 }
532             }
533             catch (Exception e) {
534                 throw processException(e);
535             }
536             finally {
537                 closeSession(session);
538             }
539         }
540         else {
541             List<Company> list = (List<Company>)result;
542 
543             if (list.size() == 0) {
544                 return null;
545             }
546             else {
547                 return list.get(0);
548             }
549         }
550     }
551 
552     public Company findByLogoId(long logoId)
553         throws NoSuchCompanyException, SystemException {
554         Company company = fetchByLogoId(logoId);
555 
556         if (company == null) {
557             StringBuilder msg = new StringBuilder();
558 
559             msg.append("No Company exists with the key {");
560 
561             msg.append("logoId=" + logoId);
562 
563             msg.append(StringPool.CLOSE_CURLY_BRACE);
564 
565             if (_log.isWarnEnabled()) {
566                 _log.warn(msg.toString());
567             }
568 
569             throw new NoSuchCompanyException(msg.toString());
570         }
571 
572         return company;
573     }
574 
575     public Company fetchByLogoId(long logoId) throws SystemException {
576         boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
577         String finderClassName = Company.class.getName();
578         String finderMethodName = "fetchByLogoId";
579         String[] finderParams = new String[] { Long.class.getName() };
580         Object[] finderArgs = new Object[] { new Long(logoId) };
581 
582         Object result = null;
583 
584         if (finderClassNameCacheEnabled) {
585             result = FinderCacheUtil.getResult(finderClassName,
586                     finderMethodName, finderParams, finderArgs, this);
587         }
588 
589         if (result == null) {
590             Session session = null;
591 
592             try {
593                 session = openSession();
594 
595                 StringBuilder query = new StringBuilder();
596 
597                 query.append("FROM com.liferay.portal.model.Company WHERE ");
598 
599                 query.append("logoId = ?");
600 
601                 query.append(" ");
602 
603                 Query q = session.createQuery(query.toString());
604 
605                 QueryPos qPos = QueryPos.getInstance(q);
606 
607                 qPos.add(logoId);
608 
609                 List<Company> list = q.list();
610 
611                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
612                     finderClassName, finderMethodName, finderParams,
613                     finderArgs, list);
614 
615                 if (list.size() == 0) {
616                     return null;
617                 }
618                 else {
619                     return list.get(0);
620                 }
621             }
622             catch (Exception e) {
623                 throw processException(e);
624             }
625             finally {
626                 closeSession(session);
627             }
628         }
629         else {
630             List<Company> list = (List<Company>)result;
631 
632             if (list.size() == 0) {
633                 return null;
634             }
635             else {
636                 return list.get(0);
637             }
638         }
639     }
640 
641     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
642         throws SystemException {
643         Session session = null;
644 
645         try {
646             session = openSession();
647 
648             dynamicQuery.compile(session);
649 
650             return dynamicQuery.list();
651         }
652         catch (Exception e) {
653             throw processException(e);
654         }
655         finally {
656             closeSession(session);
657         }
658     }
659 
660     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
661         int start, int end) throws SystemException {
662         Session session = null;
663 
664         try {
665             session = openSession();
666 
667             dynamicQuery.setLimit(start, end);
668 
669             dynamicQuery.compile(session);
670 
671             return dynamicQuery.list();
672         }
673         catch (Exception e) {
674             throw processException(e);
675         }
676         finally {
677             closeSession(session);
678         }
679     }
680 
681     public List<Company> findAll() throws SystemException {
682         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
683     }
684 
685     public List<Company> findAll(int start, int end) throws SystemException {
686         return findAll(start, end, null);
687     }
688 
689     public List<Company> findAll(int start, int end, OrderByComparator obc)
690         throws SystemException {
691         boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
692         String finderClassName = Company.class.getName();
693         String finderMethodName = "findAll";
694         String[] finderParams = new String[] {
695                 "java.lang.Integer", "java.lang.Integer",
696                 "com.liferay.portal.kernel.util.OrderByComparator"
697             };
698         Object[] finderArgs = new Object[] {
699                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
700             };
701 
702         Object result = null;
703 
704         if (finderClassNameCacheEnabled) {
705             result = FinderCacheUtil.getResult(finderClassName,
706                     finderMethodName, finderParams, finderArgs, this);
707         }
708 
709         if (result == null) {
710             Session session = null;
711 
712             try {
713                 session = openSession();
714 
715                 StringBuilder query = new StringBuilder();
716 
717                 query.append("FROM com.liferay.portal.model.Company ");
718 
719                 if (obc != null) {
720                     query.append("ORDER BY ");
721                     query.append(obc.getOrderBy());
722                 }
723 
724                 Query q = session.createQuery(query.toString());
725 
726                 List<Company> list = null;
727 
728                 if (obc == null) {
729                     list = (List<Company>)QueryUtil.list(q, getDialect(),
730                             start, end, false);
731 
732                     Collections.sort(list);
733                 }
734                 else {
735                     list = (List<Company>)QueryUtil.list(q, getDialect(),
736                             start, end);
737                 }
738 
739                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
740                     finderClassName, finderMethodName, finderParams,
741                     finderArgs, list);
742 
743                 return list;
744             }
745             catch (Exception e) {
746                 throw processException(e);
747             }
748             finally {
749                 closeSession(session);
750             }
751         }
752         else {
753             return (List<Company>)result;
754         }
755     }
756 
757     public void removeByWebId(String webId)
758         throws NoSuchCompanyException, SystemException {
759         Company company = findByWebId(webId);
760 
761         remove(company);
762     }
763 
764     public void removeByVirtualHost(String virtualHost)
765         throws NoSuchCompanyException, SystemException {
766         Company company = findByVirtualHost(virtualHost);
767 
768         remove(company);
769     }
770 
771     public void removeByMx(String mx)
772         throws NoSuchCompanyException, SystemException {
773         Company company = findByMx(mx);
774 
775         remove(company);
776     }
777 
778     public void removeByLogoId(long logoId)
779         throws NoSuchCompanyException, SystemException {
780         Company company = findByLogoId(logoId);
781 
782         remove(company);
783     }
784 
785     public void removeAll() throws SystemException {
786         for (Company company : findAll()) {
787             remove(company);
788         }
789     }
790 
791     public int countByWebId(String webId) throws SystemException {
792         boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
793         String finderClassName = Company.class.getName();
794         String finderMethodName = "countByWebId";
795         String[] finderParams = new String[] { String.class.getName() };
796         Object[] finderArgs = new Object[] { webId };
797 
798         Object result = null;
799 
800         if (finderClassNameCacheEnabled) {
801             result = FinderCacheUtil.getResult(finderClassName,
802                     finderMethodName, finderParams, finderArgs, this);
803         }
804 
805         if (result == null) {
806             Session session = null;
807 
808             try {
809                 session = openSession();
810 
811                 StringBuilder query = new StringBuilder();
812 
813                 query.append("SELECT COUNT(*) ");
814                 query.append("FROM com.liferay.portal.model.Company WHERE ");
815 
816                 if (webId == null) {
817                     query.append("webId IS NULL");
818                 }
819                 else {
820                     query.append("webId = ?");
821                 }
822 
823                 query.append(" ");
824 
825                 Query q = session.createQuery(query.toString());
826 
827                 QueryPos qPos = QueryPos.getInstance(q);
828 
829                 if (webId != null) {
830                     qPos.add(webId);
831                 }
832 
833                 Long count = null;
834 
835                 Iterator<Long> itr = q.list().iterator();
836 
837                 if (itr.hasNext()) {
838                     count = itr.next();
839                 }
840 
841                 if (count == null) {
842                     count = new Long(0);
843                 }
844 
845                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
846                     finderClassName, finderMethodName, finderParams,
847                     finderArgs, count);
848 
849                 return count.intValue();
850             }
851             catch (Exception e) {
852                 throw processException(e);
853             }
854             finally {
855                 closeSession(session);
856             }
857         }
858         else {
859             return ((Long)result).intValue();
860         }
861     }
862 
863     public int countByVirtualHost(String virtualHost) throws SystemException {
864         boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
865         String finderClassName = Company.class.getName();
866         String finderMethodName = "countByVirtualHost";
867         String[] finderParams = new String[] { String.class.getName() };
868         Object[] finderArgs = new Object[] { virtualHost };
869 
870         Object result = null;
871 
872         if (finderClassNameCacheEnabled) {
873             result = FinderCacheUtil.getResult(finderClassName,
874                     finderMethodName, finderParams, finderArgs, this);
875         }
876 
877         if (result == null) {
878             Session session = null;
879 
880             try {
881                 session = openSession();
882 
883                 StringBuilder query = new StringBuilder();
884 
885                 query.append("SELECT COUNT(*) ");
886                 query.append("FROM com.liferay.portal.model.Company WHERE ");
887 
888                 if (virtualHost == null) {
889                     query.append("virtualHost IS NULL");
890                 }
891                 else {
892                     query.append("virtualHost = ?");
893                 }
894 
895                 query.append(" ");
896 
897                 Query q = session.createQuery(query.toString());
898 
899                 QueryPos qPos = QueryPos.getInstance(q);
900 
901                 if (virtualHost != null) {
902                     qPos.add(virtualHost);
903                 }
904 
905                 Long count = null;
906 
907                 Iterator<Long> itr = q.list().iterator();
908 
909                 if (itr.hasNext()) {
910                     count = itr.next();
911                 }
912 
913                 if (count == null) {
914                     count = new Long(0);
915                 }
916 
917                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
918                     finderClassName, finderMethodName, finderParams,
919                     finderArgs, count);
920 
921                 return count.intValue();
922             }
923             catch (Exception e) {
924                 throw processException(e);
925             }
926             finally {
927                 closeSession(session);
928             }
929         }
930         else {
931             return ((Long)result).intValue();
932         }
933     }
934 
935     public int countByMx(String mx) throws SystemException {
936         boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
937         String finderClassName = Company.class.getName();
938         String finderMethodName = "countByMx";
939         String[] finderParams = new String[] { String.class.getName() };
940         Object[] finderArgs = new Object[] { mx };
941 
942         Object result = null;
943 
944         if (finderClassNameCacheEnabled) {
945             result = FinderCacheUtil.getResult(finderClassName,
946                     finderMethodName, finderParams, finderArgs, this);
947         }
948 
949         if (result == null) {
950             Session session = null;
951 
952             try {
953                 session = openSession();
954 
955                 StringBuilder query = new StringBuilder();
956 
957                 query.append("SELECT COUNT(*) ");
958                 query.append("FROM com.liferay.portal.model.Company WHERE ");
959 
960                 if (mx == null) {
961                     query.append("mx IS NULL");
962                 }
963                 else {
964                     query.append("mx = ?");
965                 }
966 
967                 query.append(" ");
968 
969                 Query q = session.createQuery(query.toString());
970 
971                 QueryPos qPos = QueryPos.getInstance(q);
972 
973                 if (mx != null) {
974                     qPos.add(mx);
975                 }
976 
977                 Long count = null;
978 
979                 Iterator<Long> itr = q.list().iterator();
980 
981                 if (itr.hasNext()) {
982                     count = itr.next();
983                 }
984 
985                 if (count == null) {
986                     count = new Long(0);
987                 }
988 
989                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
990                     finderClassName, finderMethodName, finderParams,
991                     finderArgs, count);
992 
993                 return count.intValue();
994             }
995             catch (Exception e) {
996                 throw processException(e);
997             }
998             finally {
999                 closeSession(session);
1000            }
1001        }
1002        else {
1003            return ((Long)result).intValue();
1004        }
1005    }
1006
1007    public int countByLogoId(long logoId) throws SystemException {
1008        boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
1009        String finderClassName = Company.class.getName();
1010        String finderMethodName = "countByLogoId";
1011        String[] finderParams = new String[] { Long.class.getName() };
1012        Object[] finderArgs = new Object[] { new Long(logoId) };
1013
1014        Object result = null;
1015
1016        if (finderClassNameCacheEnabled) {
1017            result = FinderCacheUtil.getResult(finderClassName,
1018                    finderMethodName, finderParams, finderArgs, this);
1019        }
1020
1021        if (result == null) {
1022            Session session = null;
1023
1024            try {
1025                session = openSession();
1026
1027                StringBuilder query = new StringBuilder();
1028
1029                query.append("SELECT COUNT(*) ");
1030                query.append("FROM com.liferay.portal.model.Company WHERE ");
1031
1032                query.append("logoId = ?");
1033
1034                query.append(" ");
1035
1036                Query q = session.createQuery(query.toString());
1037
1038                QueryPos qPos = QueryPos.getInstance(q);
1039
1040                qPos.add(logoId);
1041
1042                Long count = null;
1043
1044                Iterator<Long> itr = q.list().iterator();
1045
1046                if (itr.hasNext()) {
1047                    count = itr.next();
1048                }
1049
1050                if (count == null) {
1051                    count = new Long(0);
1052                }
1053
1054                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1055                    finderClassName, finderMethodName, finderParams,
1056                    finderArgs, count);
1057
1058                return count.intValue();
1059            }
1060            catch (Exception e) {
1061                throw processException(e);
1062            }
1063            finally {
1064                closeSession(session);
1065            }
1066        }
1067        else {
1068            return ((Long)result).intValue();
1069        }
1070    }
1071
1072    public int countAll() throws SystemException {
1073        boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
1074        String finderClassName = Company.class.getName();
1075        String finderMethodName = "countAll";
1076        String[] finderParams = new String[] {  };
1077        Object[] finderArgs = new Object[] {  };
1078
1079        Object result = null;
1080
1081        if (finderClassNameCacheEnabled) {
1082            result = FinderCacheUtil.getResult(finderClassName,
1083                    finderMethodName, finderParams, finderArgs, this);
1084        }
1085
1086        if (result == null) {
1087            Session session = null;
1088
1089            try {
1090                session = openSession();
1091
1092                Query q = session.createQuery(
1093                        "SELECT COUNT(*) FROM com.liferay.portal.model.Company");
1094
1095                Long count = null;
1096
1097                Iterator<Long> itr = q.list().iterator();
1098
1099                if (itr.hasNext()) {
1100                    count = itr.next();
1101                }
1102
1103                if (count == null) {
1104                    count = new Long(0);
1105                }
1106
1107                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1108                    finderClassName, finderMethodName, finderParams,
1109                    finderArgs, count);
1110
1111                return count.intValue();
1112            }
1113            catch (Exception e) {
1114                throw processException(e);
1115            }
1116            finally {
1117                closeSession(session);
1118            }
1119        }
1120        else {
1121            return ((Long)result).intValue();
1122        }
1123    }
1124
1125    public void registerListener(ModelListener listener) {
1126        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1127
1128        listeners.add(listener);
1129
1130        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1131    }
1132
1133    public void unregisterListener(ModelListener listener) {
1134        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1135
1136        listeners.remove(listener);
1137
1138        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1139    }
1140
1141    public void afterPropertiesSet() {
1142        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1143                    com.liferay.portal.util.PropsUtil.get(
1144                        "value.object.listener.com.liferay.portal.model.Company")));
1145
1146        if (listenerClassNames.length > 0) {
1147            try {
1148                List<ModelListener> listeners = new ArrayList<ModelListener>();
1149
1150                for (String listenerClassName : listenerClassNames) {
1151                    listeners.add((ModelListener)Class.forName(
1152                            listenerClassName).newInstance());
1153                }
1154
1155                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1156            }
1157            catch (Exception e) {
1158                _log.error(e);
1159            }
1160        }
1161    }
1162
1163    private static Log _log = LogFactory.getLog(CompanyPersistenceImpl.class);
1164    private ModelListener[] _listeners = new ModelListener[0];
1165}