1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.services.persistence.generic.dao.impl;
18
19 import ch.elca.el4j.services.persistence.generic.dao.GenericDao;
20
21 /**
22 * This class extends the {@link DefaultDaoRegistry} with a fallback function:
23 * If no DAO can be found for an entityType, a generic DAO is created.
24 *
25 * @svnLink $Revision: 4071 $;$Date: 2010-01-06 10:07:00 +0100 (Mi, 06. Jan 2010) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/services/persistence/generic/dao/impl/FallbackDaoRegistry.java $
26 *
27 * @author Stefan Wismer (SWI)
28 */
29 public class FallbackDaoRegistry extends DefaultDaoRegistry {
30 /**
31 * The bean name of the fallback DAO prototype.
32 */
33 protected String m_daoPrototypeBeanName;
34
35 /** {@inheritDoc} */
36 @Override
37 public <T> GenericDao<T> getFor(Class<T> entityType) {
38 GenericDao<T> dao = super.getFor(entityType);
39
40 /* if no dao was found, try to create one automatically! */
41 if (dao == null) {
42 dao = createFor(entityType);
43 m_beanClassDaos.put(entityType, dao);
44 }
45
46 return dao;
47 }
48
49 /**
50 * @param <T> The class of entityType
51 * @param entityType
52 * The domain class for which a generic DAO should be returned.
53 * @return A new generic DAO for this entityType
54 */
55 @SuppressWarnings("unchecked")
56 protected <T> GenericDao<T> createFor(Class<T> entityType) {
57 GenericDao<T> dao = null;
58 if (m_daoPrototypeBeanName != null) {
59 dao = (GenericDao<T>) m_applicationContext.getBean(
60 m_daoPrototypeBeanName);
61 if (dao != null) {
62 dao.setPersistentClass(entityType);
63 }
64 }
65
66 return dao;
67 }
68
69 /**
70 * @return The bean name of the fallback DAO prototype.
71 */
72 public String getDaoPrototypeBeanName() {
73 return m_daoPrototypeBeanName;
74 }
75
76 /**
77 * @param daoPrototypeBeanName
78 * The bean name of the fallback DAO prototype.
79 */
80 public void setDaoPrototypeBeanName(String daoPrototypeBeanName) {
81 m_daoPrototypeBeanName = daoPrototypeBeanName;
82 }
83 }