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