Search in sources :

Example 71 with DataObject

use of org.kie.workbench.common.services.datamodeller.core.DataObject in project kie-wb-common by kiegroup.

the class MainDataObjectEditorTest method loadDataObjectTest.

@Test
public void loadDataObjectTest() {
    MainDataObjectEditor objectEditor = createObjectEditor();
    // The domain editors typically reacts upon DataModelerContext changes.
    // when the context changes the editor will typically be reloaded.
    objectEditor.onContextChange(context);
    DataObject dataObject = context.getDataObject();
    // the view should be populated with the values from the dataObject.
    verify(view, times(1)).setName(dataObject.getName());
    verify(view, times(1)).setPackageName(dataObject.getPackageName());
    verify(view, times(1)).initSuperClassList(anyList(), eq(dataObject.getSuperClassName()));
    verify(view, times(1)).setLabel(AnnotationValueHandler.getStringValue(dataObject, Label.class.getName(), "value"));
    verify(view, times(1)).setDescription(AnnotationValueHandler.getStringValue(dataObject, Description.class.getName(), "value"));
    assertFalse(objectEditor.isReadonly());
}
Also used : DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) DomainEditorBaseTest(org.kie.workbench.common.screens.datamodeller.client.widgets.DomainEditorBaseTest) Test(org.junit.Test)

Example 72 with DataObject

use of org.kie.workbench.common.services.datamodeller.core.DataObject in project kie-wb-common by kiegroup.

the class JPADataObjectFieldEditorTest method loadDataObjectFieldTest.

@Test
public void loadDataObjectFieldTest() {
    JPADataObjectFieldEditor fieldEditor = createFieldEditor();
    DataObject dataObject = context.getDataObject();
    ObjectProperty field = dataObject.getProperty("field1");
    // emulates selection of field1 in current context.
    context.setObjectProperty(field);
    // The domain editors typically reacts upon DataModelerContext changes.
    // when the context changes the editor will typically be reloaded.
    fieldEditor.onContextChange(context);
    verify(view, times(2)).loadPropertyEditorCategories(anyListOf(PropertyEditorCategory.class));
    assertFalse(fieldEditor.isReadonly());
}
Also used : ObjectProperty(org.kie.workbench.common.services.datamodeller.core.ObjectProperty) PropertyEditorCategory(org.uberfire.ext.properties.editor.model.PropertyEditorCategory) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) DomainEditorBaseTest(org.kie.workbench.common.screens.datamodeller.client.widgets.DomainEditorBaseTest) Test(org.junit.Test)

Example 73 with DataObject

use of org.kie.workbench.common.services.datamodeller.core.DataObject 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;
}
Also used : ParserException(org.jboss.forge.roaster.ParserException) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) DataModel(org.kie.workbench.common.services.datamodeller.core.DataModel) List(java.util.List) ArrayList(java.util.ArrayList) ModelDriverResult(org.kie.workbench.common.services.datamodeller.driver.model.ModelDriverResult) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) IOException(java.io.IOException) ParserException(org.jboss.forge.roaster.ParserException) DriverError(org.kie.workbench.common.services.datamodeller.driver.model.DriverError)

Example 74 with DataObject

use of org.kie.workbench.common.services.datamodeller.core.DataObject 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);
    }
}
Also used : Import(org.jboss.forge.roaster.model.source.Import) ArrayList(java.util.ArrayList) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException) JavaClassImpl(org.kie.workbench.common.services.datamodeller.core.impl.JavaClassImpl) JavaSource(org.jboss.forge.roaster.model.source.JavaSource) FieldSource(org.jboss.forge.roaster.model.source.FieldSource) Pair(org.uberfire.commons.data.Pair) ObjectProperty(org.kie.workbench.common.services.datamodeller.core.ObjectProperty) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) Method(org.jboss.forge.roaster.model.Method) DataObjectImpl(org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl) ObjectPropertyImpl(org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) Visibility(org.kie.workbench.common.services.datamodeller.core.Visibility) ImportImpl(org.kie.workbench.common.services.datamodeller.core.impl.ImportImpl) ClassTypeResolver(org.kie.soup.project.datamodel.commons.types.ClassTypeResolver)

Example 75 with DataObject

use of org.kie.workbench.common.services.datamodeller.core.DataObject in project kie-wb-common by kiegroup.

the class DataModelOracleModelDriver method addFactType.

private void addFactType(DataModel dataModel, ModuleDataModelOracle oracleDataModel, String factType, ObjectSource source, ClassLoader classLoader) throws ModelDriverException {
    String packageName = NamingUtils.extractPackageName(factType);
    String className = NamingUtils.extractClassName(factType);
    String superClass = DataModelOracleUtilities.getSuperType(oracleDataModel, factType);
    DataObject dataObject;
    logger.debug("Adding factType: " + factType + ", to dataModel: " + dataModel + ", from oracleDataModel: " + oracleDataModel);
    ClassMetadata classMetadata = readClassMetadata(factType, classLoader);
    if (classMetadata != null && !classMetadata.isMemberClass() && !classMetadata.isAnonymousClass() && !classMetadata.isLocalClass()) {
        Visibility visibility = DriverUtils.buildVisibility(classMetadata.getModifiers());
        dataObject = dataModel.addDataObject(factType, visibility, Modifier.isAbstract(classMetadata.getModifiers()), Modifier.isFinal(classMetadata.getModifiers()), source);
        dataObject.setSuperClassName(superClass);
        // process type annotations
        Set<Annotation> typeAnnotations = DataModelOracleUtilities.getTypeAnnotations(oracleDataModel, factType);
        if (typeAnnotations != null) {
            for (Annotation annotation : typeAnnotations) {
                addFactTypeAnnotation(dataObject, annotation);
            }
        }
        Map<String, ModelField[]> fields = oracleDataModel.getModuleModelFields();
        if (fields != null) {
            ModelField[] factFields = fields.get(factType);
            ModelField field;
            ObjectProperty property;
            Map<String, Set<Annotation>> typeFieldsAnnotations = DataModelOracleUtilities.getTypeFieldsAnnotations(oracleDataModel, factType);
            Set<Annotation> fieldAnnotations;
            if (factFields != null && factFields.length > 0) {
                for (int j = 0; j < factFields.length; j++) {
                    field = factFields[j];
                    if (isLoadableField(field)) {
                        if (field.getType().equals("Collection")) {
                            // particular processing for collection types
                            // read the correct bag and item classes.
                            String bag = DataModelOracleUtilities.getFieldClassName(oracleDataModel, factType, field.getName());
                            String itemsClass = DataModelOracleUtilities.getParametricFieldType(oracleDataModel, factType, field.getName());
                            if (itemsClass == null) {
                                // if we don't know the items class, the property will be managed as a simple property.
                                property = dataObject.addProperty(field.getName(), bag);
                            } else {
                                property = dataObject.addProperty(field.getName(), itemsClass, true, bag);
                            }
                        } else {
                            property = dataObject.addProperty(field.getName(), getFieldType(oracleDataModel, packageName, field.getClassName()));
                        }
                        // process property annotations
                        if (typeFieldsAnnotations != null && (fieldAnnotations = typeFieldsAnnotations.get(field.getName())) != null) {
                            for (Annotation fieldAnnotation : fieldAnnotations) {
                                addFieldAnnotation(dataObject, property, fieldAnnotation);
                            }
                        }
                    }
                }
            }
        } else {
            logger.debug("No fields for factTye: " + factType);
        }
    }
}
Also used : ObjectProperty(org.kie.workbench.common.services.datamodeller.core.ObjectProperty) Set(java.util.Set) Annotation(org.kie.soup.project.datamodel.oracle.Annotation) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) ModelField(org.kie.soup.project.datamodel.oracle.ModelField) Visibility(org.kie.workbench.common.services.datamodeller.core.Visibility)

Aggregations

DataObject (org.kie.workbench.common.services.datamodeller.core.DataObject)95 Test (org.junit.Test)45 ObjectProperty (org.kie.workbench.common.services.datamodeller.core.ObjectProperty)40 DataModel (org.kie.workbench.common.services.datamodeller.core.DataModel)29 Annotation (org.kie.workbench.common.services.datamodeller.core.Annotation)28 DataObjectImpl (org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl)21 ArrayList (java.util.ArrayList)17 AnnotationValuesAnnotation (org.kie.workbench.common.services.datamodeller.annotations.AnnotationValuesAnnotation)17 ClassAnnotation (org.kie.workbench.common.services.datamodeller.annotations.ClassAnnotation)17 EnumsAnnotation (org.kie.workbench.common.services.datamodeller.annotations.EnumsAnnotation)17 MarkerAnnotation (org.kie.workbench.common.services.datamodeller.annotations.MarkerAnnotation)17 PrimitivesAnnotation (org.kie.workbench.common.services.datamodeller.annotations.PrimitivesAnnotation)17 TestAnnotation (org.kie.workbench.common.services.datamodeller.parser.test.TestAnnotation)17 AnnotationImpl (org.kie.workbench.common.services.datamodeller.core.impl.AnnotationImpl)16 DomainEditorBaseTest (org.kie.workbench.common.screens.datamodeller.client.widgets.DomainEditorBaseTest)15 MethodImpl (org.kie.workbench.common.services.datamodeller.core.impl.MethodImpl)12 List (java.util.List)11 TypeImpl (org.kie.workbench.common.services.datamodeller.core.impl.TypeImpl)11 Method (org.kie.workbench.common.services.datamodeller.core.Method)10 Generated (javax.annotation.Generated)8