Search in sources :

Example 16 with ObjectPropertyImpl

use of org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl 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 17 with ObjectPropertyImpl

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

the class GenerationEngineTest method testKeyFieldsConstructorStringGeneration.

@Test
public void testKeyFieldsConstructorStringGeneration() {
    DataModel dataModel = dataModelOracleDriver.createModel();
    DataObject object1 = dataModel.addDataObject("com.test.Object1");
    DataObject object2 = dataModel.addDataObject("com.test.sub.Object2");
    ObjectProperty prop1 = object1.addProperty("attribute1", "java.lang.String");
    ((ObjectPropertyImpl) prop1).setFileOrder(1);
    ObjectProperty prop2 = object1.addProperty("attribute2", "java.lang.Boolean");
    ((ObjectPropertyImpl) prop2).setFileOrder(2);
    ObjectProperty prop3 = object1.addProperty("attribute3", object2.getClassName());
    ((ObjectPropertyImpl) prop3).setFileOrder(0);
    ObjectProperty prop4 = object1.addProperty("attribute4", "long");
    ((ObjectPropertyImpl) prop4).setFileOrder(3);
    Annotation key = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Key.class.getName()));
    Annotation position = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Position.class.getName()));
    prop3.addAnnotation(key);
    prop1.addAnnotation(key);
    prop2.addAnnotation(key);
    GenerationContext generationContext = new GenerationContext(dataModel);
    try {
        String result = engine.generateKeyFieldsConstructorString(generationContext, object1);
        assertEquals(results.getProperty("testKeyFieldsConstructorStringGeneration"), result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ObjectProperty(org.kie.workbench.common.services.datamodeller.core.ObjectProperty) ObjectPropertyImpl(org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) DataModel(org.kie.workbench.common.services.datamodeller.core.DataModel) AnnotationImpl(org.kie.workbench.common.services.datamodeller.core.impl.AnnotationImpl) Annotation(org.kie.workbench.common.services.datamodeller.core.Annotation) Test(org.junit.Test)

Example 18 with ObjectPropertyImpl

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

the class GenerationEngineTest method testAllConstructorsStringGeneration.

@Test
public void testAllConstructorsStringGeneration() {
    DataModel dataModel = dataModelOracleDriver.createModel();
    DataObject object1 = dataModel.addDataObject("com.test.Object1");
    DataObject object2 = dataModel.addDataObject("com.test.sub.Object2");
    ObjectProperty prop1 = object1.addProperty("attribute1", "java.lang.String");
    ((ObjectPropertyImpl) prop1).setFileOrder(0);
    ObjectProperty prop2 = object1.addProperty("attribute2", "java.lang.Boolean");
    ((ObjectPropertyImpl) prop2).setFileOrder(1);
    ObjectProperty prop3 = object1.addProperty("attribute3", object2.getClassName());
    ((ObjectPropertyImpl) prop3).setFileOrder(2);
    ObjectProperty prop4 = object1.addProperty("attribute4", "long");
    ((ObjectPropertyImpl) prop4).setFileOrder(3);
    Annotation key = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Key.class.getName()));
    Annotation position = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Position.class.getName()));
    position.setValue("value", "0");
    prop3.addAnnotation(key);
    prop3.addAnnotation(position);
    position = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Position.class.getName()));
    position.setValue("value", "1");
    prop1.addAnnotation(key);
    prop1.addAnnotation(position);
    position = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Position.class.getName()));
    position.setValue("value", "2");
    prop2.addAnnotation(key);
    prop2.addAnnotation(position);
    position = new AnnotationImpl(annotationDefinitions.get(org.kie.api.definition.type.Position.class.getName()));
    position.setValue("value", "3");
    prop4.addAnnotation(position);
    GenerationContext generationContext = new GenerationContext(dataModel);
    try {
        String result = engine.generateAllConstructorsString(generationContext, object1);
        assertEquals(results.getProperty("testAllConstructorsStringGeneration"), result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ObjectProperty(org.kie.workbench.common.services.datamodeller.core.ObjectProperty) AnnotationImpl(org.kie.workbench.common.services.datamodeller.core.impl.AnnotationImpl) Annotation(org.kie.workbench.common.services.datamodeller.core.Annotation) ObjectPropertyImpl(org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl) DataObject(org.kie.workbench.common.services.datamodeller.core.DataObject) DataModel(org.kie.workbench.common.services.datamodeller.core.DataModel) Test(org.junit.Test)

Aggregations

ObjectPropertyImpl (org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl)18 ObjectProperty (org.kie.workbench.common.services.datamodeller.core.ObjectProperty)14 Test (org.junit.Test)11 DataObject (org.kie.workbench.common.services.datamodeller.core.DataObject)10 DataObjectImpl (org.kie.workbench.common.services.datamodeller.core.impl.DataObjectImpl)7 DataModel (org.kie.workbench.common.services.datamodeller.core.DataModel)6 Annotation (org.kie.workbench.common.services.datamodeller.core.Annotation)5 AnnotationImpl (org.kie.workbench.common.services.datamodeller.core.impl.AnnotationImpl)5 ValidationMessage (org.guvnor.common.services.shared.validation.model.ValidationMessage)4 ArrayList (java.util.ArrayList)3 List (java.util.List)2 AnnotationSource (org.jboss.forge.roaster.model.source.AnnotationSource)2 FieldSource (org.jboss.forge.roaster.model.source.FieldSource)2 JavaClassSource (org.jboss.forge.roaster.model.source.JavaClassSource)2 MethodSource (org.jboss.forge.roaster.model.source.MethodSource)2 DataModelerContext (org.kie.workbench.common.screens.datamodeller.client.DataModelerContext)2 DomainEditorBaseTest (org.kie.workbench.common.screens.datamodeller.client.widgets.DomainEditorBaseTest)2 Command (org.uberfire.mvp.Command)2 Column (com.google.gwt.user.cellview.client.Column)1 GwtMockitoTestRunner (com.google.gwtmockito.GwtMockitoTestRunner)1