1
|
package eu.etaxonomy.cdm.vaadin.container;
|
2
|
|
3
|
import java.sql.DatabaseMetaData;
|
4
|
import java.sql.SQLException;
|
5
|
import java.util.UUID;
|
6
|
|
7
|
import org.apache.log4j.Logger;
|
8
|
|
9
|
import com.vaadin.data.Property;
|
10
|
import com.vaadin.data.util.sqlcontainer.SQLContainer;
|
11
|
import com.vaadin.data.util.sqlcontainer.query.QueryDelegate;
|
12
|
|
13
|
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
|
14
|
import eu.etaxonomy.cdm.vaadin.util.CdmSpringContextHelper;
|
15
|
import eu.etaxonomy.cdm.vaadin.util.SQLUtils;
|
16
|
|
17
|
public class CdmSQLContainer extends SQLContainer {
|
18
|
|
19
|
private static final long serialVersionUID = 2828992538007070061L;
|
20
|
|
21
|
private static final Logger logger = Logger.getLogger(CdmSQLContainer.class);
|
22
|
|
23
|
private DatabaseMetaData databaseMetaData;
|
24
|
|
25
|
public static final int DEFAULT_LIMIT = 5000;
|
26
|
boolean checkPropertyIdCase = false;
|
27
|
|
28
|
public CdmSQLContainer(QueryDelegate delegate) throws SQLException {
|
29
|
super(delegate);
|
30
|
databaseMetaData = CdmSpringContextHelper.getDatabaseMetaData();
|
31
|
setPageLength(DEFAULT_LIMIT);
|
32
|
}
|
33
|
|
34
|
public static CdmSQLContainer newInstance(String tableName) throws SQLException {
|
35
|
// TODO : currently the sql generator is for h2, need to make this compatible for all flavours
|
36
|
//TableQuery tq = new TableQuery(tableName, CdmSpringContextHelper.getCurrent().getConnectionPool(), new DefaultSQLGenerator());
|
37
|
//tq.setVersionColumn("updated");
|
38
|
|
39
|
CdmSQLContainer container = new CdmSQLContainer(CdmQueryFactory.generateTableQuery(tableName));
|
40
|
container.checkPropertyIdCase = true;
|
41
|
return container;
|
42
|
}
|
43
|
|
44
|
public UUID getUuid(Object itemId) {
|
45
|
return UUID.fromString((String)getProperty(itemId,CdmQueryFactory.UUID_ID).getValue());
|
46
|
}
|
47
|
|
48
|
public Property<?> getProperty(Object itemId, Object propertyId) {
|
49
|
if(checkPropertyIdCase) {
|
50
|
try {
|
51
|
return getItem(itemId).getItemProperty(SQLUtils.correctCase(propertyId.toString(), databaseMetaData));
|
52
|
} catch (SQLException e) {
|
53
|
logger.warn("Error getting property", e);
|
54
|
return null;
|
55
|
}
|
56
|
} else {
|
57
|
return getItem(itemId).getItemProperty(propertyId);
|
58
|
}
|
59
|
}
|
60
|
|
61
|
}
|