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