at.spardat.xma.datasource.TabularDomData Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
// @(#) $Id: TabularDomData.java 2089 2007-11-28 13:56:13Z s3460 $
package at.spardat.xma.datasource;
import java.util.HashMap;
import at.spardat.enterprise.util.Types;
import at.spardat.xma.mdl.Atom;
/**
* Extension of TabularData that must be used for domains. This class
* implements {@link ITabularDomData} and therefore is more convenient to
* deal with in the case of domain tables.
*
* @author YSD, 20.07.2003 18:46:21
*/
public class TabularDomData extends TabularData implements ITabularDomData {
/**
* The names of the reserved columns
*/
private static final String [] DOM_COLS = {
"COD_KEY", "SHORT_VALUE", "LONG_VALUE", "VALID_FROM", "VALID_TO"
};
/**
* Since domains are frequently accessed by COD_KEY, this maps key
* is the COD_KEY-String, values are DomRowImpl objects.
*/
private HashMap keyRowMap_ = new HashMap();
/**
* True, after the first column has been inserted and the structure matches.
*/
private boolean columnsValidated_ = false;
/**
* Constructor.
*/
public TabularDomData () {
}
/**
* Convenience method. Calls addColumn for every reserved domain column.
*/
public void addDomColumns () {
for (int i = 0; i < DOM_COLS.length; i++) {
addColumn(DOM_COLS[i]);
}
}
/**
* Given an array of column names, this method decides if a TabularDomData
* may be constructed with this structure.
*/
public static boolean isDomainColStructure (String [] columns) {
if (columns.length < DOM_COLS.length) return false;
for (int i = 0; i < DOM_COLS.length; i++) {
if (!DOM_COLS[i].equals(columns[i])) return false;
}
return true;
}
/**
* Overwrites the implementation of the superclass to ensure that
*
* - The columns are valid when the first row is added.
*
- There are no duplicate values for COD_KEY.
*
*
* @exception IllegalArgumentException if the added columns do not represent
* the reserved dom structure or a COD_KEY already exists.
* @see at.spardat.xma.datasource.TabularData#addRow(at.spardat.xma.datasource.TabularDataRow)
*/
public void addRow (TabularDataRow r) throws IllegalArgumentException {
/**
* validate column structure
*/
if (!columnsValidated_) {
columnsValidated_ = true;
if (numCols() < DOM_COLS.length) throw new IllegalArgumentException ("to few columns");
for (int i = 0; i < DOM_COLS.length; i++) {
if (!DOM_COLS[i].equals(getColumnName(i))) throw new IllegalArgumentException ("illegal column " + getColumnName(i));
}
}
/**
* check for duplicates
*/
if (r.size() == 0) throw new IllegalArgumentException ("too few values");
Atom key = r.get(0);
if (key == null || key.getType() != Types.T_STRING) throw new IllegalArgumentException();
String keyS = key.toString();
if (keyRowMap_.containsKey(keyS)) throw new IllegalArgumentException ("duplicate key " + keyS);
/**
* Add new DomRowImpl to keyRowMap_
*/
keyRowMap_.put(keyS, new DomRowImpl (r));
/**
* Finally, call supers addRow
*/
super.addRow(r);
}
/**
* @see at.spardat.xma.datasource.ITabularDomData#getDomRow(int)
*/
public IDomRow getDomRow (int index) {
TabularDataRow tr = getRow (index);
Atom key = tr.get(0);
return (IDomRow)keyRowMap_.get(key.toString());
}
/**
* @see at.spardat.xma.datasource.ITabularDomData#getDomRow(java.lang.String)
*/
public IDomRow getDomRow (String key) {
return (IDomRow)keyRowMap_.get(key);
}
/**
* Implementation for IDomRow. This class is just a thin wrapper
* around a TabularDataRow.
*
* @author YSD, 20.07.2003 19:08:07
*/
private static class DomRowImpl implements IDomRow {
/**
* Reference to the wrapped TabularDataRow.
*/
TabularDataRow tableRow_;
/**
* Calculated value for start of valid-time. Long.MIN_VALUE
* means that there is no end of valid-time specified.
*/
long validFromMillis_ = Long.MIN_VALUE;
/**
* Calculated value for end of valid-time. Long.MIN_VALUE
* means that there is no end of valid-time specified.
*/
long validToMillis_ = Long.MIN_VALUE;
/**
* Constructor
*/
DomRowImpl (TabularDataRow tr) {
tableRow_ = tr;
// calculate start of valid-time
Atom validFrom = tableRow_.get(3);
if (validFrom.getType() == Types.T_DATE && validFrom.hasValue()) {
validFromMillis_ = validFrom.toDate().getTime();
}
// calculate end of valid-time
Atom validTo = tableRow_.get(4);
if (validTo.getType() == Types.T_DATE && validTo.hasValue()) {
validToMillis_ = validTo.toDate().getTime();
// add a day and substract one millisecond
validToMillis_ += (86400L * 1000L);
validToMillis_ -= 1;
}
}
/**
* @see at.spardat.xma.datasource.IDomRow#getKey()
*/
public String getKey() {
Atom key = tableRow_.get(0);
return key.toString();
}
/**
* @see at.spardat.xma.datasource.IDomRow#getShortValue()
*/
public String getShortValue() {
Atom a = tableRow_.get(1);
return a == null ? "" : a.toString();
}
/**
* @see at.spardat.xma.datasource.IDomRow#getLongValue()
*/
public String getLongValue() {
Atom a = tableRow_.get(2);
return a == null ? "" : a.toString();
}
/**
* @see at.spardat.xma.datasource.IDomRow#isInValidTimeRange()
*/
public boolean isInValidTimeRange() {
long currentTime = System.currentTimeMillis();
// check against lower bound
if (validFromMillis_ != Long.MIN_VALUE && currentTime < validFromMillis_) return false;
// check against upper bound
if (validToMillis_ != Long.MIN_VALUE && currentTime > validToMillis_) return false;
return true;
}
/**
* @see at.spardat.xma.datasource.IDomRow#getCell(int)
*/
public Atom getCell(int columnIndex) {
return tableRow_.get(columnIndex);
}
}
}