001: package com.bm.creators;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import org.apache.log4j.Logger;
007:
008: import com.bm.datagen.Generator;
009: import com.bm.datagen.empty.EmptyCollection;
010: import com.bm.datagen.random.RandomDateGenerator;
011: import com.bm.datagen.random.RandomIntegerGenerator;
012: import com.bm.datagen.random.RandomLongGenerator;
013: import com.bm.datagen.random.RandomStringGenerator;
014: import com.bm.datagen.random.primitive.PrimitiveRandomBooleanGenerator;
015: import com.bm.datagen.random.primitive.PrimitiveRandomDoubleGenerator;
016: import com.bm.datagen.random.primitive.PrimitiveRandomFloatGenerator;
017: import com.bm.datagen.random.primitive.PrimitiveRandomShortGenerator;
018: import com.bm.introspectors.EmbeddedClassIntrospector;
019: import com.bm.introspectors.EntityBeanIntrospector;
020:
021: /**
022: * This class creates entity beans with random data.
023: *
024: * @author Daniel Wiese
025: * @param <T> -
026: * the type of the entity beans which will be created
027: * @since 07.10.2005
028: */
029: public class EntityBeanCreator<T> {
030:
031: private static final Logger log = Logger
032: .getLogger(EntityBeanCreator.class);
033:
034: private static final List<Generator> DEFAULT_GENERATORS = new ArrayList<Generator>();
035:
036: private final EntityBeanIntrospector intro;
037:
038: private final EntityInstanceCreator<T> baseCreator;
039:
040: private final List<Generator> currentGeneratorList = new ArrayList<Generator>();
041:
042: static {
043: // dafault configuration
044: DEFAULT_GENERATORS.add(new RandomDateGenerator());
045: DEFAULT_GENERATORS.add(new PrimitiveRandomBooleanGenerator());
046: DEFAULT_GENERATORS.add(new RandomIntegerGenerator());
047: DEFAULT_GENERATORS.add(new RandomLongGenerator());
048: DEFAULT_GENERATORS.add(new PrimitiveRandomFloatGenerator());
049: DEFAULT_GENERATORS.add(new PrimitiveRandomShortGenerator());
050: DEFAULT_GENERATORS.add(new RandomStringGenerator());
051: DEFAULT_GENERATORS.add(new PrimitiveRandomDoubleGenerator());
052: DEFAULT_GENERATORS.add(new EmptyCollection());
053: }
054:
055: /**
056: * Deafult constructor.
057: *
058: * @param toCreate -
059: * the class to create
060: */
061: public EntityBeanCreator(Class<T> toCreate) {
062: this (new EntityBeanIntrospector<T>(toCreate), toCreate);
063: }
064:
065: /**
066: * Deafult constructor.
067: *
068: * @param intro -
069: * a isntance of bean introspection
070: * @param toCreate -
071: * the class to create
072: */
073: public EntityBeanCreator(EntityBeanIntrospector<T> intro,
074: Class<T> toCreate) {
075: this .intro = intro;
076: this .currentGeneratorList.addAll(DEFAULT_GENERATORS);
077: this .baseCreator = new EntityInstanceCreator<T>(intro,
078: toCreate, this .currentGeneratorList);
079: }
080:
081: /**
082: * Constructor with special generator list.
083: *
084: * @param intro -
085: * a isntance of bean introspection
086: * @param toCreate -
087: * the class to create
088: * @param additionalGenerators -
089: * additional generators
090: */
091: public EntityBeanCreator(EntityBeanIntrospector<T> intro,
092: Class<T> toCreate, List<Generator> additionalGenerators) {
093: this .intro = intro;
094: this .currentGeneratorList.addAll(DEFAULT_GENERATORS);
095: this .currentGeneratorList.addAll(additionalGenerators);
096: this .baseCreator = new EntityInstanceCreator<T>(intro,
097: toCreate, this .currentGeneratorList);
098: }
099:
100: /**
101: * This method should be called every time before an entity creation task >
102: * this method tells every generator to call his prepare method(annotation).
103: */
104: public void prepare() {
105: this .baseCreator.prepare();
106: }
107:
108: /**
109: * This method should be called every time before an entity creation task >
110: * this method tells every generator to call his clenup method (annotation).
111: */
112: public void cleanup() {
113: this .baseCreator.cleanup();
114: }
115:
116: /**
117: * Creates a instance of the entyty bean (filled with random data) without
118: * any persistence operations.
119: *
120: * @return -a random instance of the entity bean
121: */
122: @SuppressWarnings("unchecked")
123: public T createBeanInstance() {
124: // for error messages
125: final T back = this .baseCreator.createInstance();
126: if (this .intro.hasEmbeddedPKClass()) {
127: try {
128: final EmbeddedClassIntrospector emInspector = this .intro
129: .getEmbeddedPKClass();
130: final EntityInstanceCreator embCreator = new EntityInstanceCreator(
131: emInspector,
132: emInspector.getEmbeddedClassName(),
133: this .currentGeneratorList);
134: final Object embeddedInstance = embCreator
135: .createInstance();
136: this .intro
137: .setField(back, emInspector.getAttibuteName(),
138: embeddedInstance);
139: } catch (IllegalAccessException e) {
140: log.error("Cannot create the Embedded PK-Class", e);
141: throw new RuntimeException(
142: "Cannot create the Embedded PK-Class", e);
143: }
144:
145: }
146:
147: return back;
148:
149: }
150:
151: }
|