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