001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.persistence;
022:
023: import com.liferay.portal.NoSuchPasswordPolicyException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.dao.DynamicQuery;
026: import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
027: import com.liferay.portal.kernel.util.GetterUtil;
028: import com.liferay.portal.kernel.util.OrderByComparator;
029: import com.liferay.portal.kernel.util.StringMaker;
030: import com.liferay.portal.kernel.util.StringPool;
031: import com.liferay.portal.kernel.util.Validator;
032: import com.liferay.portal.model.ModelListener;
033: import com.liferay.portal.model.PasswordPolicy;
034: import com.liferay.portal.model.impl.PasswordPolicyImpl;
035: import com.liferay.portal.model.impl.PasswordPolicyModelImpl;
036: import com.liferay.portal.spring.hibernate.FinderCache;
037: import com.liferay.portal.spring.hibernate.HibernateUtil;
038: import com.liferay.portal.util.PropsUtil;
039:
040: import com.liferay.util.dao.hibernate.QueryUtil;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044:
045: import org.hibernate.Query;
046: import org.hibernate.Session;
047:
048: import java.util.Collections;
049: import java.util.Iterator;
050: import java.util.List;
051:
052: /**
053: * <a href="PasswordPolicyPersistenceImpl.java.html"><b><i>View Source</i></b></a>
054: *
055: * @author Brian Wing Shun Chan
056: *
057: */
058: public class PasswordPolicyPersistenceImpl extends BasePersistence
059: implements PasswordPolicyPersistence {
060: public PasswordPolicy create(long passwordPolicyId) {
061: PasswordPolicy passwordPolicy = new PasswordPolicyImpl();
062:
063: passwordPolicy.setNew(true);
064: passwordPolicy.setPrimaryKey(passwordPolicyId);
065:
066: return passwordPolicy;
067: }
068:
069: public PasswordPolicy remove(long passwordPolicyId)
070: throws NoSuchPasswordPolicyException, SystemException {
071: Session session = null;
072:
073: try {
074: session = openSession();
075:
076: PasswordPolicy passwordPolicy = (PasswordPolicy) session
077: .get(PasswordPolicyImpl.class, new Long(
078: passwordPolicyId));
079:
080: if (passwordPolicy == null) {
081: if (_log.isWarnEnabled()) {
082: _log
083: .warn("No PasswordPolicy exists with the primary key "
084: + passwordPolicyId);
085: }
086:
087: throw new NoSuchPasswordPolicyException(
088: "No PasswordPolicy exists with the primary key "
089: + passwordPolicyId);
090: }
091:
092: return remove(passwordPolicy);
093: } catch (NoSuchPasswordPolicyException nsee) {
094: throw nsee;
095: } catch (Exception e) {
096: throw HibernateUtil.processException(e);
097: } finally {
098: closeSession(session);
099: }
100: }
101:
102: public PasswordPolicy remove(PasswordPolicy passwordPolicy)
103: throws SystemException {
104: ModelListener listener = _getListener();
105:
106: if (listener != null) {
107: listener.onBeforeRemove(passwordPolicy);
108: }
109:
110: passwordPolicy = removeImpl(passwordPolicy);
111:
112: if (listener != null) {
113: listener.onAfterRemove(passwordPolicy);
114: }
115:
116: return passwordPolicy;
117: }
118:
119: protected PasswordPolicy removeImpl(PasswordPolicy passwordPolicy)
120: throws SystemException {
121: Session session = null;
122:
123: try {
124: session = openSession();
125:
126: session.delete(passwordPolicy);
127:
128: session.flush();
129:
130: return passwordPolicy;
131: } catch (Exception e) {
132: throw HibernateUtil.processException(e);
133: } finally {
134: closeSession(session);
135:
136: FinderCache.clearCache(PasswordPolicy.class.getName());
137: }
138: }
139:
140: public PasswordPolicy update(PasswordPolicy passwordPolicy)
141: throws SystemException {
142: return update(passwordPolicy, false);
143: }
144:
145: public PasswordPolicy update(PasswordPolicy passwordPolicy,
146: boolean merge) throws SystemException {
147: ModelListener listener = _getListener();
148:
149: boolean isNew = passwordPolicy.isNew();
150:
151: if (listener != null) {
152: if (isNew) {
153: listener.onBeforeCreate(passwordPolicy);
154: } else {
155: listener.onBeforeUpdate(passwordPolicy);
156: }
157: }
158:
159: passwordPolicy = updateImpl(passwordPolicy, merge);
160:
161: if (listener != null) {
162: if (isNew) {
163: listener.onAfterCreate(passwordPolicy);
164: } else {
165: listener.onAfterUpdate(passwordPolicy);
166: }
167: }
168:
169: return passwordPolicy;
170: }
171:
172: public PasswordPolicy updateImpl(
173: com.liferay.portal.model.PasswordPolicy passwordPolicy,
174: boolean merge) throws SystemException {
175: Session session = null;
176:
177: try {
178: session = openSession();
179:
180: if (merge) {
181: session.merge(passwordPolicy);
182: } else {
183: if (passwordPolicy.isNew()) {
184: session.save(passwordPolicy);
185: }
186: }
187:
188: session.flush();
189:
190: passwordPolicy.setNew(false);
191:
192: return passwordPolicy;
193: } catch (Exception e) {
194: throw HibernateUtil.processException(e);
195: } finally {
196: closeSession(session);
197:
198: FinderCache.clearCache(PasswordPolicy.class.getName());
199: }
200: }
201:
202: public PasswordPolicy findByPrimaryKey(long passwordPolicyId)
203: throws NoSuchPasswordPolicyException, SystemException {
204: PasswordPolicy passwordPolicy = fetchByPrimaryKey(passwordPolicyId);
205:
206: if (passwordPolicy == null) {
207: if (_log.isWarnEnabled()) {
208: _log
209: .warn("No PasswordPolicy exists with the primary key "
210: + passwordPolicyId);
211: }
212:
213: throw new NoSuchPasswordPolicyException(
214: "No PasswordPolicy exists with the primary key "
215: + passwordPolicyId);
216: }
217:
218: return passwordPolicy;
219: }
220:
221: public PasswordPolicy fetchByPrimaryKey(long passwordPolicyId)
222: throws SystemException {
223: Session session = null;
224:
225: try {
226: session = openSession();
227:
228: return (PasswordPolicy) session.get(
229: PasswordPolicyImpl.class,
230: new Long(passwordPolicyId));
231: } catch (Exception e) {
232: throw HibernateUtil.processException(e);
233: } finally {
234: closeSession(session);
235: }
236: }
237:
238: public PasswordPolicy findByC_DP(long companyId,
239: boolean defaultPolicy)
240: throws NoSuchPasswordPolicyException, SystemException {
241: PasswordPolicy passwordPolicy = fetchByC_DP(companyId,
242: defaultPolicy);
243:
244: if (passwordPolicy == null) {
245: StringMaker msg = new StringMaker();
246:
247: msg.append("No PasswordPolicy exists with the key {");
248:
249: msg.append("companyId=" + companyId);
250:
251: msg.append(", ");
252: msg.append("defaultPolicy=" + defaultPolicy);
253:
254: msg.append(StringPool.CLOSE_CURLY_BRACE);
255:
256: if (_log.isWarnEnabled()) {
257: _log.warn(msg.toString());
258: }
259:
260: throw new NoSuchPasswordPolicyException(msg.toString());
261: }
262:
263: return passwordPolicy;
264: }
265:
266: public PasswordPolicy fetchByC_DP(long companyId,
267: boolean defaultPolicy) throws SystemException {
268: boolean finderClassNameCacheEnabled = PasswordPolicyModelImpl.CACHE_ENABLED;
269: String finderClassName = PasswordPolicy.class.getName();
270: String finderMethodName = "fetchByC_DP";
271: String[] finderParams = new String[] { Long.class.getName(),
272: Boolean.class.getName() };
273: Object[] finderArgs = new Object[] { new Long(companyId),
274: Boolean.valueOf(defaultPolicy) };
275:
276: Object result = null;
277:
278: if (finderClassNameCacheEnabled) {
279: result = FinderCache.getResult(finderClassName,
280: finderMethodName, finderParams, finderArgs,
281: getSessionFactory());
282: }
283:
284: if (result == null) {
285: Session session = null;
286:
287: try {
288: session = openSession();
289:
290: StringMaker query = new StringMaker();
291:
292: query
293: .append("FROM com.liferay.portal.model.PasswordPolicy WHERE ");
294:
295: query.append("companyId = ?");
296:
297: query.append(" AND ");
298:
299: query.append("defaultPolicy = ?");
300:
301: query.append(" ");
302:
303: Query q = session.createQuery(query.toString());
304:
305: int queryPos = 0;
306:
307: q.setLong(queryPos++, companyId);
308:
309: q.setBoolean(queryPos++, defaultPolicy);
310:
311: List list = q.list();
312:
313: FinderCache.putResult(finderClassNameCacheEnabled,
314: finderClassName, finderMethodName,
315: finderParams, finderArgs, list);
316:
317: if (list.size() == 0) {
318: return null;
319: } else {
320: return (PasswordPolicy) list.get(0);
321: }
322: } catch (Exception e) {
323: throw HibernateUtil.processException(e);
324: } finally {
325: closeSession(session);
326: }
327: } else {
328: List list = (List) result;
329:
330: if (list.size() == 0) {
331: return null;
332: } else {
333: return (PasswordPolicy) list.get(0);
334: }
335: }
336: }
337:
338: public PasswordPolicy findByC_N(long companyId, String name)
339: throws NoSuchPasswordPolicyException, SystemException {
340: PasswordPolicy passwordPolicy = fetchByC_N(companyId, name);
341:
342: if (passwordPolicy == null) {
343: StringMaker msg = new StringMaker();
344:
345: msg.append("No PasswordPolicy exists with the key {");
346:
347: msg.append("companyId=" + companyId);
348:
349: msg.append(", ");
350: msg.append("name=" + name);
351:
352: msg.append(StringPool.CLOSE_CURLY_BRACE);
353:
354: if (_log.isWarnEnabled()) {
355: _log.warn(msg.toString());
356: }
357:
358: throw new NoSuchPasswordPolicyException(msg.toString());
359: }
360:
361: return passwordPolicy;
362: }
363:
364: public PasswordPolicy fetchByC_N(long companyId, String name)
365: throws SystemException {
366: boolean finderClassNameCacheEnabled = PasswordPolicyModelImpl.CACHE_ENABLED;
367: String finderClassName = PasswordPolicy.class.getName();
368: String finderMethodName = "fetchByC_N";
369: String[] finderParams = new String[] { Long.class.getName(),
370: String.class.getName() };
371: Object[] finderArgs = new Object[] { new Long(companyId), name };
372:
373: Object result = null;
374:
375: if (finderClassNameCacheEnabled) {
376: result = FinderCache.getResult(finderClassName,
377: finderMethodName, finderParams, finderArgs,
378: getSessionFactory());
379: }
380:
381: if (result == null) {
382: Session session = null;
383:
384: try {
385: session = openSession();
386:
387: StringMaker query = new StringMaker();
388:
389: query
390: .append("FROM com.liferay.portal.model.PasswordPolicy WHERE ");
391:
392: query.append("companyId = ?");
393:
394: query.append(" AND ");
395:
396: if (name == null) {
397: query.append("name IS NULL");
398: } else {
399: query.append("name = ?");
400: }
401:
402: query.append(" ");
403:
404: Query q = session.createQuery(query.toString());
405:
406: int queryPos = 0;
407:
408: q.setLong(queryPos++, companyId);
409:
410: if (name != null) {
411: q.setString(queryPos++, name);
412: }
413:
414: List list = q.list();
415:
416: FinderCache.putResult(finderClassNameCacheEnabled,
417: finderClassName, finderMethodName,
418: finderParams, finderArgs, list);
419:
420: if (list.size() == 0) {
421: return null;
422: } else {
423: return (PasswordPolicy) list.get(0);
424: }
425: } catch (Exception e) {
426: throw HibernateUtil.processException(e);
427: } finally {
428: closeSession(session);
429: }
430: } else {
431: List list = (List) result;
432:
433: if (list.size() == 0) {
434: return null;
435: } else {
436: return (PasswordPolicy) list.get(0);
437: }
438: }
439: }
440:
441: public List findWithDynamicQuery(
442: DynamicQueryInitializer queryInitializer)
443: throws SystemException {
444: Session session = null;
445:
446: try {
447: session = openSession();
448:
449: DynamicQuery query = queryInitializer.initialize(session);
450:
451: return query.list();
452: } catch (Exception e) {
453: throw HibernateUtil.processException(e);
454: } finally {
455: closeSession(session);
456: }
457: }
458:
459: public List findWithDynamicQuery(
460: DynamicQueryInitializer queryInitializer, int begin, int end)
461: throws SystemException {
462: Session session = null;
463:
464: try {
465: session = openSession();
466:
467: DynamicQuery query = queryInitializer.initialize(session);
468:
469: query.setLimit(begin, end);
470:
471: return query.list();
472: } catch (Exception e) {
473: throw HibernateUtil.processException(e);
474: } finally {
475: closeSession(session);
476: }
477: }
478:
479: public List findAll() throws SystemException {
480: return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
481: }
482:
483: public List findAll(int begin, int end) throws SystemException {
484: return findAll(begin, end, null);
485: }
486:
487: public List findAll(int begin, int end, OrderByComparator obc)
488: throws SystemException {
489: boolean finderClassNameCacheEnabled = PasswordPolicyModelImpl.CACHE_ENABLED;
490: String finderClassName = PasswordPolicy.class.getName();
491: String finderMethodName = "findAll";
492: String[] finderParams = new String[] { "java.lang.Integer",
493: "java.lang.Integer",
494: "com.liferay.portal.kernel.util.OrderByComparator" };
495: Object[] finderArgs = new Object[] { String.valueOf(begin),
496: String.valueOf(end), String.valueOf(obc) };
497:
498: Object result = null;
499:
500: if (finderClassNameCacheEnabled) {
501: result = FinderCache.getResult(finderClassName,
502: finderMethodName, finderParams, finderArgs,
503: getSessionFactory());
504: }
505:
506: if (result == null) {
507: Session session = null;
508:
509: try {
510: session = openSession();
511:
512: StringMaker query = new StringMaker();
513:
514: query
515: .append("FROM com.liferay.portal.model.PasswordPolicy ");
516:
517: if (obc != null) {
518: query.append("ORDER BY ");
519: query.append(obc.getOrderBy());
520: }
521:
522: Query q = session.createQuery(query.toString());
523:
524: List list = QueryUtil.list(q, getDialect(), begin, end);
525:
526: if (obc == null) {
527: Collections.sort(list);
528: }
529:
530: FinderCache.putResult(finderClassNameCacheEnabled,
531: finderClassName, finderMethodName,
532: finderParams, finderArgs, list);
533:
534: return list;
535: } catch (Exception e) {
536: throw HibernateUtil.processException(e);
537: } finally {
538: closeSession(session);
539: }
540: } else {
541: return (List) result;
542: }
543: }
544:
545: public void removeByC_DP(long companyId, boolean defaultPolicy)
546: throws NoSuchPasswordPolicyException, SystemException {
547: PasswordPolicy passwordPolicy = findByC_DP(companyId,
548: defaultPolicy);
549:
550: remove(passwordPolicy);
551: }
552:
553: public void removeByC_N(long companyId, String name)
554: throws NoSuchPasswordPolicyException, SystemException {
555: PasswordPolicy passwordPolicy = findByC_N(companyId, name);
556:
557: remove(passwordPolicy);
558: }
559:
560: public void removeAll() throws SystemException {
561: Iterator itr = findAll().iterator();
562:
563: while (itr.hasNext()) {
564: remove((PasswordPolicy) itr.next());
565: }
566: }
567:
568: public int countByC_DP(long companyId, boolean defaultPolicy)
569: throws SystemException {
570: boolean finderClassNameCacheEnabled = PasswordPolicyModelImpl.CACHE_ENABLED;
571: String finderClassName = PasswordPolicy.class.getName();
572: String finderMethodName = "countByC_DP";
573: String[] finderParams = new String[] { Long.class.getName(),
574: Boolean.class.getName() };
575: Object[] finderArgs = new Object[] { new Long(companyId),
576: Boolean.valueOf(defaultPolicy) };
577:
578: Object result = null;
579:
580: if (finderClassNameCacheEnabled) {
581: result = FinderCache.getResult(finderClassName,
582: finderMethodName, finderParams, finderArgs,
583: getSessionFactory());
584: }
585:
586: if (result == null) {
587: Session session = null;
588:
589: try {
590: session = openSession();
591:
592: StringMaker query = new StringMaker();
593:
594: query.append("SELECT COUNT(*) ");
595: query
596: .append("FROM com.liferay.portal.model.PasswordPolicy WHERE ");
597:
598: query.append("companyId = ?");
599:
600: query.append(" AND ");
601:
602: query.append("defaultPolicy = ?");
603:
604: query.append(" ");
605:
606: Query q = session.createQuery(query.toString());
607:
608: int queryPos = 0;
609:
610: q.setLong(queryPos++, companyId);
611:
612: q.setBoolean(queryPos++, defaultPolicy);
613:
614: Long count = null;
615:
616: Iterator itr = q.list().iterator();
617:
618: if (itr.hasNext()) {
619: count = (Long) itr.next();
620: }
621:
622: if (count == null) {
623: count = new Long(0);
624: }
625:
626: FinderCache.putResult(finderClassNameCacheEnabled,
627: finderClassName, finderMethodName,
628: finderParams, finderArgs, count);
629:
630: return count.intValue();
631: } catch (Exception e) {
632: throw HibernateUtil.processException(e);
633: } finally {
634: closeSession(session);
635: }
636: } else {
637: return ((Long) result).intValue();
638: }
639: }
640:
641: public int countByC_N(long companyId, String name)
642: throws SystemException {
643: boolean finderClassNameCacheEnabled = PasswordPolicyModelImpl.CACHE_ENABLED;
644: String finderClassName = PasswordPolicy.class.getName();
645: String finderMethodName = "countByC_N";
646: String[] finderParams = new String[] { Long.class.getName(),
647: String.class.getName() };
648: Object[] finderArgs = new Object[] { new Long(companyId), name };
649:
650: Object result = null;
651:
652: if (finderClassNameCacheEnabled) {
653: result = FinderCache.getResult(finderClassName,
654: finderMethodName, finderParams, finderArgs,
655: getSessionFactory());
656: }
657:
658: if (result == null) {
659: Session session = null;
660:
661: try {
662: session = openSession();
663:
664: StringMaker query = new StringMaker();
665:
666: query.append("SELECT COUNT(*) ");
667: query
668: .append("FROM com.liferay.portal.model.PasswordPolicy WHERE ");
669:
670: query.append("companyId = ?");
671:
672: query.append(" AND ");
673:
674: if (name == null) {
675: query.append("name IS NULL");
676: } else {
677: query.append("name = ?");
678: }
679:
680: query.append(" ");
681:
682: Query q = session.createQuery(query.toString());
683:
684: int queryPos = 0;
685:
686: q.setLong(queryPos++, companyId);
687:
688: if (name != null) {
689: q.setString(queryPos++, name);
690: }
691:
692: Long count = null;
693:
694: Iterator itr = q.list().iterator();
695:
696: if (itr.hasNext()) {
697: count = (Long) itr.next();
698: }
699:
700: if (count == null) {
701: count = new Long(0);
702: }
703:
704: FinderCache.putResult(finderClassNameCacheEnabled,
705: finderClassName, finderMethodName,
706: finderParams, finderArgs, count);
707:
708: return count.intValue();
709: } catch (Exception e) {
710: throw HibernateUtil.processException(e);
711: } finally {
712: closeSession(session);
713: }
714: } else {
715: return ((Long) result).intValue();
716: }
717: }
718:
719: public int countAll() throws SystemException {
720: boolean finderClassNameCacheEnabled = PasswordPolicyModelImpl.CACHE_ENABLED;
721: String finderClassName = PasswordPolicy.class.getName();
722: String finderMethodName = "countAll";
723: String[] finderParams = new String[] {};
724: Object[] finderArgs = new Object[] {};
725:
726: Object result = null;
727:
728: if (finderClassNameCacheEnabled) {
729: result = FinderCache.getResult(finderClassName,
730: finderMethodName, finderParams, finderArgs,
731: getSessionFactory());
732: }
733:
734: if (result == null) {
735: Session session = null;
736:
737: try {
738: session = openSession();
739:
740: Query q = session
741: .createQuery("SELECT COUNT(*) FROM com.liferay.portal.model.PasswordPolicy");
742:
743: Long count = null;
744:
745: Iterator itr = q.list().iterator();
746:
747: if (itr.hasNext()) {
748: count = (Long) itr.next();
749: }
750:
751: if (count == null) {
752: count = new Long(0);
753: }
754:
755: FinderCache.putResult(finderClassNameCacheEnabled,
756: finderClassName, finderMethodName,
757: finderParams, finderArgs, count);
758:
759: return count.intValue();
760: } catch (Exception e) {
761: throw HibernateUtil.processException(e);
762: } finally {
763: closeSession(session);
764: }
765: } else {
766: return ((Long) result).intValue();
767: }
768: }
769:
770: protected void initDao() {
771: }
772:
773: private static ModelListener _getListener() {
774: if (Validator.isNotNull(_LISTENER)) {
775: try {
776: return (ModelListener) Class.forName(_LISTENER)
777: .newInstance();
778: } catch (Exception e) {
779: _log.error(e);
780: }
781: }
782:
783: return null;
784: }
785:
786: private static final String _LISTENER = GetterUtil
787: .getString(PropsUtil
788: .get("value.object.listener.com.liferay.portal.model.PasswordPolicy"));
789: private static Log _log = LogFactory
790: .getLog(PasswordPolicyPersistenceImpl.class);
791: }
|