use of org.apache.felix.schematizer.Node in project felix by apache.
the class SchematizerServiceTest method testTypeRules.
@Test
public void testTypeRules() {
Schema s = schematizer.type("MyDTO", "/embedded", new TypeReference<MyEmbeddedDTO2<String>>() {
}).type("MyDTO", "/embedded/value", String.class).schematize("MyDTO", new TypeReference<MyDTO3<MyEmbeddedDTO2<String>>>() {
}).get("MyDTO");
assertNotNull(s);
Node embeddedNode = s.nodeAtPath("/embedded/value");
assertTrue(!"ERROR".equals(embeddedNode.name()));
Node parentNode = s.parentOf(embeddedNode);
assertTrue(!"ERROR".equals(parentNode.name()));
Node grandparentNode = s.parentOf(parentNode);
assertTrue(!"ERROR".equals(grandparentNode.name()));
assertEquals("/", grandparentNode.absolutePath());
}
use of org.apache.felix.schematizer.Node in project felix by apache.
the class SchemaBasedConverter method convertToDTO.
@SuppressWarnings({ "rawtypes", "unchecked" })
private <U extends DTO> U convertToDTO(Class<U> targetCls, Map<?, ?> m, Schema schema, String contextPath) {
try {
U dto;
try {
dto = targetCls.newInstance();
} catch (Throwable t) {
throw new ConversionException("Cannot create instance of DTO " + targetCls + ". Bad constructor?", t);
}
for (Map.Entry entry : m.entrySet()) {
try {
Field f = targetCls.getField(entry.getKey().toString());
Object val = entry.getValue();
if (val == null)
continue;
String path = contextPath + f.getName();
Node node = schema.nodeAtPath(path);
Object obj;
if (node.typeReference().isPresent()) {
TypeReference<?> tr = Util.typeReferenceOf(node.typeReference().get());
if (node.isCollection())
if (!Collection.class.isAssignableFrom(val.getClass()))
// TODO: PANIC! Something is wrong... what should we do??
obj = null;
else
obj = convertToCollection((Class) Util.rawClassOf(tr), (Class) node.collectionType(), (Collection) val, schema, path);
else
obj = convertToDTO((Class<? extends DTO>) rawClassOf(tr), (Map<?, ?>) val, schema, path + "/");
} else {
if (node.isCollection()) {
Collection c = instantiateCollection(node.collectionType());
Type type = node.type();
for (Object o : (Collection) val) {
if (o == null)
c.add(null);
else if (asDTO(rawClassOf(type)))
c.add(convertToDTO((Class) Util.rawClassOf(type), (Map) o, schema, path + "/"));
else
c.add(converter.convert(o).to(type));
}
obj = c;
} else {
Class<?> rawClass = rawClassOf(node.type());
if (asDTO(rawClass))
obj = convertToDTO((Class<? extends DTO>) rawClass, (Map<?, ?>) val, schema, path + "/");
else
obj = converter.convert(val).to(node.type());
}
}
f.set(dto, obj);
} catch (NoSuchFieldException e) {
}
}
return dto;
} catch (Exception e) {
throw new ConversionException("Cannot create DTO " + targetCls, e);
}
}
use of org.apache.felix.schematizer.Node in project felix by apache.
the class SchemaBasedConverter method convertCollectionItemToDTO.
@SuppressWarnings("unchecked")
private <U> U convertCollectionItemToDTO(Object obj, Class<U> targetCls, Schema schema, String path) {
Node node = schema.nodeAtPath(path);
if (node.typeReference().isPresent()) {
TypeReference<U> tr = (TypeReference<U>) Util.typeReferenceOf(node.typeReference().get());
return converter.convert(obj).to(tr);
} else {
Type type = node.type();
type.toString();
// obj = converter.convert(val).to(type);
return null;
}
}
use of org.apache.felix.schematizer.Node in project felix by apache.
the class SchematizerServiceTest method testSchematizeDTO.
@Test
public void testSchematizeDTO() {
Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO>() {
}).get("MyDTO");
assertNotNull(s);
Node root = s.rootNode();
assertNodeEquals("", "/", false, MyDTO.class, false, root);
assertEquals(4, root.children().size());
Node pingNode = root.children().get("/ping");
assertNodeEquals("ping", "/ping", false, String.class, true, pingNode);
Node pongNode = root.children().get("/pong");
assertNodeEquals("pong", "/pong", false, Long.class, true, pongNode);
Node countNode = root.children().get("/count");
assertNodeEquals("count", "/count", false, MyDTO.Count.class, true, countNode);
Node embeddedNode = root.children().get("/embedded");
assertEquals(3, embeddedNode.children().size());
assertNodeEquals("embedded", "/embedded", false, MyEmbeddedDTO.class, true, embeddedNode);
Node marcoNode = embeddedNode.children().get("/marco");
assertNodeEquals("marco", "/embedded/marco", false, String.class, true, marcoNode);
Node poloNode = embeddedNode.children().get("/polo");
assertNodeEquals("polo", "/embedded/polo", false, Long.class, true, poloNode);
Node alphaNode = embeddedNode.children().get("/alpha");
assertNodeEquals("alpha", "/embedded/alpha", false, MyEmbeddedDTO.Alpha.class, true, alphaNode);
Node sRoot = s.nodeAtPath("/");
assertNodeEquals("", "/", false, MyDTO.class, false, sRoot);
Node sPingNode = s.nodeAtPath("/ping");
assertNodeEquals("ping", "/ping", false, String.class, true, sPingNode);
Node sPongNode = s.nodeAtPath("/pong");
assertNodeEquals("pong", "/pong", false, Long.class, true, sPongNode);
Node sCountNode = s.nodeAtPath("/count");
assertNodeEquals("count", "/count", false, MyDTO.Count.class, true, sCountNode);
Node sEmbeddedNode = s.nodeAtPath("/embedded");
assertNodeEquals("embedded", "/embedded", false, MyEmbeddedDTO.class, true, sEmbeddedNode);
Node sMarcoNode = s.nodeAtPath("/embedded/marco");
assertNodeEquals("marco", "/embedded/marco", false, String.class, true, sMarcoNode);
Node sPoloNode = s.nodeAtPath("/embedded/polo");
assertNodeEquals("polo", "/embedded/polo", false, Long.class, true, sPoloNode);
Node sAlphaNode = s.nodeAtPath("/embedded/alpha");
assertNodeEquals("alpha", "/embedded/alpha", false, MyEmbeddedDTO.Alpha.class, true, sAlphaNode);
}
use of org.apache.felix.schematizer.Node in project felix by apache.
the class SchematizerServiceTest method testGetParentNode.
@Test
public void testGetParentNode() {
Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO>() {
}).get("MyDTO");
assertNotNull(s);
Node embeddedNode = s.nodeAtPath("/embedded/marco");
assertTrue(!"ERROR".equals(embeddedNode.name()));
Node parentNode = s.parentOf(embeddedNode);
assertTrue(!"ERROR".equals(parentNode.name()));
Node grandparentNode = s.parentOf(parentNode);
assertTrue(!"ERROR".equals(grandparentNode.name()));
assertEquals("/", grandparentNode.absolutePath());
}
Aggregations