1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.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.append("ORDER BY ");
409 
410                 query.append("expandoColumn.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                     "SELECT expandoColumn FROM ExpandoColumn expandoColumn WHERE ");
466 
467                 query.append("expandoColumn.tableId = ?");
468 
469                 query.append(" ");
470 
471                 if (obc != null) {
472                     query.append("ORDER BY ");
473 
474                     String[] orderByFields = obc.getOrderByFields();
475 
476                     for (int i = 0; i < orderByFields.length; i++) {
477                         query.append("expandoColumn.");
478                         query.append(orderByFields[i]);
479 
480                         if (obc.isAscending()) {
481                             query.append(" ASC");
482                         }
483                         else {
484                             query.append(" DESC");
485                         }
486 
487                         if ((i + 1) < orderByFields.length) {
488                             query.append(", ");
489                         }
490                     }
491                 }
492 
493                 else {
494                     query.append("ORDER BY ");
495 
496                     query.append("expandoColumn.name ASC");
497                 }
498 
499                 Query q = session.createQuery(query.toString());
500 
501                 QueryPos qPos = QueryPos.getInstance(q);
502 
503                 qPos.add(tableId);
504 
505                 list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
506                         start, end);
507             }
508             catch (Exception e) {
509                 throw processException(e);
510             }
511             finally {
512                 if (list == null) {
513                     list = new ArrayList<ExpandoColumn>();
514                 }
515 
516                 cacheResult(list);
517 
518                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_TABLEID,
519                     finderArgs, list);
520 
521                 closeSession(session);
522             }
523         }
524 
525         return list;
526     }
527 
528     public ExpandoColumn findByTableId_First(long tableId, OrderByComparator obc)
529         throws NoSuchColumnException, SystemException {
530         List<ExpandoColumn> list = findByTableId(tableId, 0, 1, obc);
531 
532         if (list.isEmpty()) {
533             StringBuilder msg = new StringBuilder();
534 
535             msg.append("No ExpandoColumn exists with the key {");
536 
537             msg.append("tableId=" + tableId);
538 
539             msg.append(StringPool.CLOSE_CURLY_BRACE);
540 
541             throw new NoSuchColumnException(msg.toString());
542         }
543         else {
544             return list.get(0);
545         }
546     }
547 
548     public ExpandoColumn findByTableId_Last(long tableId, OrderByComparator obc)
549         throws NoSuchColumnException, SystemException {
550         int count = countByTableId(tableId);
551 
552         List<ExpandoColumn> list = findByTableId(tableId, count - 1, count, obc);
553 
554         if (list.isEmpty()) {
555             StringBuilder msg = new StringBuilder();
556 
557             msg.append("No ExpandoColumn exists with the key {");
558 
559             msg.append("tableId=" + tableId);
560 
561             msg.append(StringPool.CLOSE_CURLY_BRACE);
562 
563             throw new NoSuchColumnException(msg.toString());
564         }
565         else {
566             return list.get(0);
567         }
568     }
569 
570     public ExpandoColumn[] findByTableId_PrevAndNext(long columnId,
571         long tableId, OrderByComparator obc)
572         throws NoSuchColumnException, SystemException {
573         ExpandoColumn expandoColumn = findByPrimaryKey(columnId);
574 
575         int count = countByTableId(tableId);
576 
577         Session session = null;
578 
579         try {
580             session = openSession();
581 
582             StringBuilder query = new StringBuilder();
583 
584             query.append(
585                 "SELECT expandoColumn FROM ExpandoColumn expandoColumn WHERE ");
586 
587             query.append("expandoColumn.tableId = ?");
588 
589             query.append(" ");
590 
591             if (obc != null) {
592                 query.append("ORDER BY ");
593 
594                 String[] orderByFields = obc.getOrderByFields();
595 
596                 for (int i = 0; i < orderByFields.length; i++) {
597                     query.append("expandoColumn.");
598                     query.append(orderByFields[i]);
599 
600                     if (obc.isAscending()) {
601                         query.append(" ASC");
602                     }
603                     else {
604                         query.append(" DESC");
605                     }
606 
607                     if ((i + 1) < orderByFields.length) {
608                         query.append(", ");
609                     }
610                 }
611             }
612 
613             else {
614                 query.append("ORDER BY ");
615 
616                 query.append("expandoColumn.name ASC");
617             }
618 
619             Query q = session.createQuery(query.toString());
620 
621             QueryPos qPos = QueryPos.getInstance(q);
622 
623             qPos.add(tableId);
624 
625             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
626                     expandoColumn);
627 
628             ExpandoColumn[] array = new ExpandoColumnImpl[3];
629 
630             array[0] = (ExpandoColumn)objArray[0];
631             array[1] = (ExpandoColumn)objArray[1];
632             array[2] = (ExpandoColumn)objArray[2];
633 
634             return array;
635         }
636         catch (Exception e) {
637             throw processException(e);
638         }
639         finally {
640             closeSession(session);
641         }
642     }
643 
644     public ExpandoColumn findByT_N(long tableId, String name)
645         throws NoSuchColumnException, SystemException {
646         ExpandoColumn expandoColumn = fetchByT_N(tableId, name);
647 
648         if (expandoColumn == null) {
649             StringBuilder msg = new StringBuilder();
650 
651             msg.append("No ExpandoColumn exists with the key {");
652 
653             msg.append("tableId=" + tableId);
654 
655             msg.append(", ");
656             msg.append("name=" + name);
657 
658             msg.append(StringPool.CLOSE_CURLY_BRACE);
659 
660             if (_log.isWarnEnabled()) {
661                 _log.warn(msg.toString());
662             }
663 
664             throw new NoSuchColumnException(msg.toString());
665         }
666 
667         return expandoColumn;
668     }
669 
670     public ExpandoColumn fetchByT_N(long tableId, String name)
671         throws SystemException {
672         return fetchByT_N(tableId, name, true);
673     }
674 
675     public ExpandoColumn fetchByT_N(long tableId, String name,
676         boolean retrieveFromCache) throws SystemException {
677         Object[] finderArgs = new Object[] { new Long(tableId), name };
678 
679         Object result = null;
680 
681         if (retrieveFromCache) {
682             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_T_N,
683                     finderArgs, this);
684         }
685 
686         if (result == null) {
687             Session session = null;
688 
689             try {
690                 session = openSession();
691 
692                 StringBuilder query = new StringBuilder();
693 
694                 query.append(
695                     "SELECT expandoColumn FROM ExpandoColumn expandoColumn WHERE ");
696 
697                 query.append("expandoColumn.tableId = ?");
698 
699                 query.append(" AND ");
700 
701                 if (name == null) {
702                     query.append("expandoColumn.name IS NULL");
703                 }
704                 else {
705                     query.append("expandoColumn.name = ?");
706                 }
707 
708                 query.append(" ");
709 
710                 query.append("ORDER BY ");
711 
712                 query.append("expandoColumn.name ASC");
713 
714                 Query q = session.createQuery(query.toString());
715 
716                 QueryPos qPos = QueryPos.getInstance(q);
717 
718                 qPos.add(tableId);
719 
720                 if (name != null) {
721                     qPos.add(name);
722                 }
723 
724                 List<ExpandoColumn> list = q.list();
725 
726                 result = list;
727 
728                 ExpandoColumn expandoColumn = null;
729 
730                 if (list.isEmpty()) {
731                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
732                         finderArgs, list);
733                 }
734                 else {
735                     expandoColumn = list.get(0);
736 
737                     cacheResult(expandoColumn);
738 
739                     if ((expandoColumn.getTableId() != tableId) ||
740                             (expandoColumn.getName() == null) ||
741                             !expandoColumn.getName().equals(name)) {
742                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
743                             finderArgs, expandoColumn);
744                     }
745                 }
746 
747                 return expandoColumn;
748             }
749             catch (Exception e) {
750                 throw processException(e);
751             }
752             finally {
753                 if (result == null) {
754                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_N,
755                         finderArgs, new ArrayList<ExpandoColumn>());
756                 }
757 
758                 closeSession(session);
759             }
760         }
761         else {
762             if (result instanceof List) {
763                 return null;
764             }
765             else {
766                 return (ExpandoColumn)result;
767             }
768         }
769     }
770 
771     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
772         throws SystemException {
773         Session session = null;
774 
775         try {
776             session = openSession();
777 
778             dynamicQuery.compile(session);
779 
780             return dynamicQuery.list();
781         }
782         catch (Exception e) {
783             throw processException(e);
784         }
785         finally {
786             closeSession(session);
787         }
788     }
789 
790     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
791         int start, int end) throws SystemException {
792         Session session = null;
793 
794         try {
795             session = openSession();
796 
797             dynamicQuery.setLimit(start, end);
798 
799             dynamicQuery.compile(session);
800 
801             return dynamicQuery.list();
802         }
803         catch (Exception e) {
804             throw processException(e);
805         }
806         finally {
807             closeSession(session);
808         }
809     }
810 
811     public List<ExpandoColumn> findAll() throws SystemException {
812         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
813     }
814 
815     public List<ExpandoColumn> findAll(int start, int end)
816         throws SystemException {
817         return findAll(start, end, null);
818     }
819 
820     public List<ExpandoColumn> findAll(int start, int end, OrderByComparator obc)
821         throws SystemException {
822         Object[] finderArgs = new Object[] {
823                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
824             };
825 
826         List<ExpandoColumn> list = (List<ExpandoColumn>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
827                 finderArgs, this);
828 
829         if (list == null) {
830             Session session = null;
831 
832             try {
833                 session = openSession();
834 
835                 StringBuilder query = new StringBuilder();
836 
837                 query.append(
838                     "SELECT expandoColumn FROM ExpandoColumn expandoColumn ");
839 
840                 if (obc != null) {
841                     query.append("ORDER BY ");
842 
843                     String[] orderByFields = obc.getOrderByFields();
844 
845                     for (int i = 0; i < orderByFields.length; i++) {
846                         query.append("expandoColumn.");
847                         query.append(orderByFields[i]);
848 
849                         if (obc.isAscending()) {
850                             query.append(" ASC");
851                         }
852                         else {
853                             query.append(" DESC");
854                         }
855 
856                         if ((i + 1) < orderByFields.length) {
857                             query.append(", ");
858                         }
859                     }
860                 }
861 
862                 else {
863                     query.append("ORDER BY ");
864 
865                     query.append("expandoColumn.name ASC");
866                 }
867 
868                 Query q = session.createQuery(query.toString());
869 
870                 if (obc == null) {
871                     list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
872                             start, end, false);
873 
874                     Collections.sort(list);
875                 }
876                 else {
877                     list = (List<ExpandoColumn>)QueryUtil.list(q, getDialect(),
878                             start, end);
879                 }
880             }
881             catch (Exception e) {
882                 throw processException(e);
883             }
884             finally {
885                 if (list == null) {
886                     list = new ArrayList<ExpandoColumn>();
887                 }
888 
889                 cacheResult(list);
890 
891                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
892 
893                 closeSession(session);
894             }
895         }
896 
897         return list;
898     }
899 
900     public void removeByTableId(long tableId) throws SystemException {
901         for (ExpandoColumn expandoColumn : findByTableId(tableId)) {
902             remove(expandoColumn);
903         }
904     }
905 
906     public void removeByT_N(long tableId, String name)
907         throws NoSuchColumnException, SystemException {
908         ExpandoColumn expandoColumn = findByT_N(tableId, name);
909 
910         remove(expandoColumn);
911     }
912 
913     public void removeAll() throws SystemException {
914         for (ExpandoColumn expandoColumn : findAll()) {
915             remove(expandoColumn);
916         }
917     }
918 
919     public int countByTableId(long tableId) throws SystemException {
920         Object[] finderArgs = new Object[] { new Long(tableId) };
921 
922         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_TABLEID,
923                 finderArgs, this);
924 
925         if (count == null) {
926             Session session = null;
927 
928             try {
929                 session = openSession();
930 
931                 StringBuilder query = new StringBuilder();
932 
933                 query.append("SELECT COUNT(expandoColumn) ");
934                 query.append("FROM ExpandoColumn expandoColumn WHERE ");
935 
936                 query.append("expandoColumn.tableId = ?");
937 
938                 query.append(" ");
939 
940                 Query q = session.createQuery(query.toString());
941 
942                 QueryPos qPos = QueryPos.getInstance(q);
943 
944                 qPos.add(tableId);
945 
946                 count = (Long)q.uniqueResult();
947             }
948             catch (Exception e) {
949                 throw processException(e);
950             }
951             finally {
952                 if (count == null) {
953                     count = Long.valueOf(0);
954                 }
955 
956                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_TABLEID,
957                     finderArgs, count);
958 
959                 closeSession(session);
960             }
961         }
962 
963         return count.intValue();
964     }
965 
966     public int countByT_N(long tableId, String name) throws SystemException {
967         Object[] finderArgs = new Object[] { new Long(tableId), name };
968 
969         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_T_N,
970                 finderArgs, this);
971 
972         if (count == null) {
973             Session session = null;
974 
975             try {
976                 session = openSession();
977 
978                 StringBuilder query = new StringBuilder();
979 
980                 query.append("SELECT COUNT(expandoColumn) ");
981                 query.append("FROM ExpandoColumn expandoColumn WHERE ");
982 
983                 query.append("expandoColumn.tableId = ?");
984 
985                 query.append(" AND ");
986 
987                 if (name == null) {
988                     query.append("expandoColumn.name IS NULL");
989                 }
990                 else {
991                     query.append("expandoColumn.name = ?");
992                 }
993 
994                 query.append(" ");
995 
996                 Query q = session.createQuery(query.toString());
997 
998                 QueryPos qPos = QueryPos.getInstance(q);
999 
1000                qPos.add(tableId);
1001
1002                if (name != null) {
1003                    qPos.add(name);
1004                }
1005
1006                count = (Long)q.uniqueResult();
1007            }
1008            catch (Exception e) {
1009                throw processException(e);
1010            }
1011            finally {
1012                if (count == null) {
1013                    count = Long.valueOf(0);
1014                }
1015
1016                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_N, finderArgs,
1017                    count);
1018
1019                closeSession(session);
1020            }
1021        }
1022
1023        return count.intValue();
1024    }
1025
1026    public int countAll() throws SystemException {
1027        Object[] finderArgs = new Object[0];
1028
1029        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1030                finderArgs, this);
1031
1032        if (count == null) {
1033            Session session = null;
1034
1035            try {
1036                session = openSession();
1037
1038                Query q = session.createQuery(
1039                        "SELECT COUNT(expandoColumn) FROM ExpandoColumn expandoColumn");
1040
1041                count = (Long)q.uniqueResult();
1042            }
1043            catch (Exception e) {
1044                throw processException(e);
1045            }
1046            finally {
1047                if (count == null) {
1048                    count = Long.valueOf(0);
1049                }
1050
1051                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1052                    count);
1053
1054                closeSession(session);
1055            }
1056        }
1057
1058        return count.intValue();
1059    }
1060
1061    public void afterPropertiesSet() {
1062        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1063                    com.liferay.portal.util.PropsUtil.get(
1064                        "value.object.listener.com.liferay.portlet.expando.model.ExpandoColumn")));
1065
1066        if (listenerClassNames.length > 0) {
1067            try {
1068                List<ModelListener<ExpandoColumn>> listenersList = new ArrayList<ModelListener<ExpandoColumn>>();
1069
1070                for (String listenerClassName : listenerClassNames) {
1071                    listenersList.add((ModelListener<ExpandoColumn>)Class.forName(
1072                            listenerClassName).newInstance());
1073                }
1074
1075                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1076            }
1077            catch (Exception e) {
1078                _log.error(e);
1079            }
1080        }
1081    }
1082
1083    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoColumnPersistence.impl")
1084    protected com.liferay.portlet.expando.service.persistence.ExpandoColumnPersistence expandoColumnPersistence;
1085    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoRowPersistence.impl")
1086    protected com.liferay.portlet.expando.service.persistence.ExpandoRowPersistence expandoRowPersistence;
1087    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoTablePersistence.impl")
1088    protected com.liferay.portlet.expando.service.persistence.ExpandoTablePersistence expandoTablePersistence;
1089    @BeanReference(name = "com.liferay.portlet.expando.service.persistence.ExpandoValuePersistence.impl")
1090    protected com.liferay.portlet.expando.service.persistence.ExpandoValuePersistence expandoValuePersistence;
1091    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1092    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1093    private static Log _log = LogFactoryUtil.getLog(ExpandoColumnPersistenceImpl.class);
1094}