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