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             session.delete(company);
127 
128             session.flush();
129 
130             return company;
131         }
132         catch (Exception e) {
133             throw processException(e);
134         }
135         finally {
136             closeSession(session);
137 
138             FinderCacheUtil.clearCache(Company.class.getName());
139         }
140     }
141 
142     /**
143      * @deprecated Use <code>update(Company company, boolean merge)</code>.
144      */
145     public Company update(Company company) throws SystemException {
146         if (_log.isWarnEnabled()) {
147             _log.warn(
148                 "Using the deprecated update(Company company) method. Use update(Company company, boolean merge) instead.");
149         }
150 
151         return update(company, false);
152     }
153 
154     /**
155      * Add, update, or merge, the entity. This method also calls the model
156      * listeners to trigger the proper events associated with adding, deleting,
157      * or updating an entity.
158      *
159      * @param        company the entity to add, update, or merge
160      * @param        merge boolean value for whether to merge the entity. The
161      *                default value is false. Setting merge to true is more
162      *                expensive and should only be true when company is
163      *                transient. See LEP-5473 for a detailed discussion of this
164      *                method.
165      * @return        true if the portlet can be displayed via Ajax
166      */
167     public Company update(Company company, boolean merge)
168         throws SystemException {
169         boolean isNew = company.isNew();
170 
171         if (_listeners.length > 0) {
172             for (ModelListener listener : _listeners) {
173                 if (isNew) {
174                     listener.onBeforeCreate(company);
175                 }
176                 else {
177                     listener.onBeforeUpdate(company);
178                 }
179             }
180         }
181 
182         company = updateImpl(company, merge);
183 
184         if (_listeners.length > 0) {
185             for (ModelListener listener : _listeners) {
186                 if (isNew) {
187                     listener.onAfterCreate(company);
188                 }
189                 else {
190                     listener.onAfterUpdate(company);
191                 }
192             }
193         }
194 
195         return company;
196     }
197 
198     public Company updateImpl(com.liferay.portal.model.Company company,
199         boolean merge) throws SystemException {
200         Session session = null;
201 
202         try {
203             session = openSession();
204 
205             if (merge) {
206                 session.merge(company);
207             }
208             else {
209                 if (company.isNew()) {
210                     session.save(company);
211                 }
212             }
213 
214             session.flush();
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 = (List<Company>)QueryUtil.list(q,
727                         getDialect(), start, end);
728 
729                 if (obc == null) {
730                     Collections.sort(list);
731                 }
732 
733                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
734                     finderClassName, finderMethodName, finderParams,
735                     finderArgs, list);
736 
737                 return list;
738             }
739             catch (Exception e) {
740                 throw processException(e);
741             }
742             finally {
743                 closeSession(session);
744             }
745         }
746         else {
747             return (List<Company>)result;
748         }
749     }
750 
751     public void removeByWebId(String webId)
752         throws NoSuchCompanyException, SystemException {
753         Company company = findByWebId(webId);
754 
755         remove(company);
756     }
757 
758     public void removeByVirtualHost(String virtualHost)
759         throws NoSuchCompanyException, SystemException {
760         Company company = findByVirtualHost(virtualHost);
761 
762         remove(company);
763     }
764 
765     public void removeByMx(String mx)
766         throws NoSuchCompanyException, SystemException {
767         Company company = findByMx(mx);
768 
769         remove(company);
770     }
771 
772     public void removeByLogoId(long logoId)
773         throws NoSuchCompanyException, SystemException {
774         Company company = findByLogoId(logoId);
775 
776         remove(company);
777     }
778 
779     public void removeAll() throws SystemException {
780         for (Company company : findAll()) {
781             remove(company);
782         }
783     }
784 
785     public int countByWebId(String webId) throws SystemException {
786         boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
787         String finderClassName = Company.class.getName();
788         String finderMethodName = "countByWebId";
789         String[] finderParams = new String[] { String.class.getName() };
790         Object[] finderArgs = new Object[] { webId };
791 
792         Object result = null;
793 
794         if (finderClassNameCacheEnabled) {
795             result = FinderCacheUtil.getResult(finderClassName,
796                     finderMethodName, finderParams, finderArgs, this);
797         }
798 
799         if (result == null) {
800             Session session = null;
801 
802             try {
803                 session = openSession();
804 
805                 StringBuilder query = new StringBuilder();
806 
807                 query.append("SELECT COUNT(*) ");
808                 query.append("FROM com.liferay.portal.model.Company WHERE ");
809 
810                 if (webId == null) {
811                     query.append("webId IS NULL");
812                 }
813                 else {
814                     query.append("webId = ?");
815                 }
816 
817                 query.append(" ");
818 
819                 Query q = session.createQuery(query.toString());
820 
821                 QueryPos qPos = QueryPos.getInstance(q);
822 
823                 if (webId != null) {
824                     qPos.add(webId);
825                 }
826 
827                 Long count = null;
828 
829                 Iterator<Long> itr = q.list().iterator();
830 
831                 if (itr.hasNext()) {
832                     count = itr.next();
833                 }
834 
835                 if (count == null) {
836                     count = new Long(0);
837                 }
838 
839                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
840                     finderClassName, finderMethodName, finderParams,
841                     finderArgs, count);
842 
843                 return count.intValue();
844             }
845             catch (Exception e) {
846                 throw processException(e);
847             }
848             finally {
849                 closeSession(session);
850             }
851         }
852         else {
853             return ((Long)result).intValue();
854         }
855     }
856 
857     public int countByVirtualHost(String virtualHost) throws SystemException {
858         boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
859         String finderClassName = Company.class.getName();
860         String finderMethodName = "countByVirtualHost";
861         String[] finderParams = new String[] { String.class.getName() };
862         Object[] finderArgs = new Object[] { virtualHost };
863 
864         Object result = null;
865 
866         if (finderClassNameCacheEnabled) {
867             result = FinderCacheUtil.getResult(finderClassName,
868                     finderMethodName, finderParams, finderArgs, this);
869         }
870 
871         if (result == null) {
872             Session session = null;
873 
874             try {
875                 session = openSession();
876 
877                 StringBuilder query = new StringBuilder();
878 
879                 query.append("SELECT COUNT(*) ");
880                 query.append("FROM com.liferay.portal.model.Company WHERE ");
881 
882                 if (virtualHost == null) {
883                     query.append("virtualHost IS NULL");
884                 }
885                 else {
886                     query.append("virtualHost = ?");
887                 }
888 
889                 query.append(" ");
890 
891                 Query q = session.createQuery(query.toString());
892 
893                 QueryPos qPos = QueryPos.getInstance(q);
894 
895                 if (virtualHost != null) {
896                     qPos.add(virtualHost);
897                 }
898 
899                 Long count = null;
900 
901                 Iterator<Long> itr = q.list().iterator();
902 
903                 if (itr.hasNext()) {
904                     count = itr.next();
905                 }
906 
907                 if (count == null) {
908                     count = new Long(0);
909                 }
910 
911                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
912                     finderClassName, finderMethodName, finderParams,
913                     finderArgs, count);
914 
915                 return count.intValue();
916             }
917             catch (Exception e) {
918                 throw processException(e);
919             }
920             finally {
921                 closeSession(session);
922             }
923         }
924         else {
925             return ((Long)result).intValue();
926         }
927     }
928 
929     public int countByMx(String mx) throws SystemException {
930         boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
931         String finderClassName = Company.class.getName();
932         String finderMethodName = "countByMx";
933         String[] finderParams = new String[] { String.class.getName() };
934         Object[] finderArgs = new Object[] { mx };
935 
936         Object result = null;
937 
938         if (finderClassNameCacheEnabled) {
939             result = FinderCacheUtil.getResult(finderClassName,
940                     finderMethodName, finderParams, finderArgs, this);
941         }
942 
943         if (result == null) {
944             Session session = null;
945 
946             try {
947                 session = openSession();
948 
949                 StringBuilder query = new StringBuilder();
950 
951                 query.append("SELECT COUNT(*) ");
952                 query.append("FROM com.liferay.portal.model.Company WHERE ");
953 
954                 if (mx == null) {
955                     query.append("mx IS NULL");
956                 }
957                 else {
958                     query.append("mx = ?");
959                 }
960 
961                 query.append(" ");
962 
963                 Query q = session.createQuery(query.toString());
964 
965                 QueryPos qPos = QueryPos.getInstance(q);
966 
967                 if (mx != null) {
968                     qPos.add(mx);
969                 }
970 
971                 Long count = null;
972 
973                 Iterator<Long> itr = q.list().iterator();
974 
975                 if (itr.hasNext()) {
976                     count = itr.next();
977                 }
978 
979                 if (count == null) {
980                     count = new Long(0);
981                 }
982 
983                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
984                     finderClassName, finderMethodName, finderParams,
985                     finderArgs, count);
986 
987                 return count.intValue();
988             }
989             catch (Exception e) {
990                 throw processException(e);
991             }
992             finally {
993                 closeSession(session);
994             }
995         }
996         else {
997             return ((Long)result).intValue();
998         }
999     }
1000
1001    public int countByLogoId(long logoId) throws SystemException {
1002        boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
1003        String finderClassName = Company.class.getName();
1004        String finderMethodName = "countByLogoId";
1005        String[] finderParams = new String[] { Long.class.getName() };
1006        Object[] finderArgs = new Object[] { new Long(logoId) };
1007
1008        Object result = null;
1009
1010        if (finderClassNameCacheEnabled) {
1011            result = FinderCacheUtil.getResult(finderClassName,
1012                    finderMethodName, finderParams, finderArgs, this);
1013        }
1014
1015        if (result == null) {
1016            Session session = null;
1017
1018            try {
1019                session = openSession();
1020
1021                StringBuilder query = new StringBuilder();
1022
1023                query.append("SELECT COUNT(*) ");
1024                query.append("FROM com.liferay.portal.model.Company WHERE ");
1025
1026                query.append("logoId = ?");
1027
1028                query.append(" ");
1029
1030                Query q = session.createQuery(query.toString());
1031
1032                QueryPos qPos = QueryPos.getInstance(q);
1033
1034                qPos.add(logoId);
1035
1036                Long count = null;
1037
1038                Iterator<Long> itr = q.list().iterator();
1039
1040                if (itr.hasNext()) {
1041                    count = itr.next();
1042                }
1043
1044                if (count == null) {
1045                    count = new Long(0);
1046                }
1047
1048                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1049                    finderClassName, finderMethodName, finderParams,
1050                    finderArgs, count);
1051
1052                return count.intValue();
1053            }
1054            catch (Exception e) {
1055                throw processException(e);
1056            }
1057            finally {
1058                closeSession(session);
1059            }
1060        }
1061        else {
1062            return ((Long)result).intValue();
1063        }
1064    }
1065
1066    public int countAll() throws SystemException {
1067        boolean finderClassNameCacheEnabled = CompanyModelImpl.CACHE_ENABLED;
1068        String finderClassName = Company.class.getName();
1069        String finderMethodName = "countAll";
1070        String[] finderParams = new String[] {  };
1071        Object[] finderArgs = new Object[] {  };
1072
1073        Object result = null;
1074
1075        if (finderClassNameCacheEnabled) {
1076            result = FinderCacheUtil.getResult(finderClassName,
1077                    finderMethodName, finderParams, finderArgs, this);
1078        }
1079
1080        if (result == null) {
1081            Session session = null;
1082
1083            try {
1084                session = openSession();
1085
1086                Query q = session.createQuery(
1087                        "SELECT COUNT(*) FROM com.liferay.portal.model.Company");
1088
1089                Long count = null;
1090
1091                Iterator<Long> itr = q.list().iterator();
1092
1093                if (itr.hasNext()) {
1094                    count = itr.next();
1095                }
1096
1097                if (count == null) {
1098                    count = new Long(0);
1099                }
1100
1101                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1102                    finderClassName, finderMethodName, finderParams,
1103                    finderArgs, count);
1104
1105                return count.intValue();
1106            }
1107            catch (Exception e) {
1108                throw processException(e);
1109            }
1110            finally {
1111                closeSession(session);
1112            }
1113        }
1114        else {
1115            return ((Long)result).intValue();
1116        }
1117    }
1118
1119    public void registerListener(ModelListener listener) {
1120        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1121
1122        listeners.add(listener);
1123
1124        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1125    }
1126
1127    public void unregisterListener(ModelListener listener) {
1128        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1129
1130        listeners.remove(listener);
1131
1132        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1133    }
1134
1135    public void afterPropertiesSet() {
1136        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1137                    com.liferay.portal.util.PropsUtil.get(
1138                        "value.object.listener.com.liferay.portal.model.Company")));
1139
1140        if (listenerClassNames.length > 0) {
1141            try {
1142                List<ModelListener> listeners = new ArrayList<ModelListener>();
1143
1144                for (String listenerClassName : listenerClassNames) {
1145                    listeners.add((ModelListener)Class.forName(
1146                            listenerClassName).newInstance());
1147                }
1148
1149                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1150            }
1151            catch (Exception e) {
1152                _log.error(e);
1153            }
1154        }
1155    }
1156
1157    private static Log _log = LogFactory.getLog(CompanyPersistenceImpl.class);
1158    private ModelListener[] _listeners = new ModelListener[0];
1159}