use of javax.persistence.metamodel.Attribute in project tests by datanucleus.
the class MetamodelTest method testBasic.
/**
* Test for the list of classes and their basic model info
*/
public void testBasic() {
Metamodel model = emf.getMetamodel();
try {
EntityType<?> animalType = model.entity(Animal.class);
assertNotNull(animalType);
assertEquals("Number of Animal attributes is wrong", 2, animalType.getAttributes().size());
Class idType = animalType.getIdType().getJavaType();
assertEquals(String.class, idType);
try {
// String field (Id)
Attribute attr = animalType.getAttribute("name");
assertNotNull(attr);
assertEquals(attr.getName(), "name");
assertEquals(attr.getJavaType(), String.class);
assertEquals(attr.getJavaMember().getName(), "name");
assertFalse(attr.isCollection());
assertFalse(attr.isAssociation());
assertTrue(attr instanceof SingularAttribute);
assertFalse(((SingularAttribute) attr).isOptional());
assertFalse(((SingularAttribute) attr).isVersion());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"name\" field of " + Animal.class.getName());
}
try {
// N-1 field
Attribute attr = animalType.getAttribute("farm");
assertNotNull(attr);
assertEquals(attr.getName(), "farm");
assertEquals(attr.getJavaType(), Farm.class);
assertEquals(attr.getJavaMember().getName(), "farm");
assertFalse(attr.isCollection());
assertTrue(attr.isAssociation());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"farm\" field of " + Animal.class.getName());
}
// Supertype should be null
assertNull(animalType.getSupertype());
} catch (IllegalArgumentException iae) {
fail("Didnt find EntityType for " + Animal.class.getName());
}
try {
EntityType<?> farmType = model.entity(Farm.class);
assertNotNull(farmType);
assertEquals("Number of Farm attributes is wrong", 2, farmType.getAttributes().size());
try {
// String field
Attribute attr = farmType.getAttribute("name");
assertNotNull(attr);
assertEquals(attr.getName(), "name");
assertEquals(attr.getJavaType(), String.class);
assertEquals(attr.getJavaMember().getName(), "name");
assertFalse(attr.isCollection());
assertFalse(attr.isAssociation());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"name\" field of " + Farm.class.getName());
}
try {
// N-1 field
Attribute attr = farmType.getAttribute("animals");
assertNotNull(attr);
assertEquals(attr.getName(), "animals");
assertEquals(attr.getJavaType(), ArrayList.class);
assertEquals(attr.getJavaMember().getName(), "animals");
assertTrue(attr.isCollection());
assertTrue(attr.isAssociation());
assertTrue("Attribute for animals is not castable to ListAttribute!", attr instanceof ListAttribute);
ListAttribute listAttr = (ListAttribute) attr;
Type elemType = listAttr.getElementType();
assertEquals("Element type is wrong", Animal.class, elemType.getJavaType());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"animals\" field of " + Farm.class.getName());
}
} catch (IllegalArgumentException iae) {
fail("Didnt find EntityType for " + Farm.class.getName());
}
}
use of javax.persistence.metamodel.Attribute in project tests by datanucleus.
the class MetamodelTest method testInheritance.
/**
* Test for the case with inheritance.
*/
public void testInheritance() {
Metamodel model = emf.getMetamodel();
try {
EntityType<?> mgrType = model.entity(Manager.class);
assertNotNull(mgrType);
assertEquals("Number of Manager attributes is wrong", 16, mgrType.getAttributes().size());
assertEquals("Number of Manager singularAttributes is wrong", 13, mgrType.getSingularAttributes().size());
assertEquals("Number of Manager pluralAttributes is wrong", 3, mgrType.getPluralAttributes().size());
try {
// Field in Manager
Attribute attr = mgrType.getAttribute("subordinates");
assertNotNull(attr);
assertEquals(attr.getName(), "subordinates");
assertEquals(attr.getJavaType(), Set.class);
assertEquals(attr.getJavaMember().getName(), "subordinates");
assertTrue(attr.isCollection());
assertTrue(attr.isAssociation());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"subordinates\" field of " + Manager.class.getName());
}
try {
// Field in Employee
Attribute attr = mgrType.getAttribute("serialNo");
assertNotNull(attr);
assertEquals(attr.getName(), "serialNo");
assertEquals(attr.getJavaType(), String.class);
assertEquals(attr.getJavaMember().getName(), "serialNo");
assertFalse(attr.isCollection());
assertFalse(attr.isAssociation());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"serialNo\" field of " + Employee.class.getName());
}
try {
// Primitive Field in Employee
Attribute attr = mgrType.getAttribute("salary");
assertNotNull(attr);
assertEquals(attr.getName(), "salary");
assertEquals(attr.getJavaType(), float.class);
assertEquals(attr.getJavaMember().getName(), "salary");
assertFalse(attr.isCollection());
assertFalse(attr.isAssociation());
assertTrue(attr instanceof SingularAttribute);
assertFalse(((SingularAttribute) attr).isOptional());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"salary\" field of " + Employee.class.getName());
}
try {
// Field in Person
Attribute attr = mgrType.getAttribute("firstName");
assertNotNull(attr);
assertEquals(attr.getName(), "firstName");
assertEquals(attr.getJavaType(), String.class);
assertEquals(attr.getJavaMember().getName(), "firstName");
assertFalse(attr.isCollection());
assertFalse(attr.isAssociation());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"firstName\" field of " + Person.class.getName());
}
// Supertype should not be null
IdentifiableType empType = mgrType.getSupertype();
assertNotNull(empType);
IdentifiableType persType = empType.getSupertype();
assertNotNull(persType);
IdentifiableType superType = persType.getSupertype();
assertNull(superType);
} catch (IllegalArgumentException iae) {
fail("Didnt find EntityType for " + Manager.class.getName());
}
try {
EntityType<?> mugType = model.entity(ModeratedUserGroup.class);
assertNotNull(mugType);
assertNotNull(mugType.getId(long.class));
assertEquals("id", mugType.getId(long.class).getName());
} catch (IllegalArgumentException iae) {
fail("Error in metamodel tests" + iae.getMessage());
}
}
use of javax.persistence.metamodel.Attribute in project midpoint by Evolveum.
the class GeneralUpdate method updateNameCopyAttribute.
private void updateNameCopyAttribute(Attribute attribute) {
if ("name".equals(attribute.getName()) && RObject.class.isAssignableFrom(attribute.getDeclaringType().getJavaType())) {
Attribute nameCopyAttribute = findAttributeForName("nameCopy");
assert nameCopyAttribute != null;
updateAttribute(nameCopyAttribute);
}
}
use of javax.persistence.metamodel.Attribute in project midpoint by Evolveum.
the class GeneralUpdate method handleItemDelta.
void handleItemDelta() throws SchemaException {
while (segmentsIterator.hasNext()) {
Object currentPathSegment = segmentsIterator.next();
if (!ItemPath.isName(currentPathSegment)) {
// TODO Why is this? Shouldn't we throw an exception instead?
LOGGER.trace("Segment {} in path {} is not name item, finishing entity update for delta", currentPathSegment, path);
break;
}
currentItemName = ItemPath.toName(currentPathSegment);
LOGGER.trace("handleItemDelta: current item name = {}, currentBean = {}, currentBeanType = {}", currentItemName, currentBean, currentBeanType.getJavaType());
if (currentBean instanceof RAssignment) {
if (handleAssignment(currentItemName)) {
break;
}
}
Attribute attribute = findAttributeForCurrentState();
if (attribute == null) {
// there's no table/column that needs update
break;
}
if (segmentsIterator.hasNext()) {
stepThroughAttribute(attribute);
} else {
updateAttribute(attribute);
updateNameCopyAttribute(attribute);
}
}
}
use of javax.persistence.metamodel.Attribute in project midpoint by Evolveum.
the class EntityRegistry method init.
@PostConstruct
public void init() {
LOGGER.debug("Starting initialization");
metamodel = sessionFactory.getMetamodel();
for (EntityType<?> entity : metamodel.getEntities()) {
Class<?> javaType = entity.getJavaType();
Ignore ignore = javaType.getAnnotation(Ignore.class);
if (ignore != null) {
continue;
}
Class<?> jaxb;
if (RObject.class.isAssignableFrom(javaType)) {
// noinspection unchecked,rawtypes
jaxb = RObjectType.getType((Class<? extends RObject>) javaType).getJaxbClass();
} else {
JaxbType jaxbType = javaType.getAnnotation(JaxbType.class);
if (jaxbType == null) {
throw new IllegalStateException("Unknown jaxb type for " + javaType.getName());
}
jaxb = jaxbType.type();
}
jaxbMappings.put(jaxb, entity);
// create override map
Map<String, Attribute<?, ?>> overrides = new HashMap<>();
Map<UniformItemPath, Attribute<?, ?>> pathOverrides = new HashMap<>();
for (Attribute<?, ?> attribute : entity.getAttributes()) {
Class<?> jType = attribute.getJavaType();
JaxbPath[] paths = jType.getAnnotationsByType(JaxbPath.class);
if (paths == null || paths.length == 0) {
paths = ((Method) attribute.getJavaMember()).getAnnotationsByType(JaxbPath.class);
}
if (paths == null || paths.length == 0) {
JaxbName name = ((Method) attribute.getJavaMember()).getAnnotation(JaxbName.class);
if (name != null) {
overrides.put(name.localPart(), attribute);
}
continue;
}
for (JaxbPath path : paths) {
JaxbName[] names = path.itemPath();
if (names.length == 1) {
overrides.put(names[0].localPart(), attribute);
} else {
UniformItemPath customPath = prismContext.emptyPath();
for (JaxbName name : path.itemPath()) {
customPath = customPath.append(new QName(name.namespace(), name.localPart()));
}
pathOverrides.put(customPath, attribute);
}
}
}
if (!overrides.isEmpty()) {
attributeNameOverrides.put(entity, overrides);
}
if (!pathOverrides.isEmpty()) {
attributeNamePathOverrides.put(entity, pathOverrides);
}
}
LOGGER.debug("Initialization finished");
}
Aggregations