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