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.dto;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import org.springframework.beans.factory.InitializingBean;
24
25 import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
26 import ch.elca.el4j.services.persistence.generic.primarykey.PrimaryKeyGenerator;
27
28 /**
29 * This class is used to create dto instances. Each dto will get a reference to
30 * the modification key generator. This is needed to enable optimistic locking.
31 * A dto must be created with this dto factory, when the dto must be written to
32 * database. If you only need a dto to get information from it and not to store
33 * it in database you can create the dto directly. But recommended is to create
34 * each dto with help of this dto factory.
35 *
36 * @svnLink $Revision: 3874 $;$Date: 2009-08-04 14:25:40 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$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/dto/DtoFactory.java $
37 *
38 * @deprecated This class in no more used if using
39 * <code>AbstractIntOptimisticLockingDto</code>.
40 * @see AbstractIntOptimisticLockingDto
41 * @author Martin Zeltner (MZE)
42 */
43 public class DtoFactory implements InitializingBean {
44 /**
45 * Primary key generator to generate modification keys for dtos.
46 */
47 private PrimaryKeyGenerator m_modificationKeyGenerator;
48
49 /**
50 * @return Returns the modificationKeyGenerator.
51 */
52 public PrimaryKeyGenerator getModificationKeyGenerator() {
53 return m_modificationKeyGenerator;
54 }
55
56 /**
57 * @param modificationKeyGenerator
58 * Is the modificationKeyGenerator to set.
59 */
60 public void setModificationKeyGenerator(
61 PrimaryKeyGenerator modificationKeyGenerator) {
62 m_modificationKeyGenerator = modificationKeyGenerator;
63 }
64
65 /**
66 * This method creates a new dto of the given class and fills it with a
67 * modification key generator.
68 *
69 * @param clazz
70 * Is the dto class.
71 * @return Returns the created dto.
72 */
73 public AbstractDto createDto(Class<?> clazz) {
74 Object o;
75 try {
76 o = clazz.newInstance();
77 } catch (Exception e) {
78 throw new RuntimeException(e);
79 }
80 return initializeDto(o);
81 }
82
83 /**
84 * This method sets the modification key generator of a dto.
85 *
86 * @param dto
87 * Where the modification key generator has to be set.
88 * @return Returns the modificated dto.
89 */
90 public AbstractDto initializeDto(Object dto) {
91 AbstractDto abstractDto = (AbstractDto) dto;
92 abstractDto.setModificationKeyGenerator(getModificationKeyGenerator());
93 return abstractDto;
94 }
95
96 /**
97 * This metod sets the modification key generator of given
98 * <code>AbstractDto</code>s. If the given list of dtos is
99 * <code>null</code> an empty List will be returned.
100 *
101 * @param dtos
102 * Where the modification key generator has to be set.
103 * @return Returns the dto list.
104 */
105 public List<Object> initializeDtos(List<Object> dtos) {
106 if (dtos == null) {
107 return new ArrayList<Object>();
108 }
109
110 Iterator<Object> it = dtos.iterator();
111 while (it.hasNext()) {
112 initializeDto(it.next());
113 }
114 return dtos;
115 }
116
117 /**
118 * {@inheritDoc}
119 */
120 public void afterPropertiesSet() throws Exception {
121 if (getModificationKeyGenerator() == null) {
122 CoreNotificationHelper.notifyLackingEssentialProperty(
123 "modificationKeyGenerator", this);
124 }
125 }
126 }