cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r439447 - in /incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src: main/java/org/apache/cayenne/access/types/ main/java/org/apache/cayenne/dba/ test/java/org/apache/art/ test/java/org/apache/art/auto/ test/java/org/apache/cayenne/access/ t...
Date Fri, 01 Sep 2006 20:04:08 GMT
Author: aadamchik
Date: Fri Sep  1 13:04:07 2006
New Revision: 439447

URL: http://svn.apache.org/viewvc?rev=439447&view=rev
Log:
CAY-645

Added:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/CalendarType.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/CalendarTest.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/auto/_CalendarTest.java
Modified:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/dba/JdbcAdapter.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DateTimeTypesTst.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/resources/one-way-map.map.xml
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/resources/testmap.map.xml

Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/CalendarType.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/CalendarType.java?rev=439447&view=auto
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/CalendarType.java
(added)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/CalendarType.java
Fri Sep  1 13:04:07 2006
@@ -0,0 +1,169 @@
+/*****************************************************************
+ *   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.cayenne.access.types;
+
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.Timestamp;
+import java.sql.Types;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+
+import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.validation.ValidationResult;
+
+/**
+ * ExtendedType that handles {@link java.util.Calendar} fields.
+ * 
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+public class CalendarType implements ExtendedType {
+    
+    protected Class calendarClass;
+    
+    public CalendarType(Class calendarClass) {
+        if (calendarClass == null) {
+            throw new IllegalArgumentException("Null calendar class");
+        }
+
+        if (!Calendar.class.isAssignableFrom(calendarClass)) {
+            throw new IllegalArgumentException(
+                    "Must be a java.util.Calendar or a subclass: " + calendarClass);
+        }
+    
+        this.calendarClass = calendarClass;
+    }
+
+    public String getClassName() {
+        return calendarClass.getName();
+    }
+
+    public Object materializeObject(ResultSet rs, int index, int type) throws Exception {
+
+        Date val = null;
+
+        switch (type) {
+            case Types.TIMESTAMP:
+                val = rs.getTimestamp(index);
+                break;
+            case Types.DATE:
+                val = rs.getDate(index);
+                break;
+            case Types.TIME:
+                val = rs.getTime(index);
+                break;
+            default:
+                // here the driver can "surpirse" us
+                // check the type of returned value...
+                Object object = rs.getObject(index);
+
+                if (object != null && !(object instanceof Date)) {
+                    throw new CayenneRuntimeException(
+                            "Expected an instance of java.util.Date, instead got "
+                                    + object.getClass().getName()
+                                    + ", column index: "
+                                    + index);
+                }
+
+                val = (Date) object;
+                break;
+        }
+
+        if(rs.wasNull()) {
+            return null;
+        }
+        
+        GregorianCalendar calendar = new GregorianCalendar();
+        calendar.setTime(val);
+        return calendar;
+    }
+
+    public Object materializeObject(CallableStatement rs, int index, int type)
+            throws Exception {
+        Date val = null;
+
+        switch (type) {
+            case Types.TIMESTAMP:
+                val = rs.getTimestamp(index);
+                break;
+            case Types.DATE:
+                val = rs.getDate(index);
+                break;
+            case Types.TIME:
+                val = rs.getTime(index);
+                break;
+            default:
+                // here the driver can "surpirse" us
+                // check the type of returned value...
+                Object object = rs.getObject(index);
+
+                if (object != null && !(object instanceof Date)) {
+                    throw new CayenneRuntimeException(
+                            "Expected an instance of java.util.Date, instead got "
+                                    + object.getClass().getName()
+                                    + ", column index: "
+                                    + index);
+                }
+
+                val = (Date) object;
+                break;
+        }
+
+        if(rs.wasNull()) {
+            return null;
+        }
+        
+        GregorianCalendar calendar = new GregorianCalendar();
+        calendar.setTime(val);
+        return calendar;
+    }
+
+    public void setJdbcObject(
+            PreparedStatement statement,
+            Object value,
+            int pos,
+            int type,
+            int precision) throws Exception {
+
+        if (value == null) {
+            statement.setNull(pos, Types.TIMESTAMP);
+        }
+        else if (value instanceof Calendar) {
+            Calendar calendar = (Calendar) value;
+            statement.setTimestamp(pos, new Timestamp(calendar.getTimeInMillis()));
+        }
+        else {
+            throw new IllegalArgumentException("Expected java.util.Calendar, got "
+                    + value.getClass().getName());
+        }
+    }
+
+    public boolean validateProperty(
+            Object source,
+            String property,
+            Object value,
+            DbAttribute dbAttribute,
+            ValidationResult validationResult) {
+        return true;
+    }
+}

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/dba/JdbcAdapter.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/dba/JdbcAdapter.java?rev=439447&r1=439446&r2=439447&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/dba/JdbcAdapter.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/dba/JdbcAdapter.java
Fri Sep  1 13:04:07 2006
@@ -23,7 +23,9 @@
 import java.net.URL;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
+import java.util.Calendar;
 import java.util.Collection;
+import java.util.GregorianCalendar;
 import java.util.Iterator;
 
 import org.apache.cayenne.CayenneRuntimeException;
@@ -32,6 +34,7 @@
 import org.apache.cayenne.access.trans.QueryAssembler;
 import org.apache.cayenne.access.types.BooleanType;
 import org.apache.cayenne.access.types.ByteArrayType;
+import org.apache.cayenne.access.types.CalendarType;
 import org.apache.cayenne.access.types.CharType;
 import org.apache.cayenne.access.types.ExtendedType;
 import org.apache.cayenne.access.types.ExtendedTypeMap;
@@ -133,6 +136,12 @@
 
         // enable "small" BLOBs
         map.registerType(new ByteArrayType(false, true));
+        
+        // enable Calendar
+        // TODO: andrus 9/1/2006 - maybe use ExtendedTypeFactory to handle all calendar
+        // subclasses at once
+        map.registerType(new CalendarType(GregorianCalendar.class));
+        map.registerType(new CalendarType(Calendar.class));
     }
 
     /**

Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/CalendarTest.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/CalendarTest.java?rev=439447&view=auto
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/CalendarTest.java
(added)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/CalendarTest.java
Fri Sep  1 13:04:07 2006
@@ -0,0 +1,10 @@
+package org.apache.art;
+
+import org.apache.art.auto._CalendarTest;
+
+public class CalendarTest extends _CalendarTest {
+
+}
+
+
+

Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/auto/_CalendarTest.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/auto/_CalendarTest.java?rev=439447&view=auto
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/auto/_CalendarTest.java
(added)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/art/auto/_CalendarTest.java
Fri Sep  1 13:04:07 2006
@@ -0,0 +1,22 @@
+package org.apache.art.auto;
+
+/** Class _CalendarTest was generated by Cayenne.
+  * It is probably a good idea to avoid changing this class manually, 
+  * since it may be overwritten next time code is regenerated. 
+  * If you need to make any customizations, please use subclass. 
+  */
+public class _CalendarTest extends org.apache.cayenne.CayenneDataObject {
+
+    public static final String CALENDAR_FIELD_PROPERTY = "calendarField";
+
+    public static final String ID_PK_COLUMN = "ID";
+
+    public void setCalendarField(java.util.Calendar calendarField) {
+        writeProperty("calendarField", calendarField);
+    }
+    public java.util.Calendar getCalendarField() {
+        return (java.util.Calendar)readProperty("calendarField");
+    }
+    
+    
+}

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DateTimeTypesTst.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DateTimeTypesTst.java?rev=439447&r1=439446&r2=439447&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DateTimeTypesTst.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/DateTimeTypesTst.java
Fri Sep  1 13:04:07 2006
@@ -22,6 +22,7 @@
 import java.util.Calendar;
 import java.util.Date;
 
+import org.apache.art.CalendarTest;
 import org.apache.art.DateTestEntity;
 import org.apache.cayenne.query.SelectQuery;
 import org.apache.cayenne.unit.CayenneTestCase;
@@ -40,6 +41,23 @@
 
         deleteTestData();
         context = createDataContext();
+    }
+
+    public void testCalendar() throws Exception {
+
+        CalendarTest test = (CalendarTest) context.newObject(CalendarTest.class);
+        
+        Calendar cal = Calendar.getInstance();
+        cal.clear();
+        cal.set(2002, 1, 1);
+        
+        test.setCalendarField(cal);
+        context.commitChanges();
+        
+        SelectQuery q = new SelectQuery(CalendarTest.class);
+        CalendarTest testRead = (CalendarTest) context.performQuery(q).get(0);
+        assertNotNull(testRead.getCalendarField());
+        assertEquals(cal, testRead.getCalendarField());
     }
 
     public void testDate() throws Exception {

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/resources/one-way-map.map.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/resources/one-way-map.map.xml?rev=439447&r1=439446&r2=439447&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/resources/one-way-map.map.xml
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/resources/one-way-map.map.xml
Fri Sep  1 13:04:07 2006
@@ -12,7 +12,7 @@
 	</db-entity>
 	<db-entity name="PAINTING">
 		<db-attribute name="ARTIST_ID" type="INTEGER"/>
-		<db-attribute name="ESTIMATED_PRICE" type="DECIMAL" length="10" precision="2"/>
+		<db-attribute name="ESTIMATED_PRICE" type="DECIMAL" length="10" scale="2"/>
 		<db-attribute name="GALLERY_ID" type="INTEGER"/>
 		<db-attribute name="PAINTING_ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
 		<db-attribute name="PAINTING_TITLE" type="VARCHAR" isMandatory="true" length="255"/>

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/resources/testmap.map.xml
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/resources/testmap.map.xml?rev=439447&r1=439446&r2=439447&view=diff
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/resources/testmap.map.xml (original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/resources/testmap.map.xml Fri
Sep  1 13:04:07 2006
@@ -51,6 +51,10 @@
 		<db-attribute name="BOOLEAN_COLUMN" type="BOOLEAN" isMandatory="true"/>
 		<db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
 	</db-entity>
+	<db-entity name="CALENDAR_TEST">
+		<db-attribute name="CALENDAR_FIELD" type="TIMESTAMP"/>
+		<db-attribute name="ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
+	</db-entity>
 	<db-entity name="CHAR_FK_TEST">
 		<db-attribute name="FK_COL" type="CHAR" length="10"/>
 		<db-attribute name="NAME" type="VARCHAR" length="100"/>
@@ -145,7 +149,7 @@
 	</db-entity>
 	<db-entity name="PAINTING">
 		<db-attribute name="ARTIST_ID" type="INTEGER"/>
-		<db-attribute name="ESTIMATED_PRICE" type="DECIMAL" length="10" precision="2"/>
+		<db-attribute name="ESTIMATED_PRICE" type="DECIMAL" length="10" scale="2"/>
 		<db-attribute name="GALLERY_ID" type="INTEGER"/>
 		<db-attribute name="PAINTING_DESCRIPTION" type="VARCHAR" length="255"/>
 		<db-attribute name="PAINTING_ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
@@ -153,7 +157,7 @@
 	</db-entity>
 	<db-entity name="PAINTING1">
 		<db-attribute name="ARTIST_ID" type="INTEGER"/>
-		<db-attribute name="ESTIMATED_PRICE" type="DECIMAL" length="10" precision="2"/>
+		<db-attribute name="ESTIMATED_PRICE" type="DECIMAL" length="10" scale="2"/>
 		<db-attribute name="GALLERY_ID" type="INTEGER"/>
 		<db-attribute name="PAINTING_ID" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
 		<db-attribute name="PAINTING_TITLE" type="VARCHAR" isMandatory="true" length="255"/>
@@ -175,10 +179,10 @@
 		<db-attribute-derived name="ARTIST_ID" type="INTEGER" isPrimaryKey="true" isGroupBy="true"
spec="%@">
 			<db-attribute-ref name="ARTIST_ID"/>
 		</db-attribute-derived>
-		<db-attribute-derived name="ESTIMATED_PRICE" type="DECIMAL" length="10" precision="2"
spec="SUM(%@)">
+		<db-attribute-derived name="ESTIMATED_PRICE" type="DECIMAL" length="10" scale="2" spec="SUM(%@)">
 			<db-attribute-ref name="ESTIMATED_PRICE"/>
 		</db-attribute-derived>
-		<db-attribute-derived name="PAINTINGS_COUNT" type="INTEGER" length="10" precision="2"
spec="COUNT(%@)">
+		<db-attribute-derived name="PAINTINGS_COUNT" type="INTEGER" length="10" scale="2" spec="COUNT(%@)">
 			<db-attribute-ref name="PAINTING_ID"/>
 		</db-attribute-derived>
 	</db-entity>
@@ -215,6 +219,9 @@
 	</obj-entity>
 	<obj-entity name="BooleanTestEntity" className="org.apache.art.BooleanTestEntity" dbEntityName="BOOLEAN_TEST">
 		<obj-attribute name="booleanColumn" type="java.lang.Boolean" db-attribute-path="BOOLEAN_COLUMN"/>
+	</obj-entity>
+	<obj-entity name="CalendarTest" className="org.apache.art.CalendarTest" dbEntityName="CALENDAR_TEST">
+		<obj-attribute name="calendarField" type="java.util.Calendar" db-attribute-path="CALENDAR_FIELD"/>
 	</obj-entity>
 	<obj-entity name="CharFkTestEntity" className="org.apache.art.CharFkTestEntity" dbEntityName="CHAR_FK_TEST">
 		<obj-attribute name="name" type="java.lang.String" db-attribute-path="NAME"/>



Mime
View raw message