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.tags.service.persistence;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
24  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
25  import com.liferay.portal.kernel.dao.orm.Query;
26  import com.liferay.portal.kernel.dao.orm.QueryUtil;
27  import com.liferay.portal.kernel.dao.orm.Session;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.OrderByComparator;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.model.ModelListener;
34  import com.liferay.portal.service.persistence.BatchSessionUtil;
35  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
36  
37  import com.liferay.portlet.tags.NoSuchSourceException;
38  import com.liferay.portlet.tags.model.TagsSource;
39  import com.liferay.portlet.tags.model.impl.TagsSourceImpl;
40  import com.liferay.portlet.tags.model.impl.TagsSourceModelImpl;
41  
42  import java.util.ArrayList;
43  import java.util.Collections;
44  import java.util.Iterator;
45  import java.util.List;
46  
47  /**
48   * <a href="TagsSourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class TagsSourcePersistenceImpl extends BasePersistenceImpl
54      implements TagsSourcePersistence {
55      public TagsSource create(long sourceId) {
56          TagsSource tagsSource = new TagsSourceImpl();
57  
58          tagsSource.setNew(true);
59          tagsSource.setPrimaryKey(sourceId);
60  
61          return tagsSource;
62      }
63  
64      public TagsSource remove(long sourceId)
65          throws NoSuchSourceException, SystemException {
66          Session session = null;
67  
68          try {
69              session = openSession();
70  
71              TagsSource tagsSource = (TagsSource)session.get(TagsSourceImpl.class,
72                      new Long(sourceId));
73  
74              if (tagsSource == null) {
75                  if (_log.isWarnEnabled()) {
76                      _log.warn("No TagsSource exists with the primary key " +
77                          sourceId);
78                  }
79  
80                  throw new NoSuchSourceException(
81                      "No TagsSource exists with the primary key " + sourceId);
82              }
83  
84              return remove(tagsSource);
85          }
86          catch (NoSuchSourceException nsee) {
87              throw nsee;
88          }
89          catch (Exception e) {
90              throw processException(e);
91          }
92          finally {
93              closeSession(session);
94          }
95      }
96  
97      public TagsSource remove(TagsSource tagsSource) throws SystemException {
98          for (ModelListener listener : listeners) {
99              listener.onBeforeRemove(tagsSource);
100         }
101 
102         tagsSource = removeImpl(tagsSource);
103 
104         for (ModelListener listener : listeners) {
105             listener.onAfterRemove(tagsSource);
106         }
107 
108         return tagsSource;
109     }
110 
111     protected TagsSource removeImpl(TagsSource tagsSource)
112         throws SystemException {
113         Session session = null;
114 
115         try {
116             session = openSession();
117 
118             if (BatchSessionUtil.isEnabled()) {
119                 Object staleObject = session.get(TagsSourceImpl.class,
120                         tagsSource.getPrimaryKeyObj());
121 
122                 if (staleObject != null) {
123                     session.evict(staleObject);
124                 }
125             }
126 
127             session.delete(tagsSource);
128 
129             session.flush();
130 
131             return tagsSource;
132         }
133         catch (Exception e) {
134             throw processException(e);
135         }
136         finally {
137             closeSession(session);
138 
139             FinderCacheUtil.clearCache(TagsSource.class.getName());
140         }
141     }
142 
143     /**
144      * @deprecated Use <code>update(TagsSource tagsSource, boolean merge)</code>.
145      */
146     public TagsSource update(TagsSource tagsSource) throws SystemException {
147         if (_log.isWarnEnabled()) {
148             _log.warn(
149                 "Using the deprecated update(TagsSource tagsSource) method. Use update(TagsSource tagsSource, boolean merge) instead.");
150         }
151 
152         return update(tagsSource, false);
153     }
154 
155     /**
156      * Add, update, or merge, the entity. This method also calls the model
157      * listeners to trigger the proper events associated with adding, deleting,
158      * or updating an entity.
159      *
160      * @param        tagsSource the entity to add, update, or merge
161      * @param        merge boolean value for whether to merge the entity. The
162      *                default value is false. Setting merge to true is more
163      *                expensive and should only be true when tagsSource is
164      *                transient. See LEP-5473 for a detailed discussion of this
165      *                method.
166      * @return        true if the portlet can be displayed via Ajax
167      */
168     public TagsSource update(TagsSource tagsSource, boolean merge)
169         throws SystemException {
170         boolean isNew = tagsSource.isNew();
171 
172         for (ModelListener listener : listeners) {
173             if (isNew) {
174                 listener.onBeforeCreate(tagsSource);
175             }
176             else {
177                 listener.onBeforeUpdate(tagsSource);
178             }
179         }
180 
181         tagsSource = updateImpl(tagsSource, merge);
182 
183         for (ModelListener listener : listeners) {
184             if (isNew) {
185                 listener.onAfterCreate(tagsSource);
186             }
187             else {
188                 listener.onAfterUpdate(tagsSource);
189             }
190         }
191 
192         return tagsSource;
193     }
194 
195     public TagsSource updateImpl(
196         com.liferay.portlet.tags.model.TagsSource tagsSource, boolean merge)
197         throws SystemException {
198         Session session = null;
199 
200         try {
201             session = openSession();
202 
203             BatchSessionUtil.update(session, tagsSource, merge);
204 
205             tagsSource.setNew(false);
206 
207             return tagsSource;
208         }
209         catch (Exception e) {
210             throw processException(e);
211         }
212         finally {
213             closeSession(session);
214 
215             FinderCacheUtil.clearCache(TagsSource.class.getName());
216         }
217     }
218 
219     public TagsSource findByPrimaryKey(long sourceId)
220         throws NoSuchSourceException, SystemException {
221         TagsSource tagsSource = fetchByPrimaryKey(sourceId);
222 
223         if (tagsSource == null) {
224             if (_log.isWarnEnabled()) {
225                 _log.warn("No TagsSource exists with the primary key " +
226                     sourceId);
227             }
228 
229             throw new NoSuchSourceException(
230                 "No TagsSource exists with the primary key " + sourceId);
231         }
232 
233         return tagsSource;
234     }
235 
236     public TagsSource fetchByPrimaryKey(long sourceId)
237         throws SystemException {
238         Session session = null;
239 
240         try {
241             session = openSession();
242 
243             return (TagsSource)session.get(TagsSourceImpl.class,
244                 new Long(sourceId));
245         }
246         catch (Exception e) {
247             throw processException(e);
248         }
249         finally {
250             closeSession(session);
251         }
252     }
253 
254     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
255         throws SystemException {
256         Session session = null;
257 
258         try {
259             session = openSession();
260 
261             dynamicQuery.compile(session);
262 
263             return dynamicQuery.list();
264         }
265         catch (Exception e) {
266             throw processException(e);
267         }
268         finally {
269             closeSession(session);
270         }
271     }
272 
273     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
274         int start, int end) throws SystemException {
275         Session session = null;
276 
277         try {
278             session = openSession();
279 
280             dynamicQuery.setLimit(start, end);
281 
282             dynamicQuery.compile(session);
283 
284             return dynamicQuery.list();
285         }
286         catch (Exception e) {
287             throw processException(e);
288         }
289         finally {
290             closeSession(session);
291         }
292     }
293 
294     public List<TagsSource> findAll() throws SystemException {
295         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
296     }
297 
298     public List<TagsSource> findAll(int start, int end)
299         throws SystemException {
300         return findAll(start, end, null);
301     }
302 
303     public List<TagsSource> findAll(int start, int end, OrderByComparator obc)
304         throws SystemException {
305         boolean finderClassNameCacheEnabled = TagsSourceModelImpl.CACHE_ENABLED;
306         String finderClassName = TagsSource.class.getName();
307         String finderMethodName = "findAll";
308         String[] finderParams = new String[] {
309                 "java.lang.Integer", "java.lang.Integer",
310                 "com.liferay.portal.kernel.util.OrderByComparator"
311             };
312         Object[] finderArgs = new Object[] {
313                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
314             };
315 
316         Object result = null;
317 
318         if (finderClassNameCacheEnabled) {
319             result = FinderCacheUtil.getResult(finderClassName,
320                     finderMethodName, finderParams, finderArgs, this);
321         }
322 
323         if (result == null) {
324             Session session = null;
325 
326             try {
327                 session = openSession();
328 
329                 StringBuilder query = new StringBuilder();
330 
331                 query.append("FROM com.liferay.portlet.tags.model.TagsSource ");
332 
333                 if (obc != null) {
334                     query.append("ORDER BY ");
335                     query.append(obc.getOrderBy());
336                 }
337 
338                 Query q = session.createQuery(query.toString());
339 
340                 List<TagsSource> list = null;
341 
342                 if (obc == null) {
343                     list = (List<TagsSource>)QueryUtil.list(q, getDialect(),
344                             start, end, false);
345 
346                     Collections.sort(list);
347                 }
348                 else {
349                     list = (List<TagsSource>)QueryUtil.list(q, getDialect(),
350                             start, end);
351                 }
352 
353                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
354                     finderClassName, finderMethodName, finderParams,
355                     finderArgs, list);
356 
357                 return list;
358             }
359             catch (Exception e) {
360                 throw processException(e);
361             }
362             finally {
363                 closeSession(session);
364             }
365         }
366         else {
367             return (List<TagsSource>)result;
368         }
369     }
370 
371     public void removeAll() throws SystemException {
372         for (TagsSource tagsSource : findAll()) {
373             remove(tagsSource);
374         }
375     }
376 
377     public int countAll() throws SystemException {
378         boolean finderClassNameCacheEnabled = TagsSourceModelImpl.CACHE_ENABLED;
379         String finderClassName = TagsSource.class.getName();
380         String finderMethodName = "countAll";
381         String[] finderParams = new String[] {  };
382         Object[] finderArgs = new Object[] {  };
383 
384         Object result = null;
385 
386         if (finderClassNameCacheEnabled) {
387             result = FinderCacheUtil.getResult(finderClassName,
388                     finderMethodName, finderParams, finderArgs, this);
389         }
390 
391         if (result == null) {
392             Session session = null;
393 
394             try {
395                 session = openSession();
396 
397                 Query q = session.createQuery(
398                         "SELECT COUNT(*) FROM com.liferay.portlet.tags.model.TagsSource");
399 
400                 Long count = null;
401 
402                 Iterator<Long> itr = q.list().iterator();
403 
404                 if (itr.hasNext()) {
405                     count = itr.next();
406                 }
407 
408                 if (count == null) {
409                     count = new Long(0);
410                 }
411 
412                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
413                     finderClassName, finderMethodName, finderParams,
414                     finderArgs, count);
415 
416                 return count.intValue();
417             }
418             catch (Exception e) {
419                 throw processException(e);
420             }
421             finally {
422                 closeSession(session);
423             }
424         }
425         else {
426             return ((Long)result).intValue();
427         }
428     }
429 
430     public void afterPropertiesSet() {
431         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
432                     com.liferay.portal.util.PropsUtil.get(
433                         "value.object.listener.com.liferay.portlet.tags.model.TagsSource")));
434 
435         if (listenerClassNames.length > 0) {
436             try {
437                 List<ModelListener> listenersList = new ArrayList<ModelListener>();
438 
439                 for (String listenerClassName : listenerClassNames) {
440                     listenersList.add((ModelListener)Class.forName(
441                             listenerClassName).newInstance());
442                 }
443 
444                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
445             }
446             catch (Exception e) {
447                 _log.error(e);
448             }
449         }
450     }
451 
452     private static Log _log = LogFactoryUtil.getLog(TagsSourcePersistenceImpl.class);
453 }