| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| * |
| * This copyright notice shows up in the generated Java code |
| * |
| */ |
| |
| package org.eclipse.osbp.xtext.entitymock.jvmmodel |
| |
| import com.google.inject.Inject |
| import java.util.ArrayList |
| import java.util.List |
| import java.util.Map |
| import java.util.Set |
| import org.apache.commons.lang.StringUtils |
| import org.apache.commons.lang.WordUtils |
| import org.eclipse.osbp.dsl.semantic.common.types.LEnum |
| import org.eclipse.osbp.dsl.semantic.entity.LEntity |
| import org.eclipse.osbp.dsl.semantic.entity.LEntityAttribute |
| import org.eclipse.osbp.persistence.IPersistenceService |
| import org.eclipse.osbp.utils.common.EntityUtils |
| import org.eclipse.osbp.utils.entityhelper.DataType |
| import org.eclipse.osbp.utils.entitymock.ABaseMockEntity |
| import org.eclipse.osbp.utils.entitymock.ABaseMockObject |
| import org.eclipse.osbp.utils.entitymock.ABaseMockResource |
| import org.eclipse.osbp.utils.entitymock.AEntityMockDataGenerator |
| import org.eclipse.osbp.utils.entitymock.IEntityMockDataDbFiller |
| import org.eclipse.osbp.xtext.datainterchange.jvmmodel.DataDSLJvmModelInferrer |
| import org.eclipse.osbp.xtext.entitymock.Date |
| import org.eclipse.osbp.xtext.entitymock.DateIterate |
| import org.eclipse.osbp.xtext.entitymock.EntityMockAttributeByObject |
| import org.eclipse.osbp.xtext.entitymock.EntityMockAttributeByReference |
| import org.eclipse.osbp.xtext.entitymock.EntityMockAttributeFiller |
| import org.eclipse.osbp.xtext.entitymock.EntityMockEntity |
| import org.eclipse.osbp.xtext.entitymock.EntityMockEntityFunctionParameter |
| import org.eclipse.osbp.xtext.entitymock.EntityMockModel |
| import org.eclipse.osbp.xtext.entitymock.EntityMockObject |
| import org.eclipse.osbp.xtext.entitymock.EntityMockObjectArrayValue |
| import org.eclipse.osbp.xtext.entitymock.EntityMockObjectEmbed |
| import org.eclipse.osbp.xtext.entitymock.EntityMockObjectEnum |
| import org.eclipse.osbp.xtext.entitymock.EntityMockObjectFill |
| import org.eclipse.osbp.xtext.entitymock.EntityMockObjectFunction |
| import org.eclipse.osbp.xtext.entitymock.EntityMockObjectPlainValue |
| import org.eclipse.osbp.xtext.entitymock.EntityMockObjectResourceValue |
| import org.eclipse.osbp.xtext.entitymock.EntityMockPackage |
| import org.eclipse.osbp.xtext.entitymock.EntityMockResource |
| import org.eclipse.osbp.xtext.entitymock.EntityMockResourceDataRow |
| import org.eclipse.osbp.xtext.entitymock.EntityMockTemporary |
| import org.eclipse.osbp.xtext.entitymock.IEntityMockObjectUsable |
| import org.eclipse.osbp.xtext.entitymock.IntegerIterate |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerBoolean |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerDateFuture |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerDatePast |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerDateRange |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerSignedDoubleRandom |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerSignedDoubleRange |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerSignedIntegerRandom |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerSignedIntegerRange |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerTextParagraphs |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerTextRandom |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerTextSentences |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerTextWords |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerUnsignedDoubleRandom |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerUnsignedDoubleRange |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerUnsignedIntegerRandom |
| import org.eclipse.osbp.xtext.entitymock.PropertyFillerUnsignedIntegerRange |
| import org.eclipse.xtext.common.types.JvmDeclaredType |
| import org.eclipse.xtext.common.types.JvmEnumerationType |
| import org.eclipse.xtext.common.types.JvmField |
| import org.eclipse.xtext.common.types.JvmTypeReference |
| import org.eclipse.xtext.common.types.JvmVisibility |
| import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer |
| import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor |
| import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder |
| import org.slf4j.Logger |
| import org.eclipse.osbp.utils.entitymock.IEntityMockDataGenerator |
| import org.eclipse.osbp.dsl.semantic.entity.LBean |
| |
| /** |
| * <p>Infers a JVM model from the source model.</p> |
| * |
| * <p>The JVM model should contain all elements that would appear in the Java code |
| * which is generated from the source model. Other models link against the JVM model rather than the source model.</p> |
| */ |
| class EntityMockDSLJvmModelInferrer extends AbstractModelInferrer { |
| |
| /** |
| * convenience API to build and initialize JVM types and their members. |
| */ |
| @Inject extension JvmTypesBuilder |
| @Inject extension DataDSLJvmModelInferrer |
| // @Inject extension IQualifiedNameProvider |
| @Inject extension EntityUtils |
| // @Inject extension AnnotationExtension |
| |
| @Inject DataType dtType |
| |
| /** |
| * get the full qualified package name for the imported entities, removing the trailing * |
| */ |
| def static String getEntityPackageName(EntityMockPackage pckg) { |
| var pckgName = "" |
| for (import : pckg.imports) { |
| if (pckgName.isEmpty) { |
| pckgName = '''«import.importedNamespace»'''.toString |
| pckgName = pckgName.replace(".*", "") |
| } |
| } |
| return pckgName |
| } |
| |
| /** |
| * generate the full qualified mock package name corresponding to the imported entities |
| */ |
| def static String getEntityMockName(EntityMockPackage pckg) { |
| return '''«pckg.entityPackageName»mock''' |
| } |
| |
| /** |
| * generate the class name for a temporary mock object |
| */ |
| def String getEntityMockName(EntityMockTemporary temporary) { |
| return (temporary.object as EntityMockObject).entityMockName |
| } |
| |
| /** |
| * generate the class name for a mock resource class |
| */ |
| def String getEntityMockName(EntityMockResource resource) { |
| return '''Resource«WordUtils.capitalize(resource.name)»''' |
| } |
| |
| /** |
| * generate the class name for a mock object class |
| */ |
| def String getEntityMockName(EntityMockObject object) { |
| return '''Object«WordUtils.capitalize(object.name)»''' |
| } |
| |
| /** |
| * generate the class name for a mock entity |
| */ |
| def String getEntityMockName(EntityMockEntity entity) { |
| return '''Entity«WordUtils.capitalize(entity.name)»''' |
| } |
| |
| /** |
| * get the entity name referenced by a mock entity |
| */ |
| def String getEntityName(EntityMockEntity entity) { |
| return entity.entityRef.entityName |
| } |
| |
| /** |
| * get the entity name |
| */ |
| def String getEntityName(LEntity entity) { |
| return entity.name |
| } |
| |
| /** |
| * get the entity name |
| */ |
| def String getEnumName(LEnum lenum) { |
| return lenum.name |
| } |
| |
| /** |
| * get the full qualified entity name referenced by a mock entity |
| */ |
| def String getEntityDtoFQN(EntityMockEntity entity) { |
| return EntityUtils.getDtoFQNForLEntity(entity.entityRef) |
| } |
| |
| /** |
| * get the full qualified entity class name referenced by a mock entity |
| */ |
| def String getEntityDtoFQNClass(EntityMockEntity entity) { |
| return entity.entityRef.entityDtoFQNClass |
| } |
| |
| /** |
| * get the full qualified entity class name |
| */ |
| def String getEntityDtoFQNClass(LEntity entity) { |
| return '''«EntityUtils.getDtoFQNForLEntity(entity)».class''' |
| } |
| |
| /** |
| * generate a mock object class |
| */ |
| def void generateMockObject(EntityMockModel model, IJvmDeclaredTypeAcceptor acceptor, EntityMockPackage pckg, |
| EntityMockObject mockObject) { |
| acceptor.accept(model.toClass('''«pckg.entityMockName».«mockObject.entityMockName»''')) [ |
| superTypes += _typeReferenceBuilder.typeRef(ABaseMockObject) |
| // @see org.eclipse.osbp.utils.constants.GeneratorConstants.GeneratorConstants |
| documentation = '''<b>This class was auto generated! Leave it unmodified to avoid unpredictable results!</b><br> |
| «mockObject.documentation» |
| ''' |
| final = true |
| |
| var JvmField field = null |
| field = model.toField("log", _typeReferenceBuilder.typeRef(Logger)) [setInitializer([ append('''LoggerFactory.getLogger(«pckg.entityMockName».«mockObject.entityMockName».class)''') ])] |
| field.final = true |
| field.static = true |
| members += field |
| |
| // --- class attribute for each enumeration defined, almost identical to plain values |
| for (enumeration : mockObject.enumerations) { |
| if (enumeration.usingResource instanceof EntityMockResource) { |
| val resource = enumeration.usingResource as EntityMockResource |
| members += mockObject.toField('''«enumeration.name»_resource''', |
| _typeReferenceBuilder.typeRef(ABaseMockResource)) [ |
| setInitializer( |
| [ |
| append('''new «resource.entityMockName»()''') |
| ]) |
| documentation = enumeration.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } else { |
| } |
| } |
| // --- for each attribute |
| for (attribute : mockObject.attributes) { |
| switch attribute { |
| // --- class attribute for each plain value list, almost identical to enumerations |
| EntityMockObjectPlainValue: { |
| val List<String> items = new ArrayList |
| for (value : attribute.values) { |
| items.add('''"«value»"''') |
| } |
| members += mockObject.toField('''«attribute.name»_items''', |
| _typeReferenceBuilder.typeRef(typeof(Object[]))) [ |
| setInitializer( |
| [ |
| append('''{«StringUtils.join(items, ",")»}''') |
| ]) |
| documentation = attribute.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| EntityMockObjectResourceValue: { |
| } |
| // --- class attribute for each array value list, using enumerations as first level index |
| EntityMockObjectArrayValue: { |
| val List<String> enums = new ArrayList |
| val keyType = { |
| if (attribute.enumeration.usingResource instanceof EntityMockResource) { |
| "String" |
| } else { |
| '''«EntityUtils.getDtoFQNForLEnum(attribute.enumeration.usingResource)»'''.toString |
| } |
| } |
| for (enumItem : attribute.items) { |
| val List<String> items = new ArrayList |
| for (value : enumItem.values) { |
| items.add('''"«value»"''') |
| } |
| if (enumItem.datarow instanceof EntityMockResourceDataRow) { |
| enums.add( |
| '''put("«enumItem.datarow.name»", new Object[] {«StringUtils.join(items, ",")»});''') |
| } else { |
| enums.add( |
| '''put(«keyType».«enumItem.datarow.name», new Object[] {«StringUtils.join(items, ",")»});''') |
| } |
| } |
| members += mockObject.toField('''«attribute.name»_items''', _typeReferenceBuilder.typeRef(Map)) [ |
| setInitializer( |
| [ |
| append( |
| '''new HashMap<«keyType», Object[]>() {{ |
| «StringUtils.join(enums, "\r\n")» |
| }}''') |
| ]) |
| documentation = attribute.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| EntityMockObjectEmbed: { |
| } |
| EntityMockObjectFill: { |
| var type = attribute.fillerType |
| switch type { |
| // --- class attribute for each filler using random text |
| PropertyFillerTextRandom: { |
| val List<String> items = new ArrayList |
| for (item : type.items) { |
| items.add('''"«item»"''') |
| } |
| members += mockObject.toField('''«attribute.name»_items''', |
| _typeReferenceBuilder.typeRef(typeof(Object[]))) [ |
| setInitializer( |
| [ |
| append('''{«StringUtils.join(items, ",")»}''') |
| ]) |
| documentation = attribute.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| // --- class attribute for each filler using random double |
| PropertyFillerSignedDoubleRandom: { |
| val List<String> items = new ArrayList |
| for (item : type.items) { |
| items.add('''«item»''') |
| } |
| members += mockObject.toField('''«attribute.name»_items''', |
| _typeReferenceBuilder.typeRef(typeof(double[]))) [ |
| setInitializer( |
| [ |
| append('''{«StringUtils.join(items, ",")»}''') |
| ]) |
| documentation = attribute.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| // --- class attribute for each filler using random double |
| PropertyFillerUnsignedDoubleRandom: { |
| val List<String> items = new ArrayList |
| for (item : type.items) { |
| items.add('''«item»''') |
| } |
| members += mockObject.toField('''«attribute.name»_items''', |
| _typeReferenceBuilder.typeRef(typeof(double[]))) [ |
| setInitializer( |
| [ |
| append('''{«StringUtils.join(items, ",")»}''') |
| ]) |
| documentation = attribute.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| // --- class attribute for each filler using random integer |
| PropertyFillerSignedIntegerRandom: { |
| val List<String> items = new ArrayList |
| for (item : type.items) { |
| items.add('''«item»''') |
| } |
| members += mockObject.toField('''«attribute.name»_items''', |
| _typeReferenceBuilder.typeRef(typeof(int[]))) [ |
| setInitializer( |
| [ |
| append('''{«StringUtils.join(items, ",")»}''') |
| ]) |
| documentation = attribute.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| // --- class attribute for each filler using random integer |
| PropertyFillerUnsignedIntegerRandom: { |
| val List<String> items = new ArrayList |
| for (item : type.items) { |
| items.add('''«item»''') |
| } |
| members += mockObject.toField('''«attribute.name»_items''', |
| _typeReferenceBuilder.typeRef(typeof(int[]))) [ |
| setInitializer( |
| [ |
| append('''{«StringUtils.join(items, ",")»}''') |
| ]) |
| documentation = attribute.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| } |
| } |
| } |
| } |
| // --- for each calculcation |
| for (calculation : mockObject.calculations) { |
| members += mockObject.toMethod('''calculate_«calculation.name»''', calculation.oftype) [ |
| visibility = JvmVisibility.PROTECTED |
| for (param : calculation.params) { |
| var type = param.parameterType |
| // var fqn = type.fullyQualifiedName |
| parameters += param.toParameter(param.name.name, type); |
| } |
| final = true |
| body = calculation.body |
| ] |
| } |
| // --- generate a single data row |
| members += mockObject.toMethod('''generateDataRow''', _typeReferenceBuilder.typeRef(Void::TYPE)) [ |
| visibility = JvmVisibility.PROTECTED |
| final = true |
| body = [ |
| // --- each enumeration, almost the same as a plain value |
| for (enumeration : mockObject.enumerations) { |
| var doc = enumeration.documentation |
| if (doc instanceof String && !doc.trim.isEmpty) { |
| append( |
| '''/** «doc» */ |
| ''') |
| } |
| if (enumeration.usingResource instanceof EntityMockResource) { |
| append( |
| '''generateAttribute("«enumeration.name»", «enumeration.name»_resource.getDataRows().toArray()); |
| ''') |
| } else { |
| val lenum = enumeration.usingResource |
| append( |
| '''generateAttribute("«enumeration.name»",«EntityUtils.getDtoFQNForLEnum(lenum)».values()); |
| ''' |
| ) |
| } |
| } |
| // --- for each attribute |
| for (attribute : mockObject.attributes) { |
| var doc = attribute.documentation |
| if (doc instanceof String && !doc.trim.isEmpty) { |
| append( |
| '''/** «doc» */ |
| ''') |
| } |
| switch attribute { |
| // --- each plain value, almost the same as an enumeration |
| EntityMockObjectPlainValue: { |
| append( |
| '''generateAttribute("«attribute.name»", «attribute.name»_items); |
| ''') |
| } |
| EntityMockObjectResourceValue: { |
| if (attribute.resourceEnum.usingResource instanceof EntityMockResource) { |
| append( |
| '''setFixAttribute("«attribute.name»", «attribute.resourceEnum.name»_resource.getAttribute(getAttribute("«attribute. |
| resourceEnum.name»").toString(), "«attribute.resourceAttribute.name»")); |
| ''' |
| ) |
| } else { |
| append( |
| '''setFixAttribute("«attribute.name»", getAttribute("«attribute.resourceEnum. |
| name»")); |
| ''' |
| ) |
| } |
| } |
| // --- each array value, using the previously generated enumeration |
| EntityMockObjectArrayValue: { |
| append( |
| '''generateAttribute("«attribute.name»", "«attribute.enumeration.name»", «attribute. |
| name»_items); |
| ''' |
| ) |
| } |
| // --- generating an embedded mock object |
| EntityMockObjectEmbed: { |
| append( |
| '''generateAttribute("«attribute.name»", new «attribute.object.entityMockName»()); |
| ''') |
| } |
| // --- generating using filler text depending on filler type |
| EntityMockObjectFill: { |
| |
| // TODO: re-implement filler from datamart generator |
| var type = attribute.fillerType |
| switch type { |
| PropertyFillerDateFuture: { |
| append( |
| '''generateDateAttribute("«attribute.name»", 0 ,«type.dateFutureYears»); |
| ''') |
| } |
| PropertyFillerDatePast: { |
| append( |
| '''generateDateAttribute("«attribute.name»", -«type.datePastYears», 0); |
| ''') |
| } |
| PropertyFillerDateRange: { |
| append( |
| '''generateDateAttribute("«attribute.name»", «type.dateBeginYears», «type. |
| dateEndYears»); |
| ''') |
| } |
| PropertyFillerBoolean: { |
| append( |
| '''generateBooleanAttribute("«attribute.name»"); |
| ''') |
| } |
| PropertyFillerSignedDoubleRange: { |
| var beginRange = '''«type.beginRange»''' |
| var endRange = '''«type.endRange»''' |
| if (type.beginRangeRef != null) { |
| beginRange = '''get_"«type.beginRangeRef.name»"()''' |
| } |
| if (type.endRangeRef != null) { |
| endRange = '''get_"«type.endRangeRef.name»"()''' |
| } |
| append( |
| '''generateSignedAttribute("«attribute.name»", «type.decimals», «beginRange», «endRange», «type. |
| rounded»); |
| ''') |
| } |
| PropertyFillerSignedDoubleRandom: { |
| append( |
| '''generateAttribute("«attribute.name»", «attribute.name»_items); |
| ''') |
| } |
| PropertyFillerUnsignedDoubleRange: { |
| var beginRange = '''«type.beginRange»''' |
| var endRange = '''«type.endRange»''' |
| if (type.beginRangeRef != null) { |
| beginRange = '''(double)get_"«type.beginRangeRef.name»"()''' |
| } |
| if (type.endRangeRef != null) { |
| endRange = '''(double)get_"«type.endRangeRef.name»"()''' |
| } |
| append( |
| '''generateUnsignedAttribute("«attribute.name»", «type.decimals», «beginRange», «endRange», «type. |
| rounded»); |
| ''') |
| } |
| PropertyFillerUnsignedDoubleRandom: { |
| append( |
| '''generateAttribute("«attribute.name»", «attribute.name»_items); |
| ''') |
| } |
| PropertyFillerSignedIntegerRange: { |
| var beginRange = '''«type.beginRange»''' |
| var endRange = '''«type.endRange»''' |
| if (type.beginRangeRef != null) { |
| beginRange = '''(int)get_"«type.beginRangeRef.name»"()''' |
| } |
| if (type.endRangeRef != null) { |
| endRange = '''(int)get_"«type.endRangeRef.name»"()''' |
| } |
| append( |
| '''generateSignedAttribute("«attribute.name»", «beginRange», «endRange», «type. |
| rounded»); |
| ''') |
| } |
| PropertyFillerSignedIntegerRandom: { |
| append( |
| '''generateAttribute("«attribute.name»", «attribute.name»_items); |
| ''') |
| } |
| PropertyFillerUnsignedIntegerRange: { |
| var beginRange = '''«type.beginRange»''' |
| var endRange = '''«type.endRange»''' |
| if (type.beginRangeRef != null) { |
| beginRange = '''(int)get_"«type.beginRangeRef.name»"()''' |
| } |
| if (type.endRangeRef != null) { |
| endRange = '''(int)get_"«type.endRangeRef.name»"()''' |
| } |
| append( |
| '''generateUnsignedAttribute("«attribute.name»", «beginRange», «endRange», «type. |
| rounded»); |
| ''') |
| } |
| PropertyFillerUnsignedIntegerRandom: { |
| append( |
| '''generateAttribute("«attribute.name»", «attribute.name»_items); |
| ''') |
| } |
| PropertyFillerTextRandom: { |
| append( |
| '''generateAttribute("«attribute.name»", «attribute.name»_items); |
| ''') |
| } |
| PropertyFillerTextParagraphs: { |
| append( |
| '''generateParagraphsAttribute("«attribute.name»", «type.count»); |
| ''') |
| } |
| PropertyFillerTextSentences: { |
| append( |
| '''generateSentencesAttribute("«attribute.name»", «type.count»); |
| ''') |
| } |
| PropertyFillerTextWords: { |
| append( |
| '''generateWordsAttribute("«attribute.name»", «type.count»); |
| ''') |
| } |
| } |
| } |
| } |
| } |
| // --- for each calculcation |
| for (calculation : mockObject.calculations) { |
| var doc = calculation.documentation |
| if (doc instanceof String && !doc.trim.isEmpty) { |
| append( |
| '''/** «doc» */ |
| ''') |
| } |
| var paramString = '''''' |
| for (param : calculation.params) { |
| if (!paramString.isEmpty) { |
| paramString = '''«paramString»,''' |
| } |
| if (param.parameterType.type instanceof JvmEnumerationType) { |
| paramString = '''«paramString» «param.parameterType.identifier».valueOf(getAttribute("«param. |
| name.name»").toString())''' |
| } else { |
| paramString = '''«paramString»(«param.parameterType.identifier»)getAttribute("«param. |
| name.name»")''' |
| } |
| } |
| append( |
| '''try { |
| setFixAttribute("«calculation.name»", calculate_«calculation.name»(«paramString»)); |
| } |
| catch (NullPointerException npe) {} |
| catch (Exception e) { |
| log.error(e.getLocalizedMessage()+e.getCause()); |
| } |
| ''' |
| ) |
| } |
| ] |
| ] |
| ] |
| } |
| |
| def String getName(IEntityMockObjectUsable attribute) { |
| switch attribute { |
| EntityMockObjectFunction: { |
| return '''«attribute.name»''' |
| } |
| EntityMockObjectEnum: { |
| return '''«attribute.name»''' |
| } |
| EntityMockObjectPlainValue: { |
| return '''«attribute.name»''' |
| } |
| EntityMockObjectArrayValue: { |
| return '''«attribute.name»''' |
| } |
| EntityMockObjectEmbed: { |
| return '''«attribute.name»''' |
| } |
| EntityMockObjectFill: { |
| return '''«attribute.name»''' |
| } |
| EntityMockObjectResourceValue: { |
| return '''«attribute.name»''' |
| } |
| } |
| return null |
| } |
| |
| /** |
| * generate a mock entity class |
| */ |
| def void generateMockEntity(EntityMockModel model, IJvmDeclaredTypeAcceptor acceptor, EntityMockPackage pckg, |
| EntityMockEntity mockEntity) { |
| acceptor.accept(model.toClass('''«pckg.entityMockName».«mockEntity.entityMockName»''')) [ |
| superTypes += _typeReferenceBuilder.typeRef(ABaseMockEntity) |
| // @see org.eclipse.osbp.utils.constants.GeneratorConstants.GeneratorConstants |
| documentation = '''<b>This class was auto generated! Leave it unmodified to avoid unpredictable results!</b><br> |
| «mockEntity.documentation» |
| ''' |
| final = true |
| |
| var JvmField field = null |
| field = model.toField("log", _typeReferenceBuilder.typeRef(Logger)) [setInitializer([ append('''LoggerFactory.getLogger(«pckg.entityMockName».«mockEntity.entityMockName».class)''') ])] |
| field.final = true |
| field.static = true |
| members += field |
| |
| if (mockEntity.byResource != null) { |
| members += pckg.toField('''«mockEntity.byResource.name»_resource''', |
| _typeReferenceBuilder.typeRef(ABaseMockResource)) [ |
| setInitializer( |
| [ |
| append('''new «mockEntity.byResource.entityMockName»()''') |
| ]) |
| documentation = mockEntity.byResource.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| // --- the constructor |
| members += pckg.toConstructor [ |
| parameters += |
| mockEntity.toParameter("mockDataGenerator", _typeReferenceBuilder.typeRef(AEntityMockDataGenerator)) |
| body = [ |
| append('''super(mockDataGenerator, "«mockEntity.entityRef.persistenceUnit»");''') |
| ] |
| ] |
| // --- class variables for each temporary mock object needed |
| for (temporary : mockEntity.temporaries) { |
| members += pckg.toField('''«temporary.name»_temporary''', |
| _typeReferenceBuilder.typeRef(ABaseMockObject)) [ |
| setInitializer( |
| [ |
| append('''new «temporary.entityMockName»()''') |
| ]) |
| documentation = temporary.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| // --- check all attributes, if class variables are necessary |
| for (attribute : mockEntity.attributes) { |
| switch attribute { |
| EntityMockAttributeByObject: { |
| } |
| EntityMockAttributeFiller: { |
| var type = attribute.fillerType |
| switch type { |
| // --- for random text, remember all available options |
| PropertyFillerTextRandom: { |
| val List<String> items = new ArrayList |
| for (item : type.items) { |
| items.add('''"«item»"''') |
| } |
| members += pckg.toField('''«attribute.attributeRef.name»_items''', |
| _typeReferenceBuilder.typeRef(typeof(Object[]))) [ |
| setInitializer( |
| [ |
| append('''{«StringUtils.join(items, ",")»}''') |
| ]) |
| documentation = attribute.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| // --- for random doubles, remember all available options |
| PropertyFillerSignedDoubleRandom: { |
| val List<String> items = new ArrayList |
| for (item : type.items) { |
| items.add('''«item»''') |
| } |
| members += pckg.toField('''«attribute.attributeRef.name»_items''', |
| _typeReferenceBuilder.typeRef(typeof(double[]))) [ |
| setInitializer( |
| [ |
| append('''{«StringUtils.join(items, ",")»}''') |
| ]) |
| documentation = attribute.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| // --- for random doubles, remember all available options |
| PropertyFillerUnsignedDoubleRandom: { |
| val List<String> items = new ArrayList |
| for (item : type.items) { |
| items.add('''«item»''') |
| } |
| members += pckg.toField('''«attribute.attributeRef.name»_items''', |
| _typeReferenceBuilder.typeRef(typeof(double[]))) [ |
| setInitializer( |
| [ |
| append('''{«StringUtils.join(items, ",")»}''') |
| ]) |
| documentation = attribute.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| // --- for random integers, remember all available options |
| PropertyFillerSignedIntegerRandom: { |
| val List<String> items = new ArrayList |
| for (item : type.items) { |
| items.add('''«item»''') |
| } |
| members += pckg.toField('''«attribute.attributeRef.name»_items''', |
| _typeReferenceBuilder.typeRef(typeof(int[]))) [ |
| setInitializer( |
| [ |
| append('''{«StringUtils.join(items, ",")»}''') |
| ]) |
| documentation = attribute.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| // --- for random integers, remember all available options |
| PropertyFillerUnsignedIntegerRandom: { |
| val List<String> items = new ArrayList |
| for (item : type.items) { |
| items.add('''«item»''') |
| } |
| members += pckg.toField('''«attribute.attributeRef.name»_items''', |
| _typeReferenceBuilder.typeRef(typeof(int[]))) [ |
| setInitializer( |
| [ |
| append('''{«StringUtils.join(items, ",")»}''') |
| ]) |
| documentation = attribute.documentation |
| visibility = JvmVisibility::PRIVATE |
| static = true |
| ] |
| } |
| } |
| } |
| EntityMockAttributeByReference: { |
| } |
| } |
| } |
| if (mockEntity.byResource != null) { |
| members += pckg.toMethod('''getDataRows''', _typeReferenceBuilder.typeRef(Set)) [ |
| visibility = JvmVisibility.PUBLIC |
| final = true |
| body = [ |
| append('''return «mockEntity.byResource.name»_resource.getDataRows();''') |
| ] |
| ] |
| |
| members += pckg.toMethod('''generateEntity''', _typeReferenceBuilder.typeRef(Object)) [ |
| visibility = JvmVisibility.PUBLIC |
| parameters += pckg.toParameter("dataRow", _typeReferenceBuilder.typeRef(Object)) |
| final = true |
| body = [ |
| // --- generate that entity |
| append( |
| '''reset(); |
| «mockEntity.entityDtoFQN» internal = new «mockEntity.entityDtoFQN»(); |
| entity = internal; |
| generateData(); |
| try { |
| ''') |
| // --- for each entity attribute, just copy from the internal mock data |
| for (attribute : mockEntity.byResourceAttributes) { |
| append( |
| '''internal.set«WordUtils.capitalize(attribute.attributeRef.name)»(«attribute. |
| attributeRef.baseMockDataTypeCasting( |
| '''«mockEntity.byResource.name»_resource.getAttribute(dataRow.toString(), "«attribute.resourceAttribute.name»")''')»); |
| ''') |
| } |
| append( |
| '''} |
| catch (Exception e) { |
| log.error(e.getLocalizedMessage()+e.getCause()); |
| } |
| return entity;''') |
| ] |
| ] |
| } |
| // --- for each calculcation |
| for (calculation : mockEntity.calculations) { |
| members += pckg.toMethod('''calculate_«calculation.attributeRef.name»''', calculation.attributeRef.jvmTypeRef) [ |
| visibility = JvmVisibility.PROTECTED |
| for (param : calculation.params) { |
| parameters += param.toParameter(param.getParamName("__"), param.jvmType); |
| } |
| final = true |
| body = calculation.body |
| ] |
| } |
| // --- generate a single data row in the same manner as a mock object |
| members += pckg.toMethod('''generateDataRow''', _typeReferenceBuilder.typeRef(Void::TYPE)) [ |
| visibility = JvmVisibility.PROTECTED |
| final = true |
| body = [ |
| // --- first generate all requested temporary mock objects |
| for (temporary : mockEntity.temporaries) { |
| append( |
| '''generateAttribute("«temporary.name»", «temporary.name»_temporary); |
| ''') |
| } |
| // --- now all attributes |
| for (attribute : mockEntity.attributes) { |
| switch attribute { |
| // --- referencing to temporary mock objects |
| EntityMockAttributeByObject: { |
| var sourceKey = attribute.reference.temporary.name |
| for (embed : attribute.reference.embedded) { |
| sourceKey = '''«sourceKey».«embed.name»''' |
| } |
| val refAttribute = attribute.reference.attribute |
| if (refAttribute instanceof EntityMockObjectEnum) |
| sourceKey = '''«sourceKey».«(refAttribute as EntityMockObjectEnum).name»''' |
| if (refAttribute instanceof EntityMockObjectPlainValue) |
| sourceKey = '''«sourceKey».«(refAttribute as EntityMockObjectPlainValue).name»''' |
| if (refAttribute instanceof EntityMockObjectResourceValue) |
| sourceKey = '''«sourceKey».«(refAttribute as EntityMockObjectResourceValue).name»''' |
| if (refAttribute instanceof EntityMockObjectArrayValue) |
| sourceKey = '''«sourceKey».«(refAttribute as EntityMockObjectArrayValue).name»''' |
| if (refAttribute instanceof EntityMockObjectEmbed) |
| sourceKey = '''«sourceKey».«(refAttribute as EntityMockObjectEmbed).name»''' |
| if (refAttribute instanceof EntityMockObjectFill) |
| sourceKey = '''«sourceKey».«(refAttribute as EntityMockObjectFill).name»''' |
| if (refAttribute instanceof EntityMockObjectFunction) |
| sourceKey = '''«sourceKey».«(refAttribute as EntityMockObjectFunction).name»''' |
| append( |
| '''generateAttribute("«attribute.attributeRef.name»", "«sourceKey»"); |
| ''') |
| } |
| // --- filled by filler providers by filler type |
| EntityMockAttributeFiller: { |
| var type = attribute.fillerType |
| switch type { |
| PropertyFillerDateFuture: { |
| append( |
| '''generateDateAttribute("«attribute.attributeRef.name»", 0 ,«type. |
| dateFutureYears»); |
| ''') |
| } |
| PropertyFillerDatePast: { |
| append( |
| '''generateDateAttribute("«attribute.attributeRef.name»", -«type. |
| datePastYears», 0); |
| ''') |
| } |
| PropertyFillerDateRange: { |
| append( |
| '''generateDateAttribute("«attribute.attributeRef.name»", «type. |
| dateBeginYears», «type.dateEndYears»); |
| ''') |
| } |
| PropertyFillerBoolean: { |
| append( |
| '''generateBooleanAttribute("«attribute.attributeRef.name»"); |
| ''') |
| } |
| PropertyFillerSignedDoubleRange: { |
| var beginRange = '''«type.beginRange»''' |
| var endRange = '''«type.endRange»''' |
| if (type.beginRangeRef != null) { |
| beginRange = '''get_"«type.beginRangeRef.name»"()''' |
| } |
| if (type.endRangeRef != null) { |
| endRange = '''get_"«type.endRangeRef.name»"()''' |
| } |
| append( |
| '''generateSignedAttribute("«attribute.attributeRef.name»", «type.decimals», «beginRange», «endRange», «type. |
| rounded»); |
| ''') |
| } |
| PropertyFillerSignedDoubleRandom: { |
| append( |
| '''generateAttribute("«attribute.attributeRef.name»", «attribute. |
| attributeRef.name»_items); |
| ''') |
| } |
| PropertyFillerUnsignedDoubleRange: { |
| var beginRange = '''«type.beginRange»''' |
| var endRange = '''«type.endRange»''' |
| if (type.beginRangeRef != null) { |
| beginRange = '''(double)get_"«type.beginRangeRef.name»"()''' |
| } |
| if (type.endRangeRef != null) { |
| endRange = '''(double)get_"«type.endRangeRef.name»"()''' |
| } |
| append( |
| '''generateUnsignedAttribute("«attribute.attributeRef.name»", «type.decimals», «beginRange», «endRange», «type. |
| rounded»); |
| ''') |
| } |
| PropertyFillerUnsignedDoubleRandom: { |
| append( |
| '''generateAttribute("«attribute.attributeRef.name»", «attribute. |
| attributeRef.name»_items); |
| ''') |
| } |
| PropertyFillerSignedIntegerRange: { |
| var beginRange = '''«type.beginRange»''' |
| var endRange = '''«type.endRange»''' |
| if (type.beginRangeRef != null) { |
| beginRange = '''(int)get_"«type.beginRangeRef.name»"()''' |
| } |
| if (type.endRangeRef != null) { |
| endRange = '''(int)get_"«type.endRangeRef.name»"()''' |
| } |
| append( |
| '''generateSignedAttribute("«attribute.attributeRef.name»", «beginRange», «endRange», «type. |
| rounded»); |
| ''') |
| } |
| PropertyFillerSignedIntegerRandom: { |
| append( |
| '''generateAttribute("«attribute.attributeRef.name»", «attribute. |
| attributeRef.name»_items); |
| ''') |
| } |
| PropertyFillerUnsignedIntegerRange: { |
| var beginRange = '''«type.beginRange»''' |
| var endRange = '''«type.endRange»''' |
| if (type.beginRangeRef != null) { |
| beginRange = '''(int)get_"«type.beginRangeRef.name»"()''' |
| } |
| if (type.endRangeRef != null) { |
| endRange = '''(int)get_"«type.endRangeRef.name»"()''' |
| } |
| append( |
| '''generateUnsignedAttribute("«attribute.attributeRef.name»", «beginRange», «endRange», «type. |
| rounded»); |
| ''') |
| } |
| PropertyFillerUnsignedIntegerRandom: { |
| append( |
| '''generateAttribute("«attribute.attributeRef.name»", «attribute. |
| attributeRef.name»_items); |
| ''') |
| } |
| PropertyFillerTextRandom: { |
| append( |
| '''generateAttribute("«attribute.attributeRef.name»", «attribute. |
| attributeRef.name»_items); |
| ''') |
| } |
| PropertyFillerTextParagraphs: { |
| append( |
| '''generateParagraphsAttribute("«attribute.attributeRef.name»", «type.count»); |
| ''') |
| } |
| PropertyFillerTextSentences: { |
| append( |
| '''generateSentencesAttribute("«attribute.attributeRef.name»", «type.count»); |
| ''') |
| } |
| PropertyFillerTextWords: { |
| append( |
| '''generateWordsAttribute("«attribute.attributeRef.name»", «type.count»); |
| ''') |
| } |
| } |
| } |
| // --- referencing to other mock entities |
| EntityMockAttributeByReference: { |
| if (attribute.mockedEntity == null) { |
| append( |
| '''generateAttribute("«attribute.attributeRef.name»", «EntityUtils.getDtoFQNForLEntity(attribute.attributeRef.type)».class, « |
| ((attribute.optionalFor as double)/100.0)»); |
| ''') |
| } |
| else { |
| append( |
| '''generateAttribute("«attribute.attributeRef.name»", "«attribute.mockedEntity.name»", « |
| ((attribute.optionalFor as double)/100.0)»); |
| ''') |
| } |
| } |
| } |
| } |
| // --- for each entity attribute, just copy from the internal mock data |
| for (calculation : mockEntity.calculations) { |
| var doc = calculation.documentation |
| if (doc instanceof String && !doc.trim.isEmpty) { |
| append( |
| '''/** «doc» */ |
| ''') |
| } |
| var paramString = '''''' |
| for (param : calculation.params) { |
| if (!paramString.isEmpty) { |
| paramString = '''«paramString»,''' |
| } |
| if (param.attributeRef != null) { |
| paramString = '''«paramString»(«param.jvmType.identifier»)getAttribute("«param. |
| getParamName(".")»")''' |
| } |
| else if (param.isReferenceOnExistingDto) { |
| paramString = '''«paramString»(«param.jvmType.identifier»)((« |
| EntityUtils.getDtoFQNForLEntity(param.referencePaths.get(0).type) |
| »)getAttribute("« |
| param.referencePaths.get(0).name |
| »", mockDataGenerator, persistenceUnit)).get«param.referenceRef.name.toFirstUpper»()''' |
| } |
| else { |
| paramString = '''«paramString»(«param.jvmType.identifier»)getAttribute("«param. |
| getParamName(".")»", mockDataGenerator, persistenceUnit)''' |
| } |
| } |
| append( |
| '''try { |
| setFixAttribute("«calculation.attributeRef.name»", calculate_«calculation.attributeRef.name»(«paramString»)); |
| } |
| catch (NullPointerException npe) {} |
| catch (Exception e) { |
| log.error(e.getLocalizedMessage()+e.getCause()); |
| } |
| ''' |
| ) |
| } |
| ] |
| ] |
| if (mockEntity.byResource == null) { |
| // --- generate an entity based on the values generated before via generateDataRow(), just copying values into the entity |
| members += pckg.toMethod('''generateEntity''', _typeReferenceBuilder.typeRef(Object)) [ |
| visibility = JvmVisibility.PUBLIC |
| parameters += mockEntity.toParameter("dataRow", _typeReferenceBuilder.typeRef(Object)) |
| final = true |
| body = [ |
| append(''' |
| reset(); |
| ''') |
| // --- if an iterator is set |
| if (mockEntity.iterator != null) { |
| append(''' |
| setFixAttribute("«mockEntity.iterator.name»", dataRow); |
| ''') |
| } |
| // --- generate that entity |
| append( |
| '''«mockEntity.entityDtoFQN» internal = new «mockEntity.entityDtoFQN»(); |
| entity = internal; |
| generateData(); |
| mockDataGenerator.addDtoMockData(internal, mockData); |
| try { |
| ''') |
| // --- for each entity attribute, just copy from the internal mock data |
| for (attribute : mockEntity.attributes) { |
| switch attribute { |
| EntityMockAttributeByObject: { |
| append( |
| '''internal.set«WordUtils.capitalize(attribute.attributeRef.name)»(«attribute. |
| attributeRef.baseMockDataTypeCasting( |
| '''mockData.get("«attribute.attributeRef.name»")''')»); |
| ''') |
| } |
| EntityMockAttributeFiller: { |
| append( |
| '''internal.set«WordUtils.capitalize(attribute.attributeRef.name)»(«attribute. |
| attributeRef.baseMockDataTypeCasting( |
| '''mockData.get("«attribute.attributeRef.name»")''')»); |
| ''') |
| } |
| EntityMockAttributeByReference: { |
| append( |
| '''internal.set«WordUtils.capitalize(attribute.attributeRef.name)»((«EntityUtils.getDtoFQNForLEntity(attribute. |
| attributeRef.type)»)mockData.get("«attribute.attributeRef.name»")); |
| ''') |
| } |
| } |
| } |
| // --- for each entity attribute, just copy from the internal mock data |
| for (calculation : mockEntity.calculations) { |
| append( |
| '''internal.set«WordUtils.capitalize(calculation.attributeRef.name)»(«calculation. |
| attributeRef.baseMockDataTypeCasting( |
| '''mockData.get("«calculation.attributeRef.name»")''')»); |
| ''') |
| } |
| append( |
| '''} |
| catch (Exception e) { |
| log.error(e.getLocalizedMessage()+e.getCause()); |
| } |
| return entity;''') |
| ] |
| ] |
| } |
| ] |
| } |
| |
| def boolean isReferenceOnExistingDto(EntityMockEntityFunctionParameter param) { |
| val entityMock = param.eContainer.eContainer as EntityMockEntity |
| for (attribute : entityMock.attributes) { |
| if (attribute instanceof EntityMockAttributeByReference) { |
| if (attribute.mockedEntity == null) { |
| if (attribute.attributeRef.type.equals(param.referenceRef.eContainer)) { |
| return true |
| } |
| } |
| } |
| } |
| return false |
| } |
| |
| def JvmTypeReference getJvmType(EntityMockEntityFunctionParameter param) { |
| if (param.attributeRef != null) { |
| return param.attributeRef.jvmTypeRef |
| } else { |
| return param.referenceRef.typeJvm |
| } |
| } |
| |
| def String getParamName(EntityMockEntityFunctionParameter param, String separator) { |
| var paramName = '''''' |
| if (param.attributeRef != null) { |
| paramName = '''«param.attributeRef.name»''' |
| } else { |
| for (referencePath : param.referencePaths) { |
| paramName = '''«paramName»«referencePath.name»«separator»''' |
| } |
| paramName = '''«paramName»«param.referenceRef.name»''' |
| } |
| return paramName |
| } |
| |
| /** |
| * generate the JVM type reference needed for of the entity attribute |
| */ |
| def JvmTypeReference jvmTypeRef(LEntityAttribute attribute) { |
| var etype = dtType.getBasicType(attribute) |
| switch (etype) { |
| case BOOLEAN: return _typeReferenceBuilder.typeRef(boolean) |
| case BYTE: return _typeReferenceBuilder.typeRef(byte) |
| case DATE: return _typeReferenceBuilder.typeRef(java.util.Date) |
| case FLOAT: return _typeReferenceBuilder.typeRef(float) |
| case DOUBLE: return _typeReferenceBuilder.typeRef(double) |
| case SHORT: return _typeReferenceBuilder.typeRef(short) |
| case INTEGER: return _typeReferenceBuilder.typeRef(int) |
| case LONG: return _typeReferenceBuilder.typeRef(long) |
| //case TIME: return _typeReferenceBuilder.typeRef(Time) |
| case STRING: return _typeReferenceBuilder.typeRef(String) |
| default: return _typeReferenceBuilder.typeRef(Object) |
| } |
| } |
| |
| /** |
| * generate the code for casting a mock data to the data type of the entity attribute |
| */ |
| def String baseMockDataTypeCasting(LEntityAttribute attribute, String getting) { |
| if (attribute.type instanceof LEnum) { |
| return '''«EntityUtils.getDtoFQNForLEnum(attribute.type as LEnum)».valueOf(«getting».toString())''' |
| } |
| if (attribute.type instanceof LBean) { |
| return '''(«EntityUtils.getDtoFQNForLBean(attribute.type as LBean)»)(«getting»)''' |
| } |
| var etype = dtType.getBasicType(attribute) |
| switch (etype) { |
| case BOOLEAN: return '''asBoolean(«getting»)''' |
| case BYTE: return '''asByte(«getting»)''' |
| case DATE: return '''asDate(«getting»)''' |
| case FLOAT: return '''asFloat(«getting»)''' |
| case DOUBLE: return '''asDouble(«getting»)''' |
| case SHORT: return '''asShort(«getting»)''' |
| case INTEGER: return '''asInt(«getting»)''' |
| case LONG: return '''asLong(«getting»)''' |
| case TIME: return '''asTime(«getting»)''' |
| default: return '''asString(«getting»)''' |
| } |
| } |
| |
| val public static ENTITY_MOCK_GENERATOR = "EntityMockGenerator" |
| |
| def String asString(Date date) { |
| if (date.yesterday) { |
| return '''"yesterday"''' |
| } |
| if (date.today) { |
| return '''"today"''' |
| } |
| if (date.tomorrow) { |
| return '''"tomorrow"''' |
| } |
| return '''"'''+String.format("%04d-%02d-%02d", date.year, date.month, date.day)+'''"''' |
| } |
| |
| /** |
| * generate the main mock data generator for the EntityMockPackage |
| */ |
| def void generateMockDataGenerator(EntityMockModel model, IJvmDeclaredTypeAcceptor acceptor, |
| EntityMockPackage pckg) { |
| acceptor.accept(model.toClass('''«pckg.entityMockName».«ENTITY_MOCK_GENERATOR»''')) [ |
| // @TODO... AnnotationExtension needs addAnnAttr with Class<?> parameter !!! |
| // var annotationRef = _annotationTypesBuilder.annotationRef(typeof(Component)) |
| // annotationRef.addAnnAttr(model, "service", IInitializationProvider as Class) |
| // annotationRef.addAnnAttr(model, "immediate", true) |
| // annotations += annotationRef |
| |
| superTypes += _typeReferenceBuilder.typeRef(AEntityMockDataGenerator) |
| it.final = true |
| |
| var JvmField field = null |
| field = model.toField("log", _typeReferenceBuilder.typeRef(Logger)) [setInitializer([ append('''LoggerFactory.getLogger(«pckg.entityMockName».«ENTITY_MOCK_GENERATOR».class)''') ])] |
| field.final = true |
| field.static = true |
| members += field |
| |
| members += model.toMethod('''getRunWithPriority''', _typeReferenceBuilder.typeRef(int)) [ |
| annotations += pckg.toAnnotation(Override) |
| body = [ |
| var count = IEntityMockDataGenerator.DEFAULT_PRIORITY; |
| if (pckg.runPriority > 0) { |
| count = pckg.runPriority |
| } |
| append( |
| ''' |
| return «count»; |
| ''' |
| ) |
| ] |
| ] |
| members += model.toMethod('''getDataInterchangeSteps''', _typeReferenceBuilder.typeRef(int)) [ |
| annotations += pckg.toAnnotation(Override) |
| body = [ |
| var count = 0; |
| if (pckg.datainterchanges != null) { |
| count = pckg.datainterchanges.datainterchanges.size |
| } |
| append( |
| ''' |
| return «count»; |
| ''' |
| ) |
| ] |
| ] |
| members += model.toMethod('''getEntityMockSteps''', _typeReferenceBuilder.typeRef(int)) [ |
| annotations += pckg.toAnnotation(Override) |
| body = [ |
| var count = 0; |
| if (pckg.entities != null) { |
| count = pckg.entities.entities.size |
| } |
| append( |
| ''' |
| return «count»; |
| ''' |
| ) |
| ] |
| ] |
| members += model.toMethod('''runDataInterchanges''', _typeReferenceBuilder.typeRef(Void::TYPE)) [ |
| parameters += pckg.toParameter("persistenceService", _typeReferenceBuilder.typeRef(IPersistenceService)) |
| parameters += pckg.toParameter("entityMockDbFiller", _typeReferenceBuilder.typeRef(IEntityMockDataDbFiller)) |
| body = [ |
| if (pckg.datainterchanges != null) { |
| for (runDatainterchange : pckg.datainterchanges.datainterchanges) { |
| append( |
| ''' |
| try { |
| entityMockDbFiller.notifyInitializationStep("datainterchange «runDatainterchange.datainterchangeRef.name»", 1, 0.3); |
| «runDatainterchange.datainterchangeRef.getBasicRunConfiguration(true, runDatainterchange.getFileURL)» |
| entityMockDbFiller.notifyInitializationStep("datainterchange «runDatainterchange.datainterchangeRef.name»", 0, 0.4); |
| «runDatainterchange.datainterchangeRef.defaultVariableName».run(entityMockDbFiller); |
| entityMockDbFiller.notifyInitializationStep("datainterchange «runDatainterchange.datainterchangeRef.name»", 0, 0.9); |
| } |
| catch (Exception e) { |
| log.error(e.getLocalizedMessage()+e.getCause()); |
| } |
| ''') |
| } |
| } |
| ] |
| ] |
| members += pckg.toMethod('''getMockData''', _typeReferenceBuilder.typeRef(Map)) [ |
| parameters += pckg.toParameter("entityMockDbFiller", _typeReferenceBuilder.typeRef(IEntityMockDataDbFiller)) |
| body = [ |
| append( |
| '''initialize(); |
| ''') |
| // --- list of all mock entities mocked to far |
| var mockEntitiesDone = new ArrayList<EntityMockEntity> |
| // --- list of all mock entities that have to be mocked |
| var mockEntitiesToDo = new ArrayList<EntityMockEntity> |
| mockEntitiesToDo.addAll(pckg.entities?.entities) |
| var breakTodo = false |
| // --- either all mock entities are already mocked or a break condition has been reached |
| while (!mockEntitiesToDo.isEmpty && !breakTodo) { |
| var handled = 0 |
| var mockEntitesStepping = mockEntitiesToDo.toArray |
| |
| // --- every mock entity, that has to be mocked |
| for (mockEntityO : mockEntitesStepping) { |
| var mockEntity = mockEntityO as EntityMockEntity |
| |
| // --- check, if this mock entity can be mocked considering references |
| if (mockEntity.canBeHandled(mockEntitiesDone)) { |
| |
| // --- remember, that something has been mocked in this inner loop |
| handled++ |
| |
| if (mockEntity.iterate instanceof IntegerIterate) { |
| var iterate = mockEntity.iterate as IntegerIterate |
| // --- generate the mocking part |
| append( |
| ''' |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»", 1, 0.3); |
| «mockEntity.entityMockName» entity«mockEntity.name» = new «mockEntity.entityMockName»(this); |
| Iterator<Integer> iterator«mockEntity.name» = new IntegerIterator(«iterate.from», «iterate.until», «iterate.step»); |
| List list«mockEntity.name» = new ArrayList<«mockEntity.entityDtoFQN»>(); |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»", 0, 0.6); |
| while (iterator«mockEntity.name».hasNext()) { |
| list«mockEntity.name».add(entity«mockEntity.name».generateEntity(iterator«mockEntity.name».next())); |
| } |
| addMockObjects("«mockEntity.name»", «mockEntity.entityDtoFQNClass», "«mockEntity.entityRef.persistenceUnit»", list«mockEntity.name»); |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»", 0, 0.9); |
| ''') |
| } |
| if (mockEntity.iterate instanceof DateIterate) { |
| var iterate = mockEntity.iterate as DateIterate |
| // --- generate the mocking part |
| append( |
| ''' |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»", 1, 0.3); |
| «mockEntity.entityMockName» entity«mockEntity.name» = new «mockEntity.entityMockName»(this); |
| Iterator<Date> iterator«mockEntity.name» = new DateIterator(«iterate.from.asString», «iterate.until.asString», «iterate.stepCount», "«iterate.stepType»"); |
| List list«mockEntity.name» = new ArrayList<«mockEntity.entityDtoFQN»>(); |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»", 0, 0.6); |
| while (iterator«mockEntity.name».hasNext()) { |
| list«mockEntity.name».add(entity«mockEntity.name».generateEntity(iterator«mockEntity.name».next())); |
| } |
| addMockObjects("«mockEntity.name»", «mockEntity.entityDtoFQNClass», "«mockEntity.entityRef.persistenceUnit»", list«mockEntity.name»); |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»", 0, 0.9); |
| ''') |
| } |
| else if (mockEntity.byResource != null) { |
| |
| // --- generate the copy part |
| // --- generate the mocking part |
| append( |
| ''' |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»", 1, 0.3); |
| «mockEntity.entityMockName» entity«mockEntity.name» = new «mockEntity.entityMockName»(this); |
| List list«mockEntity.name» = new ArrayList<«mockEntity.entityDtoFQN»>(); |
| Iterator iterator«mockEntity.name» = entity«mockEntity.name».getDataRows().iterator(); |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»", 0, 0.6); |
| while (iterator«mockEntity.name».hasNext()) { |
| list«mockEntity.name».add(entity«mockEntity.name».generateEntity(iterator«mockEntity.name».next())); |
| } |
| addMockObjects("«mockEntity.name»", «mockEntity.entityDtoFQNClass», "«mockEntity.entityRef.persistenceUnit»", list«mockEntity.name»); |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»", 0, 0.9); |
| ''') |
| |
| // --- if blob mapping has to be generated too |
| if (mockEntity.createBlobMapping) { |
| append( |
| ''' |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»Blobs", 0, 0.93); |
| List list«mockEntity.name»Blobs = new ArrayList<org.eclipse.osbp.utils.blob.dtos.BlobDto>(); |
| BlobTypingAPI «mockEntity.name»BlobTypingAPI = new BlobTypingAPI(); |
| BlobUpload «mockEntity.name»BlobUpload = new BlobUpload(); |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»Blobs", 0, 0.96); |
| for (Object list«mockEntity.name»Object : list«mockEntity.name») { |
| if (list«mockEntity.name»Object instanceof org.eclipse.osbp.utils.blob.dtos.BlobMappingDto) { |
| org.eclipse.osbp.utils.blob.dtos.BlobMappingDto blobMappingDto = (org.eclipse.osbp.utils.blob.dtos.BlobMappingDto)list«mockEntity. |
| name»Object; |
| try { |
| InputStream inputStream = new BufferedInputStream(this.getClass().getClassLoader().getResourceAsStream("/resources/entitymock/"+blobMappingDto.getFileName())); |
| if (inputStream != null) { |
| List<org.eclipse.osbp.utils.blob.dtos.BlobDto> blobs = «mockEntity.name»BlobUpload.createBlobMappingBlobs( |
| inputStream, |
| «mockEntity.name»BlobTypingAPI.getMimeTypeContentTypeById(blobMappingDto.getMimeTypeId()), |
| «mockEntity.name»BlobTypingAPI, |
| blobMappingDto.getId() |
| ); |
| blobMappingDto.setBlobsRef(blobs); |
| for (org.eclipse.osbp.utils.blob.dtos.BlobDto blob : blobs) { |
| list«mockEntity.name»Blobs.add(blob); |
| } |
| } |
| } |
| catch (Exception e) { |
| log.error(e.getLocalizedMessage()+e.getCause()); |
| } |
| } |
| } |
| addMockObjects("«mockEntity.name»Blobs", org.eclipse.osbp.utils.blob.dtos.BlobDto.class, "blob", list«mockEntity.name»Blobs); |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»Blobs", 0, 0.99); |
| ''') |
| } |
| } else { |
| |
| // --- generate the mocking part |
| append( |
| ''' |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»", 1, 0.3); |
| «mockEntity.entityMockName» entity«mockEntity.name» = new «mockEntity.entityMockName»(this); |
| Iterator<Integer> iterator«mockEntity.name» = new IntegerIterator(1, fillerProvider.signedinteger(«mockEntity.minRows», «mockEntity.maxRows»)); |
| List list«mockEntity.name» = new ArrayList<«mockEntity.entityDtoFQN»>(); |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»", 0, 0.6); |
| while (iterator«mockEntity.name».hasNext()) { |
| list«mockEntity.name».add(entity«mockEntity.name».generateEntity(iterator«mockEntity.name».next())); |
| } |
| addMockObjects("«mockEntity.name»", «mockEntity.entityDtoFQNClass», "«mockEntity.entityRef.persistenceUnit»", list«mockEntity.name»); |
| entityMockDbFiller.notifyInitializationStep("generate «mockEntity.entityMockName»", 0, 0.9); |
| ''') |
| } |
| |
| // --- remember this mock entity as being mocked |
| mockEntitiesDone.add(mockEntity as EntityMockEntity) |
| |
| // --- remove this mock entity from "to be mocked" |
| mockEntitiesToDo.remove(mockEntity) |
| } |
| } |
| |
| // --- if nothing could be mocked in the inner loop, break the outer loop |
| breakTodo = (handled == 0) |
| } |
| append( |
| ''' |
| return mockDataClassMap;''') |
| ] |
| ] |
| ] |
| } |
| |
| /** |
| * The dispatch method {@code infer} is called for each instance of the |
| * given element's type that is contained in a resource. |
| * |
| * @param element |
| * the model to create one or more |
| * {@link JvmDeclaredType declared |
| * types} from. |
| * @param acceptor |
| * each created |
| * {@link JvmDeclaredType type} |
| * without a container should be passed to the acceptor in order |
| * get attached to the current resource. The acceptor's |
| * {@link IJvmDeclaredTypeAcceptor#accept(org.eclipse.xtext.common.types.JvmDeclaredType) |
| * accept(..)} method takes the constructed empty type for the |
| * pre-indexing phase. This one is further initialized in the |
| * indexing phase using the closure you pass to the returned |
| * {@link IPostIndexingInitializing#initializeLater(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) |
| * initializeLater(..)}. |
| * @param isPreIndexingPhase |
| * whether the method is called in a pre-indexing phase, i.e. |
| * when the global index is not yet fully updated. You must not |
| * rely on linking using the index if isPreIndexingPhase is |
| * <code>true</code>. |
| */ |
| def dispatch void infer(EntityMockModel model, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) { |
| model.packages.forEach [ pckg | |
| // --- generate one class for each mock resource |
| pckg.resources?.resources?.forEach [ mockResource | |
| try { |
| generateMockResource(model, acceptor, pckg, mockResource) |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| ] |
| // --- generate one class for each mock object defined |
| pckg.objects?.objects?.forEach [ mockObject | |
| try { |
| generateMockObject(model, acceptor, pckg, mockObject) |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| ] |
| // --- generate a class for each mock entity |
| pckg.entities?.entities?.forEach [ mockEntity | |
| try { |
| generateMockEntity(model, acceptor, pckg, mockEntity) |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| ] |
| // --- generate the mock generator |
| try { |
| generateMockDataGenerator(model, acceptor, pckg) |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| ] |
| } |
| |
| /** |
| * checks if the mockEntityTodo can be handled, considering all requested references to other mock entities, |
| * which may already been mocked |
| */ |
| def boolean canBeHandled(EntityMockEntity mockEntityTodo, ArrayList<EntityMockEntity> mockEntitiesDone) { |
| if (mockEntityTodo.byResource != null) { |
| return true |
| } |
| for (attribute : mockEntityTodo.attributes) { |
| |
| // --- check every reference to another mock entity |
| if (attribute instanceof EntityMockAttributeByReference) { |
| var referencedMockEntity = (attribute as EntityMockAttributeByReference).mockedEntity |
| // --- if the referenced entity should be mocked and it not has been mocked so far |
| if ((referencedMockEntity != null) && !mockEntitiesDone.contains(referencedMockEntity)) { |
| // --- if any referenced entity has not been mocked so far, this mock entity can't be mocked at this time |
| return false |
| } |
| } |
| } |
| return true |
| } |
| |
| /** |
| * generate a mock object class |
| */ |
| def void generateMockResource(EntityMockModel model, IJvmDeclaredTypeAcceptor acceptor, EntityMockPackage pckg, |
| EntityMockResource mockResource) { |
| acceptor.accept(model.toClass('''«pckg.entityMockName».«mockResource.entityMockName»''')) [ |
| superTypes += _typeReferenceBuilder.typeRef(ABaseMockResource) |
| // @see org.eclipse.osbp.utils.constants.GeneratorConstants.GeneratorConstants |
| documentation = '''<b>This class was auto generated! Leave it unmodified to avoid unpredictable results!</b><br> |
| «mockResource.documentation» |
| ''' |
| final = true |
| |
| var JvmField field = null |
| field = model.toField("log", _typeReferenceBuilder.typeRef(Logger)) [setInitializer([ append('''LoggerFactory.getLogger(«pckg.entityMockName».«mockResource.entityMockName».class)''') ])] |
| field.final = true |
| field.static = true |
| members += field |
| |
| members += pckg.toConstructor [ |
| body = [ |
| val List<String> attributes = new ArrayList |
| for (attribute : mockResource.attributes) { |
| attributes.add('''"«attribute.name»"''') |
| } |
| append( |
| '''setAttributes(«StringUtils.join(attributes, ",")»); |
| ''') |
| for (datarow : mockResource.datarows) { |
| val List<String> values = new ArrayList |
| values.add('''"«datarow.name»"''') |
| for (value : datarow.values) { |
| values.add('''"«value»"''') |
| } |
| append( |
| '''addDataRow(«StringUtils.join(values, ",")»); |
| ''') |
| } |
| ] |
| ] |
| ] |
| } |
| } |