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.portlet.tags.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.service.persistence.BasePersistence;
31  import com.liferay.portal.spring.hibernate.FinderCache;
32  import com.liferay.portal.spring.hibernate.HibernateUtil;
33  
34  import com.liferay.portlet.tags.NoSuchSourceException;
35  import com.liferay.portlet.tags.model.TagsSource;
36  import com.liferay.portlet.tags.model.impl.TagsSourceImpl;
37  
38  import com.liferay.util.dao.hibernate.QueryUtil;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  import org.hibernate.Query;
44  import org.hibernate.Session;
45  
46  import java.util.Collections;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  /**
51   * <a href="TagsSourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class TagsSourcePersistenceImpl extends BasePersistence
57      implements TagsSourcePersistence {
58      public TagsSource create(long sourceId) {
59          TagsSource tagsSource = new TagsSourceImpl();
60          tagsSource.setNew(true);
61          tagsSource.setPrimaryKey(sourceId);
62  
63          return tagsSource;
64      }
65  
66      public TagsSource remove(long sourceId)
67          throws NoSuchSourceException, SystemException {
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              TagsSource tagsSource = (TagsSource)session.get(TagsSourceImpl.class,
74                      new Long(sourceId));
75  
76              if (tagsSource == null) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn("No TagsSource exists with the primary key " +
79                          sourceId);
80                  }
81  
82                  throw new NoSuchSourceException(
83                      "No TagsSource exists with the primary key " + sourceId);
84              }
85  
86              return remove(tagsSource);
87          }
88          catch (NoSuchSourceException nsee) {
89              throw nsee;
90          }
91          catch (Exception e) {
92              throw HibernateUtil.processException(e);
93          }
94          finally {
95              closeSession(session);
96          }
97      }
98  
99      public TagsSource remove(TagsSource tagsSource) throws SystemException {
100         Session session = null;
101 
102         try {
103             session = openSession();
104             session.delete(tagsSource);
105             session.flush();
106 
107             return tagsSource;
108         }
109         catch (Exception e) {
110             throw HibernateUtil.processException(e);
111         }
112         finally {
113             closeSession(session);
114             FinderCache.clearCache(TagsSource.class.getName());
115         }
116     }
117 
118     public TagsSource update(
119         com.liferay.portlet.tags.model.TagsSource tagsSource)
120         throws SystemException {
121         return update(tagsSource, false);
122     }
123 
124     public TagsSource update(
125         com.liferay.portlet.tags.model.TagsSource tagsSource, boolean merge)
126         throws SystemException {
127         Session session = null;
128 
129         try {
130             session = openSession();
131 
132             if (merge) {
133                 session.merge(tagsSource);
134             }
135             else {
136                 if (tagsSource.isNew()) {
137                     session.save(tagsSource);
138                 }
139             }
140 
141             session.flush();
142             tagsSource.setNew(false);
143 
144             return tagsSource;
145         }
146         catch (Exception e) {
147             throw HibernateUtil.processException(e);
148         }
149         finally {
150             closeSession(session);
151             FinderCache.clearCache(TagsSource.class.getName());
152         }
153     }
154 
155     public TagsSource findByPrimaryKey(long sourceId)
156         throws NoSuchSourceException, SystemException {
157         TagsSource tagsSource = fetchByPrimaryKey(sourceId);
158 
159         if (tagsSource == null) {
160             if (_log.isWarnEnabled()) {
161                 _log.warn("No TagsSource exists with the primary key " +
162                     sourceId);
163             }
164 
165             throw new NoSuchSourceException(
166                 "No TagsSource exists with the primary key " + sourceId);
167         }
168 
169         return tagsSource;
170     }
171 
172     public TagsSource fetchByPrimaryKey(long sourceId)
173         throws SystemException {
174         Session session = null;
175 
176         try {
177             session = openSession();
178 
179             return (TagsSource)session.get(TagsSourceImpl.class,
180                 new Long(sourceId));
181         }
182         catch (Exception e) {
183             throw HibernateUtil.processException(e);
184         }
185         finally {
186             closeSession(session);
187         }
188     }
189 
190     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
191         throws SystemException {
192         Session session = null;
193 
194         try {
195             session = openSession();
196 
197             DynamicQuery query = queryInitializer.initialize(session);
198 
199             return query.list();
200         }
201         catch (Exception e) {
202             throw HibernateUtil.processException(e);
203         }
204         finally {
205             closeSession(session);
206         }
207     }
208 
209     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
210         int begin, int end) throws SystemException {
211         Session session = null;
212 
213         try {
214             session = openSession();
215 
216             DynamicQuery query = queryInitializer.initialize(session);
217             query.setLimit(begin, end);
218 
219             return query.list();
220         }
221         catch (Exception e) {
222             throw HibernateUtil.processException(e);
223         }
224         finally {
225             closeSession(session);
226         }
227     }
228 
229     public List findAll() throws SystemException {
230         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
231     }
232 
233     public List findAll(int begin, int end) throws SystemException {
234         return findAll(begin, end, null);
235     }
236 
237     public List findAll(int begin, int end, OrderByComparator obc)
238         throws SystemException {
239         String finderClassName = TagsSource.class.getName();
240         String finderMethodName = "findAll";
241         String[] finderParams = new String[] {
242                 "java.lang.Integer", "java.lang.Integer",
243                 "com.liferay.portal.kernel.util.OrderByComparator"
244             };
245         Object[] finderArgs = new Object[] {
246                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
247             };
248         Object result = FinderCache.getResult(finderClassName,
249                 finderMethodName, finderParams, finderArgs, getSessionFactory());
250 
251         if (result == null) {
252             Session session = null;
253 
254             try {
255                 session = openSession();
256 
257                 StringMaker query = new StringMaker();
258                 query.append("FROM com.liferay.portlet.tags.model.TagsSource ");
259 
260                 if (obc != null) {
261                     query.append("ORDER BY ");
262                     query.append(obc.getOrderBy());
263                 }
264 
265                 Query q = session.createQuery(query.toString());
266                 List list = QueryUtil.list(q, getDialect(), begin, end);
267 
268                 if (obc == null) {
269                     Collections.sort(list);
270                 }
271 
272                 FinderCache.putResult(finderClassName, finderMethodName,
273                     finderParams, finderArgs, list);
274 
275                 return list;
276             }
277             catch (Exception e) {
278                 throw HibernateUtil.processException(e);
279             }
280             finally {
281                 closeSession(session);
282             }
283         }
284         else {
285             return (List)result;
286         }
287     }
288 
289     public void removeAll() throws SystemException {
290         Iterator itr = findAll().iterator();
291 
292         while (itr.hasNext()) {
293             remove((TagsSource)itr.next());
294         }
295     }
296 
297     public int countAll() throws SystemException {
298         String finderClassName = TagsSource.class.getName();
299         String finderMethodName = "countAll";
300         String[] finderParams = new String[] {  };
301         Object[] finderArgs = new Object[] {  };
302         Object result = FinderCache.getResult(finderClassName,
303                 finderMethodName, finderParams, finderArgs, getSessionFactory());
304 
305         if (result == null) {
306             Session session = null;
307 
308             try {
309                 session = openSession();
310 
311                 StringMaker query = new StringMaker();
312                 query.append("SELECT COUNT(*) ");
313                 query.append("FROM com.liferay.portlet.tags.model.TagsSource");
314 
315                 Query q = session.createQuery(query.toString());
316                 Long count = null;
317                 Iterator itr = q.list().iterator();
318 
319                 if (itr.hasNext()) {
320                     count = (Long)itr.next();
321                 }
322 
323                 if (count == null) {
324                     count = new Long(0);
325                 }
326 
327                 FinderCache.putResult(finderClassName, finderMethodName,
328                     finderParams, finderArgs, count);
329 
330                 return count.intValue();
331             }
332             catch (Exception e) {
333                 throw HibernateUtil.processException(e);
334             }
335             finally {
336                 closeSession(session);
337             }
338         }
339         else {
340             return ((Long)result).intValue();
341         }
342     }
343 
344     protected void initDao() {
345     }
346 
347     private static Log _log = LogFactory.getLog(TagsSourcePersistenceImpl.class);
348 }