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