Search in sources :

Example 1 with Version

use of javax.jdo.annotations.Version in project estatio by estatio.

the class UdoDomainObjectContract_jdoAnnotations_Test method searchAndTest.

@SuppressWarnings("rawtypes")
@Test
public void searchAndTest() {
    Reflections reflections = new Reflections("org.estatio.dom");
    Set<Class<? extends UdoDomainObject>> subtypes = reflections.getSubTypesOf(UdoDomainObject.class);
    for (Class<? extends UdoDomainObject> subtype : subtypes) {
        if (subtype.isAnonymousClass() || subtype.isLocalClass() || subtype.isMemberClass() || subtype.getName().endsWith("ForTesting")) {
            // skip (probably a testing class)
            continue;
        }
        if (UdoDomainObject.class == subtype || UdoDomainObject2.class == subtype) {
            // skip
            continue;
        }
        System.out.println(">>> " + subtype.getName());
        // must have a @PersistenceCapable(identityType=...) annotation
        final PersistenceCapable persistenceCapable = subtype.getAnnotation(PersistenceCapable.class);
        Assertions.assertThat(persistenceCapable).isNotNull();
        IdentityType identityType = persistenceCapable.identityType();
        Assertions.assertThat(identityType).isNotNull();
        if (identityType == IdentityType.DATASTORE) {
            // NOT mandatory to have a @DatastoreIdentity, but if does, then @DatastoreIdentity(..., column="id")
            final DatastoreIdentity datastoreIdentity = subtype.getAnnotation(DatastoreIdentity.class);
            if (datastoreIdentity != null) {
                Assertions.assertThat(datastoreIdentity.column()).isEqualTo("id");
            }
        }
        Inheritance inheritance = subtype.getAnnotation(Inheritance.class);
        if (inheritance != null && inheritance.strategy() == InheritanceStrategy.SUPERCLASS_TABLE) {
            // must NOT have a @Discriminator(..., column="discriminator")
            final Annotation[] declaredAnnotations = subtype.getDeclaredAnnotations();
            for (Annotation declaredAnnotation : declaredAnnotations) {
                if (declaredAnnotation.annotationType() == Discriminator.class) {
                    Assert.fail("Class " + subtype.getName() + " inherits from " + subtype.getSuperclass().getName() + "and has (incorrectly) been annotated with @Discriminator");
                }
            }
            // check if supertype has discriminator
            // must have a @Discriminator(..., column="discriminator") on one of its supertypes
            final Discriminator superDiscriminator = subtype.getSuperclass().getAnnotation(Discriminator.class);
            Assertions.assertThat(superDiscriminator).isNotNull();
            Assertions.assertThat(superDiscriminator.column()).isEqualTo("discriminator");
        }
        if (subtype.getSuperclass().equals(UdoDomainObject.class)) {
            // must have a @Version(..., column="version")
            final Version version = getAnnotationOfTypeOfItsSupertypes(subtype, Version.class);
            Assertions.assertThat(version).isNotNull();
            Assertions.assertThat(version.column()).isEqualTo("version");
        }
    }
}
Also used : IdentityType(javax.jdo.annotations.IdentityType) DatastoreIdentity(javax.jdo.annotations.DatastoreIdentity) UdoDomainObject(org.estatio.module.base.dom.UdoDomainObject) PersistenceCapable(javax.jdo.annotations.PersistenceCapable) Inheritance(javax.jdo.annotations.Inheritance) Annotation(java.lang.annotation.Annotation) Discriminator(javax.jdo.annotations.Discriminator) Version(javax.jdo.annotations.Version) UdoDomainObject2(org.estatio.module.base.dom.UdoDomainObject2) Reflections(org.reflections.Reflections) Test(org.junit.Test)

Example 2 with Version

use of javax.jdo.annotations.Version in project motech by motech.

the class EntityMetadataBuilderImpl method addVersioningMetadata.

private void addVersioningMetadata(ClassMetadata cmd, Class<?> definition) {
    Class<Version> ann = ReflectionsUtil.getAnnotationClass(definition, Version.class);
    Version versionAnnotation = AnnotationUtils.findAnnotation(definition, ann);
    if (versionAnnotation != null) {
        VersionMetadata vmd = cmd.newVersionMetadata();
        vmd.setColumn(versionAnnotation.column());
        vmd.setStrategy(versionAnnotation.strategy());
        if (versionAnnotation.extensions().length == 0 || !"field-name".equals(versionAnnotation.extensions()[0].key())) {
            throw new MdsException(String.format("Cannot create metadata fo %s. Extension not found in @Version annotation.", cmd.getName()));
        }
        Extension extension = versionAnnotation.extensions()[0];
        vmd.newExtensionMetadata(DATANUCLEUS, "field-name", extension.value());
    }
}
Also used : Extension(javax.jdo.annotations.Extension) Version(javax.jdo.annotations.Version) VersionMetadata(javax.jdo.metadata.VersionMetadata) MdsException(org.motechproject.mds.exception.MdsException)

Example 3 with Version

use of javax.jdo.annotations.Version in project motech by motech.

the class EntityProcessor method getVersionFieldName.

private String getVersionFieldName(Class clazz) {
    if (MdsVersionedEntity.class.getName().equalsIgnoreCase(clazz.getSuperclass().getName())) {
        return "instanceVersion";
    }
    Class<Version> verAnn = ReflectionsUtil.getAnnotationClass(clazz, Version.class);
    Version versionAnnotation = AnnotationUtils.findAnnotation(clazz, verAnn);
    if (versionAnnotation != null && versionAnnotation.extensions().length != 0 && versionAnnotation.extensions()[0].key().equals("field-name")) {
        return versionAnnotation.extensions()[0].value();
    }
    return null;
}
Also used : Version(javax.jdo.annotations.Version) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity)

Aggregations

Version (javax.jdo.annotations.Version)3 Annotation (java.lang.annotation.Annotation)1 DatastoreIdentity (javax.jdo.annotations.DatastoreIdentity)1 Discriminator (javax.jdo.annotations.Discriminator)1 Extension (javax.jdo.annotations.Extension)1 IdentityType (javax.jdo.annotations.IdentityType)1 Inheritance (javax.jdo.annotations.Inheritance)1 PersistenceCapable (javax.jdo.annotations.PersistenceCapable)1 VersionMetadata (javax.jdo.metadata.VersionMetadata)1 UdoDomainObject (org.estatio.module.base.dom.UdoDomainObject)1 UdoDomainObject2 (org.estatio.module.base.dom.UdoDomainObject2)1 Test (org.junit.Test)1 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)1 MdsException (org.motechproject.mds.exception.MdsException)1 Reflections (org.reflections.Reflections)1