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.portlet.tags.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.bean.InitializingBean;
27  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
28  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
29  import com.liferay.portal.kernel.dao.jdbc.RowMapper;
30  import com.liferay.portal.kernel.dao.jdbc.SqlUpdate;
31  import com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil;
32  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
33  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
34  import com.liferay.portal.kernel.dao.orm.Query;
35  import com.liferay.portal.kernel.dao.orm.QueryPos;
36  import com.liferay.portal.kernel.dao.orm.QueryUtil;
37  import com.liferay.portal.kernel.dao.orm.SQLQuery;
38  import com.liferay.portal.kernel.dao.orm.Session;
39  import com.liferay.portal.kernel.dao.orm.Type;
40  import com.liferay.portal.kernel.util.GetterUtil;
41  import com.liferay.portal.kernel.util.ListUtil;
42  import com.liferay.portal.kernel.util.OrderByComparator;
43  import com.liferay.portal.kernel.util.StringPool;
44  import com.liferay.portal.kernel.util.StringUtil;
45  import com.liferay.portal.model.ModelListener;
46  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
47  
48  import com.liferay.portlet.tags.NoSuchAssetException;
49  import com.liferay.portlet.tags.model.TagsAsset;
50  import com.liferay.portlet.tags.model.impl.TagsAssetImpl;
51  import com.liferay.portlet.tags.model.impl.TagsAssetModelImpl;
52  
53  import org.apache.commons.logging.Log;
54  import org.apache.commons.logging.LogFactory;
55  
56  import java.sql.Types;
57  
58  import java.util.ArrayList;
59  import java.util.Collections;
60  import java.util.Iterator;
61  import java.util.List;
62  
63  /**
64   * <a href="TagsAssetPersistenceImpl.java.html"><b><i>View Source</i></b></a>
65   *
66   * @author Brian Wing Shun Chan
67   *
68   */
69  public class TagsAssetPersistenceImpl extends BasePersistenceImpl
70      implements TagsAssetPersistence, InitializingBean {
71      public TagsAsset create(long assetId) {
72          TagsAsset tagsAsset = new TagsAssetImpl();
73  
74          tagsAsset.setNew(true);
75          tagsAsset.setPrimaryKey(assetId);
76  
77          return tagsAsset;
78      }
79  
80      public TagsAsset remove(long assetId)
81          throws NoSuchAssetException, SystemException {
82          Session session = null;
83  
84          try {
85              session = openSession();
86  
87              TagsAsset tagsAsset = (TagsAsset)session.get(TagsAssetImpl.class,
88                      new Long(assetId));
89  
90              if (tagsAsset == null) {
91                  if (_log.isWarnEnabled()) {
92                      _log.warn("No TagsAsset exists with the primary key " +
93                          assetId);
94                  }
95  
96                  throw new NoSuchAssetException(
97                      "No TagsAsset exists with the primary key " + assetId);
98              }
99  
100             return remove(tagsAsset);
101         }
102         catch (NoSuchAssetException nsee) {
103             throw nsee;
104         }
105         catch (Exception e) {
106             throw processException(e);
107         }
108         finally {
109             closeSession(session);
110         }
111     }
112 
113     public TagsAsset remove(TagsAsset tagsAsset) throws SystemException {
114         if (_listeners.length > 0) {
115             for (ModelListener listener : _listeners) {
116                 listener.onBeforeRemove(tagsAsset);
117             }
118         }
119 
120         tagsAsset = removeImpl(tagsAsset);
121 
122         if (_listeners.length > 0) {
123             for (ModelListener listener : _listeners) {
124                 listener.onAfterRemove(tagsAsset);
125             }
126         }
127 
128         return tagsAsset;
129     }
130 
131     protected TagsAsset removeImpl(TagsAsset tagsAsset)
132         throws SystemException {
133         try {
134             clearTagsEntries.clear(tagsAsset.getPrimaryKey());
135         }
136         catch (Exception e) {
137             throw processException(e);
138         }
139         finally {
140             FinderCacheUtil.clearCache("TagsAssets_TagsEntries");
141         }
142 
143         Session session = null;
144 
145         try {
146             session = openSession();
147 
148             session.delete(tagsAsset);
149 
150             session.flush();
151 
152             return tagsAsset;
153         }
154         catch (Exception e) {
155             throw processException(e);
156         }
157         finally {
158             closeSession(session);
159 
160             FinderCacheUtil.clearCache(TagsAsset.class.getName());
161         }
162     }
163 
164     /**
165      * @deprecated Use <code>update(TagsAsset tagsAsset, boolean merge)</code>.
166      */
167     public TagsAsset update(TagsAsset tagsAsset) throws SystemException {
168         if (_log.isWarnEnabled()) {
169             _log.warn(
170                 "Using the deprecated update(TagsAsset tagsAsset) method. Use update(TagsAsset tagsAsset, boolean merge) instead.");
171         }
172 
173         return update(tagsAsset, false);
174     }
175 
176     /**
177      * Add, update, or merge, the entity. This method also calls the model
178      * listeners to trigger the proper events associated with adding, deleting,
179      * or updating an entity.
180      *
181      * @param        tagsAsset the entity to add, update, or merge
182      * @param        merge boolean value for whether to merge the entity. The
183      *                default value is false. Setting merge to true is more
184      *                expensive and should only be true when tagsAsset is
185      *                transient. See LEP-5473 for a detailed discussion of this
186      *                method.
187      * @return        true if the portlet can be displayed via Ajax
188      */
189     public TagsAsset update(TagsAsset tagsAsset, boolean merge)
190         throws SystemException {
191         boolean isNew = tagsAsset.isNew();
192 
193         if (_listeners.length > 0) {
194             for (ModelListener listener : _listeners) {
195                 if (isNew) {
196                     listener.onBeforeCreate(tagsAsset);
197                 }
198                 else {
199                     listener.onBeforeUpdate(tagsAsset);
200                 }
201             }
202         }
203 
204         tagsAsset = updateImpl(tagsAsset, merge);
205 
206         if (_listeners.length > 0) {
207             for (ModelListener listener : _listeners) {
208                 if (isNew) {
209                     listener.onAfterCreate(tagsAsset);
210                 }
211                 else {
212                     listener.onAfterUpdate(tagsAsset);
213                 }
214             }
215         }
216 
217         return tagsAsset;
218     }
219 
220     public TagsAsset updateImpl(
221         com.liferay.portlet.tags.model.TagsAsset tagsAsset, boolean merge)
222         throws SystemException {
223         FinderCacheUtil.clearCache("TagsAssets_TagsEntries");
224 
225         Session session = null;
226 
227         try {
228             session = openSession();
229 
230             if (merge) {
231                 session.merge(tagsAsset);
232             }
233             else {
234                 if (tagsAsset.isNew()) {
235                     session.save(tagsAsset);
236                 }
237             }
238 
239             session.flush();
240 
241             tagsAsset.setNew(false);
242 
243             return tagsAsset;
244         }
245         catch (Exception e) {
246             throw processException(e);
247         }
248         finally {
249             closeSession(session);
250 
251             FinderCacheUtil.clearCache(TagsAsset.class.getName());
252         }
253     }
254 
255     public TagsAsset findByPrimaryKey(long assetId)
256         throws NoSuchAssetException, SystemException {
257         TagsAsset tagsAsset = fetchByPrimaryKey(assetId);
258 
259         if (tagsAsset == null) {
260             if (_log.isWarnEnabled()) {
261                 _log.warn("No TagsAsset exists with the primary key " +
262                     assetId);
263             }
264 
265             throw new NoSuchAssetException(
266                 "No TagsAsset exists with the primary key " + assetId);
267         }
268 
269         return tagsAsset;
270     }
271 
272     public TagsAsset fetchByPrimaryKey(long assetId) throws SystemException {
273         Session session = null;
274 
275         try {
276             session = openSession();
277 
278             return (TagsAsset)session.get(TagsAssetImpl.class, new Long(assetId));
279         }
280         catch (Exception e) {
281             throw processException(e);
282         }
283         finally {
284             closeSession(session);
285         }
286     }
287 
288     public List<TagsAsset> findByCompanyId(long companyId)
289         throws SystemException {
290         boolean finderClassNameCacheEnabled = TagsAssetModelImpl.CACHE_ENABLED;
291         String finderClassName = TagsAsset.class.getName();
292         String finderMethodName = "findByCompanyId";
293         String[] finderParams = new String[] { Long.class.getName() };
294         Object[] finderArgs = new Object[] { new Long(companyId) };
295 
296         Object result = null;
297 
298         if (finderClassNameCacheEnabled) {
299             result = FinderCacheUtil.getResult(finderClassName,
300                     finderMethodName, finderParams, finderArgs, this);
301         }
302 
303         if (result == null) {
304             Session session = null;
305 
306             try {
307                 session = openSession();
308 
309                 StringBuilder query = new StringBuilder();
310 
311                 query.append(
312                     "FROM com.liferay.portlet.tags.model.TagsAsset WHERE ");
313 
314                 query.append("companyId = ?");
315 
316                 query.append(" ");
317 
318                 Query q = session.createQuery(query.toString());
319 
320                 QueryPos qPos = QueryPos.getInstance(q);
321 
322                 qPos.add(companyId);
323 
324                 List<TagsAsset> list = q.list();
325 
326                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
327                     finderClassName, finderMethodName, finderParams,
328                     finderArgs, list);
329 
330                 return list;
331             }
332             catch (Exception e) {
333                 throw processException(e);
334             }
335             finally {
336                 closeSession(session);
337             }
338         }
339         else {
340             return (List<TagsAsset>)result;
341         }
342     }
343 
344     public List<TagsAsset> findByCompanyId(long companyId, int start, int end)
345         throws SystemException {
346         return findByCompanyId(companyId, start, end, null);
347     }
348 
349     public List<TagsAsset> findByCompanyId(long companyId, int start, int end,
350         OrderByComparator obc) throws SystemException {
351         boolean finderClassNameCacheEnabled = TagsAssetModelImpl.CACHE_ENABLED;
352         String finderClassName = TagsAsset.class.getName();
353         String finderMethodName = "findByCompanyId";
354         String[] finderParams = new String[] {
355                 Long.class.getName(),
356                 
357                 "java.lang.Integer", "java.lang.Integer",
358                 "com.liferay.portal.kernel.util.OrderByComparator"
359             };
360         Object[] finderArgs = new Object[] {
361                 new Long(companyId),
362                 
363                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
364             };
365 
366         Object result = null;
367 
368         if (finderClassNameCacheEnabled) {
369             result = FinderCacheUtil.getResult(finderClassName,
370                     finderMethodName, finderParams, finderArgs, this);
371         }
372 
373         if (result == null) {
374             Session session = null;
375 
376             try {
377                 session = openSession();
378 
379                 StringBuilder query = new StringBuilder();
380 
381                 query.append(
382                     "FROM com.liferay.portlet.tags.model.TagsAsset WHERE ");
383 
384                 query.append("companyId = ?");
385 
386                 query.append(" ");
387 
388                 if (obc != null) {
389                     query.append("ORDER BY ");
390                     query.append(obc.getOrderBy());
391                 }
392 
393                 Query q = session.createQuery(query.toString());
394 
395                 QueryPos qPos = QueryPos.getInstance(q);
396 
397                 qPos.add(companyId);
398 
399                 List<TagsAsset> list = (List<TagsAsset>)QueryUtil.list(q,
400                         getDialect(), start, end);
401 
402                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
403                     finderClassName, finderMethodName, finderParams,
404                     finderArgs, list);
405 
406                 return list;
407             }
408             catch (Exception e) {
409                 throw processException(e);
410             }
411             finally {
412                 closeSession(session);
413             }
414         }
415         else {
416             return (List<TagsAsset>)result;
417         }
418     }
419 
420     public TagsAsset findByCompanyId_First(long companyId, OrderByComparator obc)
421         throws NoSuchAssetException, SystemException {
422         List<TagsAsset> list = findByCompanyId(companyId, 0, 1, obc);
423 
424         if (list.size() == 0) {
425             StringBuilder msg = new StringBuilder();
426 
427             msg.append("No TagsAsset exists with the key {");
428 
429             msg.append("companyId=" + companyId);
430 
431             msg.append(StringPool.CLOSE_CURLY_BRACE);
432 
433             throw new NoSuchAssetException(msg.toString());
434         }
435         else {
436             return list.get(0);
437         }
438     }
439 
440     public TagsAsset findByCompanyId_Last(long companyId, OrderByComparator obc)
441         throws NoSuchAssetException, SystemException {
442         int count = countByCompanyId(companyId);
443 
444         List<TagsAsset> list = findByCompanyId(companyId, count - 1, count, obc);
445 
446         if (list.size() == 0) {
447             StringBuilder msg = new StringBuilder();
448 
449             msg.append("No TagsAsset exists with the key {");
450 
451             msg.append("companyId=" + companyId);
452 
453             msg.append(StringPool.CLOSE_CURLY_BRACE);
454 
455             throw new NoSuchAssetException(msg.toString());
456         }
457         else {
458             return list.get(0);
459         }
460     }
461 
462     public TagsAsset[] findByCompanyId_PrevAndNext(long assetId,
463         long companyId, OrderByComparator obc)
464         throws NoSuchAssetException, SystemException {
465         TagsAsset tagsAsset = findByPrimaryKey(assetId);
466 
467         int count = countByCompanyId(companyId);
468 
469         Session session = null;
470 
471         try {
472             session = openSession();
473 
474             StringBuilder query = new StringBuilder();
475 
476             query.append("FROM com.liferay.portlet.tags.model.TagsAsset WHERE ");
477 
478             query.append("companyId = ?");
479 
480             query.append(" ");
481 
482             if (obc != null) {
483                 query.append("ORDER BY ");
484                 query.append(obc.getOrderBy());
485             }
486 
487             Query q = session.createQuery(query.toString());
488 
489             QueryPos qPos = QueryPos.getInstance(q);
490 
491             qPos.add(companyId);
492 
493             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
494                     tagsAsset);
495 
496             TagsAsset[] array = new TagsAssetImpl[3];
497 
498             array[0] = (TagsAsset)objArray[0];
499             array[1] = (TagsAsset)objArray[1];
500             array[2] = (TagsAsset)objArray[2];
501 
502             return array;
503         }
504         catch (Exception e) {
505             throw processException(e);
506         }
507         finally {
508             closeSession(session);
509         }
510     }
511 
512     public TagsAsset findByC_C(long classNameId, long classPK)
513         throws NoSuchAssetException, SystemException {
514         TagsAsset tagsAsset = fetchByC_C(classNameId, classPK);
515 
516         if (tagsAsset == null) {
517             StringBuilder msg = new StringBuilder();
518 
519             msg.append("No TagsAsset exists with the key {");
520 
521             msg.append("classNameId=" + classNameId);
522 
523             msg.append(", ");
524             msg.append("classPK=" + classPK);
525 
526             msg.append(StringPool.CLOSE_CURLY_BRACE);
527 
528             if (_log.isWarnEnabled()) {
529                 _log.warn(msg.toString());
530             }
531 
532             throw new NoSuchAssetException(msg.toString());
533         }
534 
535         return tagsAsset;
536     }
537 
538     public TagsAsset fetchByC_C(long classNameId, long classPK)
539         throws SystemException {
540         boolean finderClassNameCacheEnabled = TagsAssetModelImpl.CACHE_ENABLED;
541         String finderClassName = TagsAsset.class.getName();
542         String finderMethodName = "fetchByC_C";
543         String[] finderParams = new String[] {
544                 Long.class.getName(), Long.class.getName()
545             };
546         Object[] finderArgs = new Object[] {
547                 new Long(classNameId), new Long(classPK)
548             };
549 
550         Object result = null;
551 
552         if (finderClassNameCacheEnabled) {
553             result = FinderCacheUtil.getResult(finderClassName,
554                     finderMethodName, finderParams, finderArgs, this);
555         }
556 
557         if (result == null) {
558             Session session = null;
559 
560             try {
561                 session = openSession();
562 
563                 StringBuilder query = new StringBuilder();
564 
565                 query.append(
566                     "FROM com.liferay.portlet.tags.model.TagsAsset WHERE ");
567 
568                 query.append("classNameId = ?");
569 
570                 query.append(" AND ");
571 
572                 query.append("classPK = ?");
573 
574                 query.append(" ");
575 
576                 Query q = session.createQuery(query.toString());
577 
578                 QueryPos qPos = QueryPos.getInstance(q);
579 
580                 qPos.add(classNameId);
581 
582                 qPos.add(classPK);
583 
584                 List<TagsAsset> list = q.list();
585 
586                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
587                     finderClassName, finderMethodName, finderParams,
588                     finderArgs, list);
589 
590                 if (list.size() == 0) {
591                     return null;
592                 }
593                 else {
594                     return list.get(0);
595                 }
596             }
597             catch (Exception e) {
598                 throw processException(e);
599             }
600             finally {
601                 closeSession(session);
602             }
603         }
604         else {
605             List<TagsAsset> list = (List<TagsAsset>)result;
606 
607             if (list.size() == 0) {
608                 return null;
609             }
610             else {
611                 return list.get(0);
612             }
613         }
614     }
615 
616     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
617         throws SystemException {
618         Session session = null;
619 
620         try {
621             session = openSession();
622 
623             dynamicQuery.compile(session);
624 
625             return dynamicQuery.list();
626         }
627         catch (Exception e) {
628             throw processException(e);
629         }
630         finally {
631             closeSession(session);
632         }
633     }
634 
635     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
636         int start, int end) throws SystemException {
637         Session session = null;
638 
639         try {
640             session = openSession();
641 
642             dynamicQuery.setLimit(start, end);
643 
644             dynamicQuery.compile(session);
645 
646             return dynamicQuery.list();
647         }
648         catch (Exception e) {
649             throw processException(e);
650         }
651         finally {
652             closeSession(session);
653         }
654     }
655 
656     public List<TagsAsset> findAll() throws SystemException {
657         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
658     }
659 
660     public List<TagsAsset> findAll(int start, int end)
661         throws SystemException {
662         return findAll(start, end, null);
663     }
664 
665     public List<TagsAsset> findAll(int start, int end, OrderByComparator obc)
666         throws SystemException {
667         boolean finderClassNameCacheEnabled = TagsAssetModelImpl.CACHE_ENABLED;
668         String finderClassName = TagsAsset.class.getName();
669         String finderMethodName = "findAll";
670         String[] finderParams = new String[] {
671                 "java.lang.Integer", "java.lang.Integer",
672                 "com.liferay.portal.kernel.util.OrderByComparator"
673             };
674         Object[] finderArgs = new Object[] {
675                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
676             };
677 
678         Object result = null;
679 
680         if (finderClassNameCacheEnabled) {
681             result = FinderCacheUtil.getResult(finderClassName,
682                     finderMethodName, finderParams, finderArgs, this);
683         }
684 
685         if (result == null) {
686             Session session = null;
687 
688             try {
689                 session = openSession();
690 
691                 StringBuilder query = new StringBuilder();
692 
693                 query.append("FROM com.liferay.portlet.tags.model.TagsAsset ");
694 
695                 if (obc != null) {
696                     query.append("ORDER BY ");
697                     query.append(obc.getOrderBy());
698                 }
699 
700                 Query q = session.createQuery(query.toString());
701 
702                 List<TagsAsset> list = (List<TagsAsset>)QueryUtil.list(q,
703                         getDialect(), start, end);
704 
705                 if (obc == null) {
706                     Collections.sort(list);
707                 }
708 
709                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
710                     finderClassName, finderMethodName, finderParams,
711                     finderArgs, list);
712 
713                 return list;
714             }
715             catch (Exception e) {
716                 throw processException(e);
717             }
718             finally {
719                 closeSession(session);
720             }
721         }
722         else {
723             return (List<TagsAsset>)result;
724         }
725     }
726 
727     public void removeByCompanyId(long companyId) throws SystemException {
728         for (TagsAsset tagsAsset : findByCompanyId(companyId)) {
729             remove(tagsAsset);
730         }
731     }
732 
733     public void removeByC_C(long classNameId, long classPK)
734         throws NoSuchAssetException, SystemException {
735         TagsAsset tagsAsset = findByC_C(classNameId, classPK);
736 
737         remove(tagsAsset);
738     }
739 
740     public void removeAll() throws SystemException {
741         for (TagsAsset tagsAsset : findAll()) {
742             remove(tagsAsset);
743         }
744     }
745 
746     public int countByCompanyId(long companyId) throws SystemException {
747         boolean finderClassNameCacheEnabled = TagsAssetModelImpl.CACHE_ENABLED;
748         String finderClassName = TagsAsset.class.getName();
749         String finderMethodName = "countByCompanyId";
750         String[] finderParams = new String[] { Long.class.getName() };
751         Object[] finderArgs = new Object[] { new Long(companyId) };
752 
753         Object result = null;
754 
755         if (finderClassNameCacheEnabled) {
756             result = FinderCacheUtil.getResult(finderClassName,
757                     finderMethodName, finderParams, finderArgs, this);
758         }
759 
760         if (result == null) {
761             Session session = null;
762 
763             try {
764                 session = openSession();
765 
766                 StringBuilder query = new StringBuilder();
767 
768                 query.append("SELECT COUNT(*) ");
769                 query.append(
770                     "FROM com.liferay.portlet.tags.model.TagsAsset WHERE ");
771 
772                 query.append("companyId = ?");
773 
774                 query.append(" ");
775 
776                 Query q = session.createQuery(query.toString());
777 
778                 QueryPos qPos = QueryPos.getInstance(q);
779 
780                 qPos.add(companyId);
781 
782                 Long count = null;
783 
784                 Iterator<Long> itr = q.list().iterator();
785 
786                 if (itr.hasNext()) {
787                     count = itr.next();
788                 }
789 
790                 if (count == null) {
791                     count = new Long(0);
792                 }
793 
794                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
795                     finderClassName, finderMethodName, finderParams,
796                     finderArgs, count);
797 
798                 return count.intValue();
799             }
800             catch (Exception e) {
801                 throw processException(e);
802             }
803             finally {
804                 closeSession(session);
805             }
806         }
807         else {
808             return ((Long)result).intValue();
809         }
810     }
811 
812     public int countByC_C(long classNameId, long classPK)
813         throws SystemException {
814         boolean finderClassNameCacheEnabled = TagsAssetModelImpl.CACHE_ENABLED;
815         String finderClassName = TagsAsset.class.getName();
816         String finderMethodName = "countByC_C";
817         String[] finderParams = new String[] {
818                 Long.class.getName(), Long.class.getName()
819             };
820         Object[] finderArgs = new Object[] {
821                 new Long(classNameId), new Long(classPK)
822             };
823 
824         Object result = null;
825 
826         if (finderClassNameCacheEnabled) {
827             result = FinderCacheUtil.getResult(finderClassName,
828                     finderMethodName, finderParams, finderArgs, this);
829         }
830 
831         if (result == null) {
832             Session session = null;
833 
834             try {
835                 session = openSession();
836 
837                 StringBuilder query = new StringBuilder();
838 
839                 query.append("SELECT COUNT(*) ");
840                 query.append(
841                     "FROM com.liferay.portlet.tags.model.TagsAsset WHERE ");
842 
843                 query.append("classNameId = ?");
844 
845                 query.append(" AND ");
846 
847                 query.append("classPK = ?");
848 
849                 query.append(" ");
850 
851                 Query q = session.createQuery(query.toString());
852 
853                 QueryPos qPos = QueryPos.getInstance(q);
854 
855                 qPos.add(classNameId);
856 
857                 qPos.add(classPK);
858 
859                 Long count = null;
860 
861                 Iterator<Long> itr = q.list().iterator();
862 
863                 if (itr.hasNext()) {
864                     count = itr.next();
865                 }
866 
867                 if (count == null) {
868                     count = new Long(0);
869                 }
870 
871                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
872                     finderClassName, finderMethodName, finderParams,
873                     finderArgs, count);
874 
875                 return count.intValue();
876             }
877             catch (Exception e) {
878                 throw processException(e);
879             }
880             finally {
881                 closeSession(session);
882             }
883         }
884         else {
885             return ((Long)result).intValue();
886         }
887     }
888 
889     public int countAll() throws SystemException {
890         boolean finderClassNameCacheEnabled = TagsAssetModelImpl.CACHE_ENABLED;
891         String finderClassName = TagsAsset.class.getName();
892         String finderMethodName = "countAll";
893         String[] finderParams = new String[] {  };
894         Object[] finderArgs = new Object[] {  };
895 
896         Object result = null;
897 
898         if (finderClassNameCacheEnabled) {
899             result = FinderCacheUtil.getResult(finderClassName,
900                     finderMethodName, finderParams, finderArgs, this);
901         }
902 
903         if (result == null) {
904             Session session = null;
905 
906             try {
907                 session = openSession();
908 
909                 Query q = session.createQuery(
910                         "SELECT COUNT(*) FROM com.liferay.portlet.tags.model.TagsAsset");
911 
912                 Long count = null;
913 
914                 Iterator<Long> itr = q.list().iterator();
915 
916                 if (itr.hasNext()) {
917                     count = itr.next();
918                 }
919 
920                 if (count == null) {
921                     count = new Long(0);
922                 }
923 
924                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
925                     finderClassName, finderMethodName, finderParams,
926                     finderArgs, count);
927 
928                 return count.intValue();
929             }
930             catch (Exception e) {
931                 throw processException(e);
932             }
933             finally {
934                 closeSession(session);
935             }
936         }
937         else {
938             return ((Long)result).intValue();
939         }
940     }
941 
942     public List<com.liferay.portlet.tags.model.TagsEntry> getTagsEntries(
943         long pk) throws SystemException {
944         return getTagsEntries(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
945     }
946 
947     public List<com.liferay.portlet.tags.model.TagsEntry> getTagsEntries(
948         long pk, int start, int end) throws SystemException {
949         return getTagsEntries(pk, start, end, null);
950     }
951 
952     public List<com.liferay.portlet.tags.model.TagsEntry> getTagsEntries(
953         long pk, int start, int end, OrderByComparator obc)
954         throws SystemException {
955         boolean finderClassNameCacheEnabled = TagsAssetModelImpl.CACHE_ENABLED_TAGSASSETS_TAGSENTRIES;
956 
957         String finderClassName = "TagsAssets_TagsEntries";
958 
959         String finderMethodName = "getTagsEntries";
960         String[] finderParams = new String[] {
961                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
962                 "com.liferay.portal.kernel.util.OrderByComparator"
963             };
964         Object[] finderArgs = new Object[] {
965                 new Long(pk), String.valueOf(start), String.valueOf(end),
966                 String.valueOf(obc)
967             };
968 
969         Object result = null;
970 
971         if (finderClassNameCacheEnabled) {
972             result = FinderCacheUtil.getResult(finderClassName,
973                     finderMethodName, finderParams, finderArgs, this);
974         }
975 
976         if (result == null) {
977             Session session = null;
978 
979             try {
980                 session = openSession();
981 
982                 StringBuilder sb = new StringBuilder();
983 
984                 sb.append(_SQL_GETTAGSENTRIES);
985 
986                 if (obc != null) {
987                     sb.append("ORDER BY ");
988                     sb.append(obc.getOrderBy());
989                 }
990 
991                 else {
992                     sb.append("ORDER BY ");
993 
994                     sb.append("TagsEntry.name ASC");
995                 }
996 
997                 String sql = sb.toString();
998 
999                 SQLQuery q = session.createSQLQuery(sql);
1000
1001                q.addEntity("TagsEntry",
1002                    com.liferay.portlet.tags.model.impl.TagsEntryImpl.class);
1003
1004                QueryPos qPos = QueryPos.getInstance(q);
1005
1006                qPos.add(pk);
1007
1008                List<com.liferay.portlet.tags.model.TagsEntry> list = (List<com.liferay.portlet.tags.model.TagsEntry>)QueryUtil.list(q,
1009                        getDialect(), start, end);
1010
1011                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1012                    finderClassName, finderMethodName, finderParams,
1013                    finderArgs, list);
1014
1015                return list;
1016            }
1017            catch (Exception e) {
1018                throw processException(e);
1019            }
1020            finally {
1021                closeSession(session);
1022            }
1023        }
1024        else {
1025            return (List<com.liferay.portlet.tags.model.TagsEntry>)result;
1026        }
1027    }
1028
1029    public int getTagsEntriesSize(long pk) throws SystemException {
1030        boolean finderClassNameCacheEnabled = TagsAssetModelImpl.CACHE_ENABLED_TAGSASSETS_TAGSENTRIES;
1031
1032        String finderClassName = "TagsAssets_TagsEntries";
1033
1034        String finderMethodName = "getTagsEntriesSize";
1035        String[] finderParams = new String[] { Long.class.getName() };
1036        Object[] finderArgs = new Object[] { new Long(pk) };
1037
1038        Object result = null;
1039
1040        if (finderClassNameCacheEnabled) {
1041            result = FinderCacheUtil.getResult(finderClassName,
1042                    finderMethodName, finderParams, finderArgs, this);
1043        }
1044
1045        if (result == null) {
1046            Session session = null;
1047
1048            try {
1049                session = openSession();
1050
1051                SQLQuery q = session.createSQLQuery(_SQL_GETTAGSENTRIESSIZE);
1052
1053                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1054
1055                QueryPos qPos = QueryPos.getInstance(q);
1056
1057                qPos.add(pk);
1058
1059                Long count = null;
1060
1061                Iterator<Long> itr = q.list().iterator();
1062
1063                if (itr.hasNext()) {
1064                    count = itr.next();
1065                }
1066
1067                if (count == null) {
1068                    count = new Long(0);
1069                }
1070
1071                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1072                    finderClassName, finderMethodName, finderParams,
1073                    finderArgs, count);
1074
1075                return count.intValue();
1076            }
1077            catch (Exception e) {
1078                throw processException(e);
1079            }
1080            finally {
1081                closeSession(session);
1082            }
1083        }
1084        else {
1085            return ((Long)result).intValue();
1086        }
1087    }
1088
1089    public boolean containsTagsEntry(long pk, long tagsEntryPK)
1090        throws SystemException {
1091        boolean finderClassNameCacheEnabled = TagsAssetModelImpl.CACHE_ENABLED_TAGSASSETS_TAGSENTRIES;
1092
1093        String finderClassName = "TagsAssets_TagsEntries";
1094
1095        String finderMethodName = "containsTagsEntries";
1096        String[] finderParams = new String[] {
1097                Long.class.getName(),
1098                
1099                Long.class.getName()
1100            };
1101        Object[] finderArgs = new Object[] { new Long(pk), new Long(tagsEntryPK) };
1102
1103        Object result = null;
1104
1105        if (finderClassNameCacheEnabled) {
1106            result = FinderCacheUtil.getResult(finderClassName,
1107                    finderMethodName, finderParams, finderArgs, this);
1108        }
1109
1110        if (result == null) {
1111            try {
1112                Boolean value = Boolean.valueOf(containsTagsEntry.contains(pk,
1113                            tagsEntryPK));
1114
1115                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1116                    finderClassName, finderMethodName, finderParams,
1117                    finderArgs, value);
1118
1119                return value.booleanValue();
1120            }
1121            catch (Exception e) {
1122                throw processException(e);
1123            }
1124        }
1125        else {
1126            return ((Boolean)result).booleanValue();
1127        }
1128    }
1129
1130    public boolean containsTagsEntries(long pk) throws SystemException {
1131        if (getTagsEntriesSize(pk) > 0) {
1132            return true;
1133        }
1134        else {
1135            return false;
1136        }
1137    }
1138
1139    public void addTagsEntry(long pk, long tagsEntryPK)
1140        throws SystemException {
1141        try {
1142            addTagsEntry.add(pk, tagsEntryPK);
1143        }
1144        catch (Exception e) {
1145            throw processException(e);
1146        }
1147        finally {
1148            FinderCacheUtil.clearCache("TagsAssets_TagsEntries");
1149        }
1150    }
1151
1152    public void addTagsEntry(long pk,
1153        com.liferay.portlet.tags.model.TagsEntry tagsEntry)
1154        throws SystemException {
1155        try {
1156            addTagsEntry.add(pk, tagsEntry.getPrimaryKey());
1157        }
1158        catch (Exception e) {
1159            throw processException(e);
1160        }
1161        finally {
1162            FinderCacheUtil.clearCache("TagsAssets_TagsEntries");
1163        }
1164    }
1165
1166    public void addTagsEntries(long pk, long[] tagsEntryPKs)
1167        throws SystemException {
1168        try {
1169            for (long tagsEntryPK : tagsEntryPKs) {
1170                addTagsEntry.add(pk, tagsEntryPK);
1171            }
1172        }
1173        catch (Exception e) {
1174            throw processException(e);
1175        }
1176        finally {
1177            FinderCacheUtil.clearCache("TagsAssets_TagsEntries");
1178        }
1179    }
1180
1181    public void addTagsEntries(long pk,
1182        List<com.liferay.portlet.tags.model.TagsEntry> tagsEntries)
1183        throws SystemException {
1184        try {
1185            for (com.liferay.portlet.tags.model.TagsEntry tagsEntry : tagsEntries) {
1186                addTagsEntry.add(pk, tagsEntry.getPrimaryKey());
1187            }
1188        }
1189        catch (Exception e) {
1190            throw processException(e);
1191        }
1192        finally {
1193            FinderCacheUtil.clearCache("TagsAssets_TagsEntries");
1194        }
1195    }
1196
1197    public void clearTagsEntries(long pk) throws SystemException {
1198        try {
1199            clearTagsEntries.clear(pk);
1200        }
1201        catch (Exception e) {
1202            throw processException(e);
1203        }
1204        finally {
1205            FinderCacheUtil.clearCache("TagsAssets_TagsEntries");
1206        }
1207    }
1208
1209    public void removeTagsEntry(long pk, long tagsEntryPK)
1210        throws SystemException {
1211        try {
1212            removeTagsEntry.remove(pk, tagsEntryPK);
1213        }
1214        catch (Exception e) {
1215            throw processException(e);
1216        }
1217        finally {
1218            FinderCacheUtil.clearCache("TagsAssets_TagsEntries");
1219        }
1220    }
1221
1222    public void removeTagsEntry(long pk,
1223        com.liferay.portlet.tags.model.TagsEntry tagsEntry)
1224        throws SystemException {
1225        try {
1226            removeTagsEntry.remove(pk, tagsEntry.getPrimaryKey());
1227        }
1228        catch (Exception e) {
1229            throw processException(e);
1230        }
1231        finally {
1232            FinderCacheUtil.clearCache("TagsAssets_TagsEntries");
1233        }
1234    }
1235
1236    public void removeTagsEntries(long pk, long[] tagsEntryPKs)
1237        throws SystemException {
1238        try {
1239            for (long tagsEntryPK : tagsEntryPKs) {
1240                removeTagsEntry.remove(pk, tagsEntryPK);
1241            }
1242        }
1243        catch (Exception e) {
1244            throw processException(e);
1245        }
1246        finally {
1247            FinderCacheUtil.clearCache("TagsAssets_TagsEntries");
1248        }
1249    }
1250
1251    public void removeTagsEntries(long pk,
1252        List<com.liferay.portlet.tags.model.TagsEntry> tagsEntries)
1253        throws SystemException {
1254        try {
1255            for (com.liferay.portlet.tags.model.TagsEntry tagsEntry : tagsEntries) {
1256                removeTagsEntry.remove(pk, tagsEntry.getPrimaryKey());
1257            }
1258        }
1259        catch (Exception e) {
1260            throw processException(e);
1261        }
1262        finally {
1263            FinderCacheUtil.clearCache("TagsAssets_TagsEntries");
1264        }
1265    }
1266
1267    public void setTagsEntries(long pk, long[] tagsEntryPKs)
1268        throws SystemException {
1269        try {
1270            clearTagsEntries.clear(pk);
1271
1272            for (long tagsEntryPK : tagsEntryPKs) {
1273                addTagsEntry.add(pk, tagsEntryPK);
1274            }
1275        }
1276        catch (Exception e) {
1277            throw processException(e);
1278        }
1279        finally {
1280            FinderCacheUtil.clearCache("TagsAssets_TagsEntries");
1281        }
1282    }
1283
1284    public void setTagsEntries(long pk,
1285        List<com.liferay.portlet.tags.model.TagsEntry> tagsEntries)
1286        throws SystemException {
1287        try {
1288            clearTagsEntries.clear(pk);
1289
1290            for (com.liferay.portlet.tags.model.TagsEntry tagsEntry : tagsEntries) {
1291                addTagsEntry.add(pk, tagsEntry.getPrimaryKey());
1292            }
1293        }
1294        catch (Exception e) {
1295            throw processException(e);
1296        }
1297        finally {
1298            FinderCacheUtil.clearCache("TagsAssets_TagsEntries");
1299        }
1300    }
1301
1302    public void registerListener(ModelListener listener) {
1303        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1304
1305        listeners.add(listener);
1306
1307        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1308    }
1309
1310    public void unregisterListener(ModelListener listener) {
1311        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1312
1313        listeners.remove(listener);
1314
1315        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1316    }
1317
1318    public void afterPropertiesSet() {
1319        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1320                    com.liferay.portal.util.PropsUtil.get(
1321                        "value.object.listener.com.liferay.portlet.tags.model.TagsAsset")));
1322
1323        if (listenerClassNames.length > 0) {
1324            try {
1325                List<ModelListener> listeners = new ArrayList<ModelListener>();
1326
1327                for (String listenerClassName : listenerClassNames) {
1328                    listeners.add((ModelListener)Class.forName(
1329                            listenerClassName).newInstance());
1330                }
1331
1332                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1333            }
1334            catch (Exception e) {
1335                _log.error(e);
1336            }
1337        }
1338
1339        containsTagsEntry = new ContainsTagsEntry(this);
1340
1341        addTagsEntry = new AddTagsEntry(this);
1342        clearTagsEntries = new ClearTagsEntries(this);
1343        removeTagsEntry = new RemoveTagsEntry(this);
1344    }
1345
1346    protected ContainsTagsEntry containsTagsEntry;
1347    protected AddTagsEntry addTagsEntry;
1348    protected ClearTagsEntries clearTagsEntries;
1349    protected RemoveTagsEntry removeTagsEntry;
1350
1351    protected class ContainsTagsEntry {
1352        protected ContainsTagsEntry(TagsAssetPersistenceImpl persistenceImpl) {
1353            super();
1354
1355            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
1356                    _SQL_CONTAINSTAGSENTRY,
1357                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
1358        }
1359
1360        protected boolean contains(long assetId, long entryId) {
1361            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
1362                        new Long(assetId), new Long(entryId)
1363                    });
1364
1365            if (results.size() > 0) {
1366                Integer count = results.get(0);
1367
1368                if (count.intValue() > 0) {
1369                    return true;
1370                }
1371            }
1372
1373            return false;
1374        }
1375
1376        private MappingSqlQuery _mappingSqlQuery;
1377    }
1378
1379    protected class AddTagsEntry {
1380        protected AddTagsEntry(TagsAssetPersistenceImpl persistenceImpl) {
1381            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1382                    "INSERT INTO TagsAssets_TagsEntries (assetId, entryId) VALUES (?, ?)",
1383                    new int[] { Types.BIGINT, Types.BIGINT });
1384            _persistenceImpl = persistenceImpl;
1385        }
1386
1387        protected void add(long assetId, long entryId) {
1388            if (!_persistenceImpl.containsTagsEntry.contains(assetId, entryId)) {
1389                _sqlUpdate.update(new Object[] {
1390                        new Long(assetId), new Long(entryId)
1391                    });
1392            }
1393        }
1394
1395        private SqlUpdate _sqlUpdate;
1396        private TagsAssetPersistenceImpl _persistenceImpl;
1397    }
1398
1399    protected class ClearTagsEntries {
1400        protected ClearTagsEntries(TagsAssetPersistenceImpl persistenceImpl) {
1401            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1402                    "DELETE FROM TagsAssets_TagsEntries WHERE assetId = ?",
1403                    new int[] { Types.BIGINT });
1404        }
1405
1406        protected void clear(long assetId) {
1407            _sqlUpdate.update(new Object[] { new Long(assetId) });
1408        }
1409
1410        private SqlUpdate _sqlUpdate;
1411    }
1412
1413    protected class RemoveTagsEntry {
1414        protected RemoveTagsEntry(TagsAssetPersistenceImpl persistenceImpl) {
1415            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1416                    "DELETE FROM TagsAssets_TagsEntries WHERE assetId = ? AND entryId = ?",
1417                    new int[] { Types.BIGINT, Types.BIGINT });
1418        }
1419
1420        protected void remove(long assetId, long entryId) {
1421            _sqlUpdate.update(new Object[] { new Long(assetId), new Long(
1422                        entryId) });
1423        }
1424
1425        private SqlUpdate _sqlUpdate;
1426    }
1427
1428    private static final String _SQL_GETTAGSENTRIES = "SELECT {TagsEntry.*} FROM TagsEntry INNER JOIN TagsAssets_TagsEntries ON (TagsAssets_TagsEntries.entryId = TagsEntry.entryId) WHERE (TagsAssets_TagsEntries.assetId = ?)";
1429    private static final String _SQL_GETTAGSENTRIESSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM TagsAssets_TagsEntries WHERE assetId = ?";
1430    private static final String _SQL_CONTAINSTAGSENTRY = "SELECT COUNT(*) AS COUNT_VALUE FROM TagsAssets_TagsEntries WHERE assetId = ? AND entryId = ?";
1431    private static Log _log = LogFactory.getLog(TagsAssetPersistenceImpl.class);
1432    private ModelListener[] _listeners = new ModelListener[0];
1433}