1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.expando.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.kernel.annotation.BeanReference;
19  import com.liferay.portal.kernel.cache.CacheRegistry;
20  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
21  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
22  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
23  import com.liferay.portal.kernel.dao.orm.FinderPath;
24  import com.liferay.portal.kernel.dao.orm.Query;
25  import com.liferay.portal.kernel.dao.orm.QueryPos;
26  import com.liferay.portal.kernel.dao.orm.QueryUtil;
27  import com.liferay.portal.kernel.dao.orm.Session;
28  import com.liferay.portal.kernel.exception.SystemException;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.StringBundler;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.model.ModelListener;
38  import com.liferay.portal.service.persistence.BatchSessionUtil;
39  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
40  
41  import com.liferay.portlet.expando.NoSuchColumnException;
42  import com.liferay.portlet.expando.model.ExpandoColumn;
43  import com.liferay.portlet.expando.model.impl.ExpandoColumnImpl;
44  import com.liferay.portlet.expando.model.impl.ExpandoColumnModelImpl;
45  
46  import java.io.Serializable;
47  
48  import java.util.ArrayList;
49  import java.util.Collections;
50  import java.util.List;
51  
52  /**
53   * <a href="ExpandoColumnPersistenceImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * <p>
56   * ServiceBuilder generated this class. Modifications in this class will be
57   * overwritten the next time is generated.
58   * </p>
59   *
60   * @author    Brian Wing Shun Chan
61   * @see       ExpandoColumnPersistence
62   * @see       ExpandoColumnUtil
63   * @generated
64   */
65  public class ExpandoColumnPersistenceImpl extends BasePersistenceImpl<ExpandoColumn>
66      implements ExpandoColumnPersistence {
67      public static final String FINDER_CLASS_NAME_ENTITY = ExpandoColumnImpl.class.getName();
68      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
69          ".List";
70      public static final FinderPath FINDER_PATH_FIND_BY_TABLEID = new FinderPath(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
71              ExpandoColumnModelImpl.FINDER_CACHE_ENABLED,
72              FINDER_CLASS_NAME_LIST, "findByTableId",
73              new String[] { Long.class.getName() });
74      public static final FinderPath FINDER_PATH_FIND_BY_OBC_TABLEID = new FinderPath(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
75              ExpandoColumnModelImpl.FINDER_CACHE_ENABLED,
76              FINDER_CLASS_NAME_LIST, "findByTableId",
77              new String[] {
78                  Long.class.getName(),
79                  
80              "java.lang.Integer", "java.lang.Integer",
81                  "com.liferay.portal.kernel.util.OrderByComparator"
82              });
83      public static final FinderPath FINDER_PATH_COUNT_BY_TABLEID = new FinderPath(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
84              ExpandoColumnModelImpl.FINDER_CACHE_ENABLED,
85              FINDER_CLASS_NAME_LIST, "countByTableId",
86              new String[] { Long.class.getName() });
87      public static final FinderPath FINDER_PATH_FETCH_BY_T_N = new FinderPath(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
88              ExpandoColumnModelImpl.FINDER_CACHE_ENABLED,
89              FINDER_CLASS_NAME_ENTITY, "fetchByT_N",
90              new String[] { Long.class.getName(), String.class.getName() });
91      public static final FinderPath FINDER_PATH_COUNT_BY_T_N = new FinderPath(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
92              ExpandoColumnModelImpl.FINDER_CACHE_ENABLED,
93              FINDER_CLASS_NAME_LIST, "countByT_N",
94              new String[] { Long.class.getName(), String.class.getName() });
95      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
96              ExpandoColumnModelImpl.FINDER_CACHE_ENABLED,
97              FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
98      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
99              ExpandoColumnModelImpl.FINDER_CACHE_ENABLED,
100             FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
101 
102     public void cacheResult(ExpandoColumn expandoColumn) {
103         EntityCacheUtil.putResult(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
104             ExpandoColumnImpl.class, expandoColumn.getPrimaryKey(),
105             expandoColumn);
106 
107         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
108             new Object[] {
109                 new Long(expandoColumn.getTableId()),
110                 
111             expandoColumn.getName()
112             }, expandoColumn);
113     }
114 
115     public void cacheResult(List<ExpandoColumn> expandoColumns) {
116         for (ExpandoColumn expandoColumn : expandoColumns) {
117             if (EntityCacheUtil.getResult(
118                         ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
119                         ExpandoColumnImpl.class, expandoColumn.getPrimaryKey(),
120                         this) == null) {
121                 cacheResult(expandoColumn);
122             }
123         }
124     }
125 
126     public void clearCache() {
127         CacheRegistry.clear(ExpandoColumnImpl.class.getName());
128         EntityCacheUtil.clearCache(ExpandoColumnImpl.class.getName());
129         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
130         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
131     }
132 
133     public ExpandoColumn create(long columnId) {
134         ExpandoColumn expandoColumn = new ExpandoColumnImpl();
135 
136         expandoColumn.setNew(true);
137         expandoColumn.setPrimaryKey(columnId);
138 
139         return expandoColumn;
140     }
141 
142     public ExpandoColumn remove(Serializable primaryKey)
143         throws NoSuchModelException, SystemException {
144         return remove(((Long)primaryKey).longValue());
145     }
146 
147     public ExpandoColumn remove(long columnId)
148         throws NoSuchColumnException, SystemException {
149         Session session = null;
150 
151         try {
152             session = openSession();
153 
154             ExpandoColumn expandoColumn = (ExpandoColumn)session.get(ExpandoColumnImpl.class,
155                     new Long(columnId));
156 
157             if (expandoColumn == null) {
158                 if (_log.isWarnEnabled()) {
159                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + columnId);
160                 }
161 
162                 throw new NoSuchColumnException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
163                     columnId);
164             }
165 
166             return remove(expandoColumn);
167         }
168         catch (NoSuchColumnException nsee) {
169             throw nsee;
170         }
171         catch (Exception e) {
172             throw processException(e);
173         }
174         finally {
175             closeSession(session);
176         }
177     }
178 
179     public ExpandoColumn remove(ExpandoColumn expandoColumn)
180         throws SystemException {
181         for (ModelListener<ExpandoColumn> listener : listeners) {
182             listener.onBeforeRemove(expandoColumn);
183         }
184 
185         expandoColumn = removeImpl(expandoColumn);
186 
187         for (ModelListener<ExpandoColumn> listener : listeners) {
188             listener.onAfterRemove(expandoColumn);
189         }
190 
191         return expandoColumn;
192     }
193 
194     protected ExpandoColumn removeImpl(ExpandoColumn expandoColumn)
195         throws SystemException {
196         expandoColumn = toUnwrappedModel(expandoColumn);
197 
198         Session session = null;
199 
200         try {
201             session = openSession();
202 
203             if (expandoColumn.isCachedModel() || BatchSessionUtil.isEnabled()) {
204                 Object staleObject = session.get(ExpandoColumnImpl.class,
205                         expandoColumn.getPrimaryKeyObj());
206 
207                 if (staleObject != null) {
208                     session.evict(staleObject);
209                 }
210             }
211 
212             session.delete(expandoColumn);
213 
214             session.flush();
215         }
216         catch (Exception e) {
217             throw processException(e);
218         }
219         finally {
220             closeSession(session);
221         }
222 
223         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
224 
225         ExpandoColumnModelImpl expandoColumnModelImpl = (ExpandoColumnModelImpl)expandoColumn;
226 
227         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_N,
228             new Object[] {
229                 new Long(expandoColumnModelImpl.getOriginalTableId()),
230                 
231             expandoColumnModelImpl.getOriginalName()
232             });
233 
234         EntityCacheUtil.removeResult(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
235             ExpandoColumnImpl.class, expandoColumn.getPrimaryKey());
236 
237         return expandoColumn;
238     }
239 
240     public ExpandoColumn updateImpl(
241         com.liferay.portlet.expando.model.ExpandoColumn expandoColumn,
242         boolean merge) throws SystemException {
243         expandoColumn = toUnwrappedModel(expandoColumn);
244 
245         boolean isNew = expandoColumn.isNew();
246 
247         ExpandoColumnModelImpl expandoColumnModelImpl = (ExpandoColumnModelImpl)expandoColumn;
248 
249         Session session = null;
250 
251         try {
252             session = openSession();
253 
254             BatchSessionUtil.update(session, expandoColumn, merge);
255 
256             expandoColumn.setNew(false);
257         }
258         catch (Exception e) {
259             throw processException(e);
260         }
261         finally {
262             closeSession(session);
263         }
264 
265         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
266 
267         EntityCacheUtil.putResult(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
268             ExpandoColumnImpl.class, expandoColumn.getPrimaryKey(),
269             expandoColumn);
270 
271         if (!isNew &&
272                 ((expandoColumn.getTableId() != expandoColumnModelImpl.getOriginalTableId()) ||
273                 !Validator.equals(expandoColumn.getName(),
274                     expandoColumnModelImpl.getOriginalName()))) {
275             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_N,
276                 new Object[] {
277                     new Long(expandoColumnModelImpl.getOriginalTableId()),
278                     
279                 expandoColumnModelImpl.getOriginalName()
280                 });
281         }
282 
283         if (isNew ||
284                 ((expandoColumn.getTableId() != expandoColumnModelImpl.getOriginalTableId()) ||
285                 !Validator.equals(expandoColumn.getName(),
286                     expandoColumnModelImpl.getOriginalName()))) {
287             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
288                 new Object[] {
289                     new Long(expandoColumn.getTableId()),
290                     
291                 expandoColumn.getName()
292                 }, expandoColumn);
293         }
294 
295         return expandoColumn;
296     }
297 
298     protected ExpandoColumn toUnwrappedModel(ExpandoColumn expandoColumn) {
299         if (expandoColumn instanceof ExpandoColumnImpl) {
300             return expandoColumn;
301         }
302 
303         ExpandoColumnImpl expandoColumnImpl = new ExpandoColumnImpl();
304 
305         expandoColumnImpl.setNew(expandoColumn.isNew());
306         expandoColumnImpl.setPrimaryKey(expandoColumn.getPrimaryKey());
307 
308         expandoColumnImpl.setColumnId(expandoColumn.getColumnId());
309         expandoColumnImpl.setCompanyId(expandoColumn.getCompanyId());
310         expandoColumnImpl.setTableId(expandoColumn.getTableId());
311         expandoColumnImpl.setName(expandoColumn.getName());
312         expandoColumnImpl.setType(expandoColumn.getType());
313         expandoColumnImpl.setDefaultData(expandoColumn.getDefaultData());
314         expandoColumnImpl.setTypeSettings(expandoColumn.getTypeSettings());
315 
316         return expandoColumnImpl;
317     }
318 
319     public ExpandoColumn findByPrimaryKey(Serializable primaryKey)
320         throws NoSuchModelException, SystemException {
321         return findByPrimaryKey(((Long)primaryKey).longValue());
322     }
323 
324     public ExpandoColumn findByPrimaryKey(long columnId)
325         throws NoSuchColumnException, SystemException {
326         ExpandoColumn expandoColumn = fetchByPrimaryKey(columnId);
327 
328         if (expandoColumn == null) {
329             if (_log.isWarnEnabled()) {
330                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + columnId);
331             }
332 
333             throw new NoSuchColumnException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
334                 columnId);
335         }
336 
337         return expandoColumn;
338     }
339 
340     public ExpandoColumn fetchByPrimaryKey(Serializable primaryKey)
341         throws SystemException {
342         return fetchByPrimaryKey(((Long)primaryKey).longValue());
343     }
344 
345     public ExpandoColumn fetchByPrimaryKey(long columnId)
346         throws SystemException {
347         ExpandoColumn expandoColumn = (ExpandoColumn)EntityCacheUtil.getResult(ExpandoColumnModelImpl.ENTITY_CACHE_ENABLED,
348                 ExpandoColumnImpl.class, columnId, this);
349 
350         if (expandoColumn == null) {
351             Session session = null;
352 
353             try {
354                 session = openSession();
355 
356                 expandoColumn = (ExpandoColumn)session.get(ExpandoColumnImpl.class,
357                         new Long(columnId));
358             }
359             catch (Exception e) {
360                 throw processException(e);
361             }
362             finally {
363                 if (expandoColumn != null) {
364                     cacheResult(expandoColumn);
365                 }
366 
367                 closeSession(session);
368             }
369         }
370 
371         return expandoColumn;
372     }
373 
374     public List<ExpandoColumn> findByTableId(long tableId)
375         throws SystemException {
376         Object[] finderArgs = new Object[] { new Long(tableId) };
377 
378         List<ExpandoColumn> list = (List<ExpandoColumn>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_TABLEID,
379                 finderArgs, this);
380 
381         if (list == null) {
382             Session session = null;
383 
384             try {
385                 session = openSession();
386 
387                 StringBundler query = new StringBundler(3);
388 
389                 query.append(_SQL_SELECT_EXPANDOCOLUMN_WHERE);
390 
391                 query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
392 
393                 query.append(ExpandoColumnModelImpl.ORDER_BY_JPQL);
394 
395                 String sql = query.toString();
396 
397                 Query q = session.createQuery(sql);
398 
399                 QueryPos qPos = QueryPos.getInstance(q);
400 
401                 qPos.add(tableId);
402 
403                 list = q.list();
404             }
405             catch (Exception e) {
406                 throw processException(e);
407             }
408             finally {
409                 if (list == null) {
410                     list = new ArrayList<ExpandoColumn>();
411                 }
412 
413                 cacheResult(list);
414 
415                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_TABLEID,
416                     finderArgs, list);
417 
418                 closeSession(session);
419             }
420         }
421 
422         return list;
423     }
424 
425     public List<ExpandoColumn> findByTableId(long tableId, int start, int end)
426         throws SystemException {
427         return findByTableId(tableId, start, end, null);
428     }
429 
430     public List<ExpandoColumn> findByTableId(long tableId, int start, int end,
431         OrderByComparator obc) throws SystemException {
432         Object[] finderArgs = new Object[] {
433                 new Long(tableId),
434                 
435                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
436             };
437 
438         List<ExpandoColumn> list = (List<ExpandoColumn>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_TABLEID,
439                 finderArgs, this);
440 
441         if (list == null) {
442             Session session = null;
443 
444             try {
445                 session = openSession();
446 
447                 StringBundler query = null;
448 
449                 if (obc != null) {
450                     query = new StringBundler(3 +
451                             (obc.getOrderByFields().length * 3));
452                 }
453                 else {
454                     query = new StringBundler(3);
455                 }
456 
457                 query.append(_SQL_SELECT_EXPANDOCOLUMN_WHERE);
458 
459                 query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
460 
461                 if (obc != null) {
462                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
463                 }
464 
465                 else {
466                     query.append(ExpandoColumnModelImpl.ORDER_BY_JPQL);
467                 }
468 
469                 String sql = query.toString();
470 
471                 Query q = session.createQuery(sql);
472 
473                 QueryPos qPos = QueryPos.getInstance(q);
474 
475                 qPos.add(tableId);
476 
477                 list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
478                         start, end);
479             }
480             catch (Exception e) {
481                 throw processException(e);
482             }
483             finally {
484                 if (list == null) {
485                     list = new ArrayList<ExpandoColumn>();
486                 }
487 
488                 cacheResult(list);
489 
490                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_TABLEID,
491                     finderArgs, list);
492 
493                 closeSession(session);
494             }
495         }
496 
497         return list;
498     }
499 
500     public ExpandoColumn findByTableId_First(long tableId, OrderByComparator obc)
501         throws NoSuchColumnException, SystemException {
502         List<ExpandoColumn> list = findByTableId(tableId, 0, 1, obc);
503 
504         if (list.isEmpty()) {
505             StringBundler msg = new StringBundler(4);
506 
507             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
508 
509             msg.append("tableId=");
510             msg.append(tableId);
511 
512             msg.append(StringPool.CLOSE_CURLY_BRACE);
513 
514             throw new NoSuchColumnException(msg.toString());
515         }
516         else {
517             return list.get(0);
518         }
519     }
520 
521     public ExpandoColumn findByTableId_Last(long tableId, OrderByComparator obc)
522         throws NoSuchColumnException, SystemException {
523         int count = countByTableId(tableId);
524 
525         List<ExpandoColumn> list = findByTableId(tableId, count - 1, count, obc);
526 
527         if (list.isEmpty()) {
528             StringBundler msg = new StringBundler(4);
529 
530             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
531 
532             msg.append("tableId=");
533             msg.append(tableId);
534 
535             msg.append(StringPool.CLOSE_CURLY_BRACE);
536 
537             throw new NoSuchColumnException(msg.toString());
538         }
539         else {
540             return list.get(0);
541         }
542     }
543 
544     public ExpandoColumn[] findByTableId_PrevAndNext(long columnId,
545         long tableId, OrderByComparator obc)
546         throws NoSuchColumnException, SystemException {
547         ExpandoColumn expandoColumn = findByPrimaryKey(columnId);
548 
549         int count = countByTableId(tableId);
550 
551         Session session = null;
552 
553         try {
554             session = openSession();
555 
556             StringBundler query = null;
557 
558             if (obc != null) {
559                 query = new StringBundler(3 +
560                         (obc.getOrderByFields().length * 3));
561             }
562             else {
563                 query = new StringBundler(3);
564             }
565 
566             query.append(_SQL_SELECT_EXPANDOCOLUMN_WHERE);
567 
568             query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
569 
570             if (obc != null) {
571                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
572             }
573 
574             else {
575                 query.append(ExpandoColumnModelImpl.ORDER_BY_JPQL);
576             }
577 
578             String sql = query.toString();
579 
580             Query q = session.createQuery(sql);
581 
582             QueryPos qPos = QueryPos.getInstance(q);
583 
584             qPos.add(tableId);
585 
586             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
587                     expandoColumn);
588 
589             ExpandoColumn[] array = new ExpandoColumnImpl[3];
590 
591             array[0] = (ExpandoColumn)objArray[0];
592             array[1] = (ExpandoColumn)objArray[1];
593             array[2] = (ExpandoColumn)objArray[2];
594 
595             return array;
596         }
597         catch (Exception e) {
598             throw processException(e);
599         }
600         finally {
601             closeSession(session);
602         }
603     }
604 
605     public ExpandoColumn findByT_N(long tableId, String name)
606         throws NoSuchColumnException, SystemException {
607         ExpandoColumn expandoColumn = fetchByT_N(tableId, name);
608 
609         if (expandoColumn == null) {
610             StringBundler msg = new StringBundler(6);
611 
612             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
613 
614             msg.append("tableId=");
615             msg.append(tableId);
616 
617             msg.append(", name=");
618             msg.append(name);
619 
620             msg.append(StringPool.CLOSE_CURLY_BRACE);
621 
622             if (_log.isWarnEnabled()) {
623                 _log.warn(msg.toString());
624             }
625 
626             throw new NoSuchColumnException(msg.toString());
627         }
628 
629         return expandoColumn;
630     }
631 
632     public ExpandoColumn fetchByT_N(long tableId, String name)
633         throws SystemException {
634         return fetchByT_N(tableId, name, true);
635     }
636 
637     public ExpandoColumn fetchByT_N(long tableId, String name,
638         boolean retrieveFromCache) throws SystemException {
639         Object[] finderArgs = new Object[] { new Long(tableId), name };
640 
641         Object result = null;
642 
643         if (retrieveFromCache) {
644             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_T_N,
645                     finderArgs, this);
646         }
647 
648         if (result == null) {
649             Session session = null;
650 
651             try {
652                 session = openSession();
653 
654                 StringBundler query = new StringBundler(4);
655 
656                 query.append(_SQL_SELECT_EXPANDOCOLUMN_WHERE);
657 
658                 query.append(_FINDER_COLUMN_T_N_TABLEID_2);
659 
660                 if (name == null) {
661                     query.append(_FINDER_COLUMN_T_N_NAME_1);
662                 }
663                 else {
664                     if (name.equals(StringPool.BLANK)) {
665                         query.append(_FINDER_COLUMN_T_N_NAME_3);
666                     }
667                     else {
668                         query.append(_FINDER_COLUMN_T_N_NAME_2);
669                     }
670                 }
671 
672                 query.append(ExpandoColumnModelImpl.ORDER_BY_JPQL);
673 
674                 String sql = query.toString();
675 
676                 Query q = session.createQuery(sql);
677 
678                 QueryPos qPos = QueryPos.getInstance(q);
679 
680                 qPos.add(tableId);
681 
682                 if (name != null) {
683                     qPos.add(name);
684                 }
685 
686                 List<ExpandoColumn> list = q.list();
687 
688                 result = list;
689 
690                 ExpandoColumn expandoColumn = null;
691 
692                 if (list.isEmpty()) {
693                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
694                         finderArgs, list);
695                 }
696                 else {
697                     expandoColumn = list.get(0);
698 
699                     cacheResult(expandoColumn);
700 
701                     if ((expandoColumn.getTableId() != tableId) ||
702                             (expandoColumn.getName() == null) ||
703                             !expandoColumn.getName().equals(name)) {
704                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
705                             finderArgs, expandoColumn);
706                     }
707                 }
708 
709                 return expandoColumn;
710             }
711             catch (Exception e) {
712                 throw processException(e);
713             }
714             finally {
715                 if (result == null) {
716                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
717                         finderArgs, new ArrayList<ExpandoColumn>());
718                 }
719 
720                 closeSession(session);
721             }
722         }
723         else {
724             if (result instanceof List<?>) {
725                 return null;
726             }
727             else {
728                 return (ExpandoColumn)result;
729             }
730         }
731     }
732 
733     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
734         throws SystemException {
735         Session session = null;
736 
737         try {
738             session = openSession();
739 
740             dynamicQuery.compile(session);
741 
742             return dynamicQuery.list();
743         }
744         catch (Exception e) {
745             throw processException(e);
746         }
747         finally {
748             closeSession(session);
749         }
750     }
751 
752     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
753         int start, int end) throws SystemException {
754         Session session = null;
755 
756         try {
757             session = openSession();
758 
759             dynamicQuery.setLimit(start, end);
760 
761             dynamicQuery.compile(session);
762 
763             return dynamicQuery.list();
764         }
765         catch (Exception e) {
766             throw processException(e);
767         }
768         finally {
769             closeSession(session);
770         }
771     }
772 
773     public List<ExpandoColumn> findAll() throws SystemException {
774         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
775     }
776 
777     public List<ExpandoColumn> findAll(int start, int end)
778         throws SystemException {
779         return findAll(start, end, null);
780     }
781 
782     public List<ExpandoColumn> findAll(int start, int end, OrderByComparator obc)
783         throws SystemException {
784         Object[] finderArgs = new Object[] {
785                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
786             };
787 
788         List<ExpandoColumn> list = (List<ExpandoColumn>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
789                 finderArgs, this);
790 
791         if (list == null) {
792             Session session = null;
793 
794             try {
795                 session = openSession();
796 
797                 StringBundler query = null;
798                 String sql = null;
799 
800                 if (obc != null) {
801                     query = new StringBundler(2 +
802                             (obc.getOrderByFields().length * 3));
803 
804                     query.append(_SQL_SELECT_EXPANDOCOLUMN);
805 
806                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
807 
808                     sql = query.toString();
809                 }
810 
811                 else {
812                     sql = _SQL_SELECT_EXPANDOCOLUMN.concat(ExpandoColumnModelImpl.ORDER_BY_JPQL);
813                 }
814 
815                 Query q = session.createQuery(sql);
816 
817                 if (obc == null) {
818                     list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
819                             start, end, false);
820 
821                     Collections.sort(list);
822                 }
823                 else {
824                     list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
825                             start, end);
826                 }
827             }
828             catch (Exception e) {
829                 throw processException(e);
830             }
831             finally {
832                 if (list == null) {
833                     list = new ArrayList<ExpandoColumn>();
834                 }
835 
836                 cacheResult(list);
837 
838                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
839 
840                 closeSession(session);
841             }
842         }
843 
844         return list;
845     }
846 
847     public void removeByTableId(long tableId) throws SystemException {
848         for (ExpandoColumn expandoColumn : findByTableId(tableId)) {
849             remove(expandoColumn);
850         }
851     }
852 
853     public void removeByT_N(long tableId, String name)
854         throws NoSuchColumnException, SystemException {
855         ExpandoColumn expandoColumn = findByT_N(tableId, name);
856 
857         remove(expandoColumn);
858     }
859 
860     public void removeAll() throws SystemException {
861         for (ExpandoColumn expandoColumn : findAll()) {
862             remove(expandoColumn);
863         }
864     }
865 
866     public int countByTableId(long tableId) throws SystemException {
867         Object[] finderArgs = new Object[] { new Long(tableId) };
868 
869         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_TABLEID,
870                 finderArgs, this);
871 
872         if (count == null) {
873             Session session = null;
874 
875             try {
876                 session = openSession();
877 
878                 StringBundler query = new StringBundler(2);
879 
880                 query.append(_SQL_COUNT_EXPANDOCOLUMN_WHERE);
881 
882                 query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
883 
884                 String sql = query.toString();
885 
886                 Query q = session.createQuery(sql);
887 
888                 QueryPos qPos = QueryPos.getInstance(q);
889 
890                 qPos.add(tableId);
891 
892                 count = (Long)q.uniqueResult();
893             }
894             catch (Exception e) {
895                 throw processException(e);
896             }
897             finally {
898                 if (count == null) {
899                     count = Long.valueOf(0);
900                 }
901 
902                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_TABLEID,
903                     finderArgs, count);
904 
905                 closeSession(session);
906             }
907         }
908 
909         return count.intValue();
910     }
911 
912     public int countByT_N(long tableId, String name) throws SystemException {
913         Object[] finderArgs = new Object[] { new Long(tableId), name };
914 
915         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_T_N,
916                 finderArgs, this);
917 
918         if (count == null) {
919             Session session = null;
920 
921             try {
922                 session = openSession();
923 
924                 StringBundler query = new StringBundler(3);
925 
926                 query.append(_SQL_COUNT_EXPANDOCOLUMN_WHERE);
927 
928                 query.append(_FINDER_COLUMN_T_N_TABLEID_2);
929 
930                 if (name == null) {
931                     query.append(_FINDER_COLUMN_T_N_NAME_1);
932                 }
933                 else {
934                     if (name.equals(StringPool.BLANK)) {
935                         query.append(_FINDER_COLUMN_T_N_NAME_3);
936                     }
937                     else {
938                         query.append(_FINDER_COLUMN_T_N_NAME_2);
939                     }
940                 }
941 
942                 String sql = query.toString();
943 
944                 Query q = session.createQuery(sql);
945 
946                 QueryPos qPos = QueryPos.getInstance(q);
947 
948                 qPos.add(tableId);
949 
950                 if (name != null) {
951                     qPos.add(name);
952                 }
953 
954                 count = (Long)q.uniqueResult();
955             }
956             catch (Exception e) {
957                 throw processException(e);
958             }
959             finally {
960                 if (count == null) {
961                     count = Long.valueOf(0);
962                 }
963 
964                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_N, finderArgs,
965                     count);
966 
967                 closeSession(session);
968             }
969         }
970 
971         return count.intValue();
972     }
973 
974     public int countAll() throws SystemException {
975         Object[] finderArgs = new Object[0];
976 
977         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
978                 finderArgs, this);
979 
980         if (count == null) {
981             Session session = null;
982 
983             try {
984                 session = openSession();
985 
986                 Query q = session.createQuery(_SQL_COUNT_EXPANDOCOLUMN);
987 
988                 count = (Long)q.uniqueResult();
989             }
990             catch (Exception e) {
991                 throw processException(e);
992             }
993             finally {
994                 if (count == null) {
995                     count = Long.valueOf(0);
996                 }
997 
998                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
999                     count);
1000
1001                closeSession(session);
1002            }
1003        }
1004
1005        return count.intValue();
1006    }
1007
1008    public void afterPropertiesSet() {
1009        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1010                    com.liferay.portal.util.PropsUtil.get(
1011                        "value.object.listener.com.liferay.portlet.expando.model.ExpandoColumn")));
1012
1013        if (listenerClassNames.length > 0) {
1014            try {
1015                List<ModelListener<ExpandoColumn>> listenersList = new ArrayList<ModelListener<ExpandoColumn>>();
1016
1017                for (String listenerClassName : listenerClassNames) {
1018                    listenersList.add((ModelListener<ExpandoColumn>)Class.forName(
1019                            listenerClassName).newInstance());
1020                }
1021
1022                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1023            }
1024            catch (Exception e) {
1025                _log.error(e);
1026            }
1027        }
1028    }
1029
1030    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoColumnPersistence")
1031    protected com.liferay.portlet.expando.service.persistence.ExpandoColumnPersistence expandoColumnPersistence;
1032    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoRowPersistence")
1033    protected com.liferay.portlet.expando.service.persistence.ExpandoRowPersistence expandoRowPersistence;
1034    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoTablePersistence")
1035    protected com.liferay.portlet.expando.service.persistence.ExpandoTablePersistence expandoTablePersistence;
1036    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoValuePersistence")
1037    protected com.liferay.portlet.expando.service.persistence.ExpandoValuePersistence expandoValuePersistence;
1038    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence")
1039    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1040    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence")
1041    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1042    private static final String _SQL_SELECT_EXPANDOCOLUMN = "SELECT expandoColumn FROM ExpandoColumn expandoColumn";
1043    private static final String _SQL_SELECT_EXPANDOCOLUMN_WHERE = "SELECT expandoColumn FROM ExpandoColumn expandoColumn WHERE ";
1044    private static final String _SQL_COUNT_EXPANDOCOLUMN = "SELECT COUNT(expandoColumn) FROM ExpandoColumn expandoColumn";
1045    private static final String _SQL_COUNT_EXPANDOCOLUMN_WHERE = "SELECT COUNT(expandoColumn) FROM ExpandoColumn expandoColumn WHERE ";
1046    private static final String _FINDER_COLUMN_TABLEID_TABLEID_2 = "expandoColumn.tableId = ?";
1047    private static final String _FINDER_COLUMN_T_N_TABLEID_2 = "expandoColumn.tableId = ? AND ";
1048    private static final String _FINDER_COLUMN_T_N_NAME_1 = "expandoColumn.name IS NULL";
1049    private static final String _FINDER_COLUMN_T_N_NAME_2 = "expandoColumn.name = ?";
1050    private static final String _FINDER_COLUMN_T_N_NAME_3 = "(expandoColumn.name IS NULL OR expandoColumn.name = ?)";
1051    private static final String _ORDER_BY_ENTITY_ALIAS = "expandoColumn.";
1052    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ExpandoColumn exists with the primary key ";
1053    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ExpandoColumn exists with the key {";
1054    private static Log _log = LogFactoryUtil.getLog(ExpandoColumnPersistenceImpl.class);
1055}