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