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;
}
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"));
}
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());
}
});
}
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"));
}
});
}
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"));
}
});
}
Aggregations