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