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