You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2012/05/10 18:41:20 UTC
svn commit: r1336782 - in
/labs/magma/trunk/database-mongodb/src/main/java/org/apache/magma/database/mongo:
BeanDataAdditions.aj PropertyInfoAdditions.aj
Author: simoneg
Date: Thu May 10 16:41:20 2012
New Revision: 1336782
URL: http://svn.apache.org/viewvc?rev=1336782&view=rev
Log:
Moved JPA annotation parsing to foundation-database, since it's useful in many places
Modified:
labs/magma/trunk/database-mongodb/src/main/java/org/apache/magma/database/mongo/BeanDataAdditions.aj
labs/magma/trunk/database-mongodb/src/main/java/org/apache/magma/database/mongo/PropertyInfoAdditions.aj
Modified: labs/magma/trunk/database-mongodb/src/main/java/org/apache/magma/database/mongo/BeanDataAdditions.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/database-mongodb/src/main/java/org/apache/magma/database/mongo/BeanDataAdditions.aj?rev=1336782&r1=1336781&r2=1336782&view=diff
==============================================================================
--- labs/magma/trunk/database-mongodb/src/main/java/org/apache/magma/database/mongo/BeanDataAdditions.aj (original)
+++ labs/magma/trunk/database-mongodb/src/main/java/org/apache/magma/database/mongo/BeanDataAdditions.aj Thu May 10 16:41:20 2012
@@ -24,84 +24,11 @@ import org.apache.magma.database.Databas
public aspect BeanDataAdditions {
- private List<PropertyInfo> BeanData.jpaBasicFields = null;
- private List<PropertyInfo> BeanData.jpaRelationFields = null;
- private PropertyInfo BeanData.jpaIdField = null;
- private PropertyInfo BeanData.jpaVersionField = null;
-
- private boolean BeanData.jpaClass = false;
private boolean BeanData.jpaSubEntity = false;
- private String BeanData.jpaTableName = null;
- private Class<? extends DatabasePersisted> BeanData.jpaSuper = null;
-
- private Map<Class<? extends Annotation>, List<Method>> BeanData.jpaCallbacks = null;
-
-
- private void BeanData.buildJpaFieldList() {
- List<String> flds = new ArrayList<String>(getPropertyNames());
- Collections.sort(flds);
- jpaBasicFields = new ArrayList<PropertyInfo>();
- jpaRelationFields = new ArrayList<PropertyInfo>();
- for (String name : flds) {
- PropertyInfo info = getProperty(name);
- if (info.isJpaTransient()) continue;
- if (info.isJpaId()) {
- jpaIdField = info;
- } else if (info.isJpaVersion()) {
- jpaVersionField = info;
- } else {
- if (!info.isReadable()) continue;
- if (info.isBasicType()) {
- if (!info.isWriteable()) continue;
- jpaBasicFields.add(info);
- } else if (info.getJpaRelation() != null || info.isMap()) {
- jpaRelationFields.add(info);
- }
- }
- }
- }
-
- public List<PropertyInfo> BeanData.getJpaBasicFields() {
- if (jpaBasicFields == null) buildJpaFieldList();
- return jpaBasicFields;
- }
-
- public List<PropertyInfo> BeanData.getJpaRelationFields() {
- if (jpaRelationFields == null) buildJpaFieldList();
- return jpaRelationFields;
- }
-
- public PropertyInfo BeanData.getJpaIdField() {
- if (jpaIdField == null) buildJpaFieldList();
- return jpaIdField;
- }
-
- public PropertyInfo BeanData.getJpaVersionField() {
- if (jpaVersionField == null) buildJpaFieldList();
- return jpaVersionField;
- }
-
- public boolean BeanData.isJpaClass() {
- return this.jpaClass;
- }
-
public boolean BeanData.isJpaSubEntity() {
return this.jpaSubEntity;
}
- public String BeanData.getJpaTableName() {
- return this.jpaTableName;
- }
-
- public Class<? extends DatabasePersisted> BeanData.getJpaSuper() {
- return this.jpaSuper;
- }
-
- public List<Method> BeanData.getJpaHandlers(Class<? extends Annotation> annotation) {
- if (this.jpaCallbacks == null) return null;
- return this.jpaCallbacks.get(annotation);
- }
-
after(BeanData bd) :
execution(BeanData.new(..))
&& this(bd) {
@@ -109,74 +36,13 @@ public aspect BeanDataAdditions {
Class<? extends MagmaBeanSupport> clzz = bd.getBeanClass();
if (!DatabasePersisted.class.isAssignableFrom(clzz)) return;
- scanCallbacks(bd,clzz);
- Class sup = clzz.getSuperclass();
- BeanData supbd = null;
- while (sup != null) {
- BeanData sbd = BeanData.getFor(sup);
- if (sbd.jpaClass) {
- bd.jpaSuper = sup;
- supbd = sbd;
- scanCallbacks(sbd, sup);
- }
- sup = sup.getSuperclass();
- }
-
Entity ent = clzz.getAnnotation(Entity.class);
if (ent != null) {
- bd.jpaClass = true;
- bd.jpaTableName = ent.name();
SubEntity maine = clzz.getAnnotation(SubEntity.class);
if (maine != null) {
bd.jpaSubEntity = true;
}
}
-
- if (!bd.jpaClass) return;
-
- if (bd.jpaTableName == null || bd.jpaTableName.length() == 0) {
- if (supbd != null) {
- bd.jpaTableName = supbd.jpaTableName;
- } else {
- bd.jpaTableName = clzz.getSimpleName().toLowerCase();
- }
- }
-
}
- private static List<Class<? extends Annotation>> callbacks = new ArrayList<Class<? extends Annotation>>();
-
- static {
- callbacks.add(PrePersist.class);
- callbacks.add(PostPersist.class);
-
- callbacks.add(PreUpdate.class);
- callbacks.add(PostUpdate.class);
-
- callbacks.add(PreRemove.class);
- callbacks.add(PostRemove.class);
-
- callbacks.add(PostLoad.class);
- }
-
- private static void scanCallbacks(BeanData bd, Class clzz) {
- Method[] meths = clzz.getDeclaredMethods();
- for (Method m : meths) {
- for (Class<? extends Annotation> ann : callbacks) {
- if (m.isAnnotationPresent(ann)) {
- if (!m.isAccessible()) {
- m.setAccessible(true);
- }
- if (bd.jpaCallbacks == null)
- bd.jpaCallbacks = new HashMap<Class<? extends Annotation>, List<Method>>();
- List<Method> mlist = bd.jpaCallbacks.get(ann);
- if (mlist == null) {
- mlist = new ArrayList<Method>();
- bd.jpaCallbacks.put(ann, mlist);
- }
- mlist.add(m);
- }
- }
- }
- }
}
Modified: labs/magma/trunk/database-mongodb/src/main/java/org/apache/magma/database/mongo/PropertyInfoAdditions.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/database-mongodb/src/main/java/org/apache/magma/database/mongo/PropertyInfoAdditions.aj?rev=1336782&r1=1336781&r2=1336782&view=diff
==============================================================================
--- labs/magma/trunk/database-mongodb/src/main/java/org/apache/magma/database/mongo/PropertyInfoAdditions.aj (original)
+++ labs/magma/trunk/database-mongodb/src/main/java/org/apache/magma/database/mongo/PropertyInfoAdditions.aj Thu May 10 16:41:20 2012
@@ -41,154 +41,5 @@ public aspect PropertyInfoAdditions {
subEntity = typ.isAnnotationPresent(SubEntity.class);
return subEntity;
}
-
-
- private String PropertyInfo.jpaColName = null;
- private boolean PropertyInfo.jpaEmbedded = false;
- private boolean PropertyInfo.jpaId = false;
- private boolean PropertyInfo.jpaVersion = false;
-
- private JpaRelations PropertyInfo.jpaRelation = null;
- private CascadeType[] PropertyInfo.jpaCascade = null;
- private boolean PropertyInfo.jpaDeleteOrphans = false;
- private String PropertyInfo.jpaMappedBy = null;
-
-
-
- // XXX isJpaTransient is already defined for database validation, eventually merge the two to offer a
- // generic jpa parsing system in beandata
-
- //public boolean PropertyInfo.isJpaTransient() {
- // return this.jpaTransient;
- //}
-
- public String PropertyInfo.getJpaColName() {
- return this.jpaColName;
- }
-
- public boolean PropertyInfo.isJpaEmbedded() {
- return this.jpaEmbedded;
- }
-
- public boolean PropertyInfo.isJpaVersion() {
- return this.jpaVersion;
- }
-
- public boolean PropertyInfo.isJpaId() {
- return this.jpaId;
- }
-
- public CascadeType[] PropertyInfo.getJpaCascadeType() {
- return this.jpaCascade;
- }
-
- public JpaRelations PropertyInfo.getJpaRelation() {
- return this.jpaRelation;
- }
-
- public boolean PropertyInfo.isJpaDeleteOrphans() {
- return this.jpaDeleteOrphans;
- }
-
-
- after(PropertyInfo info, PropertyDescriptor desc, Class beanClass) :
- execution(* PropertyInfo.init(PropertyDescriptor, Class))
- && this(info)
- && args(desc, beanClass) {
-
- Method rm = desc.getReadMethod();
- if (rm == null) return;
-
- Column col = rm.getAnnotation(Column.class);
- if (col != null) {
- info.jpaColName = col.name();
- }
- if (info.jpaColName == null || info.jpaColName.length() == 0) {
- info.jpaColName = info.getName();
- }
- Embedded emb = rm.getAnnotation(Embedded.class);
- if (emb != null) {
- info.jpaEmbedded = true;
- }
-
- Id id = rm.getAnnotation(Id.class);
- if (id != null)
- info.jpaId = true;
-
- Version ver = rm.getAnnotation(Version.class);
- if (ver != null)
- info.jpaVersion = true;
-
-
- {
- OneToOne assoc = rm.getAnnotation(OneToOne.class);
- if (assoc != null) {
- info.jpaCascade = assoc.cascade();
- info.jpaRelation = JpaRelations.OneToOne;
- info.jpaMappedBy = assoc.mappedBy();
- info.jpaDeleteOrphans |= checkOrphanRemoval(assoc);
- }
- }
-
- {
- OneToMany assoc = rm.getAnnotation(OneToMany.class);
- if (assoc != null) {
- info.jpaCascade = assoc.cascade();
- info.jpaRelation = JpaRelations.OneToMany;
- info.jpaMappedBy = assoc.mappedBy();
- info.jpaDeleteOrphans |= checkOrphanRemoval(assoc);
- }
- }
-
- {
- ManyToOne assoc = rm.getAnnotation(ManyToOne.class);
- if (assoc != null) {
- info.jpaCascade = assoc.cascade();
- info.jpaRelation = JpaRelations.ManyToMany;
- info.jpaDeleteOrphans |= checkOrphanRemoval(assoc);
- }
- }
-
- {
- ManyToMany assoc = rm.getAnnotation(ManyToMany.class);
- if (assoc != null) {
- info.jpaCascade = assoc.cascade();
- info.jpaRelation = JpaRelations.ManyToOne;
- info.jpaMappedBy = assoc.mappedBy();
- info.jpaDeleteOrphans |= checkOrphanRemoval(assoc);
- }
- }
-
- if (!info.jpaDeleteOrphans) {
- Annotation[] anns = rm.getAnnotations();
- for (Annotation ann : anns) {
- info.jpaDeleteOrphans |= ann.annotationType().getName().equals("org.apache.openjpa.persistence.ElementDependent");
- // TODO eventually parse also other old ORM specific options
- }
- }
- }
-
- private boolean checkOrphanRemoval(Annotation a) {
- Method meth = null;
- try {
- meth = a.getClass().getMethod("orphanRemoval");
- } catch (SecurityException e) {
- // TODO maybe we should warn for this one?
- return false;
- } catch (NoSuchMethodException e) {
- return false;
- }
- if (meth == null) return false;
- if (!meth.getReturnType().equals(Boolean.TYPE)) return false;
- try {
- return (Boolean)meth.invoke(a);
- } catch (IllegalAccessException e) {
- // TODO maybe we should warn for this one?
- return false;
- } catch (Exception e) {
- return false;
- }
- }
-
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org