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