001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.rdbms.managers;
007:
008: import java.sql.SQLException;
009: import java.util.Locale;
010: import java.util.TimeZone;
011:
012: import javax.xml.datatype.XMLGregorianCalendar;
013:
014: import org.openrdf.model.Literal;
015: import org.openrdf.model.URI;
016: import org.openrdf.model.datatypes.XMLDatatypeUtil;
017: import org.openrdf.sail.rdbms.managers.base.ValueManagerBase;
018: import org.openrdf.sail.rdbms.model.RdbmsLiteral;
019: import org.openrdf.sail.rdbms.schema.LiteralTable;
020:
021: /**
022: * Manages RDBMS Literals. Including creation, id lookup, and inserting them
023: * into the database.
024: *
025: * @author James Leigh
026: *
027: */
028: public class LiteralManager extends ValueManagerBase<RdbmsLiteral> {
029: private static TimeZone Z = TimeZone.getTimeZone("GMT");
030:
031: public static long getCalendarValue(XMLGregorianCalendar xcal) {
032: return xcal.toGregorianCalendar(Z, Locale.US, null)
033: .getTimeInMillis();
034: }
035:
036: public static LiteralManager instance;
037: private LiteralTable table;
038:
039: public LiteralManager() {
040: instance = this ;
041: }
042:
043: public void setTable(LiteralTable table) {
044: this .table = table;
045: }
046:
047: @Override
048: public void close() throws SQLException {
049: super .close();
050: table.close();
051: }
052:
053: @Override
054: protected boolean expungeRemovedStatements(int count,
055: String condition) throws SQLException {
056: return table.expungeRemovedStatements(count, condition);
057: }
058:
059: @Override
060: protected void optimize() throws SQLException {
061: super .optimize();
062: table.optimize();
063: }
064:
065: @Override
066: protected Literal key(RdbmsLiteral value) {
067: return value;
068: }
069:
070: @Override
071: protected void insert(Number id, RdbmsLiteral literal)
072: throws SQLException, InterruptedException {
073: String label = literal.getLabel();
074: String language = literal.getLanguage();
075: URI datatype = literal.getDatatype();
076: if (datatype == null && language == null) {
077: table.insertSimple(id, label);
078: } else if (datatype == null) {
079: table.insertLanguage(id, label, language);
080: } else {
081: String dt = datatype.stringValue();
082: try {
083: if (XMLDatatypeUtil.isNumericDatatype(datatype)) {
084: table.insertNumeric(id, label, dt, literal
085: .doubleValue());
086: } else if (XMLDatatypeUtil.isCalendarDatatype(datatype)) {
087: long value = getCalendarValue(literal
088: .calendarValue());
089: table.insertDateTime(id, label, dt, value);
090: } else {
091: table.insertDatatype(id, label, dt);
092: }
093: } catch (NumberFormatException e) {
094: table.insertDatatype(id, label, dt);
095: } catch (IllegalArgumentException e) {
096: table.insertDatatype(id, label, dt);
097: }
098: }
099: }
100:
101: @Override
102: protected int getBatchSize() {
103: return table.getBatchSize();
104: }
105:
106: }
|