private static ComponentBuilder<?> builderFromGenerator( Generator<?> source, ComponentDescriptor descriptor, BeneratorContext context) { boolean nullability = DescriptorUtil.isNullable(descriptor, context); Double nullQuota = descriptor.getNullQuota(); if (nullQuota != null && nullQuota != 0) source = context.getGeneratorFactory().applyNullSettings(source, nullability, nullQuota); TypeDescriptor typeDescriptor = descriptor.getTypeDescriptor(); String scope = (typeDescriptor != null ? typeDescriptor.getScope() : null); if (descriptor instanceof ArrayElementDescriptor) { int index = ((ArrayElementDescriptor) descriptor).getIndex(); return new ArrayElementBuilder(index, source, scope); } else return new PlainEntityComponentBuilder(descriptor.getName(), source, scope); }
public boolean apply(E target, BeneratorContext context) { BeneratorContext subContext = context.createSubContext(instanceName); subContext.setCurrentProduct(new ProductWrapper<E>(target)); for (GeneratorComponent<E> component : components) { try { if (!component.execute(subContext)) { message = "Component generator for '" + instanceName + "' is not available any longer: " + component; STATE_LOGGER.debug(message); return false; } } catch (Exception e) { throw new RuntimeException( "Failure in generation of '" + instanceName + "', " + "Failed component: " + component, e); } } LOGGER.debug("Generated {}", target); subContext.close(); return true; }
@Override protected Generator<Object[]> createSourceGenerator( ArrayTypeDescriptor descriptor, Uniqueness uniqueness, BeneratorContext context) { // if no sourceObject is specified, there's nothing to do String sourceName = descriptor.getSource(); if (sourceName == null) return null; // create sourceObject generator Generator<Object[]> generator = null; Object contextSourceObject = context.get(sourceName); if (contextSourceObject != null) generator = createSourceGeneratorFromObject(descriptor, context, generator, contextSourceObject); else { String lcSourceName = sourceName.toLowerCase(); if (lcSourceName.endsWith(".csv")) generator = createCSVSourceGenerator(descriptor, context, sourceName); else if (lcSourceName.endsWith(".xls")) generator = createXLSSourceGenerator(descriptor, context, sourceName); else { try { BeanSpec sourceBeanSpec = DatabeneScriptParser.resolveBeanSpec(sourceName, context); Object sourceObject = sourceBeanSpec.getBean(); if (!sourceBeanSpec.isReference() && sourceObject instanceof ContextAware) ((ContextAware) sourceObject).setContext(context); generator = createSourceGeneratorFromObject(descriptor, context, generator, sourceObject); if (sourceBeanSpec.isReference()) generator = WrapperFactory.preventClosing(generator); return generator; } catch (Exception e) { throw new UnsupportedOperationException("Unknown source type: " + sourceName); } } } if (descriptor.getFilter() != null) { Expression<Boolean> filter = new ScriptExpression<Boolean>(ScriptUtil.parseScriptText(descriptor.getFilter())); generator = new FilteringGenerator<Object[]>(generator, filter); } Distribution distribution = FactoryUtil.getDistribution(descriptor.getDistribution(), uniqueness, false, context); if (distribution != null) generator = new DistributingGenerator<Object[]>(generator, distribution, uniqueness.isUnique()); return generator; }
@SuppressWarnings({"rawtypes", "unchecked"}) @Override protected Generator<?> applyComponentBuilders( Generator<?> generator, ArrayTypeDescriptor descriptor, String instanceName, Uniqueness uniqueness, BeneratorContext context) { Generator[] generators; // create synthetic element generators if necessary if (generator instanceof BlankArrayGenerator) { generators = createSyntheticElementGenerators(descriptor, uniqueness, context); generator = context .getGeneratorFactory() .createCompositeArrayGenerator(Object.class, generators, uniqueness); } // ... and don't forget to support the parent class' functionality generator = super.applyComponentBuilders(generator, descriptor, instanceName, uniqueness, context); return generator; }
private Generator<Object[]> createCSVSourceGenerator( ArrayTypeDescriptor arrayType, BeneratorContext context, String sourceName) { logger.debug("createCSVSourceGenerator({})", arrayType); String encoding = arrayType.getEncoding(); if (encoding == null) encoding = context.getDefaultEncoding(); char separator = DescriptorUtil.getSeparator(arrayType, context); boolean rowBased = (arrayType.isRowBased() != null ? arrayType.isRowBased() : true); DataSourceProvider<Object[]> factory = new CSVArraySourceProvider( arrayType.getName(), new ScriptConverterForStrings(context), rowBased, separator, encoding); Generator<Object[]> generator = SourceFactory.createRawSourceGenerator( arrayType.getNesting(), arrayType.getDataset(), sourceName, factory, Object[].class, context); return WrapperFactory.applyConverter(generator, new ArrayElementTypeConverter(arrayType)); }
@SuppressWarnings({"unchecked", "rawtypes"}) static ComponentBuilder<?> createReferenceBuilder( ReferenceDescriptor descriptor, BeneratorContext context) { SimpleTypeDescriptor typeDescriptor = (SimpleTypeDescriptor) descriptor.getTypeDescriptor(); // check uniqueness boolean unique = DescriptorUtil.isUnique(descriptor, context); Uniqueness uniqueness = (unique ? Uniqueness.SIMPLE : Uniqueness.NONE); // do I only need to generate nulls? if (DescriptorUtil.isNullable(descriptor, context) && DescriptorUtil.shouldNullifyEachNullable(descriptor, context)) return builderFromGenerator(createNullGenerator(descriptor, context), descriptor, context); // TODO use SimpleTypeGeneratorFactory? Generator<?> generator = null; generator = DescriptorUtil.getGeneratorByName(typeDescriptor, context); if (generator == null) generator = SimpleTypeGeneratorFactory.createScriptGenerator(typeDescriptor); if (generator == null) generator = SimpleTypeGeneratorFactory.createConstantGenerator(typeDescriptor, context); if (generator == null) generator = SimpleTypeGeneratorFactory.createValuesGenerator(typeDescriptor, uniqueness, context); // get distribution Distribution distribution = FactoryUtil.getDistribution( typeDescriptor.getDistribution(), descriptor.getUniqueness(), false, context); // check source if (generator == null) { // check target type String targetTypeName = descriptor.getTargetType(); ComplexTypeDescriptor targetType = (ComplexTypeDescriptor) context.getDataModel().getTypeDescriptor(targetTypeName); if (targetType == null) throw new ConfigurationError("Type not defined: " + targetTypeName); // check targetComponent String targetComponent = descriptor.getTargetComponent(); // check source String sourceName = typeDescriptor.getSource(); if (sourceName == null) throw new ConfigurationError("'source' is not set for " + descriptor); Object sourceObject = context.get(sourceName); if (sourceObject instanceof StorageSystem) { StorageSystem sourceSystem = (StorageSystem) sourceObject; String selector = typeDescriptor.getSelector(); String subSelector = typeDescriptor.getSubSelector(); boolean subSelect = !StringUtil.isEmpty(subSelector); String selectorToUse = (subSelect ? subSelector : selector); if (isIndividualSelector(selectorToUse)) { generator = new DataSourceGenerator(sourceSystem.query(selectorToUse, true, context)); } else { generator = new DataSourceGenerator( sourceSystem.queryEntityIds( targetTypeName, selectorToUse, context)); // TODO v0.7.2 query by targetComponent if (selectorToUse == null && distribution == null) if (context.isDefaultOneToOne()) distribution = new ExpandSequence(); else distribution = SequenceManager.RANDOM_SEQUENCE; } if (subSelect) generator = WrapperFactory.applyHeadCycler(generator); } else throw new ConfigurationError("Not a supported source type: " + sourceName); } // apply distribution if necessary if (distribution != null) generator = new DistributingGenerator(generator, distribution, unique); // check multiplicity generator = ComponentBuilderFactory.createMultiplicityWrapper(descriptor, generator, context); if (LOGGER.isDebugEnabled()) LOGGER.debug("Created " + generator); // check 'cyclic' config generator = DescriptorUtil.wrapWithProxy(generator, typeDescriptor); return builderFromGenerator(generator, descriptor, context); }