Package com.sos.JSHelper.Options

Source Code of com.sos.JSHelper.Options.SOSOptionElement

/********************************************************* begin of preamble
**
** Copyright (C) 2003-2010 Software- und Organisations-Service GmbH.
** All rights reserved.
**
** This file may be used under the terms of either the
**
**   GNU General Public License version 2.0 (GPL)
**
**   as published by the Free Software Foundation
**   http://www.gnu.org/licenses/gpl-2.0.txt and appearing in the file
**   LICENSE.GPL included in the packaging of this file.
**
** or the
** 
**   Agreement for Purchase and Licensing
**
**   as offered by Software- und Organisations-Service GmbH
**   in the respective terms of supply that ship with this file.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
********************************************************** end of preamble*/
package com.sos.JSHelper.Options;

/**
* \class JSDataElement
*
* \brief JSDataElement - Basisklasse f�r die Datenelement-Klassen des Dataswitch
*
* \details
* Basisklasse (SuperKlasse) f�r die Datenelement-Klassen des Dataswitch
*
* \section JSOptionClass_intro_sec Introduction
*
* \section JSOptionClass_samples Some Samples
*
* \code
*   .... code goes here ...
* \endcode
*
* <p style="text-align:center">
* <br />---------------------------------------------------------------------------
* <br /> APL/Software GmbH - Berlin
* <br />##### generated by ClaviusXPress (http://www.sos-berlin.com) #########
* <br />Sonntag, 7. Dezember 2008, eqbfd (eqbfd)
* <br />---------------------------------------------------------------------------
* </p>
* \author eqbfd
* @version $Id: SOSOptionElement.java 14729 2011-07-05 20:47:16Z sos $0.9
*
*/

import java.io.Serializable;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.sos.JSHelper.Exceptions.JSExceptionMandatoryOptionMissing;
import com.sos.JSHelper.io.Files.JSXMLFile;

public class SOSOptionElement extends com.sos.JSHelper.Basics.JSToolBox implements Serializable {

  /**
   *
   */
  private static final long  serialVersionUID    = -7652466722187678671L;
  private static final String  conClassName      = "JSOptionElement";
  protected static Logger    logger          = Logger.getLogger(SOSOptionElement.class);

  public static boolean    gflgProcessHashMap    = true;

  protected String      strKey          = "";
  protected String      strValue        = "";
  private String        strDefaultValue      = "";

  protected boolean      flgValue        = false;
  protected JSOptionsClass  objOptions        = null;
  protected Vector<String>  objAliase        = new Vector<String>();
  protected static final int  isOptionTypeString    = 0;
  protected static final int  isOptionTypeBoolean    = 1;
  protected static final int  isOptionTypeFileName  = 2;
  protected static final int  isOptionTypeInteger    = 3;
  public static final int    isOptionTypeOptions    = 4;
  protected static final int  isOptionTypeFolder    = 5;

  protected int        intOptionType      = 0;                                // 0 = String, 1
                                                              // = boolean, 2
                                                              // = File .....
  private boolean        flgIsMandatory      = false;

  private String        strDescription      = "";

  private int          intSize          = 0;                                // Size in Bytes

  private String        strTitle        = "";
  private String        strColumnHeader      = "";

  private String        strXMLTagName      = "";
  private String        strFormatString      = "";                                // geh�rt in
                                                              // Integer,
                                                              // Double, etc
                                                              // Datenelement

  protected JSOptionsClass  objParentClass      = null;

  protected final String    conNullButMandatory    = "%3$s : Setting %1$s (%2$s) is mandatory, must be not null.%n";
  protected final String    conChangedMsg      = "%3$s: changed from '%1$s' to '%2$s'.";

  /**
   * \todo Werte(bereichs)pr�fung mit dem regul�ren Ausdruck einbauen
   */

  @SuppressWarnings("unused")
  private final String    strRegExpr4Check    = "";
  protected boolean      isCData          = false;

  /**
   *
   * \brief toString
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public String toString() {
    return this.strKey + " (" + this.strDescription + "): " + this.strValue;
  }

  public SOSOptionElement(final JSOptionsClass pobjParent, final String pstrKey, final String pstrDescription, final String pstrValue,
      final String pstrDefaultValue, final boolean pflgIsMandatory) {

    try {
      this.objParentClass = pobjParent;
      if (objParentClass != null) {
        this.registerMessageListener(this.objParentClass.Listener());
      }

      this.strKey = pstrKey;
      // System.out.println("strKey in super " + strKey);
      this.strDefaultValue = pstrDefaultValue;
      this.Description(pstrDescription);
      this.strTitle = pstrDescription;
      this.flgIsMandatory = pflgIsMandatory;
      this.strXMLTagName = pstrKey;
      this.strColumnHeader = pstrKey;
      this.Value(pstrValue);

    } // try
    catch (final Exception objException) {
      objException.printStackTrace();
    }
    finally {
      //
    } // finally
  }

  /**
   *
   * \brief ColumnHeader
   *
   * \details
   *
   * \return JSOptionClass
   *
   * @param pstrColumnHeader
   * @return
   */
  public SOSOptionElement ColumnHeader(final String pstrColumnHeader) {
    if (pstrColumnHeader != null) {
      this.strColumnHeader = pstrColumnHeader;
    }
    return this;
  }

  /**
   *
   * \brief ColumnHeader
   *
   * \details
   *
   * \return String
   *
   * @return
   */
  public String ColumnHeader() {
    if (this.strColumnHeader == null) {
      this.strColumnHeader = "";
    }

    return this.strColumnHeader;
  }

  /**
   * \brief Title - Titel des Datenelements festlegen
   *
   * \details
   *
   * @param pstrTitle
   * @return
   */
  public SOSOptionElement Title(final String pstrTitle) {
    if (pstrTitle != null) {
      this.strTitle = pstrTitle;
    }
    return this;
  }

  /**
   * \brief Title - Titel des Datenelements liefern
   *
   * \details
   *
   * @param pstrTitle
   * @return
   */
  public String Title() {
    if (this.strTitle == null) {
      this.strTitle = "";
    }

    return this.strTitle;
  }

  /**
   *
   * \brief DefaultValue
   *
   * \details
   *
   * \return String
   *
   * @return
   */
  public String DefaultValue() {

    @SuppressWarnings("unused")
    final String conMethodName = SOSOptionElement.conClassName + "::DefaultValue";

    return this.strDefaultValue;
  } // public String DefaultValue

  public String setPrefix(final String strPrefix) {
    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::setPrefix";

    String strT = strKey;
    int i = strT.indexOf(".");
    if (i > 0) {
      strT = strT.replaceFirst("\\.", "." + strPrefix);
    }
    else {
      strT = strPrefix + strT;
    }
    strKey = strT;

    return strT;
  }

  /**
   *
   * \brief getKey
   *
   * \details
   *
   * \return String
   */
  public String getKey() {

    @SuppressWarnings("unused")
    final String conMethodName = SOSOptionElement.conClassName + "::getKey";

    return this.strKey;

  } // private String getKey

  /**
   * \brief Value - Wert des Datenelements festlegen
   *
   * \details
   *
   * @param pstrValue
   * @return
   * @throws Exception
   */
  public void Value(final String pstrValue) {
    if (this.flgIsMandatory) {
      if (pstrValue == null) {
        if (gflgProcessHashMap == false) {
          this.SignalError(String.format("%1$s (%2$s) %3$s", this.strTitle, this.strKey, this.conNullButMandatory));
        }
        else {
          return; // to avoid to overwrite a previous assigned value
        }
      }
    }

    if (pstrValue != null) {

      /**
       * \todo �ber den regexp pr�fen, ob die Werte syntaktisch korrekt sind
       */

      if (objParentClass != null) {
        final String strTemp = this.objParentClass.SubstituteVariables(pstrValue);
        this.SignalDebug(String.format(this.conChangedMsg, this.strValue, strTemp, this.strTitle));
      }

      this.strValue = pstrValue;
    }
    else {
      this.strValue = "";
    }
    // logger.debug(conClassName + ", key = " + strKey + ", value = " + strValue);
  }

  /**
   * \brief Value - Wert der Option liefern
   *
   * \details
   *
   * @param pstrValue
   * @return
   */
  public String Value() throws RuntimeException {
    if (this.strValue == null) {
      this.strValue = "";
    }

    return this.strValue;
  }

  /**
   * \brief Description - Beschreibung des Datenelements festlegen
   *
   * \details
   *
   * @param pstrDescription
   * @return
   */
  public SOSOptionElement Description(final String pstrDescription) {
    if (pstrDescription != null) {
      this.strDescription = pstrDescription;
    }
    return this;
  }

  /**
   * \brief Description - Beschreibung des Datenelements liefern
   *
   * \details
   *
   * @param pstrDescription
   * @return String
   */
  public String Description() {
    if (this.strDescription == null) {
      this.strDescription = "";
    }

    return this.strDescription;
  }

  /**
   * \brief Size - Gr��e des Datenelements festlegen
   *
   * \details
   *
   * @param pintSize
   * @return
   */
  public SOSOptionElement Size(final int pintSize) {
    this.intSize = pintSize;
    return this;
  }

  public SOSOptionElement Size(final Integer pintSize) {
    this.intSize = pintSize;
    return this;
  }

  /**
   * \brief Size - Gr��e des Datenelements liefern
   *
   * \details
   *
   * @param pintSize
   * @return String
   */
  public int Size() {
    return this.intSize;
  }

  public Integer ISize() {
    return new Integer(this.intSize);
  }

  /**
   * \brief XMLTagName - XML-TagName des Datenelements festlegen
   *
   * \details
   *
   * @param pstrXMLTagName
   * @return
   */
  public SOSOptionElement XMLTagName(final String pstrXMLTagName) {
    if (pstrXMLTagName != null) {
      this.strXMLTagName = pstrXMLTagName;
    }
    return this;
  }

  /**
   * \brief XMLTagName - XML-TagName der Option liefern
   *
   * \details
   *
   * @param pstrXMLTagName
   * @return String
   */
  public String XMLTagName() {
    if (this.strXMLTagName == null) {
      this.strXMLTagName = "";
    }

    return this.strXMLTagName;
  }

  /**
   * \brief toXml - XML-Tag mit dem Wert der Option liefern
   *
   * \details
   *
   * @return String - den Wert der Option im XML-Tag
   * @throws Exception
   */
  public String toXml() throws Exception {
    String strT = "<" + this.XMLTagName();
    strT += " mandatory=" + QuotedValue(boolean2String(flgIsMandatory));
    if (isNotEmpty(strDefaultValue)) {
      strT += " default=" + QuotedValue(strDefaultValue);
    }
    if (isNotEmpty(strTitle)) {
      strT += " title=" + QuotedValue(strTitle);
    }
    strT += ">";
    if (this.Value().length() > 0) {

      if (this.isCData) {
        strT += "<![CDATA[" + this.FormattedValue() + "]]>";
      }
      else {
        strT += this.Value();
      }
    }
    strT += "</" + this.XMLTagName() + ">";

    return strT;
  }

  /**
   *
   * \brief toXml
   *
   * \details
   *
   * \return void
   *
   * @param pobjXMLFile
   * @throws Exception
   */
  public void toXml(final JSXMLFile pobjXMLFile) throws Exception {
    pobjXMLFile.WriteLine(this.toXml());
  }

  /**
   *
   * \brief FormatString
   *
   * \details
   *
   * \return void
   *
   * @param pstrFormatString
   */
  public void FormatString(final String pstrFormatString) {
    if (pstrFormatString == null) {
      this.strFormatString = "";
    }
    else {
      this.strFormatString = pstrFormatString;
    }
  }

  /**
   *
   * \brief FormatString - liefert den f�r die Option definierten FormatString
   *
   * \details
   *
   * \return String
   *
   * @return
   */
  public String FormatString() {
    if (this.strFormatString == null) {
      this.strFormatString = "";
    }

    return this.strFormatString;
  }

  protected void doInit() {
    //

  }

  /**
   *
   * \brief FormattedValue - Liefert den Wert der Option formatiert
   *
   * \details
   * das Format (die Edit-Maske) wird �ber die Eigenschaft FormatString
   * definiert.
   *
   * Wenn kein Format-String definiert ist, so wird der Wert als String
   * zur�ckgegeben.
   *
   * \return String
   *
   * @return
   * @throws Exception
   */
  public String FormattedValue() throws Exception {
    // nur �berschreibbar

    return this.Value();
  }

  /**
   *
   * \brief IsEmpty
   *
   * \details
   * Ist Value-String leer, dann liefert die Methode true
   *
   * \return boolean
   *
   * @return
   * @throws Exception
   */
  public boolean IsEmpty() throws RuntimeException {
    return this.Value().trim().length() == 0;
  }

  public boolean IsNotEmpty() throws RuntimeException {
    return !this.IsEmpty();
  }

  /**
   *
   * \brief MapValue - Wert der Option aus der HashTable �bernehmen
   *
   * \details
   * Mit dieser Methode wird aus der HashTable f�r den f�r diese Option
   * definierten Key der eingestellte Wert �bernommen.
   *
   * Ist der gesuchte Key, und damit der Wert, nicht in der HashTable,
   * so wird der Default-Wert zugewiesen.
   * \return void
   *
   * @throws Exception
   */
  public void MapValue() {

    @SuppressWarnings("unused")
    final String conMethodName = SOSOptionElement.conClassName + "::MapValue";

    /**
     * \todo Wenn abgeleitete Klassen in der Value()-Methode pr�fungen auf g�ltige Werte haben
     * und ein "leerer" String ein ung�ltiger Wert ist, dann wird dieser ung�ltige Wert nicht erkannt.
     */
    if (this.isEmpty(this.strKey) == false) {
      String strV = this.objParentClass.getItem(this.strKey, null);
      if (strV == null) {
        for (String strAlias : objAliase) {
          strV = this.objParentClass.getItem(strAlias, null);
          if (strV != null) {
            break;
          }
        }
      }
      /*
       * // TODO check for global prefix
       */

      if (strV == null) {
        strV = this.strDefaultValue;
      }

      this.Value(strV);
      // - <remark who='EQALS' when='Dienstag, 6. Oktober 2009' id='PublishSQLStatement' >
      /**
       * TODO Werte-Trenner als Eigenschaft des JSOptionElements statt konstant
       * \todo Werte-Trenner als Eigenschaft des JSOptionElements statt konstant
       */
      // - <newcode>
      if (this.intOptionType == isOptionTypeOptions) {
        this.Value(this.objParentClass.getIndexedItem(this.strKey, this.Description(), ";"));
      }
      // - </newcode>
      // - </remark> <!-- id=<PublishSQLStatement> -->
    }
  } // public void MapValue

  /**
  * \brief CheckMandatory - pr�ft ob eine Option tats�chlich gef�llt ist
  *
  * \details
  *
  * @throws Exception - wird ausgel�st, wenn eine mandatory-Option keinen Wert hat
  */
  public void CheckMandatory() {
    try {
      if (this.flgIsMandatory) {
        if (this.isEmpty(this.strValue)) {
          this.SignalError(String.format("%1$s (%2$s) %3$s", this.strDescription, this.strKey, this.conNullButMandatory));
        }
      }
    }
    catch (final Exception e) {
      throw new JSExceptionMandatoryOptionMissing(e.toString());
    }
  } // public void CheckMandatory ()

  /**
   *
   * \brief toOut
   *
   * \details
   *
   * \return String
   *
   * @return
   * @throws Exception
   */
  public String toOut() throws Exception {

    @SuppressWarnings("unused")
    final String conMethodName = SOSOptionElement.conClassName + "::toOut";
    String strT = "";

    strT = String.format("%1$s %2$s: %3$s \n", this.strTitle, this.strDescription, this.Value());

    return strT;
  } // public String toOut}

  public int OptionType() {
    return intOptionType;
  }

  /**
   *
   * \brief OptionType
   *
   * \details
   *
   * \return void
   *
   * @param pintOptionType
   */
  public void OptionType(int pintOptionType) {
    this.intOptionType = pintOptionType;
  }

  /**
   *
   * \brief isMandatory
   *
   * \details
   *
   * \return void
   *
   * @param pflgIsMandatory
   */
  public void isMandatory(final boolean pflgIsMandatory) {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::isMandatory";

    this.flgIsMandatory = pflgIsMandatory;

  } // private void isMandatory

  public boolean isMandatory() {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::isMandatory";

    return this.flgIsMandatory;
  } // private boolean isMandatory

  public SOSOptionElement SetAlias(String pstrAliasKey) {
    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::setAlias";
    objAliase.add(pstrAliasKey);
    return this;
  } // private void setAlias

  /**
   *
   * \brief String to Boolean
   *
   * \details
   *
   * \return boolean
   *
   * @param pstrS
   */
  public boolean String2Bool() {
    boolean flgT = false;
    String pstrVal = this.strValue;
    if (isNotEmpty(pstrVal)) {
      if (pstrVal.equals("1") || pstrVal.equalsIgnoreCase("y") || pstrVal.equalsIgnoreCase("yes") || pstrVal.equalsIgnoreCase("j")
          || pstrVal.equalsIgnoreCase("on") || pstrVal.equalsIgnoreCase("true") || pstrVal.equalsIgnoreCase("wahr")) {
        flgT = true;
      }
    }

    return flgT;

  }

  /**
   *
   * \brief QuotedValue
   *
   * \details
   *
   * \return String
   *
   * @return
   */
  public String QuotedValue() {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::QuotedValue";

    return this.QuotedValue(this.strValue);
  } // private String QuotedValue

  /**
   *
   * \brief QuotedValue
   *
   * \details
   *
   * \return String
   *
   * @param pstrValue
   * @return
   */
  public String QuotedValue(String pstrValue) {

    @SuppressWarnings("unused")
    final String conMethodName = conClassName + "::QuotedValue";

    String strRet = "\"" + pstrValue.replaceAll("\"", "\"\"") + "\"";

    return strRet;
  } // private String QuotedValue

  /**
   *
   * \brief equalsIgnoreCase
   *
   * \details
   *
   * \return boolean
   *
   * @param strCompare
   * @return
   */
  public boolean equalsIgnoreCase(final String strCompare) {
    if (strValue == null) {
      return false;
    }
    return this.strValue.equalsIgnoreCase(strCompare);
  }
}
TOP

Related Classes of com.sos.JSHelper.Options.SOSOptionElement

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.