Commit b74c06fe authored by gmantele's avatar gmantele
Browse files

[TAP] Remove TAPTypes and create a new class to represent a TAP column type:...

[TAP] Remove TAPTypes and create a new class to represent a TAP column type: TAPType. + Improve the conversion between VOTable type and TAP type by embedding it in TAPType. + Modify TAPColumn in order to integrate TAPType and that its VotType is just a conversion of a TAPType. {This commit is not compilable.}
parent 60284e9b
Loading
Loading
Loading
Loading
+23 −74
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ package tap.metadata;
import java.util.ArrayList;
import java.util.Iterator;

import tap.metadata.TAPType.TAPDatatype;
import adql.db.DBColumn;
import adql.db.DBTable;

@@ -41,11 +42,9 @@ public class TAPColumn implements DBColumn {

	private String utype = null;

	private String datatype = null;
	private TAPType datatype = new TAPType(TAPDatatype.VARCHAR);

	private int size = TAPTypes.NO_SIZE;

	private VotType votType = null;
	private VotType votType = datatype.toVotType();

	private boolean principal = false;

@@ -67,21 +66,25 @@ public class TAPColumn implements DBColumn {
		dbName = adqlName;
		lstTargets = new ArrayList<TAPForeignKey>(1);
		lstSources = new ArrayList<TAPForeignKey>(1);
		setDefaultType();
	}

	public TAPColumn(String columnName, String description){
	public TAPColumn(String columnName, TAPType type){
		this(columnName);
		setDatatype(type);
	}

	public TAPColumn(String columnName, TAPType type, String description){
		this(columnName, type);
		this.description = description;
	}

	public TAPColumn(String columnName, String description, String unit){
		this(columnName, description);
	public TAPColumn(String columnName, TAPType type, String description, String unit){
		this(columnName, type, description);
		this.unit = unit;
	}

	public TAPColumn(String columnName, String description, String unit, String ucd, String utype){
		this(columnName, description, unit);
	public TAPColumn(String columnName, TAPType type, String description, String unit, String ucd, String utype){
		this(columnName, type, description, unit);
		this.ucd = ucd;
		this.utype = utype;
	}
@@ -111,6 +114,7 @@ public class TAPColumn implements DBColumn {
	/**
	 * @return The table.
	 */
	@Override
	public final DBTable getTable(){
		return table;
	}
@@ -181,79 +185,25 @@ public class TAPColumn implements DBColumn {
	/**
	 * @return The datatype.
	 */
	public final String getDatatype(){
	public final TAPType getDatatype(){
		return datatype;
	}

	/**
	 * @return Array size (>0 or 2 special values: {@link TAPTypes#NO_SIZE} and {@link TAPTypes#STAR_SIZE}).
	 */
	public final int getArraySize(){
		return size;
	}

	/**
	 * <p>Sets the DB datatype, the size and uses these information to set the corresponding VOTable type.</p>
	 * <b>Important:</b>
	 * <ul>
	 * 	<li>If the given datatype is not known according to {@link TAPTypes#getDBType(String)}, the datatype of this column is set to its default value (see {@link #setDefaultType()}),</li>
	 * 	<li>The VOTable type is set automatically thanks to {@link TAPTypes#getVotType(String, int)}.</li>
	 * </ul>
	 * 
	 * @param datatype 	The datatype to set.
	 * @param size		Array size (>0 or 2 special values: {@link TAPTypes#NO_SIZE} and {@link TAPTypes#STAR_SIZE}).
	 * 
	 * @see TAPTypes#getDBType(VotType)
	 * @see TAPTypes#getVotType(String, int)
	 * @see #setDefaultType()
	 * @param type	The new column datatype.
	 */
	public final void setDatatype(String datatype, int size){
		this.datatype = TAPTypes.getDBType(datatype);
		this.size = (size <= 0 && size != TAPTypes.STAR_SIZE) ? TAPTypes.NO_SIZE : size;

		if (this.datatype == null)
			setDefaultType();
		else
			this.votType = TAPTypes.getVotType(this.datatype, this.size);
	public final void setDatatype(final TAPType type){
		datatype = type;
		votType = datatype.toVotType();
	}

	/**
	 * @return The VOTable type to use.
	 * @return The votType.
	 */
	public final VotType getVotType(){
		return votType;
	}

	/**
	 * <p>Sets the VOTable type and uses it to set the DB datatype and its size.</p>
	 * <b>Important:</b>
	 * <ul>
	 * 	<li>If the given VOTable type is not known according to {@link TAPTypes#getDBType(VotType)}, the DB datatype of this column and its size are set to the default value (see {@link #setDefaultType()}).</li>
	 * </ul>
	 * 
	 * @param type	A full VOTable type (that's to say: <code>datatype</code>, <code>arraysize</code> and <code>xtype</code>).
	 * 
	 * @see TAPTypes#getDBType(VotType)
	 * @see #setDefaultType()
	 */
	public final void setVotType(final VotType type){
		this.votType = type;
		this.datatype = TAPTypes.getDBType(type);
		this.size = type.arraysize;

		if (this.datatype == null)
			setDefaultType();
	}

	/**
	 * Sets the default DB datatype (VARCHAR) and its corresponding VOTable type (char , *).
	 */
	protected final void setDefaultType(){
		datatype = TAPTypes.VARCHAR;
		size = TAPTypes.STAR_SIZE;
		votType = TAPTypes.getVotType(datatype, size);
	}

	/**
	 * @return The principal.
	 */
@@ -346,12 +296,12 @@ public class TAPColumn implements DBColumn {
		lstSources.clear();
	}

	@Override
	public DBColumn copy(final String dbName, final String adqlName, final DBTable dbTable){
		TAPColumn copy = new TAPColumn((adqlName == null) ? this.adqlName : adqlName, description, unit, ucd, utype);
		TAPColumn copy = new TAPColumn((adqlName == null) ? this.adqlName : adqlName, datatype, description, unit, ucd, utype);
		copy.setDBName((dbName == null) ? this.dbName : dbName);
		copy.setTable(dbTable);

		copy.setDatatype(datatype, size);
		copy.setIndexed(indexed);
		copy.setPrincipal(principal);
		copy.setStd(std);
@@ -361,10 +311,9 @@ public class TAPColumn implements DBColumn {
	}

	public DBColumn copy(){
		TAPColumn copy = new TAPColumn(adqlName, description, unit, ucd, utype);
		TAPColumn copy = new TAPColumn(adqlName, datatype, description, unit, ucd, utype);
		copy.setDBName(dbName);
		copy.setTable(table);
		copy.setDatatype(datatype, size);
		copy.setIndexed(indexed);
		copy.setPrincipal(principal);
		copy.setStd(std);
+4 −6
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ package tap.metadata;

import java.io.IOException;
import java.io.PrintWriter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@@ -30,14 +29,13 @@ import java.util.NoSuchElementException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import adql.db.DBTable;
import tap.resource.Capabilities;
import tap.resource.TAPResource;
import tap.resource.VOSIResource;
import adql.db.DBTable;

public class TAPMetadata implements Iterable<TAPSchema>, VOSIResource, TAPResource {

@@ -314,13 +312,13 @@ public class TAPMetadata implements Iterable<TAPSchema>, VOSIResource, TAPResour
		if (c.getDatatype() != null){
			writer.print(prefix);
			writer.print("<dataType xsi:type=\"vod:TAPType\"");
			if (c.getArraySize() >= 0){
			if (c.getDatatype().length > 0){
				writer.print(" size=\"");
				writer.print(c.getArraySize());
				writer.print(c.getDatatype().length);
				writer.print("\"");
			}
			writer.print('>');
			writer.print(c.getDatatype().toUpperCase());
			writer.print(c.getDatatype().type.toString().toUpperCase());
			writer.println("</dataType>");
		}

+5 −18
Original line number Diff line number Diff line
@@ -199,34 +199,20 @@ public class TAPTable implements DBTable {
		return c;
	}

	public TAPColumn addColumn(String columnName, String description, String unit, String ucd, String utype){
	public TAPColumn addColumn(String columnName, TAPType datatype, String description, String unit, String ucd, String utype){
		if (columnName == null)
			return null;

		TAPColumn c = new TAPColumn(columnName, description, unit, ucd, utype);
		TAPColumn c = new TAPColumn(columnName, datatype, description, unit, ucd, utype);
		addColumn(c);
		return c;
	}

	public TAPColumn addColumn(String columnName, String description, String unit, String ucd, String utype, String datatype, int size, boolean principal, boolean indexed, boolean std){
	public TAPColumn addColumn(String columnName, TAPType datatype, String description, String unit, String ucd, String utype, boolean principal, boolean indexed, boolean std){
		if (columnName == null)
			return null;

		TAPColumn c = new TAPColumn(columnName, description, unit, ucd, utype);
		c.setDatatype(datatype, size);
		c.setPrincipal(principal);
		c.setIndexed(indexed);
		c.setStd(std);
		addColumn(c);
		return c;
	}

	public TAPColumn addColumn(String columnName, String description, String unit, String ucd, String utype, VotType votType, boolean principal, boolean indexed, boolean std){
		if (columnName == null)
			return null;

		TAPColumn c = new TAPColumn(columnName, description, unit, ucd, utype);
		c.setVotType(votType);
		TAPColumn c = new TAPColumn(columnName, datatype, description, unit, ucd, utype);
		c.setPrincipal(principal);
		c.setIndexed(indexed);
		c.setStd(std);
@@ -482,6 +468,7 @@ public class TAPTable implements DBTable {
		}
	}

	@Override
	public DBTable copy(final String dbName, final String adqlName){
		TAPTable copy = new TAPTable((adqlName == null) ? this.adqlName : adqlName);
		copy.setDBName((dbName == null) ? this.dbName : dbName);
+257 −0
Original line number Diff line number Diff line
package tap.metadata;

/*
 * This file is part of TAPLibrary.
 * 
 * TAPLibrary is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * TAPLibrary is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with TAPLibrary.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2014 - Astronomishes Rechen Institute (ARI)
 */

import tap.metadata.VotType.VotDatatype;

/**
 * 
 * <p>
 * 	Describe a full TAP column type as it is described in the IVOA document.
 * 	Thus, this object contains 2 attributes: <code>type</code> (or datatype) and <code>length</code> (or size).
 * </p>
 * 
 * <p>The length/size may be not defined ; in this case, its value is set to {@link #NO_LENGTH} or is negative or null.</p>
 * 
 * <p>All datatypes declared in the IVOA recommendation document of TAP are listed in an enumeration type: {@link TAPDatatype}.
 * It is used to set the attribute type/datatype of this class.</p>
 *  
 * @author Gr&eacute;gory Mantelet (ARI) - gmantele@ari.uni-heidelberg.de
 * @version 2.0 (06/2014)
 * @since 2.0
 */
public class TAPType {

	/**
	 * List of all datatypes declared in the IVOA recommendation of TAP (in the section UPLOAD).
	 * 
	 * @author Gr&eacute;gory Mantelet (ARI) - gmantele@ari.uni-heidelberg.de
	 * @version 2.0 (06/2014)
	 * @since 2.0
	 */
	public static enum TAPDatatype{
		SMALLINT, INTEGER, BIGINT, REAL, DOUBLE, BINARY, VARBINARY, CHAR, VARCHAR, BLOB, CLOB, TIMESTAMP, POINT, REGION;
	}

	/** Special value in case no length/size is specified. */
	public static final int NO_LENGTH = -1;

	/** Datatype of a column. */
	public final TAPDatatype type;

	/** The length parameter (only few datatypes need this parameter: char, varchar, binary and varbinary). */
	public final int length;

	/**
	 * Build a TAP column type by specifying a datatype.
	 * 
	 * @param datatype	Column datatype.
	 */
	public TAPType(final TAPDatatype datatype){
		this(datatype, NO_LENGTH);
	}

	/**
	 * Build a TAP column type by specifying a datatype and a length (needed only for datatypes like char, varchar, binary and varbinary).
	 * 
	 * @param datatype	Column datatype.
	 * @param length	Length of the column value (needed only for datatypes like char, varchar, binary and varbinary).
	 */
	public TAPType(final TAPDatatype datatype, final int length){
		if (datatype == null)
			throw new NullPointerException("Missing TAP column datatype !");
		this.type = datatype;
		this.length = length;
	}

	/**
	 * Convert this TAP column type into a VOTable field type.
	 * 
	 * @return	The corresponding VOTable field type.
	 * 
	 * @see #convertIntoVotType(TAPType)
	 */
	public VotType toVotType(){
		return convertIntoVotType(this);
	}

	@Override
	public String toString(){
		if (length > 0)
			return type + "(" + length + ")";
		else
			return type.toString();
	}

	/**
	 * Convert the given TAP column type into a VOTable field type.
	 * 
	 * @param taptype	The TAP column type to convert.
	 * 
	 * @return	The corresponding VOTable field type.
	 */
	public static VotType convertIntoVotType(final TAPType taptype){
		VotType vot = new VotType(VotDatatype.CHAR, VotType.NO_SIZE, false);

		switch(taptype.type){
			case SMALLINT:
				vot = new VotType(VotDatatype.SHORT, 1, false);
				break;

			case INTEGER:
				vot = new VotType(VotDatatype.INT, 1, false);
				break;

			case BIGINT:
				vot = new VotType(VotDatatype.LONG, 1, false);
				break;

			case REAL:
				vot = new VotType(VotDatatype.FLOAT, 1, false);
				break;

			case DOUBLE:
				vot = new VotType(VotDatatype.DOUBLE, 1, false);
				break;

			case CHAR:
				vot = new VotType(VotDatatype.CHAR, (taptype.length > 0 ? taptype.length : 1), false);
				break;

			case BINARY:
				vot = new VotType(VotDatatype.UNSIGNED_BYTE, (taptype.length > 0 ? taptype.length : VotType.NO_SIZE), false);
				break;

			case VARBINARY:
				vot = new VotType(VotDatatype.UNSIGNED_BYTE, (taptype.length > 0 ? taptype.length : VotType.NO_SIZE), (taptype.length > 0));
				break;

			case BLOB:
				vot = new VotType(VotDatatype.UNSIGNED_BYTE, VotType.NO_SIZE, true, VotType.XTYPE_BLOB);
				break;

			case CLOB:
				vot = new VotType(VotDatatype.CHAR, VotType.NO_SIZE, true, VotType.XTYPE_CLOB);
				break;

			case TIMESTAMP:
				vot = new VotType(VotDatatype.CHAR, VotType.NO_SIZE, true, VotType.XTYPE_TIMESTAMP);
				break;

			case POINT:
				vot = new VotType(VotDatatype.CHAR, VotType.NO_SIZE, true, VotType.XTYPE_POINT);
				break;

			case REGION:
				vot = new VotType(VotDatatype.CHAR, VotType.NO_SIZE, true, VotType.XTYPE_REGION);
				break;

			case VARCHAR:
			default:
				vot = new VotType(VotDatatype.CHAR, (taptype.length > 0 ? taptype.length : VotType.NO_SIZE), (taptype.length > 0), null);
				break;
		}

		return vot;
	}

	/**
	 * Convert the given VOTable field type into a TAP column type.
	 * 
	 * @param vottype	The VOTable field type to convert.
	 * 
	 * @return	The corresponding TAP column type.
	 */
	public static TAPType convertFromVotType(final VotType vottype){
		if (vottype == null)
			return new TAPType(TAPDatatype.VARCHAR);

		switch(vottype.datatype){
			case SHORT:
			case BOOLEAN:
				if ((vottype.arraysize <= 1 || vottype.arraysize == VotType.NO_SIZE) && !vottype.unlimitedArraysize)
					return new TAPType(TAPDatatype.SMALLINT);
				else
					return new TAPType(TAPDatatype.VARBINARY);

			case INT:
				if ((vottype.arraysize <= 1 || vottype.arraysize == VotType.NO_SIZE) && !vottype.unlimitedArraysize)
					return new TAPType(TAPDatatype.INTEGER);
				else
					return new TAPType(TAPDatatype.VARBINARY);

			case LONG:
				if ((vottype.arraysize <= 1 || vottype.arraysize == VotType.NO_SIZE) && !vottype.unlimitedArraysize)
					return new TAPType(TAPDatatype.BIGINT);
				else
					return new TAPType(TAPDatatype.VARBINARY);

			case FLOAT:
				if ((vottype.arraysize <= 1 || vottype.arraysize == VotType.NO_SIZE) && !vottype.unlimitedArraysize)
					return new TAPType(TAPDatatype.REAL);
				else
					return new TAPType(TAPDatatype.VARBINARY);

			case DOUBLE:
				if ((vottype.arraysize <= 1 || vottype.arraysize == VotType.NO_SIZE) && !vottype.unlimitedArraysize)
					return new TAPType(TAPDatatype.DOUBLE);
				else
					return new TAPType(TAPDatatype.VARBINARY);

			case UNSIGNED_BYTE:
				if (vottype.arraysize > 0){
					if (vottype.unlimitedArraysize)
						return new TAPType(TAPDatatype.VARBINARY, vottype.arraysize);
					else
						return new TAPType(TAPDatatype.BINARY, vottype.arraysize);
				}else
					return new TAPType(TAPDatatype.VARBINARY);

			case CHAR:
			default:
				TAPType taptype = null;
				if (vottype.xtype != null && vottype.xtype.trim().length() > 0){
					if (vottype.xtype.equalsIgnoreCase(VotType.XTYPE_BLOB))
						taptype = new TAPType(TAPDatatype.BLOB);
					else if (vottype.xtype.equalsIgnoreCase(VotType.XTYPE_CLOB))
						taptype = new TAPType(TAPDatatype.CLOB);
					else if (vottype.xtype.equalsIgnoreCase(VotType.XTYPE_TIMESTAMP))
						taptype = new TAPType(TAPDatatype.TIMESTAMP);
					else if (vottype.xtype.equalsIgnoreCase(VotType.XTYPE_POINT))
						taptype = new TAPType(TAPDatatype.POINT);
					else if (vottype.xtype.equalsIgnoreCase(VotType.XTYPE_REGION))
						taptype = new TAPType(TAPDatatype.REGION);
				}
				if (taptype == null){
					if (vottype.unlimitedArraysize)
						taptype = new TAPType(TAPDatatype.VARCHAR, (vottype.arraysize > 0) ? vottype.arraysize : NO_LENGTH);
					else{
						if (vottype.arraysize <= 0 || vottype.arraysize == VotType.NO_SIZE)
							taptype = new TAPType(TAPDatatype.VARCHAR);
						else if (vottype.arraysize == 1)
							taptype = new TAPType(TAPDatatype.CHAR, 1);
						else
							taptype = new TAPType(TAPDatatype.CHAR, vottype.arraysize);
					}
				}
				return taptype;
		}
	}
}

src/tap/metadata/TAPTypes.java

deleted100644 → 0
+0 −359

File deleted.

Preview size limit exceeded, changes collapsed.

Loading