1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence;
24  
25  import com.liferay.portal.NoSuchResourceActionException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.annotation.BeanReference;
28  import com.liferay.portal.kernel.cache.CacheRegistry;
29  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
30  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
31  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
32  import com.liferay.portal.kernel.dao.orm.FinderPath;
33  import com.liferay.portal.kernel.dao.orm.Query;
34  import com.liferay.portal.kernel.dao.orm.QueryPos;
35  import com.liferay.portal.kernel.dao.orm.QueryUtil;
36  import com.liferay.portal.kernel.dao.orm.Session;
37  import com.liferay.portal.kernel.log.Log;
38  import com.liferay.portal.kernel.log.LogFactoryUtil;
39  import com.liferay.portal.kernel.util.GetterUtil;
40  import com.liferay.portal.kernel.util.OrderByComparator;
41  import com.liferay.portal.kernel.util.StringPool;
42  import com.liferay.portal.kernel.util.StringUtil;
43  import com.liferay.portal.kernel.util.Validator;
44  import com.liferay.portal.model.ModelListener;
45  import com.liferay.portal.model.ResourceAction;
46  import com.liferay.portal.model.impl.ResourceActionImpl;
47  import com.liferay.portal.model.impl.ResourceActionModelImpl;
48  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
49  
50  import java.util.ArrayList;
51  import java.util.Collections;
52  import java.util.List;
53  
54  /**
55   * <a href="ResourceActionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class ResourceActionPersistenceImpl extends BasePersistenceImpl
61      implements ResourceActionPersistence {
62      public static final String FINDER_CLASS_NAME_ENTITY = ResourceActionImpl.class.getName();
63      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
64          ".List";
65      public static final FinderPath FINDER_PATH_FIND_BY_NAME = new FinderPath(ResourceActionModelImpl.ENTITY_CACHE_ENABLED,
66              ResourceActionModelImpl.FINDER_CACHE_ENABLED,
67              FINDER_CLASS_NAME_LIST, "findByName",
68              new String[] { String.class.getName() });
69      public static final FinderPath FINDER_PATH_FIND_BY_OBC_NAME = new FinderPath(ResourceActionModelImpl.ENTITY_CACHE_ENABLED,
70              ResourceActionModelImpl.FINDER_CACHE_ENABLED,
71              FINDER_CLASS_NAME_LIST, "findByName",
72              new String[] {
73                  String.class.getName(),
74                  
75              "java.lang.Integer", "java.lang.Integer",
76                  "com.liferay.portal.kernel.util.OrderByComparator"
77              });
78      public static final FinderPath FINDER_PATH_COUNT_BY_NAME = new FinderPath(ResourceActionModelImpl.ENTITY_CACHE_ENABLED,
79              ResourceActionModelImpl.FINDER_CACHE_ENABLED,
80              FINDER_CLASS_NAME_LIST, "countByName",
81              new String[] { String.class.getName() });
82      public static final FinderPath FINDER_PATH_FETCH_BY_N_A = new FinderPath(ResourceActionModelImpl.ENTITY_CACHE_ENABLED,
83              ResourceActionModelImpl.FINDER_CACHE_ENABLED,
84              FINDER_CLASS_NAME_ENTITY, "fetchByN_A",
85              new String[] { String.class.getName(), String.class.getName() });
86      public static final FinderPath FINDER_PATH_COUNT_BY_N_A = new FinderPath(ResourceActionModelImpl.ENTITY_CACHE_ENABLED,
87              ResourceActionModelImpl.FINDER_CACHE_ENABLED,
88              FINDER_CLASS_NAME_LIST, "countByN_A",
89              new String[] { String.class.getName(), String.class.getName() });
90      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ResourceActionModelImpl.ENTITY_CACHE_ENABLED,
91              ResourceActionModelImpl.FINDER_CACHE_ENABLED,
92              FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
93      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ResourceActionModelImpl.ENTITY_CACHE_ENABLED,
94              ResourceActionModelImpl.FINDER_CACHE_ENABLED,
95              FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
96  
97      public void cacheResult(ResourceAction resourceAction) {
98          EntityCacheUtil.putResult(ResourceActionModelImpl.ENTITY_CACHE_ENABLED,
99              ResourceActionImpl.class, resourceAction.getPrimaryKey(),
100             resourceAction);
101 
102         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_A,
103             new Object[] { resourceAction.getName(), resourceAction.getActionId() },
104             resourceAction);
105     }
106 
107     public void cacheResult(List<ResourceAction> resourceActions) {
108         for (ResourceAction resourceAction : resourceActions) {
109             if (EntityCacheUtil.getResult(
110                         ResourceActionModelImpl.ENTITY_CACHE_ENABLED,
111                         ResourceActionImpl.class,
112                         resourceAction.getPrimaryKey(), this) == null) {
113                 cacheResult(resourceAction);
114             }
115         }
116     }
117 
118     public void clearCache() {
119         CacheRegistry.clear(ResourceActionImpl.class.getName());
120         EntityCacheUtil.clearCache(ResourceActionImpl.class.getName());
121         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
122         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
123     }
124 
125     public ResourceAction create(long resourceActionId) {
126         ResourceAction resourceAction = new ResourceActionImpl();
127 
128         resourceAction.setNew(true);
129         resourceAction.setPrimaryKey(resourceActionId);
130 
131         return resourceAction;
132     }
133 
134     public ResourceAction remove(long resourceActionId)
135         throws NoSuchResourceActionException, SystemException {
136         Session session = null;
137 
138         try {
139             session = openSession();
140 
141             ResourceAction resourceAction = (ResourceAction)session.get(ResourceActionImpl.class,
142                     new Long(resourceActionId));
143 
144             if (resourceAction == null) {
145                 if (_log.isWarnEnabled()) {
146                     _log.warn("No ResourceAction exists with the primary key " +
147                         resourceActionId);
148                 }
149 
150                 throw new NoSuchResourceActionException(
151                     "No ResourceAction exists with the primary key " +
152                     resourceActionId);
153             }
154 
155             return remove(resourceAction);
156         }
157         catch (NoSuchResourceActionException nsee) {
158             throw nsee;
159         }
160         catch (Exception e) {
161             throw processException(e);
162         }
163         finally {
164             closeSession(session);
165         }
166     }
167 
168     public ResourceAction remove(ResourceAction resourceAction)
169         throws SystemException {
170         for (ModelListener<ResourceAction> listener : listeners) {
171             listener.onBeforeRemove(resourceAction);
172         }
173 
174         resourceAction = removeImpl(resourceAction);
175 
176         for (ModelListener<ResourceAction> listener : listeners) {
177             listener.onAfterRemove(resourceAction);
178         }
179 
180         return resourceAction;
181     }
182 
183     protected ResourceAction removeImpl(ResourceAction resourceAction)
184         throws SystemException {
185         Session session = null;
186 
187         try {
188             session = openSession();
189 
190             if (resourceAction.isCachedModel() || BatchSessionUtil.isEnabled()) {
191                 Object staleObject = session.get(ResourceActionImpl.class,
192                         resourceAction.getPrimaryKeyObj());
193 
194                 if (staleObject != null) {
195                     session.evict(staleObject);
196                 }
197             }
198 
199             session.delete(resourceAction);
200 
201             session.flush();
202         }
203         catch (Exception e) {
204             throw processException(e);
205         }
206         finally {
207             closeSession(session);
208         }
209 
210         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
211 
212         ResourceActionModelImpl resourceActionModelImpl = (ResourceActionModelImpl)resourceAction;
213 
214         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_N_A,
215             new Object[] {
216                 resourceActionModelImpl.getOriginalName(),
217                 
218             resourceActionModelImpl.getOriginalActionId()
219             });
220 
221         EntityCacheUtil.removeResult(ResourceActionModelImpl.ENTITY_CACHE_ENABLED,
222             ResourceActionImpl.class, resourceAction.getPrimaryKey());
223 
224         return resourceAction;
225     }
226 
227     /**
228      * @deprecated Use <code>update(ResourceAction resourceAction, boolean merge)</code>.
229      */
230     public ResourceAction update(ResourceAction resourceAction)
231         throws SystemException {
232         if (_log.isWarnEnabled()) {
233             _log.warn(
234                 "Using the deprecated update(ResourceAction resourceAction) method. Use update(ResourceAction resourceAction, boolean merge) instead.");
235         }
236 
237         return update(resourceAction, false);
238     }
239 
240     /**
241      * Add, update, or merge, the entity. This method also calls the model
242      * listeners to trigger the proper events associated with adding, deleting,
243      * or updating an entity.
244      *
245      * @param        resourceAction the entity to add, update, or merge
246      * @param        merge boolean value for whether to merge the entity. The
247      *                default value is false. Setting merge to true is more
248      *                expensive and should only be true when resourceAction is
249      *                transient. See LEP-5473 for a detailed discussion of this
250      *                method.
251      * @return        true if the portlet can be displayed via Ajax
252      */
253     public ResourceAction update(ResourceAction resourceAction, boolean merge)
254         throws SystemException {
255         boolean isNew = resourceAction.isNew();
256 
257         for (ModelListener<ResourceAction> listener : listeners) {
258             if (isNew) {
259                 listener.onBeforeCreate(resourceAction);
260             }
261             else {
262                 listener.onBeforeUpdate(resourceAction);
263             }
264         }
265 
266         resourceAction = updateImpl(resourceAction, merge);
267 
268         for (ModelListener<ResourceAction> listener : listeners) {
269             if (isNew) {
270                 listener.onAfterCreate(resourceAction);
271             }
272             else {
273                 listener.onAfterUpdate(resourceAction);
274             }
275         }
276 
277         return resourceAction;
278     }
279 
280     public ResourceAction updateImpl(
281         com.liferay.portal.model.ResourceAction resourceAction, boolean merge)
282         throws SystemException {
283         boolean isNew = resourceAction.isNew();
284 
285         ResourceActionModelImpl resourceActionModelImpl = (ResourceActionModelImpl)resourceAction;
286 
287         Session session = null;
288 
289         try {
290             session = openSession();
291 
292             BatchSessionUtil.update(session, resourceAction, merge);
293 
294             resourceAction.setNew(false);
295         }
296         catch (Exception e) {
297             throw processException(e);
298         }
299         finally {
300             closeSession(session);
301         }
302 
303         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
304 
305         EntityCacheUtil.putResult(ResourceActionModelImpl.ENTITY_CACHE_ENABLED,
306             ResourceActionImpl.class, resourceAction.getPrimaryKey(),
307             resourceAction);
308 
309         if (!isNew &&
310                 (!Validator.equals(resourceAction.getName(),
311                     resourceActionModelImpl.getOriginalName()) ||
312                 !Validator.equals(resourceAction.getActionId(),
313                     resourceActionModelImpl.getOriginalActionId()))) {
314             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_N_A,
315                 new Object[] {
316                     resourceActionModelImpl.getOriginalName(),
317                     
318                 resourceActionModelImpl.getOriginalActionId()
319                 });
320         }
321 
322         if (isNew ||
323                 (!Validator.equals(resourceAction.getName(),
324                     resourceActionModelImpl.getOriginalName()) ||
325                 !Validator.equals(resourceAction.getActionId(),
326                     resourceActionModelImpl.getOriginalActionId()))) {
327             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_A,
328                 new Object[] {
329                     resourceAction.getName(),
330                     
331                 resourceAction.getActionId()
332                 }, resourceAction);
333         }
334 
335         return resourceAction;
336     }
337 
338     public ResourceAction findByPrimaryKey(long resourceActionId)
339         throws NoSuchResourceActionException, SystemException {
340         ResourceAction resourceAction = fetchByPrimaryKey(resourceActionId);
341 
342         if (resourceAction == null) {
343             if (_log.isWarnEnabled()) {
344                 _log.warn("No ResourceAction exists with the primary key " +
345                     resourceActionId);
346             }
347 
348             throw new NoSuchResourceActionException(
349                 "No ResourceAction exists with the primary key " +
350                 resourceActionId);
351         }
352 
353         return resourceAction;
354     }
355 
356     public ResourceAction fetchByPrimaryKey(long resourceActionId)
357         throws SystemException {
358         ResourceAction resourceAction = (ResourceAction)EntityCacheUtil.getResult(ResourceActionModelImpl.ENTITY_CACHE_ENABLED,
359                 ResourceActionImpl.class, resourceActionId, this);
360 
361         if (resourceAction == null) {
362             Session session = null;
363 
364             try {
365                 session = openSession();
366 
367                 resourceAction = (ResourceAction)session.get(ResourceActionImpl.class,
368                         new Long(resourceActionId));
369             }
370             catch (Exception e) {
371                 throw processException(e);
372             }
373             finally {
374                 if (resourceAction != null) {
375                     cacheResult(resourceAction);
376                 }
377 
378                 closeSession(session);
379             }
380         }
381 
382         return resourceAction;
383     }
384 
385     public List<ResourceAction> findByName(String name)
386         throws SystemException {
387         Object[] finderArgs = new Object[] { name };
388 
389         List<ResourceAction> list = (List<ResourceAction>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_NAME,
390                 finderArgs, this);
391 
392         if (list == null) {
393             Session session = null;
394 
395             try {
396                 session = openSession();
397 
398                 StringBuilder query = new StringBuilder();
399 
400                 query.append(
401                     "SELECT resourceAction FROM ResourceAction resourceAction WHERE ");
402 
403                 if (name == null) {
404                     query.append("resourceAction.name IS NULL");
405                 }
406                 else {
407                     query.append("resourceAction.name = ?");
408                 }
409 
410                 query.append(" ");
411 
412                 query.append("ORDER BY ");
413 
414                 query.append("resourceAction.name ASC, ");
415                 query.append("resourceAction.bitwiseValue ASC");
416 
417                 Query q = session.createQuery(query.toString());
418 
419                 QueryPos qPos = QueryPos.getInstance(q);
420 
421                 if (name != null) {
422                     qPos.add(name);
423                 }
424 
425                 list = q.list();
426             }
427             catch (Exception e) {
428                 throw processException(e);
429             }
430             finally {
431                 if (list == null) {
432                     list = new ArrayList<ResourceAction>();
433                 }
434 
435                 cacheResult(list);
436 
437                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_NAME, finderArgs,
438                     list);
439 
440                 closeSession(session);
441             }
442         }
443 
444         return list;
445     }
446 
447     public List<ResourceAction> findByName(String name, int start, int end)
448         throws SystemException {
449         return findByName(name, start, end, null);
450     }
451 
452     public List<ResourceAction> findByName(String name, int start, int end,
453         OrderByComparator obc) throws SystemException {
454         Object[] finderArgs = new Object[] {
455                 name,
456                 
457                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
458             };
459 
460         List<ResourceAction> list = (List<ResourceAction>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_NAME,
461                 finderArgs, this);
462 
463         if (list == null) {
464             Session session = null;
465 
466             try {
467                 session = openSession();
468 
469                 StringBuilder query = new StringBuilder();
470 
471                 query.append(
472                     "SELECT resourceAction FROM ResourceAction resourceAction WHERE ");
473 
474                 if (name == null) {
475                     query.append("resourceAction.name IS NULL");
476                 }
477                 else {
478                     query.append("resourceAction.name = ?");
479                 }
480 
481                 query.append(" ");
482 
483                 if (obc != null) {
484                     query.append("ORDER BY ");
485 
486                     String[] orderByFields = obc.getOrderByFields();
487 
488                     for (int i = 0; i < orderByFields.length; i++) {
489                         query.append("resourceAction.");
490                         query.append(orderByFields[i]);
491 
492                         if (obc.isAscending()) {
493                             query.append(" ASC");
494                         }
495                         else {
496                             query.append(" DESC");
497                         }
498 
499                         if ((i + 1) < orderByFields.length) {
500                             query.append(", ");
501                         }
502                     }
503                 }
504 
505                 else {
506                     query.append("ORDER BY ");
507 
508                     query.append("resourceAction.name ASC, ");
509                     query.append("resourceAction.bitwiseValue ASC");
510                 }
511 
512                 Query q = session.createQuery(query.toString());
513 
514                 QueryPos qPos = QueryPos.getInstance(q);
515 
516                 if (name != null) {
517                     qPos.add(name);
518                 }
519 
520                 list = (List<ResourceAction>)QueryUtil.list(q, getDialect(),
521                         start, end);
522             }
523             catch (Exception e) {
524                 throw processException(e);
525             }
526             finally {
527                 if (list == null) {
528                     list = new ArrayList<ResourceAction>();
529                 }
530 
531                 cacheResult(list);
532 
533                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_NAME,
534                     finderArgs, list);
535 
536                 closeSession(session);
537             }
538         }
539 
540         return list;
541     }
542 
543     public ResourceAction findByName_First(String name, OrderByComparator obc)
544         throws NoSuchResourceActionException, SystemException {
545         List<ResourceAction> list = findByName(name, 0, 1, obc);
546 
547         if (list.isEmpty()) {
548             StringBuilder msg = new StringBuilder();
549 
550             msg.append("No ResourceAction exists with the key {");
551 
552             msg.append("name=" + name);
553 
554             msg.append(StringPool.CLOSE_CURLY_BRACE);
555 
556             throw new NoSuchResourceActionException(msg.toString());
557         }
558         else {
559             return list.get(0);
560         }
561     }
562 
563     public ResourceAction findByName_Last(String name, OrderByComparator obc)
564         throws NoSuchResourceActionException, SystemException {
565         int count = countByName(name);
566 
567         List<ResourceAction> list = findByName(name, count - 1, count, obc);
568 
569         if (list.isEmpty()) {
570             StringBuilder msg = new StringBuilder();
571 
572             msg.append("No ResourceAction exists with the key {");
573 
574             msg.append("name=" + name);
575 
576             msg.append(StringPool.CLOSE_CURLY_BRACE);
577 
578             throw new NoSuchResourceActionException(msg.toString());
579         }
580         else {
581             return list.get(0);
582         }
583     }
584 
585     public ResourceAction[] findByName_PrevAndNext(long resourceActionId,
586         String name, OrderByComparator obc)
587         throws NoSuchResourceActionException, SystemException {
588         ResourceAction resourceAction = findByPrimaryKey(resourceActionId);
589 
590         int count = countByName(name);
591 
592         Session session = null;
593 
594         try {
595             session = openSession();
596 
597             StringBuilder query = new StringBuilder();
598 
599             query.append(
600                 "SELECT resourceAction FROM ResourceAction resourceAction WHERE ");
601 
602             if (name == null) {
603                 query.append("resourceAction.name IS NULL");
604             }
605             else {
606                 query.append("resourceAction.name = ?");
607             }
608 
609             query.append(" ");
610 
611             if (obc != null) {
612                 query.append("ORDER BY ");
613 
614                 String[] orderByFields = obc.getOrderByFields();
615 
616                 for (int i = 0; i < orderByFields.length; i++) {
617                     query.append("resourceAction.");
618                     query.append(orderByFields[i]);
619 
620                     if (obc.isAscending()) {
621                         query.append(" ASC");
622                     }
623                     else {
624                         query.append(" DESC");
625                     }
626 
627                     if ((i + 1) < orderByFields.length) {
628                         query.append(", ");
629                     }
630                 }
631             }
632 
633             else {
634                 query.append("ORDER BY ");
635 
636                 query.append("resourceAction.name ASC, ");
637                 query.append("resourceAction.bitwiseValue ASC");
638             }
639 
640             Query q = session.createQuery(query.toString());
641 
642             QueryPos qPos = QueryPos.getInstance(q);
643 
644             if (name != null) {
645                 qPos.add(name);
646             }
647 
648             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
649                     resourceAction);
650 
651             ResourceAction[] array = new ResourceActionImpl[3];
652 
653             array[0] = (ResourceAction)objArray[0];
654             array[1] = (ResourceAction)objArray[1];
655             array[2] = (ResourceAction)objArray[2];
656 
657             return array;
658         }
659         catch (Exception e) {
660             throw processException(e);
661         }
662         finally {
663             closeSession(session);
664         }
665     }
666 
667     public ResourceAction findByN_A(String name, String actionId)
668         throws NoSuchResourceActionException, SystemException {
669         ResourceAction resourceAction = fetchByN_A(name, actionId);
670 
671         if (resourceAction == null) {
672             StringBuilder msg = new StringBuilder();
673 
674             msg.append("No ResourceAction exists with the key {");
675 
676             msg.append("name=" + name);
677 
678             msg.append(", ");
679             msg.append("actionId=" + actionId);
680 
681             msg.append(StringPool.CLOSE_CURLY_BRACE);
682 
683             if (_log.isWarnEnabled()) {
684                 _log.warn(msg.toString());
685             }
686 
687             throw new NoSuchResourceActionException(msg.toString());
688         }
689 
690         return resourceAction;
691     }
692 
693     public ResourceAction fetchByN_A(String name, String actionId)
694         throws SystemException {
695         return fetchByN_A(name, actionId, true);
696     }
697 
698     public ResourceAction fetchByN_A(String name, String actionId,
699         boolean retrieveFromCache) throws SystemException {
700         Object[] finderArgs = new Object[] { name, actionId };
701 
702         Object result = null;
703 
704         if (retrieveFromCache) {
705             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_N_A,
706                     finderArgs, this);
707         }
708 
709         if (result == null) {
710             Session session = null;
711 
712             try {
713                 session = openSession();
714 
715                 StringBuilder query = new StringBuilder();
716 
717                 query.append(
718                     "SELECT resourceAction FROM ResourceAction resourceAction WHERE ");
719 
720                 if (name == null) {
721                     query.append("resourceAction.name IS NULL");
722                 }
723                 else {
724                     query.append("resourceAction.name = ?");
725                 }
726 
727                 query.append(" AND ");
728 
729                 if (actionId == null) {
730                     query.append("resourceAction.actionId IS NULL");
731                 }
732                 else {
733                     query.append("resourceAction.actionId = ?");
734                 }
735 
736                 query.append(" ");
737 
738                 query.append("ORDER BY ");
739 
740                 query.append("resourceAction.name ASC, ");
741                 query.append("resourceAction.bitwiseValue ASC");
742 
743                 Query q = session.createQuery(query.toString());
744 
745                 QueryPos qPos = QueryPos.getInstance(q);
746 
747                 if (name != null) {
748                     qPos.add(name);
749                 }
750 
751                 if (actionId != null) {
752                     qPos.add(actionId);
753                 }
754 
755                 List<ResourceAction> list = q.list();
756 
757                 result = list;
758 
759                 ResourceAction resourceAction = null;
760 
761                 if (list.isEmpty()) {
762                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_A,
763                         finderArgs, list);
764                 }
765                 else {
766                     resourceAction = list.get(0);
767 
768                     cacheResult(resourceAction);
769 
770                     if ((resourceAction.getName() == null) ||
771                             !resourceAction.getName().equals(name) ||
772                             (resourceAction.getActionId() == null) ||
773                             !resourceAction.getActionId().equals(actionId)) {
774                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_A,
775                             finderArgs, resourceAction);
776                     }
777                 }
778 
779                 return resourceAction;
780             }
781             catch (Exception e) {
782                 throw processException(e);
783             }
784             finally {
785                 if (result == null) {
786                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_A,
787                         finderArgs, new ArrayList<ResourceAction>());
788                 }
789 
790                 closeSession(session);
791             }
792         }
793         else {
794             if (result instanceof List) {
795                 return null;
796             }
797             else {
798                 return (ResourceAction)result;
799             }
800         }
801     }
802 
803     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
804         throws SystemException {
805         Session session = null;
806 
807         try {
808             session = openSession();
809 
810             dynamicQuery.compile(session);
811 
812             return dynamicQuery.list();
813         }
814         catch (Exception e) {
815             throw processException(e);
816         }
817         finally {
818             closeSession(session);
819         }
820     }
821 
822     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
823         int start, int end) throws SystemException {
824         Session session = null;
825 
826         try {
827             session = openSession();
828 
829             dynamicQuery.setLimit(start, end);
830 
831             dynamicQuery.compile(session);
832 
833             return dynamicQuery.list();
834         }
835         catch (Exception e) {
836             throw processException(e);
837         }
838         finally {
839             closeSession(session);
840         }
841     }
842 
843     public List<ResourceAction> findAll() throws SystemException {
844         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
845     }
846 
847     public List<ResourceAction> findAll(int start, int end)
848         throws SystemException {
849         return findAll(start, end, null);
850     }
851 
852     public List<ResourceAction> findAll(int start, int end,
853         OrderByComparator obc) throws SystemException {
854         Object[] finderArgs = new Object[] {
855                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
856             };
857 
858         List<ResourceAction> list = (List<ResourceAction>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
859                 finderArgs, this);
860 
861         if (list == null) {
862             Session session = null;
863 
864             try {
865                 session = openSession();
866 
867                 StringBuilder query = new StringBuilder();
868 
869                 query.append(
870                     "SELECT resourceAction FROM ResourceAction resourceAction ");
871 
872                 if (obc != null) {
873                     query.append("ORDER BY ");
874 
875                     String[] orderByFields = obc.getOrderByFields();
876 
877                     for (int i = 0; i < orderByFields.length; i++) {
878                         query.append("resourceAction.");
879                         query.append(orderByFields[i]);
880 
881                         if (obc.isAscending()) {
882                             query.append(" ASC");
883                         }
884                         else {
885                             query.append(" DESC");
886                         }
887 
888                         if ((i + 1) < orderByFields.length) {
889                             query.append(", ");
890                         }
891                     }
892                 }
893 
894                 else {
895                     query.append("ORDER BY ");
896 
897                     query.append("resourceAction.name ASC, ");
898                     query.append("resourceAction.bitwiseValue ASC");
899                 }
900 
901                 Query q = session.createQuery(query.toString());
902 
903                 if (obc == null) {
904                     list = (List<ResourceAction>)QueryUtil.list(q,
905                             getDialect(), start, end, false);
906 
907                     Collections.sort(list);
908                 }
909                 else {
910                     list = (List<ResourceAction>)QueryUtil.list(q,
911                             getDialect(), start, end);
912                 }
913             }
914             catch (Exception e) {
915                 throw processException(e);
916             }
917             finally {
918                 if (list == null) {
919                     list = new ArrayList<ResourceAction>();
920                 }
921 
922                 cacheResult(list);
923 
924                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
925 
926                 closeSession(session);
927             }
928         }
929 
930         return list;
931     }
932 
933     public void removeByName(String name) throws SystemException {
934         for (ResourceAction resourceAction : findByName(name)) {
935             remove(resourceAction);
936         }
937     }
938 
939     public void removeByN_A(String name, String actionId)
940         throws NoSuchResourceActionException, SystemException {
941         ResourceAction resourceAction = findByN_A(name, actionId);
942 
943         remove(resourceAction);
944     }
945 
946     public void removeAll() throws SystemException {
947         for (ResourceAction resourceAction : findAll()) {
948             remove(resourceAction);
949         }
950     }
951 
952     public int countByName(String name) throws SystemException {
953         Object[] finderArgs = new Object[] { name };
954 
955         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_NAME,
956                 finderArgs, this);
957 
958         if (count == null) {
959             Session session = null;
960 
961             try {
962                 session = openSession();
963 
964                 StringBuilder query = new StringBuilder();
965 
966                 query.append("SELECT COUNT(resourceAction) ");
967                 query.append("FROM ResourceAction resourceAction WHERE ");
968 
969                 if (name == null) {
970                     query.append("resourceAction.name IS NULL");
971                 }
972                 else {
973                     query.append("resourceAction.name = ?");
974                 }
975 
976                 query.append(" ");
977 
978                 Query q = session.createQuery(query.toString());
979 
980                 QueryPos qPos = QueryPos.getInstance(q);
981 
982                 if (name != null) {
983                     qPos.add(name);
984                 }
985 
986                 count = (Long)q.uniqueResult();
987             }
988             catch (Exception e) {
989                 throw processException(e);
990             }
991             finally {
992                 if (count == null) {
993                     count = Long.valueOf(0);
994                 }
995 
996                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_NAME,
997                     finderArgs, count);
998 
999                 closeSession(session);
1000            }
1001        }
1002
1003        return count.intValue();
1004    }
1005
1006    public int countByN_A(String name, String actionId)
1007        throws SystemException {
1008        Object[] finderArgs = new Object[] { name, actionId };
1009
1010        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_N_A,
1011                finderArgs, this);
1012
1013        if (count == null) {
1014            Session session = null;
1015
1016            try {
1017                session = openSession();
1018
1019                StringBuilder query = new StringBuilder();
1020
1021                query.append("SELECT COUNT(resourceAction) ");
1022                query.append("FROM ResourceAction resourceAction WHERE ");
1023
1024                if (name == null) {
1025                    query.append("resourceAction.name IS NULL");
1026                }
1027                else {
1028                    query.append("resourceAction.name = ?");
1029                }
1030
1031                query.append(" AND ");
1032
1033                if (actionId == null) {
1034                    query.append("resourceAction.actionId IS NULL");
1035                }
1036                else {
1037                    query.append("resourceAction.actionId = ?");
1038                }
1039
1040                query.append(" ");
1041
1042                Query q = session.createQuery(query.toString());
1043
1044                QueryPos qPos = QueryPos.getInstance(q);
1045
1046                if (name != null) {
1047                    qPos.add(name);
1048                }
1049
1050                if (actionId != null) {
1051                    qPos.add(actionId);
1052                }
1053
1054                count = (Long)q.uniqueResult();
1055            }
1056            catch (Exception e) {
1057                throw processException(e);
1058            }
1059            finally {
1060                if (count == null) {
1061                    count = Long.valueOf(0);
1062                }
1063
1064                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_N_A, finderArgs,
1065                    count);
1066
1067                closeSession(session);
1068            }
1069        }
1070
1071        return count.intValue();
1072    }
1073
1074    public int countAll() throws SystemException {
1075        Object[] finderArgs = new Object[0];
1076
1077        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1078                finderArgs, this);
1079
1080        if (count == null) {
1081            Session session = null;
1082
1083            try {
1084                session = openSession();
1085
1086                Query q = session.createQuery(
1087                        "SELECT COUNT(resourceAction) FROM ResourceAction resourceAction");
1088
1089                count = (Long)q.uniqueResult();
1090            }
1091            catch (Exception e) {
1092                throw processException(e);
1093            }
1094            finally {
1095                if (count == null) {
1096                    count = Long.valueOf(0);
1097                }
1098
1099                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1100                    count);
1101
1102                closeSession(session);
1103            }
1104        }
1105
1106        return count.intValue();
1107    }
1108
1109    public void afterPropertiesSet() {
1110        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1111                    com.liferay.portal.util.PropsUtil.get(
1112                        "value.object.listener.com.liferay.portal.model.ResourceAction")));
1113
1114        if (listenerClassNames.length > 0) {
1115            try {
1116                List<ModelListener<ResourceAction>> listenersList = new ArrayList<ModelListener<ResourceAction>>();
1117
1118                for (String listenerClassName : listenerClassNames) {
1119                    listenersList.add((ModelListener<ResourceAction>)Class.forName(
1120                            listenerClassName).newInstance());
1121                }
1122
1123                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1124            }
1125            catch (Exception e) {
1126                _log.error(e);
1127            }
1128        }
1129    }
1130
1131    @BeanReference(name = "com.liferay.portal.service.persistence.AccountPersistence.impl")
1132    protected com.liferay.portal.service.persistence.AccountPersistence accountPersistence;
1133    @BeanReference(name = "com.liferay.portal.service.persistence.AddressPersistence.impl")
1134    protected com.liferay.portal.service.persistence.AddressPersistence addressPersistence;
1135    @BeanReference(name = "com.liferay.portal.service.persistence.BrowserTrackerPersistence.impl")
1136    protected com.liferay.portal.service.persistence.BrowserTrackerPersistence browserTrackerPersistence;
1137    @BeanReference(name = "com.liferay.portal.service.persistence.ClassNamePersistence.impl")
1138    protected com.liferay.portal.service.persistence.ClassNamePersistence classNamePersistence;
1139    @BeanReference(name = "com.liferay.portal.service.persistence.CompanyPersistence.impl")
1140    protected com.liferay.portal.service.persistence.CompanyPersistence companyPersistence;
1141    @BeanReference(name = "com.liferay.portal.service.persistence.ContactPersistence.impl")
1142    protected com.liferay.portal.service.persistence.ContactPersistence contactPersistence;
1143    @BeanReference(name = "com.liferay.portal.service.persistence.CountryPersistence.impl")
1144    protected com.liferay.portal.service.persistence.CountryPersistence countryPersistence;
1145    @BeanReference(name = "com.liferay.portal.service.persistence.EmailAddressPersistence.impl")
1146    protected com.liferay.portal.service.persistence.EmailAddressPersistence emailAddressPersistence;
1147    @BeanReference(name = "com.liferay.portal.service.persistence.GroupPersistence.impl")
1148    protected com.liferay.portal.service.persistence.GroupPersistence groupPersistence;
1149    @BeanReference(name = "com.liferay.portal.service.persistence.ImagePersistence.impl")
1150    protected com.liferay.portal.service.persistence.ImagePersistence imagePersistence;
1151    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutPersistence.impl")
1152    protected com.liferay.portal.service.persistence.LayoutPersistence layoutPersistence;
1153    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutSetPersistence.impl")
1154    protected com.liferay.portal.service.persistence.LayoutSetPersistence layoutSetPersistence;
1155    @BeanReference(name = "com.liferay.portal.service.persistence.ListTypePersistence.impl")
1156    protected com.liferay.portal.service.persistence.ListTypePersistence listTypePersistence;
1157    @BeanReference(name = "com.liferay.portal.service.persistence.MembershipRequestPersistence.impl")
1158    protected com.liferay.portal.service.persistence.MembershipRequestPersistence membershipRequestPersistence;
1159    @BeanReference(name = "com.liferay.portal.service.persistence.OrganizationPersistence.impl")
1160    protected com.liferay.portal.service.persistence.OrganizationPersistence organizationPersistence;
1161    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupPermissionPersistence.impl")
1162    protected com.liferay.portal.service.persistence.OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1163    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupRolePersistence.impl")
1164    protected com.liferay.portal.service.persistence.OrgGroupRolePersistence orgGroupRolePersistence;
1165    @BeanReference(name = "com.liferay.portal.service.persistence.OrgLaborPersistence.impl")
1166    protected com.liferay.portal.service.persistence.OrgLaborPersistence orgLaborPersistence;
1167    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyPersistence.impl")
1168    protected com.liferay.portal.service.persistence.PasswordPolicyPersistence passwordPolicyPersistence;
1169    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyRelPersistence.impl")
1170    protected com.liferay.portal.service.persistence.PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1171    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordTrackerPersistence.impl")
1172    protected com.liferay.portal.service.persistence.PasswordTrackerPersistence passwordTrackerPersistence;
1173    @BeanReference(name = "com.liferay.portal.service.persistence.PermissionPersistence.impl")
1174    protected com.liferay.portal.service.persistence.PermissionPersistence permissionPersistence;
1175    @BeanReference(name = "com.liferay.portal.service.persistence.PhonePersistence.impl")
1176    protected com.liferay.portal.service.persistence.PhonePersistence phonePersistence;
1177    @BeanReference(name = "com.liferay.portal.service.persistence.PluginSettingPersistence.impl")
1178    protected com.liferay.portal.service.persistence.PluginSettingPersistence pluginSettingPersistence;
1179    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPersistence.impl")
1180    protected com.liferay.portal.service.persistence.PortletPersistence portletPersistence;
1181    @BeanReference(name = "com.liferay.portal.service.persistence.PortletItemPersistence.impl")
1182    protected com.liferay.portal.service.persistence.PortletItemPersistence portletItemPersistence;
1183    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPreferencesPersistence.impl")
1184    protected com.liferay.portal.service.persistence.PortletPreferencesPersistence portletPreferencesPersistence;
1185    @BeanReference(name = "com.liferay.portal.service.persistence.RegionPersistence.impl")
1186    protected com.liferay.portal.service.persistence.RegionPersistence regionPersistence;
1187    @BeanReference(name = "com.liferay.portal.service.persistence.ReleasePersistence.impl")
1188    protected com.liferay.portal.service.persistence.ReleasePersistence releasePersistence;
1189    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1190    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1191    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceActionPersistence.impl")
1192    protected com.liferay.portal.service.persistence.ResourceActionPersistence resourceActionPersistence;
1193    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceCodePersistence.impl")
1194    protected com.liferay.portal.service.persistence.ResourceCodePersistence resourceCodePersistence;
1195    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePermissionPersistence.impl")
1196    protected com.liferay.portal.service.persistence.ResourcePermissionPersistence resourcePermissionPersistence;
1197    @BeanReference(name = "com.liferay.portal.service.persistence.RolePersistence.impl")
1198    protected com.liferay.portal.service.persistence.RolePersistence rolePersistence;
1199    @BeanReference(name = "com.liferay.portal.service.persistence.ServiceComponentPersistence.impl")
1200    protected com.liferay.portal.service.persistence.ServiceComponentPersistence serviceComponentPersistence;
1201    @BeanReference(name = "com.liferay.portal.service.persistence.ShardPersistence.impl")
1202    protected com.liferay.portal.service.persistence.ShardPersistence shardPersistence;
1203    @BeanReference(name = "com.liferay.portal.service.persistence.SubscriptionPersistence.impl")
1204    protected com.liferay.portal.service.persistence.SubscriptionPersistence subscriptionPersistence;
1205    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1206    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1207    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupPersistence.impl")
1208    protected com.liferay.portal.service.persistence.UserGroupPersistence userGroupPersistence;
1209    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupRolePersistence.impl")
1210    protected com.liferay.portal.service.persistence.UserGroupRolePersistence userGroupRolePersistence;
1211    @BeanReference(name = "com.liferay.portal.service.persistence.UserIdMapperPersistence.impl")
1212    protected com.liferay.portal.service.persistence.UserIdMapperPersistence userIdMapperPersistence;
1213    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPersistence.impl")
1214    protected com.liferay.portal.service.persistence.UserTrackerPersistence userTrackerPersistence;
1215    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPathPersistence.impl")
1216    protected com.liferay.portal.service.persistence.UserTrackerPathPersistence userTrackerPathPersistence;
1217    @BeanReference(name = "com.liferay.portal.service.persistence.WebDAVPropsPersistence.impl")
1218    protected com.liferay.portal.service.persistence.WebDAVPropsPersistence webDAVPropsPersistence;
1219    @BeanReference(name = "com.liferay.portal.service.persistence.WebsitePersistence.impl")
1220    protected com.liferay.portal.service.persistence.WebsitePersistence websitePersistence;
1221    private static Log _log = LogFactoryUtil.getLog(ResourceActionPersistenceImpl.class);
1222}