/*
 * $Id: VariableInfo.java,v 1.9 2005/07/19 22:43:37 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * Created on 15.07.2004
 *
 * $Log: VariableInfo.java,v $
 * Revision 1.9  2005/07/19 22:43:37  marbobo
 * + Erweiterung der Spezialvariablen
 *
 * Revision 1.8  2005/07/18 14:45:59  marbobo
 * + Datentypen-Anzeigen anzeigen abschaltbar
 *
 * Revision 1.7  2004/12/21 10:24:45  koppy
 * + password-Feld
 *
 * Revision 1.6  2004/10/25 13:27:48  koppy
 * toLowercase() beim Vergleich von "Spezialvariablen"
 *
 * Revision 1.5  2004/10/15 11:24:29  marlin
 * Zusatzinfo zu Kategorie / Dokument eingebaut
 *
 * Revision 1.4  2004/10/11 06:46:34  marlin
 * Konstanten CATEGORY_ID_SUFFIX und DOCUMENT_ID_SUFFIX geändert
 *
 * Revision 1.3  2004/10/08 14:01:26  koppy
 * bugfixes
 *
 * Revision 1.2  2004/09/20 10:26:13  koppy
 * +javadoc Dokumentation
 *
 * Revision 1.1  2004/08/30 14:49:55  koppy
 * Initiale Version der Workflow-Komponente
 *
 */
package vkc.workflow.model;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Set;

import vkc.db.entities.VKCDocument;
import vkc.db.serializer.VKCCategorySerializer;
import vkc.db.serializer.VKCDocumentSerializer;
import vkc.misc.VKCLogManager;
import vkc.util.StringUtils;
import vkc.util.VKCCategoryAbsPath;
import vkc.view.WebShare;

/**
 * This class represents a java bean
 * to encapsulate infos about a process variable.
 * It is primary used in the presentation layer.
 * 
 * @author Andreas Koop
 */
public class VariableInfo {
    public static final String CATEGORY_ID_SUFFIX = "categoryid".toLowerCase();
    public static final String DOCUMENT_ID_SUFFIX = "documentid".toLowerCase();
    public static final String PASSWORD_SUFFIX = "password".toLowerCase();
    
    private static Set specialVarSuffixes = new HashSet();

    static {
        specialVarSuffixes.add(CATEGORY_ID_SUFFIX);
        specialVarSuffixes.add(DOCUMENT_ID_SUFFIX);
        specialVarSuffixes.add(PASSWORD_SUFFIX);
    }
    
    
    private Locale locale = null;
    private String id;
    private String name = null;
    private Object value;
    private String typeKey;
    private String description;
    private boolean readOnly;
    
    /**
     * Constructs a variable info for
     * a given locale which is used to
     * localize the name and type of the variable.
     * 
     * @param locale a locale object
     */
    public VariableInfo(Locale locale) {
        this.locale = locale;
    }
    
    /**
     * Returns the description of this variable.
     * 
     * @return the description
     */
    public String getDescription() {
        return description;
    }
    
    /**
     * Sets the description for this variable.
     * @param description
     */
    public void setDescription(String description) {
        this.description = description;
    }
    
    /**
     * Returns the name of this variable.<br/>
     * <b>Note:</b>If it is empty the id is return!
     * @return the name of the variable
     */
    public String getName() {
        if (StringUtils.isEmpty(name)) {
            return id;
        }
        
        return name;
    }
    
    /**
     * Sets the name of this variable.
     * @param name name of the variable
     */
    public void setName(String name) {
        this.name = name;
    }
    
    /**
     * Returns a more readable (localized) name of this
     * variable. Should be used in the presentation layer.
     * @return localized variable name
     */
    public String getDisplayName() {
        String i = getId();
        String n = getName();
        
        if (n.toLowerCase().endsWith(CATEGORY_ID_SUFFIX) || i.toLowerCase().endsWith(CATEGORY_ID_SUFFIX)) {
            return n + " (" + WebShare.resources.getMessage(locale, "vkc.category") + ")";
        } else if (n.toLowerCase().endsWith(DOCUMENT_ID_SUFFIX) || i.toLowerCase().endsWith(DOCUMENT_ID_SUFFIX)) {
            return n + " (" + WebShare.resources.getMessage(locale, "vkc.document") + ")";
        }
        
        return n;
    }
    
    /**
     * Returns the key of the variable type.
     * It is used to lookup the appropriate
     * localized name in the resource file.
     * @return type key
     */
    public String getTypeKey() {
        return typeKey;
    }
    
    /**
     * Sets the type key of this variable.
     * It is used to lookup the appropriate
     * localized name in the resource file.<br>
     * Possible values are defined as constants in
     * <code>WfConstants</code>
     * @param typeKey type key
     */
    public void setTypeKey(String typeKey) {
        this.typeKey = typeKey;
    }
    
    /**
     * Returns a more readable (localized) name of this
     * variable. Should be used in the presentation layer.
     * @return localized variable type
     */
    public String getDisplayType() {
        return WebShare.resources.getMessage(locale, this.typeKey);
    }
    
    /**
     * Returns the value of this variable.
     * @return the value
     */
    public Object getValue() {
        return value;
    }
    
    /**
     * Returns a more readable (localized) value of this
     * variable. Should be used in the presentation layer.
     * <b>Note:</b>If the variable name is set to "category_id"
     * the appropriate category path is returned!
     * 
     * @return the value
     */
    public Object getDisplayValue() {
        String i = getId();
        String n = getName();
        
        try {
	        if (n.toLowerCase().endsWith(CATEGORY_ID_SUFFIX) || i.toLowerCase().endsWith(CATEGORY_ID_SUFFIX)) {
	            if (value == null || StringUtils.isEmpty(value.toString())) {
	                return value;
	            }
	            
                Integer categoryId = Integer.valueOf(value.toString());
                VKCCategoryAbsPath cap = VKCCategorySerializer.getCategoryAbsPath(categoryId);

                return cap.getAbsolutePathString();
	        } else if (n.toLowerCase().endsWith(DOCUMENT_ID_SUFFIX) || i.toLowerCase().endsWith(DOCUMENT_ID_SUFFIX)) {
	            Integer documentId = Integer.valueOf(value.toString());
	            VKCDocument doc = VKCDocumentSerializer.retrieveDocument(documentId);
	            VKCCategoryAbsPath cap = VKCCategorySerializer.getCategoryAbsPath(doc.getCategoryId());
	            
	            return cap.getAbsolutePathString() + doc.getName();
	        }
        } catch (Exception e) {
            VKCLogManager.infoLog(this, e.getLocalizedMessage());
        }
            
        return value;
    }
    
    /**
     * Sets the value of this variable
     * @param value
     */
    public void setValue(Object value) {
        this.value = value;
    }
    
    /**
     * Indicates whether this variable is readonly or not.
     * @return true if variable is read-only, false otherwise.
     */
    public boolean isReadOnly() {
        return readOnly;
    }
    
    /**
     * Sets whether this variable is readonly or not.
     * @param readOnly the read-only state
     */
    public void setReadOnly(boolean readOnly) {
        this.readOnly = readOnly;
    }
    
    /**
     * Returns the id of this variable
     * @return the id
     */
    public String getId() {
        return id;
    }
    
    /**
     * Sets the id of this variable
     * @param id the id
     */
    public void setId(String id) {
        this.id = id;
    }
    
    /**
     * Überprüft, ob es sich beim übergebenen Variablennamen
     * um eine spezielle Variable wie "category_id" ...handelt.
     * @param varName Name der Variable
     * @return true, falls der Variablenname auf eines der SpezialSuffixe
     * endet
     */
    public static boolean isSpecialVar(String varName) {
        for (Iterator iter = specialVarSuffixes.iterator(); iter.hasNext();) {
            String suffix = (String) iter.next();
            
            if (varName.toLowerCase().endsWith(suffix)) {
                return true;
            }
        }
        
        return false;
    }

    public boolean isShowDataTypes() {
        return Integer.parseInt(WebShare.getSysParameterValue("WORKFLOW", "ShowDatatype", "1")) != 0 ? true : false;
    }
}