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