Author: simoneg
Date: Wed Jul 22 01:03:11 2009
New Revision: 796594

URL: http://svn.apache.org/viewvc?rev=796594&view=rev
Log:
LABS-389 : Automatic injection of @Id and @Version on JPA entities

Added:
    
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/InstallIdByDefault.aj
    
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/InstallVersionByDefault.aj
    
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/NoId.java
    
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/NoVersion.java
    
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/EmptyFakeBean.java
    
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/NoIdFakeBean.java
    
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/NoVersionFakeBean.java
Modified:
    
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/TestJPAPersistedInjection.java

Added: 
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/InstallIdByDefault.aj
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/InstallIdByDefault.aj?rev=796594&view=auto
==============================================================================
--- 
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/InstallIdByDefault.aj
 (added)
+++ 
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/InstallIdByDefault.aj
 Wed Jul 22 01:03:11 2009
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.database.openjpa;
+
+
+import javax.persistence.GeneratedValue;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.EmbeddedId;
+
+/**
+ * Adds by default an id to those entities that does not have it.
+ *
+ * @author Simone Gianni <[email protected]>
+ */
+public aspect InstallIdByDefault {
+
+       /**
+        * Marker interface for beans receiving the default id.
+        *
+        * @author Simone Gianni <[email protected]>
+        */
+       public static interface WithDefaultId {
+               
+       }
+       
+       declare parents : ((@Entity *) && !(@NoId *) && 
!hasmethod(@(Id||EmbeddedId) public * get*()) && !hasfield(@(Id||EmbeddedId) * 
*)) implements WithDefaultId;
+       
+       /**
+        * The default provided id field.
+        */
+       private long WithDefaultId.id;
+       
+       /**
+        * Getter for the default id.
+        * @return The entity id.
+        */
+       @Id
+       @GeneratedValue
+       public long WithDefaultId.getId() {
+               return id;
+       }
+       
+       /**
+        * Setter for the default id.
+        * @param newId The entity id.
+        */
+       void WithDefaultId.setId(long newId) {
+               id = newId;
+       }
+       
+}

Added: 
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/InstallVersionByDefault.aj
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/InstallVersionByDefault.aj?rev=796594&view=auto
==============================================================================
--- 
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/InstallVersionByDefault.aj
 (added)
+++ 
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/InstallVersionByDefault.aj
 Wed Jul 22 01:03:11 2009
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.database.openjpa;
+
+import javax.persistence.GeneratedValue;
+import javax.persistence.Version;
+import javax.persistence.Entity;
+
+/**
+ * Provides by default a version field to those entities missing it.
+ *
+ * @author Simone Gianni <[email protected]>
+ */
+
+public aspect InstallVersionByDefault {
+
+       /**
+        * Marker interface for beans receiving the default version field.
+        *
+        * @author Simone Gianni <[email protected]>
+        */
+       public static interface WithDefaultVersion {
+               
+       }
+       
+       declare parents : ((@Entity *) && !(@NoVersion *) && 
!hasmethod(@Version public * get*()) && !hasfield(@Version * *)) implements 
WithDefaultVersion;
+       
+       /**
+        * The default version field.
+        */
+       private long WithDefaultVersion.version;
+       
+       /**
+        * Getter for the default version field.
+        * @return The version of the bean.
+        */
+       @Version
+       public long WithDefaultVersion.getVersion() {
+               return version;
+       }
+       
+       /**
+        * Setter for the default version field.
+        * @param newVersion The new version of the bean.
+        */
+       public void WithDefaultVersion.setVersion(long newVersion) {
+               version = newVersion;
+       }
+       
+       
+}

Added: 
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/NoId.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/NoId.java?rev=796594&view=auto
==============================================================================
--- 
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/NoId.java
 (added)
+++ 
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/NoId.java
 Wed Jul 22 01:03:11 2009
@@ -0,0 +1,21 @@
+package org.apache.magma.database.openjpa;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+/**
+ * Use this annotation on {...@link Entity} beans that does not have an 
{...@link Id} and should
+ * not receive the default one.
+ *
+ * @author Simone Gianni <[email protected]>
+ */
+...@retention(RetentionPolicy.RUNTIME)
+...@target(ElementType.TYPE)
+public @interface NoId {
+
+}

Added: 
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/NoVersion.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/NoVersion.java?rev=796594&view=auto
==============================================================================
--- 
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/NoVersion.java
 (added)
+++ 
labs/magma/trunk/database-jpa/src/main/java/org/apache/magma/database/openjpa/NoVersion.java
 Wed Jul 22 01:03:11 2009
@@ -0,0 +1,21 @@
+package org.apache.magma.database.openjpa;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import javax.persistence.Entity;
+import javax.persistence.Version;
+
+/**
+ * Use this annotation on {...@link Entity} beans that does not have a 
{...@link Version} and should
+ * not receive the default one.
+ *
+ * @author Simone Gianni <[email protected]>
+ */
+...@retention(RetentionPolicy.RUNTIME)
+...@target(ElementType.TYPE)
+public @interface NoVersion {
+
+}

Modified: 
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/TestJPAPersistedInjection.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/TestJPAPersistedInjection.java?rev=796594&r1=796593&r2=796594&view=diff
==============================================================================
--- 
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/TestJPAPersistedInjection.java
 (original)
+++ 
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/TestJPAPersistedInjection.java
 Wed Jul 22 01:03:11 2009
@@ -19,7 +19,13 @@
 import static org.junit.Assert.*;
 
 import org.apache.magma.database.DatabasePersisted;
+import org.apache.magma.database.openjpa.InstallIdByDefault.WithDefaultId;
+import 
org.apache.magma.database.openjpa.InstallVersionByDefault.WithDefaultVersion;
+import org.apache.magma.database.openjpa.stuff.EmptyFakeBean;
 import org.apache.magma.database.openjpa.stuff.FakeBean;
+import org.apache.magma.database.openjpa.stuff.FakeBeanPropertyAccess;
+import org.apache.magma.database.openjpa.stuff.NoIdFakeBean;
+import org.apache.magma.database.openjpa.stuff.NoVersionFakeBean;
 
 import org.junit.Test;
 
@@ -31,4 +37,28 @@
                assertTrue(b instanceof DatabasePersisted);
        }
        
+       @Test
+       public void versionAndId() throws Exception {
+               Object b = new EmptyFakeBean();
+               assertTrue("No default id on " + b.getClass(), b instanceof 
WithDefaultId);
+               assertTrue("No default version" + b.getClass(), b instanceof 
WithDefaultVersion);
+               
+               b = new FakeBean();
+               assertFalse("Default id on " + b.getClass(), b instanceof 
WithDefaultId);
+               assertTrue("No default version" + b.getClass(), b instanceof 
WithDefaultVersion);
+
+               b = new FakeBeanPropertyAccess();
+               assertFalse("Default id on " + b.getClass(), b instanceof 
WithDefaultId);
+               assertTrue("No default version" + b.getClass(), b instanceof 
WithDefaultVersion);
+               
+               b = new NoVersionFakeBean();
+               assertTrue("No default id on " + b.getClass(), b instanceof 
WithDefaultId);
+               assertFalse("Default version" + b.getClass(), b instanceof 
WithDefaultVersion);
+               
+               b = new NoIdFakeBean();
+               assertFalse("Default id on " + b.getClass(), b instanceof 
WithDefaultId);
+               assertTrue("No default version" + b.getClass(), b instanceof 
WithDefaultVersion);
+               
+       }
+       
 }

Added: 
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/EmptyFakeBean.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/EmptyFakeBean.java?rev=796594&view=auto
==============================================================================
--- 
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/EmptyFakeBean.java
 (added)
+++ 
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/EmptyFakeBean.java
 Wed Jul 22 01:03:11 2009
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.database.openjpa.stuff;
+
+import javax.persistence.Entity;
+
+...@entity
+public class EmptyFakeBean {
+
+       private String name;
+
+       public String getName() {
+               return name;
+       }
+
+       public void setName(String name) {
+               this.name = name;
+       }
+       
+       
+}

Added: 
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/NoIdFakeBean.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/NoIdFakeBean.java?rev=796594&view=auto
==============================================================================
--- 
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/NoIdFakeBean.java
 (added)
+++ 
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/NoIdFakeBean.java
 Wed Jul 22 01:03:11 2009
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.database.openjpa.stuff;
+
+import javax.persistence.Entity;
+
+import org.apache.magma.database.openjpa.NoId;
+
+...@entity
+...@noid
+public class NoIdFakeBean {
+
+}

Added: 
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/NoVersionFakeBean.java
URL: 
http://svn.apache.org/viewvc/labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/NoVersionFakeBean.java?rev=796594&view=auto
==============================================================================
--- 
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/NoVersionFakeBean.java
 (added)
+++ 
labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/stuff/NoVersionFakeBean.java
 Wed Jul 22 01:03:11 2009
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.database.openjpa.stuff;
+
+import javax.persistence.Entity;
+
+import org.apache.magma.database.openjpa.NoVersion;
+
+...@entity
+...@noversion
+public class NoVersionFakeBean {
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to