001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.benerator.composite;
028:
029: import org.databene.model.data.Entity;
030: import org.databene.model.data.EntityDescriptor;
031: import org.databene.benerator.Generator;
032: import org.databene.benerator.wrapper.IdGenerator;
033: import org.databene.commons.Context;
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: import java.util.Map;
038:
039: /**
040: * Generates instances of an entity type as specified by its EntityDescriptor.<br/>
041: * <br/>
042: * Created: 27.06.2007 23:51:42
043: * @author Volker Bergmann
044: */
045: public class EntityGenerator implements Generator<Entity> {
046:
047: private static final Log stateLogger = LogFactory
048: .getLog("org.databene.benerator.STATE");
049:
050: private String entityName;
051: private Generator<Entity> source;
052: private Map<String, Generator<? extends Object>> componentGenerators;
053: private Context context;
054:
055: // constructors --------------------------------------------------------------------------------------
056:
057: /**
058: * @param descriptor Entity descriptor.
059: * @param componentGenerators Generators that generate values for the entities' components
060: */
061: public EntityGenerator(
062: EntityDescriptor descriptor,
063: Map<String, Generator<? extends Object>> componentGenerators,
064: Context context) {
065: this (descriptor, new SimpleEntityGenerator(descriptor),
066: componentGenerators, context);
067: }
068:
069: /**
070: * @param descriptor Entity descriptor.
071: * @param source another Generator of entities that serves as Entity builder.
072: * It may construct empty Entities or may import them (so this may overwrite imported attributes).
073: * @param componentGenerators Generators that generate values for the entities' components
074: */
075: public EntityGenerator(
076: EntityDescriptor descriptor,
077: Generator<Entity> source,
078: Map<String, Generator<? extends Object>> componentGenerators,
079: Context context) {
080: this .entityName = descriptor.getName();
081: this .source = source;
082: this .componentGenerators = componentGenerators;
083: this .context = context;
084: }
085:
086: // Generator interface -----------------------------------------------------------------------------------
087:
088: public Class<Entity> getGeneratedType() {
089: return Entity.class;
090: }
091:
092: public void validate() {
093: source.validate();
094: for (Generator<? extends Object> compGen : componentGenerators
095: .values())
096: compGen.validate();
097: }
098:
099: public boolean available() {
100: if (!source.available()) {
101: if (stateLogger.isDebugEnabled())
102: stateLogger.debug("Source for entity '" + entityName
103: + "' is not available any more: " + source);
104: return false;
105: }
106: for (Generator<? extends Object> compGen : componentGenerators
107: .values()) {
108: if (!compGen.available()) {
109: if (stateLogger.isDebugEnabled())
110: stateLogger
111: .debug("Generator for entity '"
112: + entityName
113: + "' is not available any more: "
114: + compGen);
115: return false;
116: }
117: }
118: return true;
119: }
120:
121: public Entity generate() {
122: Entity entity = source.generate();
123: context.set(this .entityName, entity);
124: for (Map.Entry<String, Generator<? extends Object>> entry : componentGenerators
125: .entrySet()) {
126: String componentName = entry.getKey();
127: try {
128: Object componentValue = entry.getValue().generate();
129: entity.setComponent(componentName, componentValue);
130: } catch (Exception e) {
131: throw new RuntimeException(
132: "Failure in generation of component '"
133: + componentName + "'", e);
134: }
135: }
136: return entity;
137: }
138:
139: public void close() {
140: source.close();
141: for (Generator<? extends Object> compGen : componentGenerators
142: .values())
143: if (!(compGen instanceof IdGenerator))
144: compGen.close();
145: }
146:
147: public void reset() {
148: source.reset();
149: for (Generator<? extends Object> compGen : componentGenerators
150: .values())
151: compGen.reset();
152: }
153:
154: // java.lang.Object overrides --------------------------------------------------------------------------
155:
156: public String toString() {
157: return getClass().getSimpleName() + '[' + entityName + ']'
158: + componentGenerators;
159: }
160:
161: }
|