1   /**
2    * Copyright (c) 2000-2007 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.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.ModelListener;
28  import com.liferay.portal.util.PropsUtil;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  /**
34   * <a href="ResourceUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class ResourceUtil {
40      public static com.liferay.portal.model.Resource create(long resourceId) {
41          return getPersistence().create(resourceId);
42      }
43  
44      public static com.liferay.portal.model.Resource remove(long resourceId)
45          throws com.liferay.portal.SystemException, 
46              com.liferay.portal.NoSuchResourceException {
47          ModelListener listener = _getListener();
48  
49          if (listener != null) {
50              listener.onBeforeRemove(findByPrimaryKey(resourceId));
51          }
52  
53          com.liferay.portal.model.Resource resource = getPersistence().remove(resourceId);
54  
55          if (listener != null) {
56              listener.onAfterRemove(resource);
57          }
58  
59          return resource;
60      }
61  
62      public static com.liferay.portal.model.Resource remove(
63          com.liferay.portal.model.Resource resource)
64          throws com.liferay.portal.SystemException {
65          ModelListener listener = _getListener();
66  
67          if (listener != null) {
68              listener.onBeforeRemove(resource);
69          }
70  
71          resource = getPersistence().remove(resource);
72  
73          if (listener != null) {
74              listener.onAfterRemove(resource);
75          }
76  
77          return resource;
78      }
79  
80      public static com.liferay.portal.model.Resource update(
81          com.liferay.portal.model.Resource resource)
82          throws com.liferay.portal.SystemException {
83          ModelListener listener = _getListener();
84          boolean isNew = resource.isNew();
85  
86          if (listener != null) {
87              if (isNew) {
88                  listener.onBeforeCreate(resource);
89              }
90              else {
91                  listener.onBeforeUpdate(resource);
92              }
93          }
94  
95          resource = getPersistence().update(resource);
96  
97          if (listener != null) {
98              if (isNew) {
99                  listener.onAfterCreate(resource);
100             }
101             else {
102                 listener.onAfterUpdate(resource);
103             }
104         }
105 
106         return resource;
107     }
108 
109     public static com.liferay.portal.model.Resource update(
110         com.liferay.portal.model.Resource resource, boolean merge)
111         throws com.liferay.portal.SystemException {
112         ModelListener listener = _getListener();
113         boolean isNew = resource.isNew();
114 
115         if (listener != null) {
116             if (isNew) {
117                 listener.onBeforeCreate(resource);
118             }
119             else {
120                 listener.onBeforeUpdate(resource);
121             }
122         }
123 
124         resource = getPersistence().update(resource, merge);
125 
126         if (listener != null) {
127             if (isNew) {
128                 listener.onAfterCreate(resource);
129             }
130             else {
131                 listener.onAfterUpdate(resource);
132             }
133         }
134 
135         return resource;
136     }
137 
138     public static com.liferay.portal.model.Resource findByPrimaryKey(
139         long resourceId)
140         throws com.liferay.portal.SystemException, 
141             com.liferay.portal.NoSuchResourceException {
142         return getPersistence().findByPrimaryKey(resourceId);
143     }
144 
145     public static com.liferay.portal.model.Resource fetchByPrimaryKey(
146         long resourceId) throws com.liferay.portal.SystemException {
147         return getPersistence().fetchByPrimaryKey(resourceId);
148     }
149 
150     public static java.util.List findByCodeId(long codeId)
151         throws com.liferay.portal.SystemException {
152         return getPersistence().findByCodeId(codeId);
153     }
154 
155     public static java.util.List findByCodeId(long codeId, int begin, int end)
156         throws com.liferay.portal.SystemException {
157         return getPersistence().findByCodeId(codeId, begin, end);
158     }
159 
160     public static java.util.List findByCodeId(long codeId, int begin, int end,
161         com.liferay.portal.kernel.util.OrderByComparator obc)
162         throws com.liferay.portal.SystemException {
163         return getPersistence().findByCodeId(codeId, begin, end, obc);
164     }
165 
166     public static com.liferay.portal.model.Resource findByCodeId_First(
167         long codeId, com.liferay.portal.kernel.util.OrderByComparator obc)
168         throws com.liferay.portal.SystemException, 
169             com.liferay.portal.NoSuchResourceException {
170         return getPersistence().findByCodeId_First(codeId, obc);
171     }
172 
173     public static com.liferay.portal.model.Resource findByCodeId_Last(
174         long codeId, com.liferay.portal.kernel.util.OrderByComparator obc)
175         throws com.liferay.portal.SystemException, 
176             com.liferay.portal.NoSuchResourceException {
177         return getPersistence().findByCodeId_Last(codeId, obc);
178     }
179 
180     public static com.liferay.portal.model.Resource[] findByCodeId_PrevAndNext(
181         long resourceId, long codeId,
182         com.liferay.portal.kernel.util.OrderByComparator obc)
183         throws com.liferay.portal.SystemException, 
184             com.liferay.portal.NoSuchResourceException {
185         return getPersistence().findByCodeId_PrevAndNext(resourceId, codeId, obc);
186     }
187 
188     public static com.liferay.portal.model.Resource findByC_P(long codeId,
189         java.lang.String primKey)
190         throws com.liferay.portal.SystemException, 
191             com.liferay.portal.NoSuchResourceException {
192         return getPersistence().findByC_P(codeId, primKey);
193     }
194 
195     public static com.liferay.portal.model.Resource fetchByC_P(long codeId,
196         java.lang.String primKey) throws com.liferay.portal.SystemException {
197         return getPersistence().fetchByC_P(codeId, primKey);
198     }
199 
200     public static java.util.List findWithDynamicQuery(
201         com.liferay.portal.kernel.dao.DynamicQueryInitializer queryInitializer)
202         throws com.liferay.portal.SystemException {
203         return getPersistence().findWithDynamicQuery(queryInitializer);
204     }
205 
206     public static java.util.List findWithDynamicQuery(
207         com.liferay.portal.kernel.dao.DynamicQueryInitializer queryInitializer,
208         int begin, int end) throws com.liferay.portal.SystemException {
209         return getPersistence().findWithDynamicQuery(queryInitializer, begin,
210             end);
211     }
212 
213     public static java.util.List findAll()
214         throws com.liferay.portal.SystemException {
215         return getPersistence().findAll();
216     }
217 
218     public static java.util.List findAll(int begin, int end)
219         throws com.liferay.portal.SystemException {
220         return getPersistence().findAll(begin, end);
221     }
222 
223     public static java.util.List findAll(int begin, int end,
224         com.liferay.portal.kernel.util.OrderByComparator obc)
225         throws com.liferay.portal.SystemException {
226         return getPersistence().findAll(begin, end, obc);
227     }
228 
229     public static void removeByCodeId(long codeId)
230         throws com.liferay.portal.SystemException {
231         getPersistence().removeByCodeId(codeId);
232     }
233 
234     public static void removeByC_P(long codeId, java.lang.String primKey)
235         throws com.liferay.portal.SystemException, 
236             com.liferay.portal.NoSuchResourceException {
237         getPersistence().removeByC_P(codeId, primKey);
238     }
239 
240     public static void removeAll() throws com.liferay.portal.SystemException {
241         getPersistence().removeAll();
242     }
243 
244     public static int countByCodeId(long codeId)
245         throws com.liferay.portal.SystemException {
246         return getPersistence().countByCodeId(codeId);
247     }
248 
249     public static int countByC_P(long codeId, java.lang.String primKey)
250         throws com.liferay.portal.SystemException {
251         return getPersistence().countByC_P(codeId, primKey);
252     }
253 
254     public static int countAll() throws com.liferay.portal.SystemException {
255         return getPersistence().countAll();
256     }
257 
258     public static ResourcePersistence getPersistence() {
259         return _getUtil()._persistence;
260     }
261 
262     public void setPersistence(ResourcePersistence persistence) {
263         _persistence = persistence;
264     }
265 
266     private static ResourceUtil _getUtil() {
267         if (_util == null) {
268             _util = (ResourceUtil)com.liferay.portal.kernel.bean.BeanLocatorUtil.locate(_UTIL);
269         }
270 
271         return _util;
272     }
273 
274     private static ModelListener _getListener() {
275         if (Validator.isNotNull(_LISTENER)) {
276             try {
277                 return (ModelListener)Class.forName(_LISTENER).newInstance();
278             }
279             catch (Exception e) {
280                 _log.error(e);
281             }
282         }
283 
284         return null;
285     }
286 
287     private static final String _UTIL = ResourceUtil.class.getName();
288     private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(
289                 "value.object.listener.com.liferay.portal.model.Resource"));
290     private static Log _log = LogFactory.getLog(ResourceUtil.class);
291     private static ResourceUtil _util;
292     private ResourcePersistence _persistence;
293 }