labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simo...@apache.org
Subject svn commit: r718578 - in /labs/magma/trunk/foundation-database: ./ src/main/java/org/apache/magma/database/ src/main/resources/ src/main/resources/META-INF/ src/main/resources/META-INF/services/ src/test/java/org/apache/magma/database/
Date Tue, 18 Nov 2008 13:25:21 GMT
Author: simoneg
Date: Tue Nov 18 05:25:20 2008
New Revision: 718578

URL: http://svn.apache.org/viewvc?rev=718578&view=rev
Log:
Converter now in the database package
Velidator now supports identity

Added:
    labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddDatabaseIdentityToBeanData.aj
    labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddDatabasePersisted.aj
    labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseConverter.java
    labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseIdentity.java
    labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabasePersisted.java
    labs/magma/trunk/foundation-database/src/main/resources/
    labs/magma/trunk/foundation-database/src/main/resources/META-INF/
      - copied from r706222, labs/magma/trunk/database-jpa/src/main/resources/META-INF/
    labs/magma/trunk/foundation-database/src/test/java/org/apache/magma/database/TestJPAConverter.java
      - copied, changed from r706222, labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/TestJPAConverter.java
Modified:
    labs/magma/trunk/foundation-database/pom.xml
    labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddColumnAnnotationParsing.aj
    labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseValidator.java
    labs/magma/trunk/foundation-database/src/main/resources/META-INF/services/org.apache.magma.conversion.Converter

Modified: labs/magma/trunk/foundation-database/pom.xml
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-database/pom.xml?rev=718578&r1=718577&r2=718578&view=diff
==============================================================================
--- labs/magma/trunk/foundation-database/pom.xml (original)
+++ labs/magma/trunk/foundation-database/pom.xml Tue Nov 18 05:25:20 2008
@@ -14,8 +14,7 @@
 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.
--->
-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+--><project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 	<modelVersion>4.0.0</modelVersion>
 	<parent>
 	  <groupId>org.apache.magma</groupId>
@@ -52,5 +51,10 @@
       <artifactId>foundation-validation</artifactId>
       <version>0.0.1-SNAPSHOT</version>
     </dependency>
+    <dependency>
+      <groupId>org.apache.magma</groupId>
+      <artifactId>foundation-conversion</artifactId>
+      <version>0.0.1-SNAPSHOT</version>
+    </dependency>
   </dependencies>
-</project>
+</project>
\ No newline at end of file

Modified: labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddColumnAnnotationParsing.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddColumnAnnotationParsing.aj?rev=718578&r1=718577&r2=718578&view=diff
==============================================================================
--- labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddColumnAnnotationParsing.aj
(original)
+++ labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddColumnAnnotationParsing.aj
Tue Nov 18 05:25:20 2008
@@ -19,7 +19,8 @@
 import javax.persistence.Column;
 import java.lang.annotation.Annotation;
 import org.apache.magma.validation.Validator;
-import org.apache.magma.validation.Validators;;
+import org.apache.magma.validation.Validators;
+import org.apache.magma.beans.PropertyInfo;
 
 public aspect AddColumnAnnotationParsing {
 

Added: labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddDatabaseIdentityToBeanData.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddDatabaseIdentityToBeanData.aj?rev=718578&view=auto
==============================================================================
--- labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddDatabaseIdentityToBeanData.aj
(added)
+++ labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddDatabaseIdentityToBeanData.aj
Tue Nov 18 05:25:20 2008
@@ -0,0 +1,19 @@
+package org.apache.magma.database;
+
+import org.apache.magma.beans.BeanData;
+
+public aspect AddDatabaseIdentityToBeanData {
+
+	private DatabaseIdentity BeanData.databaseIdentity = null;
+	
+	
+	public DatabaseIdentity BeanData.getDatabaseIdentity() {
+		if (databaseIdentity == null) {
+			if (DatabasePersisted.class.isAssignableFrom(getBeanClass())) {
+				databaseIdentity = new DatabaseIdentity(getBeanClass());
+			}
+		}
+		return databaseIdentity;
+	}
+	
+}

Added: labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddDatabasePersisted.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddDatabasePersisted.aj?rev=718578&view=auto
==============================================================================
--- labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddDatabasePersisted.aj
(added)
+++ labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/AddDatabasePersisted.aj
Tue Nov 18 05:25:20 2008
@@ -0,0 +1,37 @@
+/*
+ * 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;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+import org.apache.magma.basics.MagmaException;
+import org.apache.magma.conversion.Converter;
+import org.apache.magma.conversion.Converters;
+
+public aspect AddDatabasePersisted {
+
+	declare parents : (@Entity *) implements DatabasePersisted;
+	
+	public Object DatabasePersisted.getPrimaryKey() {
+		return this.beanData().getDatabaseIdentity().getPrimaryKey(this);
+	}
+}

Added: labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseConverter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseConverter.java?rev=718578&view=auto
==============================================================================
--- labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseConverter.java
(added)
+++ labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseConverter.java
Tue Nov 18 05:25:20 2008
@@ -0,0 +1,68 @@
+/*
+ * 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;
+
+import org.apache.magma.basics.MagmaException;
+import org.apache.magma.beans.BeanData;
+import org.apache.magma.conversion.Converter;
+import org.apache.magma.conversion.Converters;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import javax.persistence.Id;
+
+public class DatabaseConverter implements Converter<DatabasePersisted> {
+
+	Class<? extends DatabasePersisted> myclass = null;
+	
+	public DatabaseConverter() {}
+	
+	public DatabaseConverter(Class<? extends DatabasePersisted> clazz) {
+		myclass = clazz;
+	}
+
+	public DatabasePersisted fromString(String value) {
+		if (value == null || value.equals("null")) return null;
+		DatabaseIdentity id = BeanData.getFor(myclass).getDatabaseIdentity();
+		Object key = id.getSubconverter().fromString(value);
+		Database db = new Database();
+		return db.load(myclass, key);
+	}
+
+	@SuppressWarnings("unchecked")
+	public String toString(DatabasePersisted value) {
+		if (value == null) return "null";
+		DatabaseIdentity id = BeanData.getFor(myclass).getDatabaseIdentity();
+		String ret = id.getSubconverter().toString(value.getPrimaryKey());
+		return ret;
+	}
+
+	public boolean converts(Class<?> clazz) {
+		return (DatabasePersisted.class.isAssignableFrom(clazz));
+	}
+
+	public Converter<DatabasePersisted> build(Class<? extends DatabasePersisted>
clazz) {
+		return new DatabaseConverter(clazz);
+	}
+
+	public int getMaximumStringSize() {
+		DatabaseIdentity id = BeanData.getFor(myclass).getDatabaseIdentity();
+		return id.getSubconverter().getMaximumStringSize();
+	}
+
+}

Added: labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseIdentity.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseIdentity.java?rev=718578&view=auto
==============================================================================
--- labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseIdentity.java
(added)
+++ labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseIdentity.java
Tue Nov 18 05:25:20 2008
@@ -0,0 +1,89 @@
+package org.apache.magma.database;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import javax.persistence.Id;
+
+import org.apache.magma.basics.MagmaException;
+import org.apache.magma.conversion.Converter;
+import org.apache.magma.conversion.Converters;
+
+public class DatabaseIdentity {
+
+	Class<? extends DatabasePersisted> myclass = null;	
+	Method idgetter = null;
+	Field idfield = null;
+	Class<?> idclass = null;
+	@SuppressWarnings("unchecked")
+	Converter subconverter = null;
+
+	public DatabaseIdentity(Class<? extends DatabasePersisted> clazz) {
+		this.myclass = clazz;
+		Class<?> acclass = clazz;
+		// TODO check wether to convert this to use GenericClass 
+		while (idclass == null && idgetter == null && acclass != null) {
+			// Check for methods first
+			Method[] methods = acclass.getDeclaredMethods();
+			for (Method method : methods) {
+				if (method.isAnnotationPresent(Id.class)) {
+					idclass = method.getReturnType();
+					idgetter = method;
+					idgetter.setAccessible(true);
+					break;
+				}
+			}
+			if (idgetter == null) {
+				// Then for fields
+				Field[] flds = acclass.getDeclaredFields();
+				for (int i = 0; i < flds.length; i++) {
+					if (flds[i].isAnnotationPresent(Id.class)) {
+						idclass = flds[i].getType();
+						idfield = flds[i];
+						idfield.setAccessible(true);
+						break;
+					}
+				}
+			}
+			acclass = acclass.getSuperclass();
+		}
+		if (idclass == null && idgetter == null) throw new MagmaException("Cannot find
a key for class {0}", clazz.getName());
+		subconverter = Converters.getConverterFor(idclass);
+		if (subconverter == null) throw new MagmaException("Cannot find a converter for {0}, which
is key of {1}", idclass.getName(), clazz.getName());					
+	}
+
+	public Method getIdgetter() {
+		return idgetter;
+	}
+
+	public Field getIdfield() {
+		return idfield;
+	}
+
+	public Class<?> getIdclass() {
+		return idclass;
+	}
+
+	public Converter getSubconverter() {
+		return subconverter;
+	}
+	
+	public Object getPrimaryKey(Object value) {
+		Object idval = null;
+		if (idgetter != null) {
+			try {
+				idval = idgetter.invoke(value);
+			} catch (Exception e) {
+				throw new MagmaException("Error accessing the field {0}.{1}, which is key for the class
{2}", idfield.getDeclaringClass().getName(), idfield.getName(), myclass.getName());
+			}			
+		} else if (idfield != null) {
+			try {
+				idval = idfield.get(value);
+			} catch (Exception e) {
+				throw new MagmaException("Error accessing the field {0}.{1}, which is key for the class
{2}", idfield.getDeclaringClass().getName(), idfield.getName(), myclass.getName());
+			}
+		}		
+		return idval;
+	}
+
+}

Added: labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabasePersisted.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabasePersisted.java?rev=718578&view=auto
==============================================================================
--- labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabasePersisted.java
(added)
+++ labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabasePersisted.java
Tue Nov 18 05:25:20 2008
@@ -0,0 +1,25 @@
+/*
+ * 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;
+
+import org.apache.magma.beans.MagmaBeanSupport;
+
+public interface DatabasePersisted extends MagmaBeanSupport {
+	
+	public Object getPrimaryKey();
+	
+}

Modified: labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseValidator.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseValidator.java?rev=718578&r1=718577&r2=718578&view=diff
==============================================================================
--- labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseValidator.java
(original)
+++ labs/magma/trunk/foundation-database/src/main/java/org/apache/magma/database/DatabaseValidator.java
Tue Nov 18 05:25:20 2008
@@ -20,27 +20,48 @@
 import java.util.List;
 
 import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.beans.PropertyInfo;
 import org.apache.magma.validation.Validator;
 
 public class DatabaseValidator implements Validator {
 
+	private boolean unique = false;
 	private boolean nullable = true;
 	private int length = -1;
 	
-	public List<LocalizableString> validate(Object value) {
-		List<LocalizableString> ret = new ArrayList<LocalizableString>(1);
+	
+	public List<LocalizableString> validate(Object bean, PropertyInfo property, Object
value) {
+		List<LocalizableString> ret = new ArrayList<LocalizableString>();
 		if (value == null) {
 			if (!nullable) {
 				ret.add(new LocalizableString("Cannot be null, as specified on database constraint"));
-				return ret;
 			}
-		} else if (value instanceof String && length != -1) {
-			if (((String)value).length() > length) {
-				ret.add(new LocalizableString("Cannot exceed {0} characters, as specified on database
constraint", length));
-				return ret;				
+		} else {
+			if (value instanceof String && length != -1) {
+				if (((String)value).length() > length) {
+					ret.add(new LocalizableString("Cannot exceed {0} characters, as specified on database
constraint", length));
+				}
+			}
+			if (unique) {
+				LocalizableString msg = new LocalizableString("{0} already used", value);					
+				Database db = new Database();
+				List query = db.query(bean.getClass(), "WHERE x." + property.getName() + "=?1", value);
+				if (query.size() > 1) {
+					ret.add(msg);					
+				} else if (query.size() == 1) {
+					DatabasePersisted my = (DatabasePersisted) bean;
+					DatabasePersisted other = (DatabasePersisted)query.get(0);
+					if (my.getPrimaryKey() == null) {
+						ret.add(msg);
+					} else {
+						if (!my.getPrimaryKey().equals(other.getPrimaryKey())) {
+							ret.add(msg);							
+						}
+					}
+				}
 			}
 		}
-		return null;
+		return ret.size() > 0 ? ret : null;						
 	}
 
 	public boolean isNullable() {
@@ -59,4 +80,12 @@
 		this.length = length;
 	}
 
+	public boolean isUnique() {
+		return unique;
+	}
+
+	public void setUnique(boolean unique) {
+		this.unique = unique;
+	}
+
 }

Modified: labs/magma/trunk/foundation-database/src/main/resources/META-INF/services/org.apache.magma.conversion.Converter
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-database/src/main/resources/META-INF/services/org.apache.magma.conversion.Converter?rev=718578&r1=706222&r2=718578&view=diff
==============================================================================
--- labs/magma/trunk/foundation-database/src/main/resources/META-INF/services/org.apache.magma.conversion.Converter
(original)
+++ labs/magma/trunk/foundation-database/src/main/resources/META-INF/services/org.apache.magma.conversion.Converter
Tue Nov 18 05:25:20 2008
@@ -12,4 +12,4 @@
 #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.
-org.apache.magma.database.openjpa.JPAConverter
+org.apache.magma.database.DatabaseConverter

Copied: labs/magma/trunk/foundation-database/src/test/java/org/apache/magma/database/TestJPAConverter.java
(from r706222, labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/TestJPAConverter.java)
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-database/src/test/java/org/apache/magma/database/TestJPAConverter.java?p2=labs/magma/trunk/foundation-database/src/test/java/org/apache/magma/database/TestJPAConverter.java&p1=labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/TestJPAConverter.java&r1=706222&r2=718578&rev=718578&view=diff
==============================================================================
--- labs/magma/trunk/database-jpa/src/test/java/org/apache/magma/database/openjpa/TestJPAConverter.java
(original)
+++ labs/magma/trunk/foundation-database/src/test/java/org/apache/magma/database/TestJPAConverter.java
Tue Nov 18 05:25:20 2008
@@ -14,14 +14,14 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.magma.database.openjpa;
+package org.apache.magma.database;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
-import static org.junit.Assert.*;
 import org.apache.magma.conversion.Converter;
 import org.apache.magma.conversion.Converters;
-import org.apache.magma.database.openjpa.stuff.FakeBean;
-import org.apache.magma.database.openjpa.stuff.FakeBeanPropertyAccess;
-
 import org.junit.Test;
 
 
@@ -29,41 +29,18 @@
 
 	@Test
 	public void converter() throws Exception {
-		Converter converter = Converters.getConverterFor(FakeBean.class);
+		Converter converter = Converters.getConverterFor(ValidableBean.class);
 		assertNotNull(converter);
-		assertTrue(converter instanceof JPAConverter);
-		JPAConverter conv = (JPAConverter) converter;
+		assertTrue(converter instanceof DatabaseConverter);
+		DatabaseConverter conv = (DatabaseConverter) converter;
 		assertNotNull(conv.myclass);
-		assertEquals(FakeBean.class, conv.myclass);
-		assertNotNull(conv.idclass);
-		assertEquals(Integer.TYPE, conv.idclass);
-		assertNotNull(conv.idfield);
-		assertNotNull(conv.subconverter);
-		FakeBean b = new FakeBean();
+		assertEquals(ValidableBean.class, conv.myclass);
+		ValidableBean b = new ValidableBean();
 		b.setId(5);
 		String tos = converter.toString(b);
 		assertNotNull(tos);
 		assertEquals("5", tos);
 	}
 
-	@Test
-	public void converterOnProperties() throws Exception {
-		Converter converter = Converters.getConverterFor(FakeBeanPropertyAccess.class);
-		assertNotNull(converter);
-		assertTrue(converter instanceof JPAConverter);
-		JPAConverter conv = (JPAConverter) converter;
-		assertNotNull(conv.myclass);
-		assertEquals(FakeBeanPropertyAccess.class, conv.myclass);
-		assertNotNull(conv.idclass);
-		assertEquals(Integer.TYPE, conv.idclass);
-		assertNull(conv.idfield);
-		assertNotNull(conv.idgetter);
-		assertNotNull(conv.subconverter);
-		FakeBeanPropertyAccess b = new FakeBeanPropertyAccess();
-		b.setId(5);
-		String tos = converter.toString(b);
-		assertNotNull(tos);
-		assertEquals("5", tos);
-	}
 	
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org


Mime
View raw message