Search in sources :

Example 6 with ObjectPropertyImpl

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

the class GenerationEngineTest method testEqualsStringGeneration.

@Test
public void testEqualsStringGeneration() {
    DataModel dataModel = dataModelOracleDriver.createModel();
    DataObject object1 = dataModel.addDataObject("com.test.Object1");
    DataObject object2 = dataModel.addDataObject("com.test.sub.Object2");
    ObjectPropertyImpl prop1 = (ObjectPropertyImpl) object1.addProperty("attribute1", "java.lang.String");
    prop1.setFileOrder(0);
    ObjectPropertyImpl prop2 = (ObjectPropertyImpl) object1.addProperty("attribute2", "java.lang.Boolean");
    prop2.setFileOrder(1);
    ObjectPropertyImpl prop3 = (ObjectPropertyImpl) object1.addProperty("attribute3", object2.getClassName());
    prop3.setFileOrder(2);
    ObjectPropertyImpl prop4 = (ObjectPropertyImpl) object1.addProperty("attribute4", "long");
    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");
    prop4.addAnnotation(key);
    prop4.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");
    prop3.addAnnotation(position);
    GenerationContext generationContext = new GenerationContext(dataModel);
    try {
        String result = engine.generateEqualsString(generationContext, object1);
        assertEquals(results.getProperty("testEqualsStringGeneration"), result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : 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)

Example 7 with ObjectPropertyImpl

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

the class GenerationEngineTest method testMaxFieldsForConstructorsStringGeneration1.

@Test
public void testMaxFieldsForConstructorsStringGeneration1() {
    DataModel dataModel = dataModelOracleDriver.createModel();
    DataObject object1 = dataModel.addDataObject("com.test.MaxFieldsForConstructor1");
    ObjectProperty prop1;
    // The constructor for this data object should be generated, since we don't reach the limit.
    for (int i = 0; i < GenerationTools.MAX_FIELDS_FOR_DEFAULT_CONSTRUCTOR - 1; i++) {
        prop1 = object1.addProperty("attribute" + normalize(i), "java.lang.String");
        ((ObjectPropertyImpl) prop1).setFileOrder(i);
    }
    GenerationContext generationContext = new GenerationContext(dataModel);
    try {
        String result = engine.generateAllConstructorsString(generationContext, object1);
        assertEquals(results.getProperty("testMaxFieldsForConstructorsStringGeneration1"), 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) Test(org.junit.Test)

Example 8 with ObjectPropertyImpl

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

the class GenerationEngineTest method testHashCodeStringGeneration.

@Test
public void testHashCodeStringGeneration() {
    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");
    prop4.addAnnotation(key);
    prop4.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");
    prop3.addAnnotation(position);
    GenerationContext generationContext = new GenerationContext(dataModel);
    try {
        String result = engine.generateHashCodeString(generationContext, object1);
        assertEquals(results.getProperty("testHashCodeStringGeneration"), 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)

Example 9 with ObjectPropertyImpl

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

the class JavaRoasterModelDriver method parseProperty.

public ObjectProperty parseProperty(FieldSource<JavaClassSource> field, ClassTypeResolver classTypeResolver) throws ModelDriverException {
    Type type;
    boolean multiple = false;
    String className;
    String bag = null;
    ObjectProperty property;
    Visibility visibility = DriverUtils.buildVisibility(field.getVisibility());
    try {
        type = field.getType();
        if (type.isPrimitive()) {
            className = type.getName();
        } else {
            if (DriverUtils.isSimpleClass(type)) {
                className = resolveTypeName(classTypeResolver, type.getName());
            } else {
                // if this point was reached, we know it's a Collection. Managed type check was done previous to adding the property.
                multiple = true;
                @SuppressWarnings("unchecked") Type elementsType = ((List<Type>) type.getTypeArguments()).get(0);
                className = resolveTypeName(classTypeResolver, elementsType.getName());
                bag = resolveTypeName(classTypeResolver, type.getName());
            }
        }
        property = new ObjectPropertyImpl(field.getName(), className, multiple, bag, visibility, field.isStatic(), field.isFinal());
        List<AnnotationSource<JavaClassSource>> annotations = field.getAnnotations();
        if (annotations != null) {
            for (AnnotationSource annotation : annotations) {
                addPropertyAnnotation(property, annotation, classTypeResolver);
            }
        }
    } catch (ClassNotFoundException e) {
        logger.error(errorMessage(DATA_OBJECT_FIELD_LOAD_ERROR, field.getName(), field.getOrigin().getName()), e);
        throw new ModelDriverException(errorMessage(DATA_OBJECT_FIELD_LOAD_ERROR, field.getName(), field.getOrigin().getName()), e);
    }
    return property;
}
Also used : ObjectProperty(org.kie.workbench.common.services.datamodeller.core.ObjectProperty) ObjectPropertyImpl(org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl) Type(org.jboss.forge.roaster.model.Type) JavaType(org.jboss.forge.roaster.model.JavaType) ElementType(org.kie.workbench.common.services.datamodeller.core.ElementType) AnnotationSource(org.jboss.forge.roaster.model.source.AnnotationSource) Visibility(org.kie.workbench.common.services.datamodeller.core.Visibility) List(java.util.List) ArrayList(java.util.ArrayList) ModelDriverException(org.kie.workbench.common.services.datamodeller.driver.ModelDriverException)

Example 10 with ObjectPropertyImpl

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

the class JavaRoasterModelDriver method updateConstructors.

public void updateConstructors(JavaClassSource javaClassSource, DataObject dataObject, List<MethodSource<JavaClassSource>> allFieldsConstructorCandidates, List<MethodSource<JavaClassSource>> keyFieldsConstructorCandidates, List<MethodSource<JavaClassSource>> positionFieldsConstructorCandidates, ClassTypeResolver classTypeResolver) throws Exception {
    GenerationContext generationContext = new GenerationContext(null);
    GenerationEngine engine = GenerationEngine.getInstance();
    GenerationTools genTools = new GenerationTools();
    JavaRoasterModelDriver modelDriver = new JavaRoasterModelDriver();
    boolean needsAllFieldsConstructor;
    boolean needsKeyFieldsConstructor;
    boolean needsPositionFieldsConstructor;
    boolean needsEmptyConstructor;
    String defaultConstructorSource;
    String allFieldsConstructorSource;
    String keyFieldsConstructorSource;
    String positionFieldsConstructorSource;
    String equalsMethodSource;
    String hashCodeMethodSource;
    // check if the candidate methods has exactly the same body of the generated by the data modeller.
    List<MethodSource<JavaClassSource>> currentAllFieldsConstructors = modelDriver.filterGeneratedConstructors(allFieldsConstructorCandidates);
    List<MethodSource<JavaClassSource>> currentKeyFieldsConstructors = modelDriver.filterGeneratedConstructors(keyFieldsConstructorCandidates);
    List<MethodSource<JavaClassSource>> currentPositionFieldsConstructors = modelDriver.filterGeneratedConstructors(positionFieldsConstructorCandidates);
    if (logger.isDebugEnabled()) {
        logger.debug("allFieldsConstructorCandidates candidates: " + allFieldsConstructorCandidates.size());
        logger.debug(allFieldsConstructorCandidates.size() > 0 ? allFieldsConstructorCandidates.get(0).toString() : "");
        logger.debug("\n\n");
        logger.debug("currentAllFieldsConstructors: " + currentAllFieldsConstructors.size());
        logger.debug(currentAllFieldsConstructors.size() > 0 ? currentAllFieldsConstructors.get(0).toString() : "");
        logger.debug("\n\n");
        logger.debug("KeyFieldsConstructorCandidates: " + keyFieldsConstructorCandidates.size());
        logger.debug(keyFieldsConstructorCandidates.size() > 0 ? keyFieldsConstructorCandidates.get(0).toString() : "");
        logger.debug("\n\n");
        logger.debug("currentKeyFieldsConstructors: " + currentKeyFieldsConstructors.size());
        logger.debug(currentKeyFieldsConstructors.size() > 0 ? currentKeyFieldsConstructors.get(0).toString() : "");
        logger.debug("\n\n");
        logger.debug("positionFieldsConstructorCandidates: " + positionFieldsConstructorCandidates.size());
        logger.debug(positionFieldsConstructorCandidates.size() > 0 ? positionFieldsConstructorCandidates.get(0).toString() : "");
        logger.debug("\n\n");
        logger.debug("currentPositionFieldsConstructors: " + currentPositionFieldsConstructors.size());
        logger.debug(currentPositionFieldsConstructors.size() > 0 ? currentPositionFieldsConstructors.get(0).toString() : "");
        logger.debug("\n\n");
    }
    // delete current data modeller generated all fields, key fields, and position fields constructors if there are any.
    for (MethodSource<JavaClassSource> constructor : currentAllFieldsConstructors) {
        javaClassSource.removeMethod(constructor);
    }
    for (MethodSource<JavaClassSource> constructor : currentKeyFieldsConstructors) {
        javaClassSource.removeMethod(constructor);
    }
    for (MethodSource<JavaClassSource> constructor : currentPositionFieldsConstructors) {
        javaClassSource.removeMethod(constructor);
    }
    // calculate the file order for the fields.
    List<FieldSource<JavaClassSource>> fields = javaClassSource.getFields();
    if (fields != null && fields.size() > 0) {
        int fileOrder = 0;
        for (FieldSource<JavaClassSource> field : fields) {
            ObjectPropertyImpl objectProperty = (ObjectPropertyImpl) dataObject.getProperty(field.getName());
            if (objectProperty != null) {
                objectProperty.setFileOrder(fileOrder);
            }
            fileOrder++;
        }
    }
    // get the sorted list of all fields, position annotated and key annotated fields. These lists will be used
    // to identify collisions with client provided constructors.
    List<ObjectProperty> allFields = DataModelUtils.sortByFileOrder(DataModelUtils.filterAssignableFields(dataObject));
    List<ObjectProperty> positionFields = DataModelUtils.sortByPosition(DataModelUtils.filterPositionFields(dataObject));
    List<ObjectProperty> keyFields = DataModelUtils.sortByFileOrder(DataModelUtils.filterKeyFields(dataObject));
    // we always wants to generate the default constructor.
    needsEmptyConstructor = true;
    needsAllFieldsConstructor = allFields.size() > 0;
    needsPositionFieldsConstructor = positionFields.size() > 0 && !DataModelUtils.equalsByFieldName(allFields, positionFields) && !DataModelUtils.equalsByFieldType(allFields, positionFields);
    needsKeyFieldsConstructor = keyFields.size() > 0 && !DataModelUtils.equalsByFieldName(allFields, keyFields) && !DataModelUtils.equalsByFieldType(allFields, keyFields) && !DataModelUtils.equalsByFieldName(positionFields, keyFields) && !DataModelUtils.equalsByFieldType(positionFields, keyFields);
    List<MethodSource<JavaClassSource>> currentConstructors = new ArrayList<MethodSource<JavaClassSource>>();
    MethodSource<JavaClassSource> currentEquals = null;
    MethodSource<JavaClassSource> currentHashCode = null;
    MethodSource<JavaClassSource> newConstructor;
    // Iterate remaining methods looking for client provided constructors, hashCode and equals methods.
    List<MethodSource<JavaClassSource>> methods = javaClassSource.getMethods();
    if (methods != null) {
        for (MethodSource<JavaClassSource> method : methods) {
            if (method.isConstructor()) {
                currentConstructors.add(method);
                if (method.getParameters() == null || method.getParameters().size() == 0) {
                    needsEmptyConstructor = false;
                }
            } else if (isEquals(method)) {
                currentEquals = method;
            } else if (isHashCode(method)) {
                currentHashCode = method;
            }
        }
    }
    // check collisions with remaining constructors first.
    needsAllFieldsConstructor = needsAllFieldsConstructor && (findMatchingConstructorsByTypes(javaClassSource, allFields, classTypeResolver).size() == 0);
    needsPositionFieldsConstructor = needsPositionFieldsConstructor && (findMatchingConstructorsByTypes(javaClassSource, positionFields, classTypeResolver).size() == 0);
    needsKeyFieldsConstructor = needsKeyFieldsConstructor && (findMatchingConstructorsByTypes(javaClassSource, keyFields, classTypeResolver).size() == 0);
    // remove current equals and hashCode methods
    if (currentEquals != null) {
        javaClassSource.removeMethod(currentEquals);
    }
    if (currentHashCode != null) {
        javaClassSource.removeMethod(currentHashCode);
    }
    if (needsEmptyConstructor) {
        defaultConstructorSource = genTools.indent(engine.generateDefaultConstructorString(generationContext, dataObject));
        newConstructor = javaClassSource.addMethod(defaultConstructorSource);
        newConstructor.setConstructor(true);
    }
    if (needsAllFieldsConstructor) {
        allFieldsConstructorSource = genTools.indent(engine.generateAllFieldsConstructorString(generationContext, dataObject));
        if (allFieldsConstructorSource != null && !allFieldsConstructorSource.trim().isEmpty()) {
            newConstructor = javaClassSource.addMethod(allFieldsConstructorSource);
            newConstructor.setConstructor(true);
        }
    }
    if (needsPositionFieldsConstructor) {
        positionFieldsConstructorSource = genTools.indent(engine.generatePositionFieldsConstructorString(generationContext, dataObject));
        if (positionFieldsConstructorSource != null && !positionFieldsConstructorSource.trim().isEmpty()) {
            newConstructor = javaClassSource.addMethod(positionFieldsConstructorSource);
            newConstructor.setConstructor(true);
        }
    }
    if (needsKeyFieldsConstructor) {
        keyFieldsConstructorSource = genTools.indent(engine.generateKeyFieldsConstructorString(generationContext, dataObject));
        if (keyFieldsConstructorSource != null && !keyFieldsConstructorSource.trim().isEmpty()) {
            newConstructor = javaClassSource.addMethod(keyFieldsConstructorSource);
            newConstructor.setConstructor(true);
        }
    }
    if (keyFields.size() > 0) {
        equalsMethodSource = genTools.indent(engine.generateEqualsString(generationContext, dataObject));
        javaClassSource.addMethod(equalsMethodSource);
        hashCodeMethodSource = genTools.indent(engine.generateHashCodeString(generationContext, dataObject));
        javaClassSource.addMethod(hashCodeMethodSource);
    }
}
Also used : GenerationContext(org.kie.workbench.common.services.datamodeller.codegen.GenerationContext) GenerationTools(org.kie.workbench.common.services.datamodeller.codegen.GenerationTools) ObjectProperty(org.kie.workbench.common.services.datamodeller.core.ObjectProperty) ArrayList(java.util.ArrayList) JavaClassSource(org.jboss.forge.roaster.model.source.JavaClassSource) ObjectPropertyImpl(org.kie.workbench.common.services.datamodeller.core.impl.ObjectPropertyImpl) GenerationEngine(org.kie.workbench.common.services.datamodeller.codegen.GenerationEngine) MethodSource(org.jboss.forge.roaster.model.source.MethodSource) FieldSource(org.jboss.forge.roaster.model.source.FieldSource)

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