Package appl.Portal.DB

Source Code of appl.Portal.DB.AttributedTable

/*
*  This software and supporting documentation were developed by
*
*    Siemens Corporate Technology
*    Competence Center Knowledge Management and Business Transformation
*    D-81730 Munich, Germany
*
*    Authors (representing a really great team ;-) )
*            Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
*  This software is Open Source under GNU General Public License (GPL).
*  Read the text of this license in LICENSE.TXT
*  or look at www.opensource.org/licenses/
*
*  Once more we emphasize, that:
*  THIS SOFTWARE IS MADE AVAILABLE,  AS IS,  WITHOUT ANY WARRANTY
*  REGARDING  THE  SOFTWARE,  ITS  PERFORMANCE OR
*  FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
*  ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
*  PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/


// AttributedTable

// Package
package appl.Portal.DB;

// Imports

// This application/module packages

// Other application/module packages

// KFM packages
import KFM.Exceptions.KFM_SQLException;
import KFM.Exceptions.ProgrammerException;
import KFM.DB.*;

// Java packages
import java.sql.*;
import java.util.Hashtable;

/** THIS CLASS IS DEPRECATED AND BROKEN, but still in use in `Portal_ApplicationPage�.
*
* This class was intended to provide access to a DB table with generic attributes (inlined, dedicated,
* hashed). But the class was never finished. Unfinished as it is, it is in use in
* `Portal_ApplicationPage�.
*
* This will be refactored to `AttributedTableView�, a view on the attributes of one category.
*
* <P>Original documentation: AttributedTable implements the access to a DB table with generic attributes
* as specified in `CategoryDB/categorydb.html�.</P>
*
*
* <H2>Related classes</H2>
*
* <P>No cut&paste-source.</P>
*
* @author  ThH
* @version 0.1 (991012)
*/
public class AttributedTable
{

    // ************************************************************
    // Constants
    // ************************************************************

    /** Separator to construct DB table names. */
    public final String mSeparator = "_";

    /** Suffix of the DB Table with the hashed attributes. */
    public final String mHashedTableSuffix = "Attribute";

    // ************************************************************
    // Variables
    // ************************************************************

    //
    protected KFM_JdbcAdapter mDbA;

    /** Main DB table name, e.g. "Category" or "Link". */
    protected String mTableName;

    /** True iff arbitrary attributes may be stored, false iff only attributes listed in `mInlined�,
     *  `mDedicated� and `mHashed� may be stored.
     *
     * Code for `false� not implemented yet.
     */
    protected boolean mArbitraryAttributes = true;


    // ************************************************************
    // Methods
    // ************************************************************

    /**
     * @param aTableName  Table name.
     */
    public AttributedTable (
        KFM_JdbcAdapter aDbA,
        String aTableName)
    {
        mTableName = aTableName;
        mDbA = aDbA;
    }

    // contains (string) attribute.name -> (string) attribute.value
    private Hashtable mNameValues = new Hashtable();

    /** Loads all inline attributes with one query and stores them in a cache.
     *
     * @param aId ID to look for in the table
     * @param aAttributes array of attributes to load (may contain non-inline, they are ignored)
     */
    public Hashtable loadInlineAttributes(String aId, Attribute[] aAttributes)
        throws IllegalAttribute, KFM_SQLException
    {
        mNameValues.clear();

        // build querystring with all named "inlined" columns, use id as key.
        StringBuffer tSQL = new StringBuffer("SELECT ");
        int n = 0;
        for(int i = 0; i < aAttributes.length; i++) {
            if(aAttributes[i].isInlined()) {
                if(n > 0) { tSQL.append(", "); }
                tSQL.append(aAttributes[i].getId());
                n++;
            }
        }
        tSQL.append(" FROM " + mTableName + " WHERE ID = ?");

        int tCmd = 0;
        try {
            tCmd = mDbA.prepareStmt(tSQL.toString());
            mDbA.setValue(tCmd, 1, aId);

            // clear all attributes from previous queries
            ResultSet tRs = mDbA.executeQuery(tCmd);
            if(tRs.next()) {
                // build new hashtable with all "inlined" attributes
                for(int i = 0; i < aAttributes.length; i++) {
                    if(aAttributes[i].isInlined()) {
                        String tValue = convertResult(tRs.getString(aAttributes[i].getId()));
                        mNameValues.put(aAttributes[i].getId(), tValue);
                    }
                }
            }
        } catch(SQLException e) {
            throw new KFM_SQLException(e);
        } finally {
            try {
                mDbA.closeStmt(tCmd);
            } catch(SQLException e) {
                throw new KFM_SQLException(e);
            }
        }

        return mNameValues;
    }

    /** retrieve an inline attribute previously loaded to the cache.
     * @param aId passed through to getAttributeString
     * @param aAttribute return the value of this attribute
     */
    public String getCachedInlineAttribute(String aId, Attribute aAttribute)
        throws IllegalAttribute, KFM_SQLException
    {
        // if attribute is inline and available in hash, then return it
        if(aAttribute.isInlined() || mNameValues.contains(aAttribute.getId())) {
            return (String) mNameValues.get(aAttribute.getId());
        } else { // otherwise use default handling
            return getAttributeString(aId, aAttribute);
        }
    }

    /** Get one string attribute of a record.
     *
     * BROKEN, but still in use in `Portal_ApplicationPage� (where it works due to sheer luck).
     *
     * @param aId         Record ID.
     * @param aAttribute  Name of attribute.
     * @return            Value of attribute.
     * @exception IllegalAttribute
     */
    public String getAttributeString (
        String aId,
        Attribute aAttribute)
        throws IllegalAttribute, KFM_SQLException
    {
        try {
            String tAttributeName = aAttribute.getId();

            int tCmd;
            if(aAttribute.isInlined()) {
                // * Get inlined attribute.
                // Either row name or table name may not be "?".
                tCmd = mDbA.prepareStmt("SELECT " + tAttributeName
                    + " FROM " + mTableName
                    + " WHERE ID = ?");
                mDbA.setValue(tCmd, 1, aId);
            } else if(aAttribute.isDedicated()) {
                // * Get dedicated attribute.
                // Either row name or table name may not be "?".
                StringBuffer tSb = new StringBuffer();
                tSb.append("SELECT ");
                tSb.append("Value");
                if (mTableName.equals("Link"))
                {
                    tSb.append(", Value2, Value3");
                }
                tSb.append(" FROM ");
                tSb.append(mTableName);
                tSb.append(mSeparator);
                tSb.append(tAttributeName);
                tSb.append(" WHERE Link_ID = ?");// @@@ BUGGY! Only works for Links!
                                            // Currently never called for Categories.
                tCmd = mDbA.prepareStmt(tSb.toString());
                mDbA.setValue(tCmd, 1, aId);
            } else {
                if(! mArbitraryAttributes && ! aAttribute.isHashed()) {
                    throw new IllegalAttribute(aAttribute);
                }
                // * Get hashed attribute.
                // Either row name or table name may not be "?".
                StringBuffer tSb = new StringBuffer();
                tSb.append("SELECT ");
                tSb.append("Value");
                tSb.append(" FROM ");
                tSb.append(mTableName);
                tSb.append(mSeparator);
                tSb.append(mHashedTableSuffix);
                tSb.append(" WHERE Link_ID = ? AND Name = ?");// @@@ BUGGY! Only works for Links!
                                                        // Currently never called for Categories.
                tCmd = mDbA.prepareStmt(tSb.toString());
                mDbA.setValue(tCmd, 1, aId);
                mDbA.setValue(tCmd, 2, tAttributeName);
            }

            ResultSet tRs = mDbA.executeQuery(tCmd);
            if(tRs.next()) {
                String tRet = tRs.getString(1);
                // It's not necessary to convert null->"" here, because
                // it is done below. That will also work correctly
                // for "Link_Content".
                if ((mTableName.equals("Link")) &&  (aAttribute.isDedicated())){
                    //in this case the table Link_Content has also the columns Value2 and Value3
                    String tValuePart = tRs.getString("Value2"); //fetch the value of Value2
                    tValuePart = convertResult(tValuePart);
                    if ((null == tValuePart)||(tValuePart.trim().equalsIgnoreCase("NULL")))
                        tValuePart = "";
                    if (!tValuePart.equals("")){ //if it has content
                        tRet += tValuePart;
                        tValuePart = tRs.getString("Value3"); //fetch the value of Value3
                        if ((null == tValuePart)||(tValuePart.trim().equalsIgnoreCase("NULL")))
                            tValuePart = "";
                        if (!tValuePart.equals("")){//if it has content
                            tRet += tValuePart;
                        }
                    }

                }
                mDbA.closeStmt(tCmd);
                tRet = convertResult(tRet);
                return tRet;
            } else {
                mDbA.closeStmt(tCmd);

                if(aAttribute.isRequired()) {
                    throw new ProgrammerException("AttributedTable::getAttributeString: "
                        + "No attribute '" + tAttributeName
                        + "' for id '" + aId
                        + "' in tables '" + mTableName + "*'.");
                } else {
                    // @@@ Maybe `null�?
                    return "";
                }
            }
        } catch(SQLException e) {
            throw new KFM_SQLException(e);
        }
    }

    /**
    * Converts a db null result to a null string or trims a result string
    */
    private String convertResult (
        String aResult)
    {
        String tResult = aResult;
        if ((null == tResult)||(tResult.trim().equalsIgnoreCase("NULL")))
            tResult = "";
        else tResult = tResult.trim();
        return tResult;
    }

    /** Set one attribute of a record.
     *
     * NOT FINISHED, and never used.
     *
     * @param aId         Record ID.
     * @param aAttribute  Name of attribute.
     * @param aValue      Value of attribute.
     * @exception IllegalAttribute
     */
    public void setAttribute (
        String aId,
        Attribute aAttribute,
        String aValue)
        throws IllegalAttribute
    {
        if(aAttribute.isInlined()) {
            // * Set inlined attribute.
        } else if(aAttribute.isDedicated()) {
            // * Set dedicated attribute.
        } else {
            if(! mArbitraryAttributes && ! aAttribute.isHashed()) {
                throw new IllegalAttribute(aAttribute);
            }
            // * Set hashed attribute.
        }
    }
}
TOP

Related Classes of appl.Portal.DB.AttributedTable

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.