Search in sources :

Example 6 with AnnotationObject

use of org.datanucleus.metadata.annotations.AnnotationObject in project datanucleus-api-jdo by datanucleus.

the class JDOAnnotationReader method processNamedQueries.

/**
 * Convenience method to process @Queries, @Query.
 * @param cmd Metadata for the class, to which any queries will be added
 * @param cls Class that the named queries are registered against
 * @param annotations Annotations specified on the class
 */
protected void processNamedQueries(AbstractClassMetaData cmd, Class cls, AnnotationObject[] annotations) {
    for (AnnotationObject annotation : annotations) {
        Map<String, Object> annotationValues = annotation.getNameValueMap();
        String annName = annotation.getName();
        if (annName.equals(JDOAnnotationUtils.QUERIES)) {
            Query[] qs = (Query[]) annotationValues.get("value");
            for (Query query : qs) {
                String lang = JDOAnnotationUtils.getQueryLanguageName(query.language());
                if (!StringUtils.isWhitespace(lang)) {
                    if (// Convert to JDOQL
                    lang.equals(JDOQuery.JDOQL_QUERY_LANGUAGE)) {
                        lang = QueryLanguage.JDOQL.toString();
                    } else if (// Convert to SQL
                    lang.equals(JDOQuery.SQL_QUERY_LANGUAGE)) {
                        lang = QueryLanguage.SQL.toString();
                    } else if (// Convert to JPQL
                    lang.equals(JDOQuery.JPQL_QUERY_LANGUAGE)) {
                        lang = QueryLanguage.JPQL.toString();
                    }
                }
                String resultClassName = (query.resultClass() != null && query.resultClass() != void.class ? query.resultClass().getName() : null);
                if (StringUtils.isWhitespace(query.name())) {
                    throw new InvalidClassMetaDataException("044154", cmd.getFullClassName());
                }
                QueryMetaData qmd = new QueryMetaData(query.name());
                qmd.setScope(cls.getName());
                qmd.setLanguage(lang);
                qmd.setUnmodifiable(query.unmodifiable());
                qmd.setResultClass(resultClassName);
                qmd.setUnique(query.unique());
                qmd.setFetchPlanName(query.fetchPlan());
                qmd.setQuery(query.value());
                JDOAnnotationUtils.addExtensionsToMetaData(qmd, query.extensions());
                cmd.addQuery(qmd);
                qmd.setParent(cmd);
            }
        } else if (annName.equals(JDOAnnotationUtils.QUERY)) {
            String unmodifiable = "" + annotationValues.get("unmodifiable");
            Class resultClassValue = (Class) annotationValues.get("resultClass");
            String resultClassName = (resultClassValue != null && resultClassValue != void.class ? resultClassValue.getName() : null);
            String lang = JDOAnnotationUtils.getQueryLanguageName((String) annotationValues.get("language"));
            if (!StringUtils.isWhitespace(lang)) {
                if (// Convert to JDOQL
                lang.equals(JDOQuery.JDOQL_QUERY_LANGUAGE)) {
                    lang = QueryLanguage.JDOQL.toString();
                } else if (// Convert to SQL
                lang.equals(JDOQuery.SQL_QUERY_LANGUAGE)) {
                    lang = QueryLanguage.SQL.toString();
                } else if (// Convert to JPQL
                lang.equals(JDOQuery.JPQL_QUERY_LANGUAGE)) {
                    lang = QueryLanguage.JPQL.toString();
                }
            }
            if (StringUtils.isWhitespace((String) annotationValues.get("name"))) {
                throw new InvalidClassMetaDataException("044154", cmd.getFullClassName());
            }
            QueryMetaData qmd = new QueryMetaData((String) annotationValues.get("name"));
            qmd.setScope(cls.getName());
            qmd.setLanguage(lang);
            qmd.setUnmodifiable(unmodifiable);
            qmd.setResultClass(resultClassName);
            qmd.setUnique((String) annotationValues.get("unique"));
            qmd.setFetchPlanName((String) annotationValues.get("fetchPlan"));
            qmd.setQuery((String) annotationValues.get("value"));
            JDOAnnotationUtils.addExtensionsToMetaData(qmd, (Extension[]) annotationValues.get("extensions"));
            cmd.addQuery(qmd);
            qmd.setParent(cmd);
        }
    }
}
Also used : QueryMetaData(org.datanucleus.metadata.QueryMetaData) InvalidClassMetaDataException(org.datanucleus.metadata.InvalidClassMetaDataException) JDOQuery(org.datanucleus.api.jdo.JDOQuery) Query(javax.jdo.annotations.Query) AnnotationObject(org.datanucleus.metadata.annotations.AnnotationObject) AnnotationObject(org.datanucleus.metadata.annotations.AnnotationObject)

Example 7 with AnnotationObject

use of org.datanucleus.metadata.annotations.AnnotationObject in project motech by motech.

the class MdsJdoAnnotationReaderTest method shouldNotRecognizeEntityAnnotationWhenFileMetadataIsEmpty.

@Test
public void shouldNotRecognizeEntityAnnotationWhenFileMetadataIsEmpty() {
    AnnotationObject result = mdsJdoAnnotationReader.isClassPersistable(Record.class);
    assertNull(result);
}
Also used : AnnotationObject(org.datanucleus.metadata.annotations.AnnotationObject) Test(org.junit.Test)

Example 8 with AnnotationObject

use of org.datanucleus.metadata.annotations.AnnotationObject in project motech by motech.

the class MdsJdoAnnotationReaderTest method shouldRecognizeRegularPCAnnotation.

@Test
public void shouldRecognizeRegularPCAnnotation() {
    AnnotationObject result = mdsJdoAnnotationReader.isClassPersistable(JustPc.class);
    assertEquals(PersistenceCapable.class.getName(), result.getName());
    assertNotNull(result.getNameValueMap());
    assertEquals("false", result.getNameValueMap().get("cacheable"));
    assertEquals("true", result.getNameValueMap().get("detachable"));
}
Also used : PersistenceCapable(javax.jdo.annotations.PersistenceCapable) AnnotationObject(org.datanucleus.metadata.annotations.AnnotationObject) Test(org.junit.Test)

Example 9 with AnnotationObject

use of org.datanucleus.metadata.annotations.AnnotationObject in project motech by motech.

the class MdsJdoAnnotationReaderTest method shouldPrioritizePersistenceCapableAnnotationValuesOverEntityDefaults.

@Test
public void shouldPrioritizePersistenceCapableAnnotationValuesOverEntityDefaults() {
    AnnotationObject result = mdsJdoAnnotationReader.isClassPersistable(PcAndEntity.class);
    assertEquals(PersistenceCapable.class.getName(), result.getName());
    assertNotNull(result.getNameValueMap());
    assertEquals(IdentityType.APPLICATION, result.getNameValueMap().get("identityType"));
    assertEquals("false", result.getNameValueMap().get("detachable"));
    assertEquals("testCatalog", result.getNameValueMap().get("catalog"));
}
Also used : PersistenceCapable(javax.jdo.annotations.PersistenceCapable) AnnotationObject(org.datanucleus.metadata.annotations.AnnotationObject) Test(org.junit.Test)

Aggregations

AnnotationObject (org.datanucleus.metadata.annotations.AnnotationObject)9 PersistenceCapable (javax.jdo.annotations.PersistenceCapable)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 Column (javax.jdo.annotations.Column)2 Extension (javax.jdo.annotations.Extension)2 ForeignKeyAction (javax.jdo.annotations.ForeignKeyAction)2 Persistent (javax.jdo.annotations.Persistent)2 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)2 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)2 ForeignKeyMetaData (org.datanucleus.metadata.ForeignKeyMetaData)2 IndexMetaData (org.datanucleus.metadata.IndexMetaData)2 InvalidMetaDataException (org.datanucleus.metadata.InvalidMetaDataException)2 JoinMetaData (org.datanucleus.metadata.JoinMetaData)2 PrimaryKeyMetaData (org.datanucleus.metadata.PrimaryKeyMetaData)2 UniqueMetaData (org.datanucleus.metadata.UniqueMetaData)2 Map (java.util.Map)1 AttributeConverter (javax.jdo.AttributeConverter)1 UseDefault (javax.jdo.AttributeConverter.UseDefault)1 Discriminator (javax.jdo.annotations.Discriminator)1