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.NoSuchClassNameException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.DynamicQuery;
28  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.ClassName;
33  import com.liferay.portal.model.impl.ClassNameImpl;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  import com.liferay.portal.spring.hibernate.FinderCache;
36  import com.liferay.portal.spring.hibernate.HibernateUtil;
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="ClassNamePersistenceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class ClassNamePersistenceImpl extends BasePersistence
57      implements ClassNamePersistence {
58      public ClassName create(long classNameId) {
59          ClassName className = new ClassNameImpl();
60          className.setNew(true);
61          className.setPrimaryKey(classNameId);
62  
63          return className;
64      }
65  
66      public ClassName remove(long classNameId)
67          throws NoSuchClassNameException, SystemException {
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              ClassName className = (ClassName)session.get(ClassNameImpl.class,
74                      new Long(classNameId));
75  
76              if (className == null) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn("No ClassName exists with the primary key " +
79                          classNameId);
80                  }
81  
82                  throw new NoSuchClassNameException(
83                      "No ClassName exists with the primary key " + classNameId);
84              }
85  
86              return remove(className);
87          }
88          catch (NoSuchClassNameException 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 ClassName remove(ClassName className) throws SystemException {
100         Session session = null;
101 
102         try {
103             session = openSession();
104             session.delete(className);
105             session.flush();
106 
107             return className;
108         }
109         catch (Exception e) {
110             throw HibernateUtil.processException(e);
111         }
112         finally {
113             closeSession(session);
114             FinderCache.clearCache(ClassName.class.getName());
115         }
116     }
117 
118     public ClassName update(com.liferay.portal.model.ClassName className)
119         throws SystemException {
120         return update(className, false);
121     }
122 
123     public ClassName update(com.liferay.portal.model.ClassName className,
124         boolean merge) throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             if (merge) {
131                 session.merge(className);
132             }
133             else {
134                 if (className.isNew()) {
135                     session.save(className);
136                 }
137             }
138 
139             session.flush();
140             className.setNew(false);
141 
142             return className;
143         }
144         catch (Exception e) {
145             throw HibernateUtil.processException(e);
146         }
147         finally {
148             closeSession(session);
149             FinderCache.clearCache(ClassName.class.getName());
150         }
151     }
152 
153     public ClassName findByPrimaryKey(long classNameId)
154         throws NoSuchClassNameException, SystemException {
155         ClassName className = fetchByPrimaryKey(classNameId);
156 
157         if (className == null) {
158             if (_log.isWarnEnabled()) {
159                 _log.warn("No ClassName exists with the primary key " +
160                     classNameId);
161             }
162 
163             throw new NoSuchClassNameException(
164                 "No ClassName exists with the primary key " + classNameId);
165         }
166 
167         return className;
168     }
169 
170     public ClassName fetchByPrimaryKey(long classNameId)
171         throws SystemException {
172         Session session = null;
173 
174         try {
175             session = openSession();
176 
177             return (ClassName)session.get(ClassNameImpl.class,
178                 new Long(classNameId));
179         }
180         catch (Exception e) {
181             throw HibernateUtil.processException(e);
182         }
183         finally {
184             closeSession(session);
185         }
186     }
187 
188     public ClassName findByValue(String value)
189         throws NoSuchClassNameException, SystemException {
190         ClassName className = fetchByValue(value);
191 
192         if (className == null) {
193             StringMaker msg = new StringMaker();
194             msg.append("No ClassName exists with the key ");
195             msg.append(StringPool.OPEN_CURLY_BRACE);
196             msg.append("value=");
197             msg.append(value);
198             msg.append(StringPool.CLOSE_CURLY_BRACE);
199 
200             if (_log.isWarnEnabled()) {
201                 _log.warn(msg.toString());
202             }
203 
204             throw new NoSuchClassNameException(msg.toString());
205         }
206 
207         return className;
208     }
209 
210     public ClassName fetchByValue(String value) throws SystemException {
211         String finderClassName = ClassName.class.getName();
212         String finderMethodName = "fetchByValue";
213         String[] finderParams = new String[] { String.class.getName() };
214         Object[] finderArgs = new Object[] { value };
215         Object result = FinderCache.getResult(finderClassName,
216                 finderMethodName, finderParams, finderArgs, getSessionFactory());
217 
218         if (result == null) {
219             Session session = null;
220 
221             try {
222                 session = openSession();
223 
224                 StringMaker query = new StringMaker();
225                 query.append("FROM com.liferay.portal.model.ClassName WHERE ");
226 
227                 if (value == null) {
228                     query.append("value IS NULL");
229                 }
230                 else {
231                     query.append("value = ?");
232                 }
233 
234                 query.append(" ");
235 
236                 Query q = session.createQuery(query.toString());
237                 int queryPos = 0;
238 
239                 if (value != null) {
240                     q.setString(queryPos++, value);
241                 }
242 
243                 List list = q.list();
244                 FinderCache.putResult(finderClassName, finderMethodName,
245                     finderParams, finderArgs, list);
246 
247                 if (list.size() == 0) {
248                     return null;
249                 }
250                 else {
251                     return (ClassName)list.get(0);
252                 }
253             }
254             catch (Exception e) {
255                 throw HibernateUtil.processException(e);
256             }
257             finally {
258                 closeSession(session);
259             }
260         }
261         else {
262             List list = (List)result;
263 
264             if (list.size() == 0) {
265                 return null;
266             }
267             else {
268                 return (ClassName)list.get(0);
269             }
270         }
271     }
272 
273     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
274         throws SystemException {
275         Session session = null;
276 
277         try {
278             session = openSession();
279 
280             DynamicQuery query = queryInitializer.initialize(session);
281 
282             return query.list();
283         }
284         catch (Exception e) {
285             throw HibernateUtil.processException(e);
286         }
287         finally {
288             closeSession(session);
289         }
290     }
291 
292     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
293         int begin, int end) throws SystemException {
294         Session session = null;
295 
296         try {
297             session = openSession();
298 
299             DynamicQuery query = queryInitializer.initialize(session);
300             query.setLimit(begin, end);
301 
302             return query.list();
303         }
304         catch (Exception e) {
305             throw HibernateUtil.processException(e);
306         }
307         finally {
308             closeSession(session);
309         }
310     }
311 
312     public List findAll() throws SystemException {
313         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
314     }
315 
316     public List findAll(int begin, int end) throws SystemException {
317         return findAll(begin, end, null);
318     }
319 
320     public List findAll(int begin, int end, OrderByComparator obc)
321         throws SystemException {
322         String finderClassName = ClassName.class.getName();
323         String finderMethodName = "findAll";
324         String[] finderParams = new String[] {
325                 "java.lang.Integer", "java.lang.Integer",
326                 "com.liferay.portal.kernel.util.OrderByComparator"
327             };
328         Object[] finderArgs = new Object[] {
329                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
330             };
331         Object result = FinderCache.getResult(finderClassName,
332                 finderMethodName, finderParams, finderArgs, getSessionFactory());
333 
334         if (result == null) {
335             Session session = null;
336 
337             try {
338                 session = openSession();
339 
340                 StringMaker query = new StringMaker();
341                 query.append("FROM com.liferay.portal.model.ClassName ");
342 
343                 if (obc != null) {
344                     query.append("ORDER BY ");
345                     query.append(obc.getOrderBy());
346                 }
347 
348                 Query q = session.createQuery(query.toString());
349                 List list = QueryUtil.list(q, getDialect(), begin, end);
350 
351                 if (obc == null) {
352                     Collections.sort(list);
353                 }
354 
355                 FinderCache.putResult(finderClassName, finderMethodName,
356                     finderParams, finderArgs, list);
357 
358                 return list;
359             }
360             catch (Exception e) {
361                 throw HibernateUtil.processException(e);
362             }
363             finally {
364                 closeSession(session);
365             }
366         }
367         else {
368             return (List)result;
369         }
370     }
371 
372     public void removeByValue(String value)
373         throws NoSuchClassNameException, SystemException {
374         ClassName className = findByValue(value);
375         remove(className);
376     }
377 
378     public void removeAll() throws SystemException {
379         Iterator itr = findAll().iterator();
380 
381         while (itr.hasNext()) {
382             remove((ClassName)itr.next());
383         }
384     }
385 
386     public int countByValue(String value) throws SystemException {
387         String finderClassName = ClassName.class.getName();
388         String finderMethodName = "countByValue";
389         String[] finderParams = new String[] { String.class.getName() };
390         Object[] finderArgs = new Object[] { value };
391         Object result = FinderCache.getResult(finderClassName,
392                 finderMethodName, finderParams, finderArgs, getSessionFactory());
393 
394         if (result == null) {
395             Session session = null;
396 
397             try {
398                 session = openSession();
399 
400                 StringMaker query = new StringMaker();
401                 query.append("SELECT COUNT(*) ");
402                 query.append("FROM com.liferay.portal.model.ClassName WHERE ");
403 
404                 if (value == null) {
405                     query.append("value IS NULL");
406                 }
407                 else {
408                     query.append("value = ?");
409                 }
410 
411                 query.append(" ");
412 
413                 Query q = session.createQuery(query.toString());
414                 int queryPos = 0;
415 
416                 if (value != null) {
417                     q.setString(queryPos++, value);
418                 }
419 
420                 Long count = null;
421                 Iterator itr = q.list().iterator();
422 
423                 if (itr.hasNext()) {
424                     count = (Long)itr.next();
425                 }
426 
427                 if (count == null) {
428                     count = new Long(0);
429                 }
430 
431                 FinderCache.putResult(finderClassName, finderMethodName,
432                     finderParams, finderArgs, count);
433 
434                 return count.intValue();
435             }
436             catch (Exception e) {
437                 throw HibernateUtil.processException(e);
438             }
439             finally {
440                 closeSession(session);
441             }
442         }
443         else {
444             return ((Long)result).intValue();
445         }
446     }
447 
448     public int countAll() throws SystemException {
449         String finderClassName = ClassName.class.getName();
450         String finderMethodName = "countAll";
451         String[] finderParams = new String[] {  };
452         Object[] finderArgs = new Object[] {  };
453         Object result = FinderCache.getResult(finderClassName,
454                 finderMethodName, finderParams, finderArgs, getSessionFactory());
455 
456         if (result == null) {
457             Session session = null;
458 
459             try {
460                 session = openSession();
461 
462                 StringMaker query = new StringMaker();
463                 query.append("SELECT COUNT(*) ");
464                 query.append("FROM com.liferay.portal.model.ClassName");
465 
466                 Query q = session.createQuery(query.toString());
467                 Long count = null;
468                 Iterator itr = q.list().iterator();
469 
470                 if (itr.hasNext()) {
471                     count = (Long)itr.next();
472                 }
473 
474                 if (count == null) {
475                     count = new Long(0);
476                 }
477 
478                 FinderCache.putResult(finderClassName, finderMethodName,
479                     finderParams, finderArgs, count);
480 
481                 return count.intValue();
482             }
483             catch (Exception e) {
484                 throw HibernateUtil.processException(e);
485             }
486             finally {
487                 closeSession(session);
488             }
489         }
490         else {
491             return ((Long)result).intValue();
492         }
493     }
494 
495     protected void initDao() {
496     }
497 
498     private static Log _log = LogFactory.getLog(ClassNamePersistenceImpl.class);
499 }