at.spardat.xma.datasource.TableSpec Maven / Gradle / Ivy
/*******************************************************************************
* 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: TableSpec.java 2089 2007-11-28 13:56:13Z s3460 $
package at.spardat.xma.datasource;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import at.spardat.xma.security.XMAContext;
import at.spardat.xma.session.XMASession;
/**
* Encapsulates a table spec as defined in {@link ITabularDataSource}.
*
* @author YSD, 16.06.2003 15:45:45
*/
public class TableSpec {
/**
* The properties
*/
private TreeMap props_ = new TreeMap();
/**
* Indicates if the spec provided in the constructor is well formed
*/
private boolean isValid_ = true;
/**
* Constructor. Stores the spec string. If the provided String
* is not valid, the method isValid will indicate that fact.
*
* @param spec a data source specification as defined in {@link ITabularDataSource}.
*/
public TableSpec (String spec) {
if (spec == null) throw new IllegalArgumentException();
// split
StringTokenizer tokizer = new StringTokenizer(spec, ",");
while (tokizer.hasMoreElements()) {
String parameter = (String) tokizer.nextElement();
int indexOfEqual = parameter.indexOf('=');
if (indexOfEqual == -1) {
isValid_ = false; return;
}
String key = parameter.substring(0, indexOfEqual);
String value = parameter.substring(indexOfEqual+1);
props_.put(key, value);
}
if (getProperty("type") == null) {
isValid_ = false;
}
}
/**
* Constructs with no properties. Properties may be added by calling {@link #setProperty}.
*/
public TableSpec () {
}
/**
* Indicates if the spec String provided at construction time has
* been a valid one.
*/
public boolean isValid () {
return isValid_;
}
/**
* Returns the table specification String. The order is defined as follows: The first attribute
* always is type. The remaining attributes are sorted
* according to key.compareTo. If !isValid(), the empty
* String is returned.
*/
public String toString () {
if (!isValid()) return "";
StringBuffer b = new StringBuffer();
b.append("type=").append(getType());
Iterator iter = props_.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry e = (Map.Entry) iter.next();
if ("type".equals(e.getKey())) continue;
b.append(',');
b.append(e.getKey()).append("=").append(e.getValue());
}
return b.toString();
}
/**
* Returns the type which has been part of the specification string provided
* in the constructor. Returns null, if there was not type-property
* in the spec string.
*/
public String getType() {
return getProperty ("type");
}
/**
* Returns a property for a provided key or null if there
* is no property for that key.
*/
public String getProperty (String key) {
if (props_ == null) return null;
return (String) props_.get(key);
}
/**
* Adds a property (key, value). An old property with the same
* key is replaced.
*
* @param key the key of the property.
* @param value the value of the property.
*/
public void setProperty (String key, String value) {
props_.put (key, value);
}
/**
* Removes property with a given key from this or does nothing
* if there is no such property.
*/
public void removeProperty (String key) {
props_.remove (key);
}
/**
* Adds the reserved parameters "_loc", "_man", "_env", "_av" which
* are extracted from the provided XMASession.
*/
public void addContextParams (XMASession session) {
XMAContext ctx = session.getContext();
setProperty ("_loc", ctx.getLocale().toString());
setProperty ("_man", ctx.getMandant());
setProperty ("_env", ctx.getEnvironment());
setProperty ("_av", String.valueOf(Math.abs(session.getApplicationVersionShort()))); // absolute value of integer-app-version
}
/**
* Returns true if key is one of the reserved context keys that
* are added with addContextParams.
*/
public static boolean isContextKey (String key) {
return "_loc".equals(key) || "_man".equals(key) || "_env".equals(key) || "_av".equals(key);
}
/**
* Requires that this contains the attribute _loc and
* returns the corresponding Locale object.
*
* @return null, if there is no locale
*/
public Locale getLocale () {
String loc = getProperty("_loc");
if (loc == null) return null;
StringTokenizer tokizer = new StringTokenizer (loc, "_");
String language = "";
String country = "";
String variant = "";
if (tokizer.hasMoreTokens()) language = tokizer.nextToken();
if (tokizer.hasMoreTokens()) country = tokizer.nextToken();
if (tokizer.hasMoreTokens()) variant = tokizer.nextToken();
return new Locale (language, country, variant);
}
/**
* Sets the TableSec's Locale.
* Usually the Locale is taken out of the XMAContext by the XMA framework.
* But there might be rare cases were a manipulation of the Locale is feasable.
* @param locale
* @since version_number
* @author s3460
*/
public void setLocale(Locale locale){
setProperty("_loc", locale.toString());
}
/**
* Returns an Iterator to iterate over the properties. The iterator
* returns the keys of type String. Please use method
* getProperty to extract the value lateron.
*/
public Iterator iterator () {
return props_.keySet().iterator();
}
// public static void main(String[] args) {
// TableSpec s = new TableSpec ("type=hugo");
// //s.setProperty("z", "fux");
// System.out.println (s.toString());
// }
}