OpenLSD ENOpenLSD

Download at SourceForge.net

ConceptLogic Technical Aspects Future Plan Howto Benchmarks
Home OpenLSD OpenLSM OpenR66

HowTo Use OpenLSD Framework-Data

In this story example, You will find the database step, then the Data structure step, the Functions step, the Web step, the Jar Implementation Step and the final step.

 

 

Data Structure Step

First we have to modify the Business implementation to fit our needs. To do that, we create a copy of BusinessImpl directory in Example and continue to implement it. For the safety of example, I rename database.business in database.newbusiness in the Example directory in the OpenLSD distribution.

In Blue, you will find what was modified, in Black what is still the same.

I create also a new Eclipse project named OpenLSDImpl which will include the same classes but in database.business container and an associate OpenLSD-ImplExample.jar which must substitute the default simple OpenLSD-Impl.jar.

 

In Blue, you will find what was modified, in Black what is still the same. You have two parts:

 

 1) Definition part:

  • First you define new data (long idbunit, String idowner, …),

  • then the database column names (in fieldsext),

  • then the accessor (int ridb, int ridbunit, …) that allow to access the correct column name from fieldsext,

  • then some default SQL code as the allFieldsExt (select request based on “the business where condition” so excluding the business index),

  • the getWhereClause (to be append to the Legacy where condition with the business index),

  • the getFieldsSetExt (update values from a “business where condition”),

  • the getFieldsSetAllExt (update values from a “standard where condition”, i.e. including business index in the update),

  • the getFieldsSetExtNULL (update to NULL values for every fields),

  • the doc_hintBusiness() (hint - for Oracle - to place in SELECT "hint" 1 FROM LSDDOCUMENT WHERE getWhereClauseBusiness())

 2) Methods part:

  • Two constructors: empty constructor, from another LSDBusiness object (clone), from an array of integers and an array of Strings which enables a standard constructor from any part of the value using a switch-case construction and cast to the correct type. Take care about false cast!

  • Getter from SQL select based on all fields (getFromResultSet)

  • A Setter for Insert command for all fields (getFieldsValues)

  • Add on PreparedStatement for the “business where condition” from getWhereClause  (setToPrepStmtGetWhereClause)

  • Add on PreparedStatement for the update of values except Business index from getFieldsSetExt (setToPrepStmtGetFieldsSetExt)

  • Add on PreparedStatement for the update of values  from getFieldsSetAllExt (setToPrepStmtGetFieldsSetAllExt)

  • clone, clear, equals and toString methods

  • setFromString and setFromStrings methods to fill in attributes from String or Strings (Delete, GetPaths, GetCopy and Import functions)

  • Add on toString and toCSV the new fields (toCSV is intend for database replication)

public class LSDBusinessImpl extends LSDBusiness {

/** Business index */

public String idb = null;

/** Business Unit */

public long idbunit = LSDConstants.invalide_idx;

/** Business Owner */

public String idowner = null;

/** Limit of time validity */

public Timestamp limitdate = null;

/** Extended fields for Business */

protected static final String[] fieldsext = {"IDMETIER","IDBUNIT","IDOWNER","LIMITDATE"};

/** Index for extended fields for Business */

public static final int ridb = 0;

/** Index for extended fields for Business */

public static final int ridbunit = 1;

/** Index for extended fields for Business */

public static final int ridowner = 2;

/** Index for extended fields for Business */

public static final int rlimitdate = 3;

/** Values to add to allfields (replace select *) */

public static final String allFieldsExt = ","+fieldsext[ridb]+","+fieldsext[ridbunit]+

       ","+fieldsext[ridowner]+","+fieldsext[rlimitdate]+" ";

/** Where condition in " condition " format which will be added

 * to " WHERE Legacy = ? AND condition " and that must returns 1 unique document.      */

public static String getWhereClause = fieldsext[ridb]+" = ? ";

/** Set for all fields except fields used in getWhereClause */

public static String getFieldsSetExt = ","+fieldsext[ridbunit]+

       " = ?,"+fieldsext[ridowner]+" = ?,"+fieldsext[rlimitdate]+" = ? ";

/** Set for all fields     */

public static String getFieldsSetAllExt = ","+fieldsext[ridb]+" = ?,"+fieldsext[ridbunit]+

       "= ?,"+fieldsext[ridowner]+"= ?,"+fieldsext[rlimitdate]+" = ? ";

/** Set values as NULL (or empty) including where clause */

public static String getFieldsSetExtNULL = ","+fieldsext[ridb]+" = NULL,"+fieldsext[ridbunit]+

"= "+LSDConstants.invalide_idx+","+fieldsext[ridowner]+"=NULL,"+

fieldsext[rlimitdate]+" = NULL ";

/** @see openlsd.database.business.LSDBusiness#getAllFieldsExt() */

public String getAllFieldsExt() {

       return allFieldsExt;

}

/** @see openlsd.database.business.LSDBusiness#getFieldsExt(int) */

public String getFieldsExt(int number) {

       return fieldsext[number];

}

/** @see openlsd.database.business.LSDBusiness#getFieldsSetAllExt() */

public String getFieldsSetAllExt() {

       return getFieldsSetAllExt;

}

/** @see openlsd.database.business.LSDBusiness#getFieldsSetExt()   */

public String getFieldsSetExt() {

       return getFieldsSetExt;

}

/** @see openlsd.database.business.LSDBusiness#getFieldsSetExtNULL() */

public String getFieldsSetExtNULL() {

       return getFieldsSetExtNULL;

}

/** @see openlsd.database.business.LSDBusiness#getWhereClause()    */

public String getWhereClause() {

       return getWhereClause;

}

/** @see openlsd.database.business.LSDBusiness#doc_hintBusiness()  */

public String doc_hintBusiness() {

       switch (LSDConstants.typeDriver) {

       case LSDConstants.typeMySQL:

             return " ";

       case LSDConstants.typeOracle:

             return " /*+ index(lsddocument IDX_DOC_LIDMETIERDATE)*/ ";

       case LSDConstants.typePostgresql:

             return " ";

       default:

             LSDConstants.logger.error("NO DRIVER CORRECTLY SPECIFIED:"+

                           LSDConstants.typeDriver);

             return " ";

       }

}

/** Default empty constructor */

public LSDBusinessImpl() {

       this.idb = null;

       this.idbunit = LSDConstants.invalide_idx;

       this.idowner = null;

       this.limitdate = null;

}

/** Constructor from data

 * @param lsdb      */

public LSDBusinessImpl(LSDBusiness lsdb) {

if (LSDBusiness.class.getClass() == LSDBusinessImpl.class.getClass())

             this.setFromLSDBusiness((LSDBusinessImpl) lsdb);

}

/** Constructor from 1 int[] for rank and 1 String[] for value

 * @param ranks

 * @param args      */

public LSDBusinessImpl(int []ranks, String []args) {

       this.setFromStringArray(ranks, args);

}

/** Constructor from data

 * @param lsdb      */

public LSDBusinessImpl(LSDBusinessImpl lsdb) {

       this.idb = lsdb.idb;

       this.idbunit = lsdb.idbunit;

       this.idowner = lsdb.idowner;

       this.limitdate = lsdb.limitdate;

}

/** Set from 1 int[] for rank and 1 String[] for value

 * @param ranks

 * @param args

 * @return True if OK, else False */

public boolean setFromStringArray(int []ranks, String []args) {

       boolean retour = false;

       if (ranks.length > args.length) {

             LSDConstants.logger.warn("During initialization of LSDBusinessImpl: not correct ranks and args:"+ranks.length+">"+args.length);

             return false;

       }

       int i, rank;

       String arg;

       for (i = 0; i < ranks.length; i++) {

             arg = args[i];

             rank = ranks[i];

             switch (rank) {

                    case ridb:

                           this.idb = arg;

                           retour = true;

                           break;

                    case ridbunit:

                           try  {

                                  this.idbunit = Long.parseLong(arg);

                           } catch (NumberFormatException e) {

                                  LSDConstants.logger.fatal("Bad Long:"+arg+", revert to -1");

                                  this.idbunit = LSDConstants.invalide_idx;

                           }

                           retour = true;

                           break;

                    case ridowner:

                           this.idowner = arg;

                           retour = true;

                           break;

                    case rlimitdate:

                           // Replace ; as separator of date and time by blank character

                           arg = arg.replace(';', ' ');

                           try {

                                  this.limitdate = Timestamp.valueOf(arg);

                           } catch (IllegalArgumentException e) {

                                  LSDConstants.logger.fatal("Bad Timestamp:"+arg+", revert to NULL");

                                  this.limitdate = null;

                           }

                           retour = true;

                           break;

                    default:

                          LSDConstants.logger.warn("During initialization of LSDBusinessImpl: Unknown index "

                                  +rank+" for value "+arg);

                           break;

             }

       }

       return retour;

}

/** @see openlsd.database.business.LSDBusiness#equals(openlsd.database.business.LSDBusiness)

        */

public boolean equals(LSDBusiness other) {

       if (LSDBusiness.class.getClass() == LSDBusinessImpl.class.getClass())

             return this.equals((LSDBusinessImpl) other);

       return false;

}

/** @see openlsd.database.business.LSDBusiness#setFromLSDBusiness(openlsd.database.business.LSDBusiness)       */

public boolean setFromLSDBusiness(LSDBusiness lsdb) {

       if (LSDBusiness.class.getClass() == LSDBusinessImpl.class.getClass())

             return setFromLSDBusiness((LSDBusinessImpl) lsdb);

       return false;

}

/** Set internal value from ResultSet

 * @param resultSet

 * @return True if OK, else False */

public boolean getFromResultSet(ResultSet resultSet) {

       try {

             this.idb = resultSet.getString(fieldsext[ridb]);

             this.idbunit = resultSet.getLong(fieldsext[ridbunit]);

             this.idowner = resultSet.getString(fieldsext[ridowner]);

             this.limitdate = resultSet.getTimestamp(fieldsext[rlimitdate]);

             return true;

       } catch (SQLException e) {

             LSDConstants.logger.error("SQL Exception LSDExtDbDocument");

             if (LSDConstants.LOGGING) {

                    LSDDbSession.error(e);

             }

             return false;

       }

}

/** Set Values for Insert command

 * @return the String for Insert command Value part

 */

public String getFieldsValues() {

       String retour;

       if (idb == null) {

             retour = ",NULL,"+idbunit+",";

       } else {

             retour = ",'"+idb+"',"+idbunit+",";

       }

       if (idowner == null) {

             retour = retour+"NULL,";

       } else {

             retour = retour+"'"+idowner+"',";

       }

       if (limitdate == null) {

             return retour+"NULL) ";

       } else {

             return retour+"timestamp '"+limitdate+"') ";

       }

}

/** Set the value into the prepareStatement from Object for the Where Condition in getWhereClause

 * @param startrank The value to start from in the preparedStatement.setXXX call

 * @param preparedStatement

 * @return the next startrank or 0 if KO */

public int setToPrepStmtGetWhereClause(int startrank, LSDDbPreparedStatement preparedStatement) {

       try {

             preparedStatement.setString(startrank, idb);

       } catch (SQLException e) {

             LSDConstants.logger.error("Bad Prep Statement");

             preparedStatement.realClose();

             return 0;

       }

       return (startrank+1);

}

/** Set the value into the prepareStatement from Object for the set in GetFieldsSetExt

 * @param startrank The value to start from in the preparedStatement.setXXX call

 * @param preparedStatement

 * @return the next startrank or 0 if KO */

public int setToPrepStmtGetFieldsSetExt(int startrank, LSDDbPreparedStatement preparedStatement) {

       try {

             preparedStatement.setLong(startrank, idbunit);

             preparedStatement.setString(startrank+1, idowner);

             preparedStatement.setTimestamp(startrank+2, limitdate);

       } catch (SQLException e) {

             LSDConstants.logger.error("Bad Prep Statement");

             preparedStatement.realClose();

             return 0;

       }

       return (startrank+3);

}

/** Set the value into the prepareStatement from Object for the set in GetFieldsSetAllExt

 * @param startrank The value to start from in the preparedStatement.setXXX call

 * @param preparedStatement

 * @return the next startrank or 0 if KO */

public int setToPrepStmtGetFieldsSetAllExt(int startrank, LSDDbPreparedStatement preparedStatement) {

       try {

             preparedStatement.setString(startrank, idb);

             preparedStatement.setLong(startrank+1, idbunit);

             preparedStatement.setString(startrank+2, idowner);

             preparedStatement.setTimestamp(startrank+3, limitdate);

       } catch (SQLException e) {

             LSDConstants.logger.error("Bad Prep Statement");

             preparedStatement.realClose();

             return 0;

       }

       return (startrank+4);

}

/** Clear all data in the object */

public void clear() {

       this.idb = null;

       this.idbunit = LSDConstants.invalide_idx;

       this.idowner = null;

       this.limitdate = null;

}

/** Clone the current object but only copy values

 * @return The new object as a clone */

public LSDBusiness clone() {

       LSDBusinessImpl newbusiness = new LSDBusinessImpl(this);

       return newbusiness;

}

/**

 * @param lsdb

 * @return True if OK, else False

 * @see openlsd.database.business.LSDBusiness#setFromLSDBusiness(openlsd.database.business.LSDBusiness) */

public boolean setFromLSDBusiness(LSDBusinessImpl lsdb) {

       this.idb = lsdb.idb;

       this.idbunit = lsdb.idbunit;

       this.idowner = lsdb.idowner;

       this.limitdate = lsdb.limitdate;

       return true;

}

/** Comparison of two LSDBusinessImpl object on primary key (Business Index)

 * @param other

 * @return True if Business index are the same, else False */

public boolean equals(LSDBusinessImpl other) {

       return this.idb.equals(other.idb);

}

/** Object to String

 * @return the string that displays this object

 * @see java.lang.Object#toString() */

public String toString() {

       return " IDM:"+idb+";BUnit:"+idbunit+";Owner:"+idowner+";Lim:"+limitdate.toString();

}

/** (non-Javadoc)

 * @see openlsd.database.business.LSDBusiness#setFromStringForDelete(java.lang.String)

 */

public void setFromString(String line) {

int []ranks = {LSDBusinessImpl.ridb };

       String []values = {line};

       this.setFromStringArray(ranks,values);

}

/** (non-Javadoc)

 * @see openlsd.database.business.LSDBusiness#setFromStringForImport(int, java.lang.String[])

 */

public int setFromStrings(int curargs, String[] args) {

       int []ranks = {LSDBusinessImpl.ridb,LSDBusinessImpl.ridbunit,LSDBusinessImpl.ridowner,

LSDBusinessImpl.rlimitdate};

       String []newargs = {args[curargs],args[curargs+1],args[curargs+2],args[curargs+3]};

       this.setFromStringArray(ranks, newargs);

       return (curargs+4);

}

/** (non-Javadoc)

 * @see openlsd.database.business.LSDBusiness#toCSV()

 */

@Override

public String toCSV() {

       return idb+";"+idbunit+";"+idowner+";"+limitdate.getTime();

}

}