Browse Source

Added Java Date to Sql Date converter to better support sql dates (#11224)

Change-Id: I83d8c96a6877ea33b270a1a4a2c6f360a8147518
tags/7.1.0
Artur Signell 11 years ago
parent
commit
f7f1e3ece5

+ 59
- 0
server/src/com/vaadin/data/util/converter/DateToSqlDateConverter.java View File

@@ -0,0 +1,59 @@
/*
* Copyright 2000-2013 Vaadin Ltd.
*
* Licensed 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 com.vaadin.data.util.converter;

import java.util.Date;
import java.util.Locale;

/**
* Converter for handling conversion between {@link java.util.Date} and
* {@link java.sql.Date}. This is used when a PopupDateField or InlineDateField
* is connected to a java.sql.Date property, typically through a JPAContainer or
* SQLContainer. Note that information (time information) is lost when
* converting from {@link java.util.Date} to {@link java.sql.Date}.
*
* @since 7.1
* @author Vaadin Ltd
*/
public class DateToSqlDateConverter implements Converter<Date, java.sql.Date> {

@Override
public java.sql.Date convertToModel(Date value, Locale locale)
throws ConversionException {
return new java.sql.Date(value.getTime());
}

@Override
public Date convertToPresentation(java.sql.Date value, Locale locale)
throws ConversionException {
return new Date(value.getTime());
}

@Override
public Class<java.sql.Date> getModelType() {
return java.sql.Date.class;
}

@Override
public Class<Date> getPresentationType() {
return Date.class;
}

}

+ 2
- 0
server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java View File

@@ -87,6 +87,8 @@ public class DefaultConverterFactory implements ConverterFactory {
protected Converter<Date, ?> createDateConverter(Class<?> sourceType) {
if (Long.class.isAssignableFrom(sourceType)) {
return new DateToLongConverter();
} else if (java.sql.Date.class.isAssignableFrom(sourceType)) {
return new DateToSqlDateConverter();
} else {
return null;
}

+ 72
- 0
uitest/src/com/vaadin/tests/components/table/TableSqlContainer.html View File

@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://localhost:8888/" />
<title>New Test</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">New Test</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>/run/com.vaadin.tests.components.table.TableSqlContainer?restartApplication</td>
<td></td>
</tr>
<tr>
<td>assertText</td>
<td>vaadin=runcomvaadintestscomponentstableTableSqlContainer::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VVerticalLayout[0]/Slot[0]/VScrollTable[0]/domChild[1]/domChild[0]/domChild[1]/domChild[0]/domChild[0]/domChild[1]/domChild[0]</td>
<td>May 24, 2013 12:00:00 AM</td>
</tr>
<tr>
<td>mouseClick</td>
<td>vaadin=runcomvaadintestscomponentstableTableSqlContainer::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[1]/VCheckBox[0]/domChild[0]</td>
<td>12,7</td>
</tr>
<tr>
<td>assertValue</td>
<td>vaadin=runcomvaadintestscomponentstableTableSqlContainer::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VVerticalLayout[0]/Slot[0]/VScrollTable[0]/FocusableScrollPanel[0]/VScrollTable$VScrollTableBody[0]/VScrollTable$VScrollTableBody$VScrollTableRow[0]/VPopupCalendar[0]#field</td>
<td>5/24/13</td>
</tr>
<tr>
<td>mouseClick</td>
<td>vaadin=runcomvaadintestscomponentstableTableSqlContainer::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VVerticalLayout[0]/Slot[0]/VScrollTable[0]/FocusableScrollPanel[0]/VScrollTable$VScrollTableBody[0]/VScrollTable$VScrollTableBody$VScrollTableRow[0]/VPopupCalendar[0]#popupButton</td>
<td>9,7</td>
</tr>
<tr>
<td>mouseClick</td>
<td>//table[@id='PID_VAADIN_POPUPCAL']/tbody/tr[2]/td/table/tbody/tr[4]/td[5]/span</td>
<td>11,10</td>
</tr>
<tr>
<td>mouseClick</td>
<td>vaadin=runcomvaadintestscomponentstableTableSqlContainer::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[1]/VCheckBox[0]/domChild[0]</td>
<td>9,12</td>
</tr>
<tr>
<td>assertText</td>
<td>vaadin=runcomvaadintestscomponentstableTableSqlContainer::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VVerticalLayout[0]/Slot[0]/VScrollTable[0]/domChild[1]/domChild[0]/domChild[1]/domChild[0]/domChild[0]/domChild[1]/domChild[0]</td>
<td>May 15, 2013 12:00:00 AM</td>
</tr>
<tr>
<td>assertText</td>
<td>vaadin=runcomvaadintestscomponentstableTableSqlContainer::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VVerticalLayout[0]/Slot[0]/VScrollTable[0]/domChild[1]/domChild[0]/domChild[1]/domChild[0]/domChild[1]/domChild[1]/domChild[0]</td>
<td>Apr 26, 2013 12:00:00 AM</td>
</tr>
<tr>
<td>assertText</td>
<td>vaadin=runcomvaadintestscomponentstableTableSqlContainer::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VVerticalLayout[0]/Slot[0]/VScrollTable[0]/domChild[1]/domChild[0]/domChild[1]/domChild[0]/domChild[2]/domChild[1]/domChild[0]</td>
<td>May 27, 2013 12:00:00 AM</td>
</tr>
<tr>
<td>assertText</td>
<td>vaadin=runcomvaadintestscomponentstableTableSqlContainer::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VVerticalLayout[0]/Slot[0]/VScrollTable[0]/domChild[1]/domChild[0]/domChild[1]/domChild[0]/domChild[3]/domChild[1]/domChild[0]</td>
<td>Apr 28, 2013 12:00:00 AM</td>
</tr>

</tbody></table>
</body>
</html>

+ 142
- 0
uitest/src/com/vaadin/tests/components/table/TableSqlContainer.java View File

@@ -0,0 +1,142 @@
package com.vaadin.tests.components.table;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Locale;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.sqlcontainer.SQLContainer;
import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool;
import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool;
import com.vaadin.data.util.sqlcontainer.query.TableQuery;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

public class TableSqlContainer extends AbstractTestUI {

@Override
protected void setup(VaadinRequest request) {
setLocale(Locale.ENGLISH);
VerticalLayout layout = new VerticalLayout();
addComponent(layout);

final Table table = new Table("Table with SQLContainer");
layout.addComponent(table);

final Label selectedLabel = new Label("Selected: null");
layout.addComponent(selectedLabel);

try {
JDBCConnectionPool connectionPool = new SimpleJDBCConnectionPool(
"org.hsqldb.jdbc.JDBCDriver",
"jdbc:hsqldb:mem:sqlcontainer", "SA", "", 2, 20);

createTestTable(connectionPool);
insertTestData(connectionPool);

TableQuery q = new TableQuery("mytable", connectionPool);
q.setVersionColumn("version");
SQLContainer myContainer = new SQLContainer(q);

table.setContainerDataSource(myContainer);

} catch (SQLException e) {
e.printStackTrace();
}

table.setImmediate(true);
table.setSizeFull();
table.setSelectable(true);
table.addValueChangeListener(new Property.ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
selectedLabel.setValue("Selected: "
+ event.getProperty().getValue());
}
});

final CheckBox editMode = new CheckBox("Edit mode");
editMode.addValueChangeListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
table.setEditable(editMode.getValue());
}
});
addComponent(editMode);
}

/**
* (Re)creates the test table
*
* @param connectionPool
*/
private void createTestTable(JDBCConnectionPool connectionPool) {
Connection conn = null;
try {
conn = connectionPool.reserveConnection();
Statement statement = conn.createStatement();
try {
statement.executeUpdate("DROP TABLE mytable");
} catch (SQLException e) {
}
statement.execute("CREATE TABLE mytable "
+ "(id INTEGER GENERATED BY DEFAULT AS IDENTITY, D DATE,"
+ "MYFIELD VARCHAR(45), " + "PRIMARY KEY(ID))");
statement.close();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
connectionPool.releaseConnection(conn);
}
}

/**
* Adds test data to the test table
*
* @param connectionPool
* @throws SQLException
*/
private void insertTestData(JDBCConnectionPool connectionPool)
throws SQLException {
Connection conn = null;
try {
conn = connectionPool.reserveConnection();
Statement statement = conn.createStatement();

statement
.executeUpdate("INSERT INTO mytable VALUES(1, '2013-05-24', 'A0')");
statement
.executeUpdate("INSERT INTO mytable VALUES(2, '2013-04-26', 'A1')");
statement
.executeUpdate("INSERT INTO mytable VALUES(3, '2013-05-27', 'B0')");
statement
.executeUpdate("INSERT INTO mytable VALUES(4, '2013-04-28', 'B1')");

statement.close();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
connectionPool.releaseConnection(conn);
}
}

@Override
protected String getTestDescription() {
return "A test with Table connected to a SQLContainer using TableQuery";
}

@Override
protected Integer getTicketNumber() {
return 11224;
}

}

Loading…
Cancel
Save