1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.tags.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.service.persistence.BasePersistence;
32  import com.liferay.portal.spring.hibernate.FinderCache;
33  import com.liferay.portal.spring.hibernate.HibernateUtil;
34  
35  import com.liferay.portlet.tags.NoSuchAssetException;
36  import com.liferay.portlet.tags.model.TagsAsset;
37  import com.liferay.portlet.tags.model.impl.TagsAssetImpl;
38  
39  import com.liferay.util.dao.hibernate.QueryPos;
40  import com.liferay.util.dao.hibernate.QueryUtil;
41  
42  import org.apache.commons.logging.Log;
43  import org.apache.commons.logging.LogFactory;
44  
45  import org.hibernate.Hibernate;
46  import org.hibernate.Query;
47  import org.hibernate.SQLQuery;
48  import org.hibernate.Session;
49  
50  import org.springframework.dao.DataAccessException;
51  
52  import org.springframework.jdbc.core.SqlParameter;
53  import org.springframework.jdbc.object.MappingSqlQuery;
54  import org.springframework.jdbc.object.SqlUpdate;
55  
56  import java.sql.ResultSet;
57  import java.sql.SQLException;
58  import java.sql.Types;
59  
60  import java.util.Collections;
61  import java.util.Iterator;
62  import java.util.List;
63  
64  /**
65   * <a href="TagsAssetPersistenceImpl.java.html"><b><i>View Source</i></b></a>
66   *
67   * @author Brian Wing Shun Chan
68   *
69   */
70  public class TagsAssetPersistenceImpl extends BasePersistence
71      implements TagsAssetPersistence {
72      public TagsAsset create(long assetId) {
73          TagsAsset tagsAsset = new TagsAssetImpl();
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 HibernateUtil.processException(e);
107         }
108         finally {
109             closeSession(session);
110         }
111     }
112 
113     public TagsAsset remove(TagsAsset tagsAsset) throws SystemException {
114         try {
115             clearTagsEntries.clear(tagsAsset.getPrimaryKey());
116         }
117         catch (Exception e) {
118             throw HibernateUtil.processException(e);
119         }
120         finally {
121             FinderCache.clearCache("TagsAssets_TagsEntries");
122         }
123 
124         Session session = null;
125 
126         try {
127             session = openSession();
128             session.delete(tagsAsset);
129             session.flush();
130 
131             return tagsAsset;
132         }
133         catch (Exception e) {
134             throw HibernateUtil.processException(e);
135         }
136         finally {
137             closeSession(session);
138             FinderCache.clearCache(TagsAsset.class.getName());
139         }
140     }
141 
142     public TagsAsset update(com.liferay.portlet.tags.model.TagsAsset tagsAsset)
143         throws SystemException {
144         return update(tagsAsset, false);
145     }
146 
147     public TagsAsset update(
148         com.liferay.portlet.tags.model.TagsAsset tagsAsset, boolean merge)
149         throws SystemException {
150         FinderCache.clearCache("TagsAssets_TagsEntries");
151 
152         Session session = null;
153 
154         try {
155             session = openSession();
156 
157             if (merge) {
158                 session.merge(tagsAsset);
159             }
160             else {
161                 if (tagsAsset.isNew()) {
162                     session.save(tagsAsset);
163                 }
164             }
165 
166             session.flush();
167             tagsAsset.setNew(false);
168 
169             return tagsAsset;
170         }
171         catch (Exception e) {
172             throw HibernateUtil.processException(e);
173         }
174         finally {
175             closeSession(session);
176             FinderCache.clearCache(TagsAsset.class.getName());
177         }
178     }
179 
180     public TagsAsset findByPrimaryKey(long assetId)
181         throws NoSuchAssetException, SystemException {
182         TagsAsset tagsAsset = fetchByPrimaryKey(assetId);
183 
184         if (tagsAsset == null) {
185             if (_log.isWarnEnabled()) {
186                 _log.warn("No TagsAsset exists with the primary key " +
187                     assetId);
188             }
189 
190             throw new NoSuchAssetException(
191                 "No TagsAsset exists with the primary key " + assetId);
192         }
193 
194         return tagsAsset;
195     }
196 
197     public TagsAsset fetchByPrimaryKey(long assetId) throws SystemException {
198         Session session = null;
199 
200         try {
201             session = openSession();
202 
203             return (TagsAsset)session.get(TagsAssetImpl.class, new Long(assetId));
204         }
205         catch (Exception e) {
206             throw HibernateUtil.processException(e);
207         }
208         finally {
209             closeSession(session);
210         }
211     }
212 
213     public List findByCompanyId(long companyId) throws SystemException {
214         String finderClassName = TagsAsset.class.getName();
215         String finderMethodName = "findByCompanyId";
216         String[] finderParams = new String[] { Long.class.getName() };
217         Object[] finderArgs = new Object[] { new Long(companyId) };
218         Object result = FinderCache.getResult(finderClassName,
219                 finderMethodName, finderParams, finderArgs, getSessionFactory());
220 
221         if (result == null) {
222             Session session = null;
223 
224             try {
225                 session = openSession();
226 
227                 StringMaker query = new StringMaker();
228                 query.append(
229                     "FROM com.liferay.portlet.tags.model.TagsAsset WHERE ");
230                 query.append("companyId = ?");
231                 query.append(" ");
232 
233                 Query q = session.createQuery(query.toString());
234                 int queryPos = 0;
235                 q.setLong(queryPos++, companyId);
236 
237                 List list = q.list();
238                 FinderCache.putResult(finderClassName, finderMethodName,
239                     finderParams, finderArgs, list);
240 
241                 return list;
242             }
243             catch (Exception e) {
244                 throw HibernateUtil.processException(e);
245             }
246             finally {
247                 closeSession(session);
248             }
249         }
250         else {
251             return (List)result;
252         }
253     }
254 
255     public List findByCompanyId(long companyId, int begin, int end)
256         throws SystemException {
257         return findByCompanyId(companyId, begin, end, null);
258     }
259 
260     public List findByCompanyId(long companyId, int begin, int end,
261         OrderByComparator obc) throws SystemException {
262         String finderClassName = TagsAsset.class.getName();
263         String finderMethodName = "findByCompanyId";
264         String[] finderParams = new String[] {
265                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
266                 "com.liferay.portal.kernel.util.OrderByComparator"
267             };
268         Object[] finderArgs = new Object[] {
269                 new Long(companyId), String.valueOf(begin), String.valueOf(end),
270                 String.valueOf(obc)
271             };
272         Object result = FinderCache.getResult(finderClassName,
273                 finderMethodName, finderParams, finderArgs, getSessionFactory());
274 
275         if (result == null) {
276             Session session = null;
277 
278             try {
279                 session = openSession();
280 
281                 StringMaker query = new StringMaker();
282                 query.append(
283                     "FROM com.liferay.portlet.tags.model.TagsAsset WHERE ");
284                 query.append("companyId = ?");
285                 query.append(" ");
286 
287                 if (obc != null) {
288                     query.append("ORDER BY ");
289                     query.append(obc.getOrderBy());
290                 }
291 
292                 Query q = session.createQuery(query.toString());
293                 int queryPos = 0;
294                 q.setLong(queryPos++, companyId);
295 
296                 List list = QueryUtil.list(q, getDialect(), begin, end);
297                 FinderCache.putResult(finderClassName, finderMethodName,
298                     finderParams, finderArgs, list);
299 
300                 return list;
301             }
302             catch (Exception e) {
303                 throw HibernateUtil.processException(e);
304             }
305             finally {
306                 closeSession(session);
307             }
308         }
309         else {
310             return (List)result;
311         }
312     }
313 
314     public TagsAsset findByCompanyId_First(long companyId, OrderByComparator obc)
315         throws NoSuchAssetException, SystemException {
316         List list = findByCompanyId(companyId, 0, 1, obc);
317 
318         if (list.size() == 0) {
319             StringMaker msg = new StringMaker();
320             msg.append("No TagsAsset exists with the key ");
321             msg.append(StringPool.OPEN_CURLY_BRACE);
322             msg.append("companyId=");
323             msg.append(companyId);
324             msg.append(StringPool.CLOSE_CURLY_BRACE);
325             throw new NoSuchAssetException(msg.toString());
326         }
327         else {
328             return (TagsAsset)list.get(0);
329         }
330     }
331 
332     public TagsAsset findByCompanyId_Last(long companyId, OrderByComparator obc)
333         throws NoSuchAssetException, SystemException {
334         int count = countByCompanyId(companyId);
335         List list = findByCompanyId(companyId, count - 1, count, obc);
336 
337         if (list.size() == 0) {
338             StringMaker msg = new StringMaker();
339             msg.append("No TagsAsset exists with the key ");
340             msg.append(StringPool.OPEN_CURLY_BRACE);
341             msg.append("companyId=");
342             msg.append(companyId);
343             msg.append(StringPool.CLOSE_CURLY_BRACE);
344             throw new NoSuchAssetException(msg.toString());
345         }
346         else {
347             return (TagsAsset)list.get(0);
348         }
349     }
350 
351     public TagsAsset[] findByCompanyId_PrevAndNext(long assetId,
352         long companyId, OrderByComparator obc)
353         throws NoSuchAssetException, SystemException {
354         TagsAsset tagsAsset = findByPrimaryKey(assetId);
355         int count = countByCompanyId(companyId);
356         Session session = null;
357 
358         try {
359             session = openSession();
360 
361             StringMaker query = new StringMaker();
362             query.append("FROM com.liferay.portlet.tags.model.TagsAsset WHERE ");
363             query.append("companyId = ?");
364             query.append(" ");
365 
366             if (obc != null) {
367                 query.append("ORDER BY ");
368                 query.append(obc.getOrderBy());
369             }
370 
371             Query q = session.createQuery(query.toString());
372             int queryPos = 0;
373             q.setLong(queryPos++, companyId);
374 
375             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
376                     tagsAsset);
377             TagsAsset[] array = new TagsAssetImpl[3];
378             array[0] = (TagsAsset)objArray[0];
379             array[1] = (TagsAsset)objArray[1];
380             array[2] = (TagsAsset)objArray[2];
381 
382             return array;
383         }
384         catch (Exception e) {
385             throw HibernateUtil.processException(e);
386         }
387         finally {
388             closeSession(session);
389         }
390     }
391 
392     public TagsAsset findByC_C(long classNameId, long classPK)
393         throws NoSuchAssetException, SystemException {
394         TagsAsset tagsAsset = fetchByC_C(classNameId, classPK);
395 
396         if (tagsAsset == null) {
397             StringMaker msg = new StringMaker();
398             msg.append("No TagsAsset exists with the key ");
399             msg.append(StringPool.OPEN_CURLY_BRACE);
400             msg.append("classNameId=");
401             msg.append(classNameId);
402             msg.append(", ");
403             msg.append("classPK=");
404             msg.append(classPK);
405             msg.append(StringPool.CLOSE_CURLY_BRACE);
406 
407             if (_log.isWarnEnabled()) {
408                 _log.warn(msg.toString());
409             }
410 
411             throw new NoSuchAssetException(msg.toString());
412         }
413 
414         return tagsAsset;
415     }
416 
417     public TagsAsset fetchByC_C(long classNameId, long classPK)
418         throws SystemException {
419         String finderClassName = TagsAsset.class.getName();
420         String finderMethodName = "fetchByC_C";
421         String[] finderParams = new String[] {
422                 Long.class.getName(), Long.class.getName()
423             };
424         Object[] finderArgs = new Object[] {
425                 new Long(classNameId), new Long(classPK)
426             };
427         Object result = FinderCache.getResult(finderClassName,
428                 finderMethodName, finderParams, finderArgs, getSessionFactory());
429 
430         if (result == null) {
431             Session session = null;
432 
433             try {
434                 session = openSession();
435 
436                 StringMaker query = new StringMaker();
437                 query.append(
438                     "FROM com.liferay.portlet.tags.model.TagsAsset WHERE ");
439                 query.append("classNameId = ?");
440                 query.append(" AND ");
441                 query.append("classPK = ?");
442                 query.append(" ");
443 
444                 Query q = session.createQuery(query.toString());
445                 int queryPos = 0;
446                 q.setLong(queryPos++, classNameId);
447                 q.setLong(queryPos++, classPK);
448 
449                 List list = q.list();
450                 FinderCache.putResult(finderClassName, finderMethodName,
451                     finderParams, finderArgs, list);
452 
453                 if (list.size() == 0) {
454                     return null;
455                 }
456                 else {
457                     return (TagsAsset)list.get(0);
458                 }
459             }
460             catch (Exception e) {
461                 throw HibernateUtil.processException(e);
462             }
463             finally {
464                 closeSession(session);
465             }
466         }
467         else {
468             List list = (List)result;
469 
470             if (list.size() == 0) {
471                 return null;
472             }
473             else {
474                 return (TagsAsset)list.get(0);
475             }
476         }
477     }
478 
479     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
480         throws SystemException {
481         Session session = null;
482 
483         try {
484             session = openSession();
485 
486             DynamicQuery query = queryInitializer.initialize(session);
487 
488             return query.list();
489         }
490         catch (Exception e) {
491             throw HibernateUtil.processException(e);
492         }
493         finally {
494             closeSession(session);
495         }
496     }
497 
498     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
499         int begin, int end) throws SystemException {
500         Session session = null;
501 
502         try {
503             session = openSession();
504 
505             DynamicQuery query = queryInitializer.initialize(session);
506             query.setLimit(begin, end);
507 
508             return query.list();
509         }
510         catch (Exception e) {
511             throw HibernateUtil.processException(e);
512         }
513         finally {
514             closeSession(session);
515         }
516     }
517 
518     public List findAll() throws SystemException {
519         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
520     }
521 
522     public List findAll(int begin, int end) throws SystemException {
523         return findAll(begin, end, null);
524     }
525 
526     public List findAll(int begin, int end, OrderByComparator obc)
527         throws SystemException {
528         String finderClassName = TagsAsset.class.getName();
529         String finderMethodName = "findAll";
530         String[] finderParams = new String[] {
531                 "java.lang.Integer", "java.lang.Integer",
532                 "com.liferay.portal.kernel.util.OrderByComparator"
533             };
534         Object[] finderArgs = new Object[] {
535                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
536             };
537         Object result = FinderCache.getResult(finderClassName,
538                 finderMethodName, finderParams, finderArgs, getSessionFactory());
539 
540         if (result == null) {
541             Session session = null;
542 
543             try {
544                 session = openSession();
545 
546                 StringMaker query = new StringMaker();
547                 query.append("FROM com.liferay.portlet.tags.model.TagsAsset ");
548 
549                 if (obc != null) {
550                     query.append("ORDER BY ");
551                     query.append(obc.getOrderBy());
552                 }
553 
554                 Query q = session.createQuery(query.toString());
555                 List list = QueryUtil.list(q, getDialect(), begin, end);
556 
557                 if (obc == null) {
558                     Collections.sort(list);
559                 }
560 
561                 FinderCache.putResult(finderClassName, finderMethodName,
562                     finderParams, finderArgs, list);
563 
564                 return list;
565             }
566             catch (Exception e) {
567                 throw HibernateUtil.processException(e);
568             }
569             finally {
570                 closeSession(session);
571             }
572         }
573         else {
574             return (List)result;
575         }
576     }
577 
578     public void removeByCompanyId(long companyId) throws SystemException {
579         Iterator itr = findByCompanyId(companyId).iterator();
580 
581         while (itr.hasNext()) {
582             TagsAsset tagsAsset = (TagsAsset)itr.next();
583             remove(tagsAsset);
584         }
585     }
586 
587     public void removeByC_C(long classNameId, long classPK)
588         throws NoSuchAssetException, SystemException {
589         TagsAsset tagsAsset = findByC_C(classNameId, classPK);
590         remove(tagsAsset);
591     }
592 
593     public void removeAll() throws SystemException {
594         Iterator itr = findAll().iterator();
595 
596         while (itr.hasNext()) {
597             remove((TagsAsset)itr.next());
598         }
599     }
600 
601     public int countByCompanyId(long companyId) throws SystemException {
602         String finderClassName = TagsAsset.class.getName();
603         String finderMethodName = "countByCompanyId";
604         String[] finderParams = new String[] { Long.class.getName() };
605         Object[] finderArgs = new Object[] { new Long(companyId) };
606         Object result = FinderCache.getResult(finderClassName,
607                 finderMethodName, finderParams, finderArgs, getSessionFactory());
608 
609         if (result == null) {
610             Session session = null;
611 
612             try {
613                 session = openSession();
614 
615                 StringMaker query = new StringMaker();
616                 query.append("SELECT COUNT(*) ");
617                 query.append(
618                     "FROM com.liferay.portlet.tags.model.TagsAsset WHERE ");
619                 query.append("companyId = ?");
620                 query.append(" ");
621 
622                 Query q = session.createQuery(query.toString());
623                 int queryPos = 0;
624                 q.setLong(queryPos++, companyId);
625 
626                 Long count = null;
627                 Iterator itr = q.list().iterator();
628 
629                 if (itr.hasNext()) {
630                     count = (Long)itr.next();
631                 }
632 
633                 if (count == null) {
634                     count = new Long(0);
635                 }
636 
637                 FinderCache.putResult(finderClassName, finderMethodName,
638                     finderParams, finderArgs, count);
639 
640                 return count.intValue();
641             }
642             catch (Exception e) {
643                 throw HibernateUtil.processException(e);
644             }
645             finally {
646                 closeSession(session);
647             }
648         }
649         else {
650             return ((Long)result).intValue();
651         }
652     }
653 
654     public int countByC_C(long classNameId, long classPK)
655         throws SystemException {
656         String finderClassName = TagsAsset.class.getName();
657         String finderMethodName = "countByC_C";
658         String[] finderParams = new String[] {
659                 Long.class.getName(), Long.class.getName()
660             };
661         Object[] finderArgs = new Object[] {
662                 new Long(classNameId), new Long(classPK)
663             };
664         Object result = FinderCache.getResult(finderClassName,
665                 finderMethodName, finderParams, finderArgs, getSessionFactory());
666 
667         if (result == null) {
668             Session session = null;
669 
670             try {
671                 session = openSession();
672 
673                 StringMaker query = new StringMaker();
674                 query.append("SELECT COUNT(*) ");
675                 query.append(
676                     "FROM com.liferay.portlet.tags.model.TagsAsset WHERE ");
677                 query.append("classNameId = ?");
678                 query.append(" AND ");
679                 query.append("classPK = ?");
680                 query.append(" ");
681 
682                 Query q = session.createQuery(query.toString());
683                 int queryPos = 0;
684                 q.setLong(queryPos++, classNameId);
685                 q.setLong(queryPos++, classPK);
686 
687                 Long count = null;
688                 Iterator itr = q.list().iterator();
689 
690                 if (itr.hasNext()) {
691                     count = (Long)itr.next();
692                 }
693 
694                 if (count == null) {
695                     count = new Long(0);
696                 }
697 
698                 FinderCache.putResult(finderClassName, finderMethodName,
699                     finderParams, finderArgs, count);
700 
701                 return count.intValue();
702             }
703             catch (Exception e) {
704                 throw HibernateUtil.processException(e);
705             }
706             finally {
707                 closeSession(session);
708             }
709         }
710         else {
711             return ((Long)result).intValue();
712         }
713     }
714 
715     public int countAll() throws SystemException {
716         String finderClassName = TagsAsset.class.getName();
717         String finderMethodName = "countAll";
718         String[] finderParams = new String[] {  };
719         Object[] finderArgs = new Object[] {  };
720         Object result = FinderCache.getResult(finderClassName,
721                 finderMethodName, finderParams, finderArgs, getSessionFactory());
722 
723         if (result == null) {
724             Session session = null;
725 
726             try {
727                 session = openSession();
728 
729                 StringMaker query = new StringMaker();
730                 query.append("SELECT COUNT(*) ");
731                 query.append("FROM com.liferay.portlet.tags.model.TagsAsset");
732 
733                 Query q = session.createQuery(query.toString());
734                 Long count = null;
735                 Iterator itr = q.list().iterator();
736 
737                 if (itr.hasNext()) {
738                     count = (Long)itr.next();
739                 }
740 
741                 if (count == null) {
742                     count = new Long(0);
743                 }
744 
745                 FinderCache.putResult(finderClassName, finderMethodName,
746                     finderParams, finderArgs, count);
747 
748                 return count.intValue();
749             }
750             catch (Exception e) {
751                 throw HibernateUtil.processException(e);
752             }
753             finally {
754                 closeSession(session);
755             }
756         }
757         else {
758             return ((Long)result).intValue();
759         }
760     }
761 
762     public List getTagsEntries(long pk)
763         throws NoSuchAssetException, SystemException {
764         return getTagsEntries(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
765     }
766 
767     public List getTagsEntries(long pk, int begin, int end)
768         throws NoSuchAssetException, SystemException {
769         return getTagsEntries(pk, begin, end, null);
770     }
771 
772     public List getTagsEntries(long pk, int begin, int end,
773         OrderByComparator obc) throws NoSuchAssetException, SystemException {
774         String finderClassName = "TagsAssets_TagsEntries";
775         String finderMethodName = "getTagsEntries";
776         String[] finderParams = new String[] {
777                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
778                 "com.liferay.portal.kernel.util.OrderByComparator"
779             };
780         Object[] finderArgs = new Object[] {
781                 new Long(pk), String.valueOf(begin), String.valueOf(end),
782                 String.valueOf(obc)
783             };
784         Object result = FinderCache.getResult(finderClassName,
785                 finderMethodName, finderParams, finderArgs, getSessionFactory());
786 
787         if (result == null) {
788             Session session = null;
789 
790             try {
791                 session = HibernateUtil.openSession();
792 
793                 StringMaker sm = new StringMaker();
794                 sm.append(_SQL_GETTAGSENTRYS);
795 
796                 if (obc != null) {
797                     sm.append("ORDER BY ");
798                     sm.append(obc.getOrderBy());
799                 }
800                 else {
801                     sm.append("ORDER BY ");
802                     sm.append("TagsEntry.name ASC");
803                 }
804 
805                 String sql = sm.toString();
806                 SQLQuery q = session.createSQLQuery(sql);
807                 q.addEntity("TagsEntry",
808                     com.liferay.portlet.tags.model.impl.TagsEntryImpl.class);
809 
810                 QueryPos qPos = QueryPos.getInstance(q);
811                 qPos.add(pk);
812 
813                 List list = QueryUtil.list(q, getDialect(), begin, end);
814                 FinderCache.putResult(finderClassName, finderMethodName,
815                     finderParams, finderArgs, list);
816 
817                 return list;
818             }
819             catch (Exception e) {
820                 throw new SystemException(e);
821             }
822             finally {
823                 closeSession(session);
824             }
825         }
826         else {
827             return (List)result;
828         }
829     }
830 
831     public int getTagsEntriesSize(long pk) throws SystemException {
832         String finderClassName = "TagsAssets_TagsEntries";
833         String finderMethodName = "getTagsEntriesSize";
834         String[] finderParams = new String[] { Long.class.getName() };
835         Object[] finderArgs = new Object[] { new Long(pk) };
836         Object result = FinderCache.getResult(finderClassName,
837                 finderMethodName, finderParams, finderArgs, getSessionFactory());
838 
839         if (result == null) {
840             Session session = null;
841 
842             try {
843                 session = openSession();
844 
845                 SQLQuery q = session.createSQLQuery(_SQL_GETTAGSENTRYSSIZE);
846                 q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
847 
848                 QueryPos qPos = QueryPos.getInstance(q);
849                 qPos.add(pk);
850 
851                 Long count = null;
852                 Iterator itr = q.list().iterator();
853 
854                 if (itr.hasNext()) {
855                     count = (Long)itr.next();
856                 }
857 
858                 if (count == null) {
859                     count = new Long(0);
860                 }
861 
862                 FinderCache.putResult(finderClassName, finderMethodName,
863                     finderParams, finderArgs, count);
864 
865                 return count.intValue();
866             }
867             catch (Exception e) {
868                 throw HibernateUtil.processException(e);
869             }
870             finally {
871                 closeSession(session);
872             }
873         }
874         else {
875             return ((Long)result).intValue();
876         }
877     }
878 
879     public boolean containsTagsEntry(long pk, long tagsEntryPK)
880         throws SystemException {
881         String finderClassName = "TagsAssets_TagsEntries";
882         String finderMethodName = "containsTagsEntries";
883         String[] finderParams = new String[] {
884                 Long.class.getName(), Long.class.getName()
885             };
886         Object[] finderArgs = new Object[] { new Long(pk), new Long(tagsEntryPK) };
887         Object result = FinderCache.getResult(finderClassName,
888                 finderMethodName, finderParams, finderArgs, getSessionFactory());
889 
890         if (result == null) {
891             try {
892                 Boolean value = Boolean.valueOf(containsTagsEntry.contains(pk,
893                             tagsEntryPK));
894                 FinderCache.putResult(finderClassName, finderMethodName,
895                     finderParams, finderArgs, value);
896 
897                 return value.booleanValue();
898             }
899             catch (DataAccessException dae) {
900                 throw new SystemException(dae);
901             }
902         }
903         else {
904             return ((Boolean)result).booleanValue();
905         }
906     }
907 
908     public boolean containsTagsEntrys(long pk) throws SystemException {
909         if (getTagsEntriesSize(pk) > 0) {
910             return true;
911         }
912         else {
913             return false;
914         }
915     }
916 
917     public void addTagsEntry(long pk, long tagsEntryPK)
918         throws NoSuchAssetException, 
919             com.liferay.portlet.tags.NoSuchEntryException, SystemException {
920         try {
921             addTagsEntry.add(pk, tagsEntryPK);
922         }
923         catch (DataAccessException dae) {
924             throw new SystemException(dae);
925         }
926         finally {
927             FinderCache.clearCache("TagsAssets_TagsEntries");
928         }
929     }
930 
931     public void addTagsEntry(long pk,
932         com.liferay.portlet.tags.model.TagsEntry tagsEntry)
933         throws NoSuchAssetException, 
934             com.liferay.portlet.tags.NoSuchEntryException, SystemException {
935         try {
936             addTagsEntry.add(pk, tagsEntry.getPrimaryKey());
937         }
938         catch (DataAccessException dae) {
939             throw new SystemException(dae);
940         }
941         finally {
942             FinderCache.clearCache("TagsAssets_TagsEntries");
943         }
944     }
945 
946     public void addTagsEntries(long pk, long[] tagsEntryPKs)
947         throws NoSuchAssetException, 
948             com.liferay.portlet.tags.NoSuchEntryException, SystemException {
949         try {
950             for (int i = 0; i < tagsEntryPKs.length; i++) {
951                 addTagsEntry.add(pk, tagsEntryPKs[i]);
952             }
953         }
954         catch (DataAccessException dae) {
955             throw new SystemException(dae);
956         }
957         finally {
958             FinderCache.clearCache("TagsAssets_TagsEntries");
959         }
960     }
961 
962     public void addTagsEntries(long pk, List tagsEntries)
963         throws NoSuchAssetException, 
964             com.liferay.portlet.tags.NoSuchEntryException, SystemException {
965         try {
966             for (int i = 0; i < tagsEntries.size(); i++) {
967                 com.liferay.portlet.tags.model.TagsEntry tagsEntry = (com.liferay.portlet.tags.model.TagsEntry)tagsEntries.get(i);
968                 addTagsEntry.add(pk, tagsEntry.getPrimaryKey());
969             }
970         }
971         catch (DataAccessException dae) {
972             throw new SystemException(dae);
973         }
974         finally {
975             FinderCache.clearCache("TagsAssets_TagsEntries");
976         }
977     }
978 
979     public void clearTagsEntries(long pk)
980         throws NoSuchAssetException, SystemException {
981         try {
982             clearTagsEntries.clear(pk);
983         }
984         catch (DataAccessException dae) {
985             throw new SystemException(dae);
986         }
987         finally {
988             FinderCache.clearCache("TagsAssets_TagsEntries");
989         }
990     }
991 
992     public void removeTagsEntry(long pk, long tagsEntryPK)
993         throws NoSuchAssetException, 
994             com.liferay.portlet.tags.NoSuchEntryException, SystemException {
995         try {
996             removeTagsEntry.remove(pk, tagsEntryPK);
997         }
998         catch (DataAccessException dae) {
999             throw new SystemException(dae);
1000        }
1001        finally {
1002            FinderCache.clearCache("TagsAssets_TagsEntries");
1003        }
1004    }
1005
1006    public void removeTagsEntry(long pk,
1007        com.liferay.portlet.tags.model.TagsEntry tagsEntry)
1008        throws NoSuchAssetException, 
1009            com.liferay.portlet.tags.NoSuchEntryException, SystemException {
1010        try {
1011            removeTagsEntry.remove(pk, tagsEntry.getPrimaryKey());
1012        }
1013        catch (DataAccessException dae) {
1014            throw new SystemException(dae);
1015        }
1016        finally {
1017            FinderCache.clearCache("TagsAssets_TagsEntries");
1018        }
1019    }
1020
1021    public void removeTagsEntries(long pk, long[] tagsEntryPKs)
1022        throws NoSuchAssetException, 
1023            com.liferay.portlet.tags.NoSuchEntryException, SystemException {
1024        try {
1025            for (int i = 0; i < tagsEntryPKs.length; i++) {
1026                removeTagsEntry.remove(pk, tagsEntryPKs[i]);
1027            }
1028        }
1029        catch (DataAccessException dae) {
1030            throw new SystemException(dae);
1031        }
1032        finally {
1033            FinderCache.clearCache("TagsAssets_TagsEntries");
1034        }
1035    }
1036
1037    public void removeTagsEntries(long pk, List tagsEntries)
1038        throws NoSuchAssetException, 
1039            com.liferay.portlet.tags.NoSuchEntryException, SystemException {
1040        try {
1041            for (int i = 0; i < tagsEntries.size(); i++) {
1042                com.liferay.portlet.tags.model.TagsEntry tagsEntry = (com.liferay.portlet.tags.model.TagsEntry)tagsEntries.get(i);
1043                removeTagsEntry.remove(pk, tagsEntry.getPrimaryKey());
1044            }
1045        }
1046        catch (DataAccessException dae) {
1047            throw new SystemException(dae);
1048        }
1049        finally {
1050            FinderCache.clearCache("TagsAssets_TagsEntries");
1051        }
1052    }
1053
1054    public void setTagsEntries(long pk, long[] tagsEntryPKs)
1055        throws NoSuchAssetException, 
1056            com.liferay.portlet.tags.NoSuchEntryException, SystemException {
1057        try {
1058            clearTagsEntries.clear(pk);
1059
1060            for (int i = 0; i < tagsEntryPKs.length; i++) {
1061                addTagsEntry.add(pk, tagsEntryPKs[i]);
1062            }
1063        }
1064        catch (DataAccessException dae) {
1065            throw new SystemException(dae);
1066        }
1067        finally {
1068            FinderCache.clearCache("TagsAssets_TagsEntries");
1069        }
1070    }
1071
1072    public void setTagsEntries(long pk, List tagsEntries)
1073        throws NoSuchAssetException, 
1074            com.liferay.portlet.tags.NoSuchEntryException, SystemException {
1075        try {
1076            clearTagsEntries.clear(pk);
1077
1078            for (int i = 0; i < tagsEntries.size(); i++) {
1079                com.liferay.portlet.tags.model.TagsEntry tagsEntry = (com.liferay.portlet.tags.model.TagsEntry)tagsEntries.get(i);
1080                addTagsEntry.add(pk, tagsEntry.getPrimaryKey());
1081            }
1082        }
1083        catch (DataAccessException dae) {
1084            throw new SystemException(dae);
1085        }
1086        finally {
1087            FinderCache.clearCache("TagsAssets_TagsEntries");
1088        }
1089    }
1090
1091    protected void initDao() {
1092        containsTagsEntry = new ContainsTagsEntry(this);
1093        addTagsEntry = new AddTagsEntry(this);
1094        clearTagsEntries = new ClearTagsEntries(this);
1095        removeTagsEntry = new RemoveTagsEntry(this);
1096    }
1097
1098    protected ContainsTagsEntry containsTagsEntry;
1099    protected AddTagsEntry addTagsEntry;
1100    protected ClearTagsEntries clearTagsEntries;
1101    protected RemoveTagsEntry removeTagsEntry;
1102
1103    protected class ContainsTagsEntry extends MappingSqlQuery {
1104        protected ContainsTagsEntry(TagsAssetPersistenceImpl persistenceImpl) {
1105            super(persistenceImpl.getDataSource(), _SQL_CONTAINSTAGSENTRY);
1106            declareParameter(new SqlParameter(Types.BIGINT));
1107            declareParameter(new SqlParameter(Types.BIGINT));
1108            compile();
1109        }
1110
1111        protected Object mapRow(ResultSet rs, int rowNumber)
1112            throws SQLException {
1113            return new Integer(rs.getInt("COUNT_VALUE"));
1114        }
1115
1116        protected boolean contains(long assetId, long entryId) {
1117            List results = execute(new Object[] {
1118                        new Long(assetId), new Long(entryId)
1119                    });
1120
1121            if (results.size() > 0) {
1122                Integer count = (Integer)results.get(0);
1123
1124                if (count.intValue() > 0) {
1125                    return true;
1126                }
1127            }
1128
1129            return false;
1130        }
1131    }
1132
1133    protected class AddTagsEntry extends SqlUpdate {
1134        protected AddTagsEntry(TagsAssetPersistenceImpl persistenceImpl) {
1135            super(persistenceImpl.getDataSource(),
1136                "INSERT INTO TagsAssets_TagsEntries (assetId, entryId) VALUES (?, ?)");
1137            _persistenceImpl = persistenceImpl;
1138            declareParameter(new SqlParameter(Types.BIGINT));
1139            declareParameter(new SqlParameter(Types.BIGINT));
1140            compile();
1141        }
1142
1143        protected void add(long assetId, long entryId) {
1144            if (!_persistenceImpl.containsTagsEntry.contains(assetId, entryId)) {
1145                update(new Object[] { new Long(assetId), new Long(entryId) });
1146            }
1147        }
1148
1149        private TagsAssetPersistenceImpl _persistenceImpl;
1150    }
1151
1152    protected class ClearTagsEntries extends SqlUpdate {
1153        protected ClearTagsEntries(TagsAssetPersistenceImpl persistenceImpl) {
1154            super(persistenceImpl.getDataSource(),
1155                "DELETE FROM TagsAssets_TagsEntries WHERE assetId = ?");
1156            declareParameter(new SqlParameter(Types.BIGINT));
1157            compile();
1158        }
1159
1160        protected void clear(long assetId) {
1161            update(new Object[] { new Long(assetId) });
1162        }
1163    }
1164
1165    protected class RemoveTagsEntry extends SqlUpdate {
1166        protected RemoveTagsEntry(TagsAssetPersistenceImpl persistenceImpl) {
1167            super(persistenceImpl.getDataSource(),
1168                "DELETE FROM TagsAssets_TagsEntries WHERE assetId = ? AND entryId = ?");
1169            declareParameter(new SqlParameter(Types.BIGINT));
1170            declareParameter(new SqlParameter(Types.BIGINT));
1171            compile();
1172        }
1173
1174        protected void remove(long assetId, long entryId) {
1175            update(new Object[] { new Long(assetId), new Long(entryId) });
1176        }
1177    }
1178
1179    private static final String _SQL_GETTAGSENTRYS = "SELECT {TagsEntry.*} FROM TagsEntry INNER JOIN TagsAssets_TagsEntries ON (TagsAssets_TagsEntries.entryId = TagsEntry.entryId) WHERE (TagsAssets_TagsEntries.assetId = ?)";
1180    private static final String _SQL_GETTAGSENTRYSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM TagsAssets_TagsEntries WHERE assetId = ?";
1181    private static final String _SQL_CONTAINSTAGSENTRY = "SELECT COUNT(*) AS COUNT_VALUE FROM TagsAssets_TagsEntries WHERE assetId = ? AND entryId = ?";
1182    private static Log _log = LogFactory.getLog(TagsAssetPersistenceImpl.class);
1183}