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                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
403 
404                 query.append("tableId = ?");
405 
406                 query.append(" ");
407 
408                 query.append("ORDER BY ");
409 
410                 query.append("name ASC");
411 
412                 Query q = session.createQuery(query.toString());
413 
414                 QueryPos qPos = QueryPos.getInstance(q);
415 
416                 qPos.add(tableId);
417 
418                 list = q.list();
419             }
420             catch (Exception e) {
421                 throw processException(e);
422             }
423             finally {
424                 if (list == null) {
425                     list = new ArrayList<ExpandoColumn>();
426                 }
427 
428                 cacheResult(list);
429 
430                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_TABLEID,
431                     finderArgs, list);
432 
433                 closeSession(session);
434             }
435         }
436 
437         return list;
438     }
439 
440     public List<ExpandoColumn> findByTableId(long tableId, int start, int end)
441         throws SystemException {
442         return findByTableId(tableId, start, end, null);
443     }
444 
445     public List<ExpandoColumn> findByTableId(long tableId, int start, int end,
446         OrderByComparator obc) throws SystemException {
447         Object[] finderArgs = new Object[] {
448                 new Long(tableId),
449                 
450                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
451             };
452 
453         List<ExpandoColumn> list = (List<ExpandoColumn>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_TABLEID,
454                 finderArgs, this);
455 
456         if (list == null) {
457             Session session = null;
458 
459             try {
460                 session = openSession();
461 
462                 StringBuilder query = new StringBuilder();
463 
464                 query.append(
465                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
466 
467                 query.append("tableId = ?");
468 
469                 query.append(" ");
470 
471                 if (obc != null) {
472                     query.append("ORDER BY ");
473                     query.append(obc.getOrderBy());
474                 }
475 
476                 else {
477                     query.append("ORDER BY ");
478 
479                     query.append("name ASC");
480                 }
481 
482                 Query q = session.createQuery(query.toString());
483 
484                 QueryPos qPos = QueryPos.getInstance(q);
485 
486                 qPos.add(tableId);
487 
488                 list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
489                         start, end);
490             }
491             catch (Exception e) {
492                 throw processException(e);
493             }
494             finally {
495                 if (list == null) {
496                     list = new ArrayList<ExpandoColumn>();
497                 }
498 
499                 cacheResult(list);
500 
501                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_TABLEID,
502                     finderArgs, list);
503 
504                 closeSession(session);
505             }
506         }
507 
508         return list;
509     }
510 
511     public ExpandoColumn findByTableId_First(long tableId, OrderByComparator obc)
512         throws NoSuchColumnException, SystemException {
513         List<ExpandoColumn> list = findByTableId(tableId, 0, 1, obc);
514 
515         if (list.isEmpty()) {
516             StringBuilder msg = new StringBuilder();
517 
518             msg.append("No ExpandoColumn exists with the key {");
519 
520             msg.append("tableId=" + tableId);
521 
522             msg.append(StringPool.CLOSE_CURLY_BRACE);
523 
524             throw new NoSuchColumnException(msg.toString());
525         }
526         else {
527             return list.get(0);
528         }
529     }
530 
531     public ExpandoColumn findByTableId_Last(long tableId, OrderByComparator obc)
532         throws NoSuchColumnException, SystemException {
533         int count = countByTableId(tableId);
534 
535         List<ExpandoColumn> list = findByTableId(tableId, count - 1, count, obc);
536 
537         if (list.isEmpty()) {
538             StringBuilder msg = new StringBuilder();
539 
540             msg.append("No ExpandoColumn exists with the key {");
541 
542             msg.append("tableId=" + tableId);
543 
544             msg.append(StringPool.CLOSE_CURLY_BRACE);
545 
546             throw new NoSuchColumnException(msg.toString());
547         }
548         else {
549             return list.get(0);
550         }
551     }
552 
553     public ExpandoColumn[] findByTableId_PrevAndNext(long columnId,
554         long tableId, OrderByComparator obc)
555         throws NoSuchColumnException, SystemException {
556         ExpandoColumn expandoColumn = findByPrimaryKey(columnId);
557 
558         int count = countByTableId(tableId);
559 
560         Session session = null;
561 
562         try {
563             session = openSession();
564 
565             StringBuilder query = new StringBuilder();
566 
567             query.append(
568                 "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
569 
570             query.append("tableId = ?");
571 
572             query.append(" ");
573 
574             if (obc != null) {
575                 query.append("ORDER BY ");
576                 query.append(obc.getOrderBy());
577             }
578 
579             else {
580                 query.append("ORDER BY ");
581 
582                 query.append("name ASC");
583             }
584 
585             Query q = session.createQuery(query.toString());
586 
587             QueryPos qPos = QueryPos.getInstance(q);
588 
589             qPos.add(tableId);
590 
591             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
592                     expandoColumn);
593 
594             ExpandoColumn[] array = new ExpandoColumnImpl[3];
595 
596             array[0] = (ExpandoColumn)objArray[0];
597             array[1] = (ExpandoColumn)objArray[1];
598             array[2] = (ExpandoColumn)objArray[2];
599 
600             return array;
601         }
602         catch (Exception e) {
603             throw processException(e);
604         }
605         finally {
606             closeSession(session);
607         }
608     }
609 
610     public ExpandoColumn findByT_N(long tableId, String name)
611         throws NoSuchColumnException, SystemException {
612         ExpandoColumn expandoColumn = fetchByT_N(tableId, name);
613 
614         if (expandoColumn == null) {
615             StringBuilder msg = new StringBuilder();
616 
617             msg.append("No ExpandoColumn exists with the key {");
618 
619             msg.append("tableId=" + tableId);
620 
621             msg.append(", ");
622             msg.append("name=" + name);
623 
624             msg.append(StringPool.CLOSE_CURLY_BRACE);
625 
626             if (_log.isWarnEnabled()) {
627                 _log.warn(msg.toString());
628             }
629 
630             throw new NoSuchColumnException(msg.toString());
631         }
632 
633         return expandoColumn;
634     }
635 
636     public ExpandoColumn fetchByT_N(long tableId, String name)
637         throws SystemException {
638         return fetchByT_N(tableId, name, true);
639     }
640 
641     public ExpandoColumn fetchByT_N(long tableId, String name,
642         boolean retrieveFromCache) throws SystemException {
643         Object[] finderArgs = new Object[] { new Long(tableId), name };
644 
645         Object result = null;
646 
647         if (retrieveFromCache) {
648             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_T_N,
649                     finderArgs, this);
650         }
651 
652         if (result == null) {
653             Session session = null;
654 
655             try {
656                 session = openSession();
657 
658                 StringBuilder query = new StringBuilder();
659 
660                 query.append(
661                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
662 
663                 query.append("tableId = ?");
664 
665                 query.append(" AND ");
666 
667                 if (name == null) {
668                     query.append("name IS NULL");
669                 }
670                 else {
671                     query.append("name = ?");
672                 }
673 
674                 query.append(" ");
675 
676                 query.append("ORDER BY ");
677 
678                 query.append("name ASC");
679 
680                 Query q = session.createQuery(query.toString());
681 
682                 QueryPos qPos = QueryPos.getInstance(q);
683 
684                 qPos.add(tableId);
685 
686                 if (name != null) {
687                     qPos.add(name);
688                 }
689 
690                 List<ExpandoColumn> list = q.list();
691 
692                 result = list;
693 
694                 ExpandoColumn expandoColumn = null;
695 
696                 if (list.isEmpty()) {
697                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
698                         finderArgs, list);
699                 }
700                 else {
701                     expandoColumn = list.get(0);
702 
703                     cacheResult(expandoColumn);
704 
705                     if ((expandoColumn.getTableId() != tableId) ||
706                             (expandoColumn.getName() == null) ||
707                             !expandoColumn.getName().equals(name)) {
708                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
709                             finderArgs, expandoColumn);
710                     }
711                 }
712 
713                 return expandoColumn;
714             }
715             catch (Exception e) {
716                 throw processException(e);
717             }
718             finally {
719                 if (result == null) {
720                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
721                         finderArgs, new ArrayList<ExpandoColumn>());
722                 }
723 
724                 closeSession(session);
725             }
726         }
727         else {
728             if (result instanceof List) {
729                 return null;
730             }
731             else {
732                 return (ExpandoColumn)result;
733             }
734         }
735     }
736 
737     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
738         throws SystemException {
739         Session session = null;
740 
741         try {
742             session = openSession();
743 
744             dynamicQuery.compile(session);
745 
746             return dynamicQuery.list();
747         }
748         catch (Exception e) {
749             throw processException(e);
750         }
751         finally {
752             closeSession(session);
753         }
754     }
755 
756     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
757         int start, int end) throws SystemException {
758         Session session = null;
759 
760         try {
761             session = openSession();
762 
763             dynamicQuery.setLimit(start, end);
764 
765             dynamicQuery.compile(session);
766 
767             return dynamicQuery.list();
768         }
769         catch (Exception e) {
770             throw processException(e);
771         }
772         finally {
773             closeSession(session);
774         }
775     }
776 
777     public List<ExpandoColumn> findAll() throws SystemException {
778         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
779     }
780 
781     public List<ExpandoColumn> findAll(int start, int end)
782         throws SystemException {
783         return findAll(start, end, null);
784     }
785 
786     public List<ExpandoColumn> findAll(int start, int end, OrderByComparator obc)
787         throws SystemException {
788         Object[] finderArgs = new Object[] {
789                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
790             };
791 
792         List<ExpandoColumn> list = (List<ExpandoColumn>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
793                 finderArgs, this);
794 
795         if (list == null) {
796             Session session = null;
797 
798             try {
799                 session = openSession();
800 
801                 StringBuilder query = new StringBuilder();
802 
803                 query.append(
804                     "FROM com.liferay.portlet.expando.model.ExpandoColumn ");
805 
806                 if (obc != null) {
807                     query.append("ORDER BY ");
808                     query.append(obc.getOrderBy());
809                 }
810 
811                 else {
812                     query.append("ORDER BY ");
813 
814                     query.append("name ASC");
815                 }
816 
817                 Query q = session.createQuery(query.toString());
818 
819                 if (obc == null) {
820                     list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
821                             start, end, false);
822 
823                     Collections.sort(list);
824                 }
825                 else {
826                     list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
827                             start, end);
828                 }
829             }
830             catch (Exception e) {
831                 throw processException(e);
832             }
833             finally {
834                 if (list == null) {
835                     list = new ArrayList<ExpandoColumn>();
836                 }
837 
838                 cacheResult(list);
839 
840                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
841 
842                 closeSession(session);
843             }
844         }
845 
846         return list;
847     }
848 
849     public void removeByTableId(long tableId) throws SystemException {
850         for (ExpandoColumn expandoColumn : findByTableId(tableId)) {
851             remove(expandoColumn);
852         }
853     }
854 
855     public void removeByT_N(long tableId, String name)
856         throws NoSuchColumnException, SystemException {
857         ExpandoColumn expandoColumn = findByT_N(tableId, name);
858 
859         remove(expandoColumn);
860     }
861 
862     public void removeAll() throws SystemException {
863         for (ExpandoColumn expandoColumn : findAll()) {
864             remove(expandoColumn);
865         }
866     }
867 
868     public int countByTableId(long tableId) throws SystemException {
869         Object[] finderArgs = new Object[] { new Long(tableId) };
870 
871         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_TABLEID,
872                 finderArgs, this);
873 
874         if (count == null) {
875             Session session = null;
876 
877             try {
878                 session = openSession();
879 
880                 StringBuilder query = new StringBuilder();
881 
882                 query.append("SELECT COUNT(*) ");
883                 query.append(
884                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
885 
886                 query.append("tableId = ?");
887 
888                 query.append(" ");
889 
890                 Query q = session.createQuery(query.toString());
891 
892                 QueryPos qPos = QueryPos.getInstance(q);
893 
894                 qPos.add(tableId);
895 
896                 count = (Long)q.uniqueResult();
897             }
898             catch (Exception e) {
899                 throw processException(e);
900             }
901             finally {
902                 if (count == null) {
903                     count = Long.valueOf(0);
904                 }
905 
906                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_TABLEID,
907                     finderArgs, count);
908 
909                 closeSession(session);
910             }
911         }
912 
913         return count.intValue();
914     }
915 
916     public int countByT_N(long tableId, String name) throws SystemException {
917         Object[] finderArgs = new Object[] { new Long(tableId), name };
918 
919         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_T_N,
920                 finderArgs, this);
921 
922         if (count == null) {
923             Session session = null;
924 
925             try {
926                 session = openSession();
927 
928                 StringBuilder query = new StringBuilder();
929 
930                 query.append("SELECT COUNT(*) ");
931                 query.append(
932                     "FROM com.liferay.portlet.expando.model.ExpandoColumn WHERE ");
933 
934                 query.append("tableId = ?");
935 
936                 query.append(" AND ");
937 
938                 if (name == null) {
939                     query.append("name IS NULL");
940                 }
941                 else {
942                     query.append("name = ?");
943                 }
944 
945                 query.append(" ");
946 
947                 Query q = session.createQuery(query.toString());
948 
949                 QueryPos qPos = QueryPos.getInstance(q);
950 
951                 qPos.add(tableId);
952 
953                 if (name != null) {
954                     qPos.add(name);
955                 }
956 
957                 count = (Long)q.uniqueResult();
958             }
959             catch (Exception e) {
960                 throw processException(e);
961             }
962             finally {
963                 if (count == null) {
964                     count = Long.valueOf(0);
965                 }
966 
967                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_N, finderArgs,
968                     count);
969 
970                 closeSession(session);
971             }
972         }
973 
974         return count.intValue();
975     }
976 
977     public int countAll() throws SystemException {
978         Object[] finderArgs = new Object[0];
979 
980         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
981                 finderArgs, this);
982 
983         if (count == null) {
984             Session session = null;
985 
986             try {
987                 session = openSession();
988 
989                 Query q = session.createQuery(
990                         "SELECT COUNT(*) FROM com.liferay.portlet.expando.model.ExpandoColumn");
991 
992                 count = (Long)q.uniqueResult();
993             }
994             catch (Exception e) {
995                 throw processException(e);
996             }
997             finally {
998                 if (count == null) {
999                     count = Long.valueOf(0);
1000                }
1001
1002                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1003                    count);
1004
1005                closeSession(session);
1006            }
1007        }
1008
1009        return count.intValue();
1010    }
1011
1012    public void afterPropertiesSet() {
1013        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1014                    com.liferay.portal.util.PropsUtil.get(
1015                        "value.object.listener.com.liferay.portlet.expando.model.ExpandoColumn")));
1016
1017        if (listenerClassNames.length > 0) {
1018            try {
1019                List<ModelListener<ExpandoColumn>> listenersList = new ArrayList<ModelListener<ExpandoColumn>>();
1020
1021                for (String listenerClassName : listenerClassNames) {
1022                    listenersList.add((ModelListener<ExpandoColumn>)Class.forName(
1023                            listenerClassName).newInstance());
1024                }
1025
1026                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1027            }
1028            catch (Exception e) {
1029                _log.error(e);
1030            }
1031        }
1032    }
1033
1034    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoColumnPersistence.impl")
1035    protected com.liferay.portlet.expando.service.persistence.ExpandoColumnPersistence expandoColumnPersistence;
1036    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoRowPersistence.impl")
1037    protected com.liferay.portlet.expando.service.persistence.ExpandoRowPersistence expandoRowPersistence;
1038    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoTablePersistence.impl")
1039    protected com.liferay.portlet.expando.service.persistence.ExpandoTablePersistence expandoTablePersistence;
1040    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoValuePersistence.impl")
1041    protected com.liferay.portlet.expando.service.persistence.ExpandoValuePersistence expandoValuePersistence;
1042    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1043    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1044    private static Log _log = LogFactoryUtil.getLog(ExpandoColumnPersistenceImpl.class);
1045}