/*
* Copyright (c) 2006 by Know-Center, Graz, Austria
*
* This software is the confidential and proprietary information of Know-Center,
* Graz, Austria. You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with Know-Center.
*
* KNOW-CENTER MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT. KNOW-CENTER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
* LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES.
*
* $Id: SignatureTypeDefinition.java,v 1.3 2006/08/25 17:09:41 wprinz Exp $
*/
package at.knowcenter.wag.egov.egiz.sig;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import at.knowcenter.wag.egov.egiz.cfg.SettingsReader;
import at.knowcenter.wag.egov.egiz.exceptions.SignatureException;
public class SignatureTypeDefinition implements Serializable
{
/**
* SVUID.
*/
private static final long serialVersionUID = 1327407307346061147L;
/**
* The type of this definition
*/
private String type_ = null;
/**
* A map of all key to caption tupls.
*/
private Map keyCaptionMap_ = new HashMap();
/**
* A map of all key to value tupls.
*/
private Map keyValueMap_ = new HashMap();
/**
* A list of sorted keys
*/
private Vector sortedKeys_ = null;
/**
* A list of sorted captions
*/
private Vector sortedCaptions_ = null;
/**
* A revert of sorted keys
*/
private Vector revertSortedKeys_ = new Vector();
/**
* A revert list of sorted captions
*/
private Vector revertSortedCaptions_ = new Vector();
/**
* The settings reader reference
*/
private SettingsReader settings_ = null;
/**
* The constructor of the signature type definition. It reads the configured
* table definition of the signature block and load the type definition of a
* given type.
*
* @param settings
* a SettingsReader instance
* @param type
* the signature type to load
* @throws SignatureException
* @see SettingsReader
*/
public SignatureTypeDefinition(SettingsReader settings, String type) throws SignatureException
{
settings_ = settings;
type_ = type;
readSigTable(SignatureTypes.MAIN_TABLE);
loadTypeDefinition();
readFieldDefinitions();
}
/**
* Load the configured signature type definitions. It reads all key-captions
* tupls that are used in the signature table. It also reads all key-value
* tupls.
*
* @throws SignatureException
*/
private void loadTypeDefinition() throws SignatureException
{
if (sortedKeys_ == null)
{
sortKeys();
}
String key_prefix = SignatureTypes.SIG_OBJ + type_ + ".key";
ArrayList keys = settings_.getKeys(key_prefix);
if (keys == null)
{
SignatureException se = new SignatureException(100, "There is no key defined for type:" + type_);
;
throw se;
}
for (int key_idx = 0; key_idx < keys.size(); key_idx++)
{
String sig_key = (String) keys.get(key_idx);
String sig_key_val = settings_.getValueFromKey(key_prefix + "." + sig_key);
if (sortedKeys_.contains(sig_key))
{
keyCaptionMap_.put(sig_key, sig_key_val);
}
}
String value_prefix = SignatureTypes.SIG_OBJ + type_ + ".value";
ArrayList values = settings_.getKeys(value_prefix);
if (values != null)
{
for (int key_idx = 0; key_idx < values.size(); key_idx++)
{
String val_key = (String) values.get(key_idx);
String val_key_val = settings_.getValueFromKey(value_prefix + "." + val_key);
keyValueMap_.put(val_key, val_key_val);
}
}
}
/**
* This method reads the table definition of singature type. It takes care
* about the linearization of the defined key-value pairs or sub tables. The
* linearisation is done reading a table from left to right and top to bottom.
* A sub table is alwais a normal cell element in the linearisation prozess.
* If a sub table exists therefore the linearisation of the subtable is taken
* es cell element in the parent table. t This method stores a revert sorted
* linearisation list of used keys in the table. This method is called
* recursivley if defined nested tables.
*
* @param tableKey
* the name of the table definition
*/
private void readSigTable(String tableKey)
{
// System.err.println("read table:" + type_ + "." + tableKey);
String table_key_prefix = SignatureTypes.SIG_OBJ + type_ + "." + SignatureTypes.TABLE;
String table_key = table_key_prefix + tableKey;
String key_prefix = SignatureTypes.SIG_OBJ + type_ + ".key.";
// ArrayList table_def_keys = settings_.getKeys(table_key);
Vector table_def_keys = settings_.getSettingKeys(table_key);
if (table_def_keys != null)
{
for (int table_key_idx = 0; table_key_idx < table_def_keys.size(); table_key_idx++)
{
String table_row_id = (String) table_def_keys.get(table_key_idx);
String table_def_keys_name = table_key + "." + table_row_id;
String table_def_string = settings_.getValueFromKey(table_def_keys_name);
if (table_row_id.matches("\\D*"))
{
continue;
}
if (table_def_string != null)
{
// analyse the row definition
String[] elems = table_def_string.split("\\|");
// ArrayList row = new ArrayList();
int elem_idx = elems.length;
while (elem_idx > 0)
{
elem_idx--;
String elem = elems[elem_idx];
String[] key_type = elem.split("-");
if (key_type.length < 2)
{
return;
}
String key = key_type[0];
String type = key_type[1];
// System.err.println("key:" + type_ + "." + tableKey +
// "." + key + "=" + type);
if (SignatureTypes.TYPE_TABLE.equals(key))
{
// read sub table
readSigTable(type);
}
if (SignatureTypes.TYPE_IMAGE.equals(type))
{
// ignore images
}
if (SignatureTypes.TYPE_VALUE.equals(type))
{
String sig_key_val = settings_.getValueFromKey(key_prefix + key);
if (sig_key_val != null)
{
revertSortedKeys_.add(key);
revertSortedCaptions_.add(sig_key_val);
}
// ignore values without caption
}
if ((SignatureTypes.TYPE_VALUE + SignatureTypes.TYPE_CAPTION).equals(type) || (SignatureTypes.TYPE_CAPTION + SignatureTypes.TYPE_VALUE).equals(type))
{
String sig_key_val = settings_.getValueFromKey(key_prefix + key);
if (sig_key_val != null)
{
revertSortedKeys_.add(key);
revertSortedCaptions_.add(sig_key_val);
}
}
}
}
}
}
}
/**
* @return Returns the keys.
*/
public Map getKeyCaptionMap()
{
return keyCaptionMap_;
}
/**
* @return Returns the keyValueMap.
*/
public Map getKeyValueMap()
{
return keyValueMap_;
}
/**
* Returns a caption to a given key
*
* @param key
* @return the caption or null if the key is not found
*/
public String getCaptionFromKey(String key)
{
return (String) keyCaptionMap_.get(key);
}
/**
* Returns a value to given key
*
* @param key
* @return the value or null if the key is not found
*/
public String getValueFromKey(String key)
{
return (String) keyValueMap_.get(key);
}
/**
* @return Returns the sortedKeys.
*/
public Vector getSortedKeys()
{
if (sortedKeys_ == null)
{
sortKeys();
}
return sortedKeys_;
}
/**
* @return Returns the sortedCaptions.
*/
public Vector getSortedCaptions()
{
if (sortedCaptions_ == null)
{
sortKeys();
}
return sortedCaptions_;
}
/**
* @return Returns the revertSortedCaptions.
*/
public Vector getRevertSortedCaptions()
{
return revertSortedCaptions_;
}
/**
* @return Returns the revertSortedKeys.
*/
public Vector getRevertSortedKeys()
{
return revertSortedKeys_;
}
/**
* This method sort the reverted sorted key-caption and key-value lists.
*
*/
private void sortKeys()
{
// String key_prefix = SignatureTypes.SIG_OBJ + type_ + ".key.";
sortedKeys_ = new Vector(revertSortedKeys_.size());
sortedCaptions_ = new Vector(revertSortedCaptions_.size());
for (int key_idx = revertSortedKeys_.size() - 1; key_idx >= 0; key_idx--)
{
sortedKeys_.add(revertSortedKeys_.get(key_idx));
sortedCaptions_.add(revertSortedCaptions_.get(key_idx));
}
}
/**
* This method checks if a given key is defined.
*
* @param key
* to find
* @return true if the key is find false otherwise
*/
public boolean contains(String key)
{
return (keyValueMap_.get(key) != null);
}
/**
* The standard toString method. Used for internal tests only.
*/
public String toString()
{
String strg = this.type_ + "\n";
Vector sk = getSortedKeys();
Vector sc = getSortedCaptions();
for (int i = 0; i < sk.size(); i++)
{
strg += sk.get(i) + "=" + sc.get(i) + "\n";
}
return strg;
}
/**
* @return Returns the signature type string.
*/
public String getType()
{
return type_;
}
/**
* @return Returns the signature type description.
*/
public String getDescription()
{
String descr_key = SignatureTypes.SIG_OBJ + type_ + ".description";
return settings_.getValueFromKey(descr_key);
}
protected String getSettingsKeyBase()
{
return SignatureTypes.SIG_OBJ + type_;
}
/**
* Gets the field definition of the given Field.
*
* @param field_name
* The name of the field.
* @return Returns the field's definition.
*/
public SignatureFieldDefinition readFieldDefinition(String field_name)
{
SignatureFieldDefinition sfd = new SignatureFieldDefinition();
sfd.field_name = field_name;
sfd.caption = this.settings_.getValueFromKey(getSettingsKeyBase() + ".key." + field_name);
sfd.value = this.settings_.getValueFromKey(getSettingsKeyBase() + type_ + ".value." + field_name);
sfd.placeholder_length = -1;
String phlen_str = this.settings_.getValueFromKey(getSettingsKeyBase() + ".phlength." + field_name);
if (phlen_str == null)
{
phlen_str = this.settings_.getValueFromKey("defaults.phlength." + field_name);
}
if (phlen_str != null)
{
sfd.placeholder_length = Integer.parseInt(phlen_str);
}
return sfd;
}
List field_definitions_ = null;
protected void readFieldDefinitions()
{
this.field_definitions_ = new ArrayList();
for (int i = 0; i < this.sortedKeys_.size(); i++)
{
String key = (String) this.sortedKeys_.get(i);
SignatureFieldDefinition sfd = readFieldDefinition(key);
// sfd.brev = SignatureTypes.ALL_SIG_BREV[i];
this.field_definitions_.add(sfd);
}
}
/**
* Returns the list of field definitions of this Signature profile.
*
* @return Returns the list of field definitions of this Signature profile.
*/
public List getFieldDefinitions()
{
return this.field_definitions_;
}
/**
* Tells if this signature profile is semantically equal to the other
* signature profile.
*
*
* One profile is semantically equal to another one if the captions and keys
* of both profiles are equal and have the same order.
*
*
* @param other
* The other signature profile.
* @return Returns true, if this profile is semantically equivalent to the
* other profile.
*/
public boolean isSemanticallyEqual(SignatureTypeDefinition other)
{
List this_keys = filterOutNonRequiredFoundKeys(this.sortedKeys_);
List other_keys = filterOutNonRequiredFoundKeys(other.sortedKeys_);
if (this_keys.size() != other_keys.size())
{
return false;
}
for (int i = 0; i < this_keys.size(); i++)
{
String this_key = (String) this_keys.get(i);
String other_key = (String) other_keys.get(i);
if (!this_key.equals(other_key))
{
return false;
}
String this_caption = this.getCaptionFromKey(this_key);
String other_caption = other.getCaptionFromKey(other_key);
if (!this_caption.equals(other_caption))
{
return false;
}
}
return true;
}
/**
* Filters out all non required keys from the List of keys.
*
* @param keys The List of keys.
*
* @return Returns the subset List which contains only the required keys.
*/
protected static List filterOutNonRequiredFoundKeys (List keys)
{
List required_keys = new ArrayList(keys.size());
for (int i = 0; i < keys.size(); i++)
{
String this_key = (String) keys.get(i);
if (!SignatureTypes.isRequiredKey(this_key))
{
continue;
}
required_keys.add(this_key);
}
return required_keys;
}
}