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