use of org.kie.workbench.common.services.datamodeller.driver.ModelDriverException in project kie-wb-common by kiegroup.
the class FormGenerationIntegrationTest method testNestedFormsGeneration.
@Test
public void testNestedFormsGeneration() {
when(module.getRootPath()).thenReturn(rootPathWithoutNestedForms);
ModelDriver modelDriver = new JavaRoasterModelDriver(ioService, Paths.convert(rootPathWithoutNestedForms), moduleClassLoader, filterHolder);
try {
ModelDriverResult result = modelDriver.loadModel();
dataModel = result.getDataModel();
} catch (ModelDriverException e) {
throw new ServiceException("It was not possible to load model for URI: " + rootPathWithoutNestedForms.toURI(), e);
}
when(dataModelerService.loadModel(module)).thenReturn(dataModel);
when(commentedOptionFactory.makeCommentedOption(any())).thenReturn(commentedOption);
final FormGenerationResult formGenerationResult = generateForm("FormGenerationTest_TwinTasks-taskform.frm", taskFormModels.get(4));
final List<String> expectedDataObjectFormFields = Arrays.asList("nestedObjectForSubform", SUBFORM_FIELDTYPE, "nestedObjectListForMultipleSubform", MULTIPLESUBFORM_FIELDTYPE, "bigDecimalDecimalBox", DECIMALBOX_FIELDTYPE, "bigIntegerIntegerBox", INTEGERBOX_FIELDTYPE, "booleanCheckBox", CHECKBOX_FIELDTYPE, "byteIntegerBox", INTEGERBOX_FIELDTYPE, "charTextBox", TEXTBOX_FIELDTYPE, "dateDatePicker", DATEPICKER_FIELDTYPE, "doubleDecimalBox", DECIMALBOX_FIELDTYPE, "floatDecimalBox", DECIMALBOX_FIELDTYPE, "longIntegerBox", INTEGERBOX_FIELDTYPE, "stringListBox", TEXTBOX_FIELDTYPE, "intIntegerBox", INTEGERBOX_FIELDTYPE, "shortIntegerBox", INTEGERBOX_FIELDTYPE, "bigDecimalRadioGroup", DECIMALBOX_FIELDTYPE, "bigIntegerRadioGroup", INTEGERBOX_FIELDTYPE, "byteRadioGroup", INTEGERBOX_FIELDTYPE, "charRadioGroup", TEXTBOX_FIELDTYPE, "doubleSlider", DECIMALBOX_FIELDTYPE, "doubleRadioGroup", DECIMALBOX_FIELDTYPE, "floatRadioGroup", DECIMALBOX_FIELDTYPE, "intRadioGroup", INTEGERBOX_FIELDTYPE, "intSlider", INTEGERBOX_FIELDTYPE, "longRadioGroup", INTEGERBOX_FIELDTYPE, "stringPicture", TEXTBOX_FIELDTYPE, "stringRadioGroup", TEXTBOX_FIELDTYPE, "stringTextArea", TEXTBOX_FIELDTYPE, "stringTextBox", TEXTBOX_FIELDTYPE, "shortRadioGroup", INTEGERBOX_FIELDTYPE);
final List<String> expectedNestedDataObjectFormFields = Arrays.asList("stringListBox", TEXBOX_FIELDTYPE, "doubleDecimalBox", DECIMALBOX_FIELDTYPE, "booleanCheckBox", CHECKBOX_FIELDTYPE, "dateDatePicker", DATEPICKER_FIELDTYPE, "charRadioGroup", TEXTBOX_FIELDTYPE, "doubleSlider", DECIMALBOX_FIELDTYPE, "intIntegerBox", INTEGERBOX_FIELDTYPE, "stringPicture", TEXBOX_FIELDTYPE, "stringTextArea", TEXTBOX_FIELDTYPE, "stringTextBox", TEXTBOX_FIELDTYPE);
final FormDefinition rootForm = formGenerationResult.getRootForm();
final List<FormDefinition> nestedForms = formGenerationResult.getNestedForms();
final FormDefinition dataObjectForm = nestedForms.get(0);
final FormDefinition nestedDataObjectForm = nestedForms.get(1);
final String dataObjectFormID = dataObjectForm.getId();
testNestedFormsHaveCorrectId(rootForm, "_dataObject1", dataObjectFormID);
testNestedFormsHaveCorrectId(rootForm, "_dataObject2", dataObjectFormID);
testNestedFormsHaveCorrectId(rootForm, "dataObject_", dataObjectFormID);
final String nestedDataObjectFormID = nestedDataObjectForm.getId();
testFormDefinition(dataObjectForm, expectedDataObjectFormFields);
testNestedFormsHaveCorrectId(dataObjectForm, "nestedObjectForSubform", nestedDataObjectFormID);
testFormDefinition(nestedDataObjectForm, expectedNestedDataObjectFormFields);
}
use of org.kie.workbench.common.services.datamodeller.driver.ModelDriverException in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method loadModel.
@Override
public ModelDriverResult loadModel() throws ModelDriverException {
ModelDriverResult result = new ModelDriverResult();
DataModel dataModel;
String fileContent;
dataModel = createModel();
result.setDataModel(dataModel);
List<Path> rootPaths = new ArrayList<Path>();
rootPaths.add(javaRootPath);
Collection<FileUtils.ScanResult> scanResults = FileUtils.getInstance().scan(ioService, rootPaths, ".java", true);
if (scanResults != null) {
for (FileUtils.ScanResult scanResult : scanResults) {
logger.debug("Starting file loading into model, file: " + scanResult.getFile());
fileContent = ioService.readAllString(scanResult.getFile());
if (fileContent == null || "".equals(fileContent)) {
logger.debug("file: " + scanResult.getFile() + " is empty.");
result.addError(new DriverError("File has no content", Paths.convert(scanResult.getFile())));
continue;
}
try {
JavaType<?> javaType = Roaster.parse(fileContent);
final boolean isManaged = isManagedJavaType(javaType);
final boolean vetoed = (isManaged ? isVetoed(javaType) : false);
if (isManaged && !vetoed) {
if (javaType.getSyntaxErrors() != null && !javaType.getSyntaxErrors().isEmpty()) {
// if a file has parsing errors it will be skipped.
addSyntaxErrors(result, scanResult.getFile(), javaType.getSyntaxErrors());
} else if (javaType.isEnum()) {
loadFromJavaEnum((JavaEnumSource) javaType, scanResult.getFile(), dataModel, result);
} else {
loadFromJavaClass((JavaClassSource) javaType, scanResult.getFile(), dataModel, result);
}
} else if (vetoed) {
logger.debug("The class, {}, in the file, {}, was vetoed and will be skipped.", javaType.getQualifiedName(), scanResult.getFile());
} else {
logger.debug("File: " + scanResult.getFile() + " do not contain a managed java type, it will be skipped.");
}
} catch (ParserException e) {
result.addError(new DriverError(e.getMessage(), Paths.convert(scanResult.getFile())));
} catch (Exception e) {
// Unexpected error.
logger.error(errorMessage(MODEL_LOAD_GENERIC_ERROR, javaRootPath.toUri()), e);
throw new ModelDriverException(errorMessage(MODEL_LOAD_GENERIC_ERROR, javaRootPath.toUri()), e);
}
}
}
return result;
}
use of org.kie.workbench.common.services.datamodeller.driver.ModelDriverException in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method parseDataObject.
private Pair<DataObject, List<ObjectProperty>> parseDataObject(JavaClassSource javaClassSource) throws ModelDriverException {
String className;
String packageName;
String superClass;
String qualifiedName;
ClassTypeResolver classTypeResolver;
className = javaClassSource.getName();
packageName = javaClassSource.getPackage();
qualifiedName = NamingUtils.createQualifiedName(packageName, className);
if (logger.isDebugEnabled()) {
logger.debug("Building DataObject for, packageName: " + packageName + ", className: " + className);
}
classTypeResolver = DriverUtils.createClassTypeResolver(javaClassSource, classLoader);
Visibility visibility = DriverUtils.buildVisibility(javaClassSource.getVisibility());
DataObject dataObject = new DataObjectImpl(packageName, className, visibility, javaClassSource.isAbstract(), javaClassSource.isFinal());
List<ObjectProperty> unmanagedProperties = new ArrayList<ObjectProperty>();
try {
if (javaClassSource.getSuperType() != null) {
superClass = resolveTypeName(classTypeResolver, javaClassSource.getSuperType());
dataObject.setSuperClassName(superClass);
}
List<AnnotationSource<JavaClassSource>> annotations = javaClassSource.getAnnotations();
if (annotations != null) {
for (AnnotationSource annotation : annotations) {
addJavaClassAnnotation(dataObject, annotation, classTypeResolver);
}
}
List<MethodSource<JavaClassSource>> classMethods = javaClassSource.getMethods();
if (classMethods != null) {
for (MethodSource<JavaClassSource> classMethod : classMethods) {
if (isAccepted(classMethod)) {
addMethod(dataObject, classMethod, classTypeResolver);
}
}
}
List<JavaSource<?>> nestedTypes = javaClassSource.getNestedTypes();
if (nestedTypes != null) {
for (JavaSource nestedType : nestedTypes) {
if (isAccepted(nestedType)) {
if (nestedType instanceof JavaClassSource) {
JavaClassImpl nestedJavaClass = new JavaClassImpl("", nestedType.getName(), DriverUtils.buildVisibility(nestedType.getVisibility()));
dataObject.addNestedClass(nestedJavaClass);
if (javaClassSource.getInterfaces() != null) {
for (String interfaceDefinition : ((JavaClassSource) nestedType).getInterfaces()) {
nestedJavaClass.addInterface(interfaceDefinition);
}
}
List<AnnotationSource<JavaClassSource>> nestedClassAnnotations = nestedType.getAnnotations();
if (nestedClassAnnotations != null) {
for (AnnotationSource annotation : nestedClassAnnotations) {
addJavaClassAnnotation(nestedJavaClass, annotation, classTypeResolver);
}
}
List<MethodSource<JavaClassSource>> nestedClassMethods = ((JavaClassSource) nestedType).getMethods();
if (nestedClassMethods != null) {
for (Method nestedClassMethod : nestedClassMethods) {
if (isAccepted(nestedClassMethod)) {
addMethod(nestedJavaClass, nestedClassMethod, classTypeResolver);
}
}
}
}
}
}
}
List<FieldSource<JavaClassSource>> fields = javaClassSource.getFields();
if (fields != null) {
for (FieldSource<JavaClassSource> field : fields) {
if (DriverUtils.isManagedType(field.getType(), classTypeResolver)) {
addProperty(dataObject, field, classTypeResolver);
} else {
logger.debug("field: " + field + "with fieldName: " + field.getName() + " won't be loaded by the diver because type: " + field.getType().getName() + " isn't a managed type.");
unmanagedProperties.add(new ObjectPropertyImpl(field.getName(), field.getType().toString(), false, DriverUtils.buildVisibility(field.getVisibility()), field.isStatic(), field.isFinal()));
}
}
}
List<Import> imports = javaClassSource.getImports();
if (imports != null) {
for (Import _import : imports) {
dataObject.addImport(new ImportImpl(_import.getQualifiedName()));
}
}
return new Pair<DataObject, List<ObjectProperty>>(dataObject, unmanagedProperties);
} catch (ClassNotFoundException e) {
logger.error(errorMessage(DATA_OBJECT_LOAD_ERROR, qualifiedName), e);
throw new ModelDriverException(errorMessage(DATA_OBJECT_LOAD_ERROR, qualifiedName), e);
} catch (ModelDriverException e) {
logger.error(errorMessage(DATA_OBJECT_LOAD_ERROR, qualifiedName), e);
throw new ModelDriverException(errorMessage(DATA_OBJECT_LOAD_ERROR, qualifiedName), e);
}
}
use of org.kie.workbench.common.services.datamodeller.driver.ModelDriverException in project kie-wb-common by kiegroup.
the class JavaRoasterModelDriver method loadDataObject.
public ModelDriverResult loadDataObject(final String source, final Path path) throws ModelDriverException {
ModelDriverResult result = new ModelDriverResult();
DataModel dataModel = createModel();
result.setDataModel(dataModel);
if (source == null || "".equals(source)) {
logger.debug("source: " + source + " is empty.");
result.addError(new DriverError("Source is empty", Paths.convert(path)));
return result;
}
try {
JavaType<?> javaType = Roaster.parse(source);
if (javaType.isClass()) {
if (javaType.getSyntaxErrors() != null && !javaType.getSyntaxErrors().isEmpty()) {
// if a file has parsing errors it will be skipped.
addSyntaxErrors(result, path, javaType.getSyntaxErrors());
} else {
try {
// try to load the data object.
Pair<DataObject, List<ObjectProperty>> pair = parseDataObject((JavaClassSource) javaType);
dataModel.addDataObject(pair.getK1());
result.setClassPath(pair.getK1().getClassName(), Paths.convert(path));
result.setUnmanagedProperties(pair.getK1().getClassName(), pair.getK2());
} catch (ModelDriverException e) {
logger.error("An error was produced when source: " + source + " was being loaded into a DataObject.", e);
addModelDriverError(result, path, e);
}
}
} else {
logger.debug("No Class definition was found for source: " + source + ", it will be skipped.");
}
} catch (ParserException e) {
result.addError(new DriverError(e.getMessage(), Paths.convert(path)));
} catch (Exception e) {
// Unexpected exception.
logger.error(errorMessage(MODEL_LOAD_GENERIC_ERROR, javaRootPath.toUri()), e);
throw new ModelDriverException(errorMessage(MODEL_LOAD_GENERIC_ERROR, javaRootPath.toUri()), e);
}
return result;
}
use of org.kie.workbench.common.services.datamodeller.driver.ModelDriverException in project kie-wb-common by kiegroup.
the class DriverUtils method isSimpleGeneric.
public static Object[] isSimpleGeneric(Type type, ClassTypeResolver classTypeResolver) throws ModelDriverException {
Object[] result = new Object[3];
result[0] = false;
result[1] = null;
result[2] = null;
if (type.isArray() || type.isPrimitive() || !type.isParameterized() || (type.isParameterized() && type.getTypeArguments().size() != 1)) {
return result;
}
Type<?> argument = ((List<Type>) type.getTypeArguments()).get(0);
if (!isSimpleClass(argument)) {
return result;
}
try {
String outerClass = classTypeResolver.getFullTypeName(type.getName());
String argumentClass = classTypeResolver.getFullTypeName(argument.getName());
result[0] = true;
result[1] = outerClass;
result[2] = argumentClass;
return result;
} catch (ClassNotFoundException e) {
throw new ModelDriverException("Class could not be resolved for name: " + type.getName() + ". " + e.getMessage(), e);
}
}
Aggregations