View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.benerator.factory;
28  
29  import com.rapiddweller.benerator.Generator;
30  import com.rapiddweller.benerator.engine.BeneratorContext;
31  import com.rapiddweller.benerator.primitive.IncrementGenerator;
32  import com.rapiddweller.model.data.ComponentDescriptor;
33  import com.rapiddweller.model.data.IdDescriptor;
34  import com.rapiddweller.model.data.InstanceDescriptor;
35  import com.rapiddweller.model.data.Mode;
36  import com.rapiddweller.model.data.SimpleTypeDescriptor;
37  import com.rapiddweller.model.data.TypeDescriptor;
38  import com.rapiddweller.model.data.Uniqueness;
39  import org.apache.logging.log4j.LogManager;
40  import org.apache.logging.log4j.Logger;
41  
42  /**
43   * Creates entity generators from entity metadata.<br/>
44   * <br/>
45   * Created: 08.09.2007 07:45:40
46   *
47   * @author Volker Bergmann
48   */
49  public class InstanceGeneratorFactory {
50  
51    private static final Logger LOGGER = LogManager.getLogger(InstanceGeneratorFactory.class);
52  
53    // protected constructor for preventing instantiation --------------------------------------------------------------
54  
55    /**
56     * Instantiates a new Instance generator factory.
57     */
58    protected InstanceGeneratorFactory() {
59    }
60  
61    /**
62     * Create single instance generator generator.
63     *
64     * @param descriptor      the descriptor
65     * @param ownerUniqueness the owner uniqueness
66     * @param context         the context
67     * @return the generator
68     */
69    public static Generator<?> createSingleInstanceGenerator(
70        InstanceDescriptor descriptor, Uniqueness ownerUniqueness, BeneratorContext context) {
71      // check if nullQuota is 1
72      Generator<?> generator = DescriptorUtil.createNullQuotaOneGenerator(descriptor, context);
73      if (generator != null) {
74        return generator;
75      }
76  
77      // check uniqueness setting
78      Uniqueness uniqueness = DescriptorUtil.getUniqueness(descriptor, context);
79      if (!uniqueness.isUnique()) {
80        uniqueness = ownerUniqueness;
81      }
82  
83      // check nullability
84      boolean nullable = DescriptorUtil.isNullable(descriptor, context);
85  
86      // check 'ignored' setting
87      if (descriptor.getMode() == Mode.ignored) {
88        LOGGER.debug("Ignoring descriptor {}", descriptor);
89        return null;
90      }
91  
92      // create an appropriate generator
93      TypeDescriptor type = descriptor.getTypeDescriptor();
94      String instanceName = descriptor.getName();
95      if (type != null) {
96        generator = MetaGeneratorFactory.createTypeGenerator(type, instanceName, nullable, uniqueness, context);
97      } else {
98        ComponentDescriptor defaultConfig = context.getDefaultComponentConfig(instanceName);
99        if (defaultConfig != null) {
100         return createSingleInstanceGenerator(defaultConfig, ownerUniqueness, context);
101       }
102       if (nullable && DescriptorUtil.shouldNullifyEachNullable(descriptor, context)) {
103         return createNullGenerator(descriptor, context);
104       }
105       if (descriptor instanceof IdDescriptor) {
106         generator = new IncrementGenerator(1);
107       } else {
108         throw new UnsupportedOperationException("Type of " + instanceName + " is not defined");
109       }
110     }
111     GeneratorFactory generatorFactory = context.getGeneratorFactory();
112     generator = generatorFactory.applyNullSettings(generator, nullable, descriptor.getNullQuota());
113     return generator;
114   }
115 
116   /**
117    * Create configured default generator generator.
118    *
119    * @param componentName   the component name
120    * @param ownerUniqueness the owner uniqueness
121    * @param context         the context
122    * @return the generator
123    */
124   public static Generator<?> createConfiguredDefaultGenerator(String componentName, Uniqueness ownerUniqueness, BeneratorContext context) {
125     ComponentDescriptor defaultConfig = context.getDefaultComponentConfig(componentName);
126     if (defaultConfig != null) {
127       return createSingleInstanceGenerator(defaultConfig, ownerUniqueness, context);
128     }
129     return null;
130   }
131 
132   /**
133    * Create null generator generator.
134    *
135    * @param descriptor the descriptor
136    * @param context    the context
137    * @return the generator
138    */
139   protected static Generator<?> createNullGenerator(InstanceDescriptor descriptor, BeneratorContext context) {
140     Class<?> generatedType;
141     TypeDescriptor typeDescriptor = descriptor.getTypeDescriptor();
142     if (typeDescriptor instanceof SimpleTypeDescriptor) {
143       generatedType = ((SimpleTypeDescriptor) typeDescriptor).getPrimitiveType().getJavaType();
144     } else {
145       generatedType = String.class;
146     }
147     return context.getGeneratorFactory().createNullGenerator(generatedType);
148   }
149 
150 }