Search in sources :

Example 1 with Annotation

use of org.kie.soup.project.datamodel.oracle.Annotation in project kie-wb-common by kiegroup.

the class DMOModelResolver method resolveModelForType.

private static DataObjectFormModel resolveModelForType(final ModuleDataModelOracle oracle, final String modelType, final Predicate<TypeSource> sourcePredicate) {
    PortablePreconditions.checkNotNull("oracle", oracle);
    PortablePreconditions.checkNotNull("modelType", modelType);
    PortablePreconditions.checkNotNull("sourcePredicate", sourcePredicate);
    if (FormModelPropertiesUtil.isBaseType(modelType)) {
        throw new IllegalArgumentException("Cannot extract FormModel for type '" + modelType + "'.");
    }
    final String modelName = modelType.substring(modelType.lastIndexOf(".") + 1);
    final DataObjectFormModel formModel = new DataObjectFormModel(modelName, modelType);
    TypeSource typeSource = oracle.getModuleTypeSources().get(modelType);
    if (typeSource == null || !sourcePredicate.test(typeSource)) {
        return null;
    }
    if (typeSource.equals(TypeSource.JAVA_DEPENDENCY)) {
        formModel.setSource(Source.EXTERNAL);
    }
    ModelField[] fields = oracle.getModuleModelFields().get(modelType);
    Map<String, Set<Annotation>> fieldAnnotations = oracle.getModuleTypeFieldsAnnotations().getOrDefault(modelType, Collections.emptyMap());
    Arrays.stream(fields).forEach(modelField -> {
        if (modelField.getName().equals("this")) {
            return;
        }
        if (!FieldAccessorsAndMutators.BOTH.equals(modelField.getAccessorsAndMutators())) {
            return;
        }
        Set<Annotation> annotations = Optional.ofNullable(fieldAnnotations.get(modelField.getName())).orElse(Collections.EMPTY_SET);
        if (!isValidModelProperty(modelField, annotations)) {
            return;
        }
        try {
            String fieldType = modelField.getClassName();
            boolean isEnum = oracle.getModuleJavaEnumDefinitions().get(modelType + "#" + modelField.getName()) != null;
            boolean isList = DataType.TYPE_COLLECTION.equals(modelField.getType());
            if (isList) {
                fieldType = oracle.getModuleFieldParametersType().get(modelType + "#" + modelField.getName());
            }
            TypeKind typeKind = isEnum ? TypeKind.ENUM : FormModelPropertiesUtil.isBaseType(fieldType) ? TypeKind.BASE : TypeKind.OBJECT;
            TypeInfo info = new TypeInfoImpl(typeKind, fieldType, isList);
            ModelProperty modelProperty = new ModelPropertyImpl(modelField.getName(), info);
            annotations.stream().filter(annotation -> annotation.getQualifiedTypeName().equalsIgnoreCase(LABEL_ANNOTATION)).findAny().ifPresent(annotation -> {
                String label = (String) annotation.getParameters().get(LABEL_ANNOTATION_VALUE_PARAM);
                modelProperty.getMetaData().addEntry(new FieldLabelEntry(label));
                modelProperty.getMetaData().addEntry(new FieldPlaceHolderEntry(label));
            });
            formModel.addProperty(modelProperty);
        } catch (Exception ex) {
            logger.warn("Error processing model '" + modelType + "' impossible generate FieldDefinition for model field '" + modelField.getName() + "' (" + modelField.getType() + ")");
        }
    });
    return formModel;
}
Also used : Set(java.util.Set) FieldLabelEntry(org.kie.workbench.common.forms.model.impl.meta.entries.FieldLabelEntry) TypeKind(org.kie.workbench.common.forms.model.TypeKind) TypeSource(org.kie.soup.project.datamodel.oracle.TypeSource) ModelPropertyImpl(org.kie.workbench.common.forms.model.impl.ModelPropertyImpl) TypeInfo(org.kie.workbench.common.forms.model.TypeInfo) Annotation(org.kie.soup.project.datamodel.oracle.Annotation) ModelField(org.kie.soup.project.datamodel.oracle.ModelField) ModelProperty(org.kie.workbench.common.forms.model.ModelProperty) FieldPlaceHolderEntry(org.kie.workbench.common.forms.model.impl.meta.entries.FieldPlaceHolderEntry) TypeInfoImpl(org.kie.workbench.common.forms.model.impl.TypeInfoImpl) DataObjectFormModel(org.kie.workbench.common.forms.data.modeller.model.DataObjectFormModel)

Example 2 with Annotation

use of org.kie.soup.project.datamodel.oracle.Annotation in project kie-wb-common by kiegroup.

the class ModuleDataModelFactAnnotationsTest method testModuleDMOAnnotationAttributes2.

@Test
public void testModuleDMOAnnotationAttributes2() throws Exception {
    final ModuleDataModelOracleBuilder builder = ModuleDataModelOracleBuilder.newModuleOracleBuilder(new RawMVELEvaluator());
    final ModuleDataModelOracleImpl oracle = new ModuleDataModelOracleImpl();
    final ClassFactBuilder cb = new ClassFactBuilder(builder, RoleSmurf.class, false, type -> TypeSource.JAVA_PROJECT);
    cb.build(oracle);
    assertEquals(1, oracle.getModuleModelFields().size());
    assertContains("org.kie.workbench.common.services.datamodel.backend.server.testclasses.annotations.RoleSmurf", oracle.getModuleModelFields().keySet());
    final Set<Annotation> annotations = oracle.getModuleTypeAnnotations().get("org.kie.workbench.common.services.datamodel.backend.server.testclasses.annotations.RoleSmurf");
    assertNotNull(annotations);
    assertEquals(1, annotations.size());
    final Annotation annotation = annotations.iterator().next();
    assertEquals("org.kie.api.definition.type.Role", annotation.getQualifiedTypeName());
    assertEquals("EVENT", annotation.getParameters().get("value"));
}
Also used : RawMVELEvaluator(org.kie.soup.project.datamodel.commons.util.RawMVELEvaluator) ModuleDataModelOracleImpl(org.kie.soup.project.datamodel.commons.oracle.ModuleDataModelOracleImpl) ClassFactBuilder(org.kie.workbench.common.services.datamodel.backend.server.builder.projects.ClassFactBuilder) Annotation(org.kie.soup.project.datamodel.oracle.Annotation) ModuleDataModelOracleBuilder(org.kie.workbench.common.services.datamodel.backend.server.builder.projects.ModuleDataModelOracleBuilder) Test(org.junit.Test)

Example 3 with Annotation

use of org.kie.soup.project.datamodel.oracle.Annotation in project kie-wb-common by kiegroup.

the class PackageDataModelFactFieldsAnnotationsTest method testIncorrectPackageDMOAnnotationAttributes.

@Test
public void testIncorrectPackageDMOAnnotationAttributes() throws Exception {
    // Build ModuleDMO
    final ModuleDataModelOracleBuilder moduleBuilder = ModuleDataModelOracleBuilder.newModuleOracleBuilder(new RawMVELEvaluator());
    final ModuleDataModelOracleImpl moduleLoader = new ModuleDataModelOracleImpl();
    final ClassFactBuilder cb = new ClassFactBuilder(moduleBuilder, SmurfHouse.class, false, type -> TypeSource.JAVA_PROJECT);
    cb.build(moduleLoader);
    // Build PackageDMO. Defaults to defaultpkg
    final PackageDataModelOracleBuilder packageBuilder = PackageDataModelOracleBuilder.newPackageOracleBuilder(new RawMVELEvaluator());
    packageBuilder.setModuleOracle(moduleLoader);
    final PackageDataModelOracle packageLoader = packageBuilder.build();
    // Emulate server-to-client conversions
    final Caller<IncrementalDataModelService> service = new MockIncrementalDataModelServiceCaller(packageLoader);
    final AsyncPackageDataModelOracle oracle = new AsyncPackageDataModelOracleImpl(service, validatorInstance);
    final PackageDataModelOracleBaselinePayload dataModel = new PackageDataModelOracleBaselinePayload();
    dataModel.setPackageName(packageLoader.getPackageName());
    dataModel.setModelFields(packageLoader.getModuleModelFields());
    dataModel.setTypeAnnotations(packageLoader.getModuleTypeAnnotations());
    dataModel.setTypeFieldsAnnotations(packageLoader.getModuleTypeFieldsAnnotations());
    PackageDataModelOracleTestUtils.populateDataModelOracle(mock(Path.class), new MockHasImports(), oracle, dataModel);
    assertEquals(0, oracle.getFactTypes().length);
    oracle.getTypeFieldsAnnotations("SmurfHouse", new Callback<Map<String, Set<Annotation>>>() {

        @Override
        public void callback(final Map<String, Set<Annotation>> fieldAnnotations) {
            assertNotNull(fieldAnnotations);
            assertEquals(0, fieldAnnotations.size());
        }
    });
}
Also used : Path(org.uberfire.backend.vfs.Path) IncrementalDataModelService(org.kie.workbench.common.services.datamodel.service.IncrementalDataModelService) Set(java.util.Set) PackageDataModelOracleBuilder(org.kie.workbench.common.services.datamodel.backend.server.builder.packages.PackageDataModelOracleBuilder) ModuleDataModelOracleImpl(org.kie.soup.project.datamodel.commons.oracle.ModuleDataModelOracleImpl) Annotation(org.kie.soup.project.datamodel.oracle.Annotation) ModuleDataModelOracleBuilder(org.kie.workbench.common.services.datamodel.backend.server.builder.projects.ModuleDataModelOracleBuilder) PackageDataModelOracleBaselinePayload(org.kie.workbench.common.services.datamodel.model.PackageDataModelOracleBaselinePayload) RawMVELEvaluator(org.kie.soup.project.datamodel.commons.util.RawMVELEvaluator) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) Map(java.util.Map) ClassFactBuilder(org.kie.workbench.common.services.datamodel.backend.server.builder.projects.ClassFactBuilder) Test(org.junit.Test)

Example 4 with Annotation

use of org.kie.soup.project.datamodel.oracle.Annotation in project kie-wb-common by kiegroup.

the class PackageDataModelFactAnnotationsTest method testCorrectPackageDMOAnnotationAttributes.

@Test
public void testCorrectPackageDMOAnnotationAttributes() throws Exception {
    // Build ModuleDMO
    final ModuleDataModelOracleBuilder moduleBuilder = ModuleDataModelOracleBuilder.newModuleOracleBuilder(new RawMVELEvaluator());
    final ModuleDataModelOracleImpl moduleLoader = new ModuleDataModelOracleImpl();
    final ClassFactBuilder cb = new ClassFactBuilder(moduleBuilder, Smurf.class, false, type -> TypeSource.JAVA_PROJECT);
    cb.build(moduleLoader);
    // Build PackageDMO
    final PackageDataModelOracleBuilder packageBuilder = PackageDataModelOracleBuilder.newPackageOracleBuilder(new RawMVELEvaluator(), "org.kie.workbench.common.widgets.client.datamodel.testclasses.annotations");
    packageBuilder.setModuleOracle(moduleLoader);
    final PackageDataModelOracle packageLoader = packageBuilder.build();
    // Emulate server-to-client conversions
    final Caller<IncrementalDataModelService> service = new MockIncrementalDataModelServiceCaller(packageLoader);
    final AsyncPackageDataModelOracle oracle = new AsyncPackageDataModelOracleImpl(service, validatorInstance);
    final PackageDataModelOracleBaselinePayload dataModel = new PackageDataModelOracleBaselinePayload();
    dataModel.setPackageName(packageLoader.getPackageName());
    dataModel.setModelFields(packageLoader.getModuleModelFields());
    dataModel.setTypeAnnotations(packageLoader.getModuleTypeAnnotations());
    dataModel.setTypeFieldsAnnotations(packageLoader.getModuleTypeFieldsAnnotations());
    PackageDataModelOracleTestUtils.populateDataModelOracle(mock(Path.class), new MockHasImports(), oracle, dataModel);
    assertEquals(1, oracle.getFactTypes().length);
    assertEquals("Smurf", oracle.getFactTypes()[0]);
    oracle.getTypeAnnotations("Smurf", new Callback<Set<Annotation>>() {

        @Override
        public void callback(final Set<Annotation> annotations) {
            assertNotNull(annotations);
            assertEquals(1, annotations.size());
            final Annotation annotation = annotations.iterator().next();
            assertEquals("org.kie.workbench.common.widgets.client.datamodel.testclasses.annotations.SmurfDescriptor", annotation.getQualifiedTypeName());
            assertEquals("blue", annotation.getParameters().get("colour"));
            assertEquals("M", annotation.getParameters().get("gender"));
            assertEquals("Brains", annotation.getParameters().get("description"));
        }
    });
}
Also used : Path(org.uberfire.backend.vfs.Path) IncrementalDataModelService(org.kie.workbench.common.services.datamodel.service.IncrementalDataModelService) Set(java.util.Set) PackageDataModelOracleBuilder(org.kie.workbench.common.services.datamodel.backend.server.builder.packages.PackageDataModelOracleBuilder) ModuleDataModelOracleImpl(org.kie.soup.project.datamodel.commons.oracle.ModuleDataModelOracleImpl) Annotation(org.kie.soup.project.datamodel.oracle.Annotation) ModuleDataModelOracleBuilder(org.kie.workbench.common.services.datamodel.backend.server.builder.projects.ModuleDataModelOracleBuilder) PackageDataModelOracleBaselinePayload(org.kie.workbench.common.services.datamodel.model.PackageDataModelOracleBaselinePayload) RawMVELEvaluator(org.kie.soup.project.datamodel.commons.util.RawMVELEvaluator) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) ClassFactBuilder(org.kie.workbench.common.services.datamodel.backend.server.builder.projects.ClassFactBuilder) Test(org.junit.Test)

Example 5 with Annotation

use of org.kie.soup.project.datamodel.oracle.Annotation in project kie-wb-common by kiegroup.

the class PackageDataModelFactAnnotationsTest method testCorrectPackageDMOAnnotationAttributes2.

@Test
public void testCorrectPackageDMOAnnotationAttributes2() throws Exception {
    // Build ModuleDMO
    final ModuleDataModelOracleBuilder moduleBuilder = ModuleDataModelOracleBuilder.newModuleOracleBuilder(new RawMVELEvaluator());
    final ModuleDataModelOracleImpl moduleLoader = new ModuleDataModelOracleImpl();
    final ClassFactBuilder cb = new ClassFactBuilder(moduleBuilder, RoleSmurf.class, false, type -> TypeSource.JAVA_PROJECT);
    cb.build(moduleLoader);
    // Build PackageDMO
    final PackageDataModelOracleBuilder packageBuilder = PackageDataModelOracleBuilder.newPackageOracleBuilder(new RawMVELEvaluator(), "org.kie.workbench.common.widgets.client.datamodel.testclasses.annotations");
    packageBuilder.setModuleOracle(moduleLoader);
    final PackageDataModelOracle packageLoader = packageBuilder.build();
    // Emulate server-to-client conversions
    final Caller<IncrementalDataModelService> service = new MockIncrementalDataModelServiceCaller(packageLoader);
    final AsyncPackageDataModelOracle oracle = new AsyncPackageDataModelOracleImpl(service, validatorInstance);
    final PackageDataModelOracleBaselinePayload dataModel = new PackageDataModelOracleBaselinePayload();
    dataModel.setPackageName(packageLoader.getPackageName());
    dataModel.setModelFields(packageLoader.getModuleModelFields());
    dataModel.setTypeAnnotations(packageLoader.getModuleTypeAnnotations());
    dataModel.setTypeFieldsAnnotations(packageLoader.getModuleTypeFieldsAnnotations());
    PackageDataModelOracleTestUtils.populateDataModelOracle(mock(Path.class), new MockHasImports(), oracle, dataModel);
    assertEquals(1, oracle.getFactTypes().length);
    assertEquals("RoleSmurf", oracle.getFactTypes()[0]);
    oracle.getTypeAnnotations("RoleSmurf", new Callback<Set<Annotation>>() {

        @Override
        public void callback(final Set<Annotation> annotations) {
            assertNotNull(annotations);
            assertEquals(1, annotations.size());
            final Annotation annotation = annotations.iterator().next();
            assertEquals("org.kie.api.definition.type.Role", annotation.getQualifiedTypeName());
            assertEquals("EVENT", annotation.getParameters().get("value"));
        }
    });
}
Also used : Path(org.uberfire.backend.vfs.Path) IncrementalDataModelService(org.kie.workbench.common.services.datamodel.service.IncrementalDataModelService) Set(java.util.Set) PackageDataModelOracleBuilder(org.kie.workbench.common.services.datamodel.backend.server.builder.packages.PackageDataModelOracleBuilder) ModuleDataModelOracleImpl(org.kie.soup.project.datamodel.commons.oracle.ModuleDataModelOracleImpl) Annotation(org.kie.soup.project.datamodel.oracle.Annotation) ModuleDataModelOracleBuilder(org.kie.workbench.common.services.datamodel.backend.server.builder.projects.ModuleDataModelOracleBuilder) PackageDataModelOracleBaselinePayload(org.kie.workbench.common.services.datamodel.model.PackageDataModelOracleBaselinePayload) RawMVELEvaluator(org.kie.soup.project.datamodel.commons.util.RawMVELEvaluator) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) ClassFactBuilder(org.kie.workbench.common.services.datamodel.backend.server.builder.projects.ClassFactBuilder) Test(org.junit.Test)

Aggregations

Annotation (org.kie.soup.project.datamodel.oracle.Annotation)21 Test (org.junit.Test)14 ModuleDataModelOracleImpl (org.kie.soup.project.datamodel.commons.oracle.ModuleDataModelOracleImpl)14 RawMVELEvaluator (org.kie.soup.project.datamodel.commons.util.RawMVELEvaluator)14 ClassFactBuilder (org.kie.workbench.common.services.datamodel.backend.server.builder.projects.ClassFactBuilder)14 ModuleDataModelOracleBuilder (org.kie.workbench.common.services.datamodel.backend.server.builder.projects.ModuleDataModelOracleBuilder)14 Set (java.util.Set)12 PackageDataModelOracle (org.kie.soup.project.datamodel.oracle.PackageDataModelOracle)9 PackageDataModelOracleBuilder (org.kie.workbench.common.services.datamodel.backend.server.builder.packages.PackageDataModelOracleBuilder)9 PackageDataModelOracleBaselinePayload (org.kie.workbench.common.services.datamodel.model.PackageDataModelOracleBaselinePayload)9 IncrementalDataModelService (org.kie.workbench.common.services.datamodel.service.IncrementalDataModelService)9 Path (org.uberfire.backend.vfs.Path)9 Map (java.util.Map)5 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 ModelField (org.kie.soup.project.datamodel.oracle.ModelField)3 FieldInfo (org.kie.workbench.common.services.datamodel.backend.server.builder.projects.ClassFieldInspector.FieldInfo)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 TreeMap (java.util.TreeMap)1