Package com.sos.JSHelper.Basics

Source Code of com.sos.JSHelper.Basics.JSToolBox

/********************************************************* 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.Basics;

import java.io.BufferedWriter;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

import com.sos.JSHelper.Exceptions.JobSchedulerException;
import com.sos.JSHelper.Listener.JSListenerClass;
import com.sos.localization.Messages;

/**
* \class JSToolBox
*
* \brief JSToolBox - Klasse mit kleinen Helferleins
*
* \details
* Diese Klasse ist immer als SuperKlasse zu verwenden und stellt eine Reihe von Methoden bereit, die immer wieder ben�tigt werden
*
* \section JSToolBox_intro_sec Introduction
*
* \section JSToolBox_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, 26. Oktober 2008, sgx2343 (sgx2343)
* <br />---------------------------------------------------------------------------
* </p>
* \author sgx2343
* @version $Id: JSToolBox.java 14731 2011-07-05 20:50:42Z sos $0.9
* \see reference
*
*/

public class JSToolBox extends JSListenerClass {
  @SuppressWarnings("hiding")
  private final String  conClassName      = "JSToolBox";
  protected final String  EMPTY_STRING      = "";
  BufferedWriter      objOut          = null;

  protected boolean    flgStackTracePrinted  = false;

  public JSToolBox() {
  } // public JSToolBox

  /**
   *
   * \brief MakeFullPathName
   *
   * \details
   *
   * \return String
   *
   * @param pstrPathname
   * @param pstrFileName
   * @return
   */
  protected String MakeFullPathName(String pstrPathname, String pstrFileName) {
    String strT = pstrFileName;

    if (pstrFileName.startsWith(pstrPathname)) {
      //
    }
    else {
      if (pstrPathname.endsWith("/")) {
        strT = pstrPathname + strT;
      }
      else {
        strT = pstrPathname + "/" + strT;
      }
    }

    return strT;
  }

  /**
   *
   * \brief getI18N
   *
   * \details
   *
   * \return String
   *
   * @param pstrI18NKey
   * @return
   */
  public String getI18N(final String pstrI18NKey) {
    String strM = Messages.getMsg(pstrI18NKey);
    strM = pstrI18NKey + ": " + strM;
    return strM;
  }

  /**
   *
   * \brief Bool2String
   *
   * \details
   *
   * \return String
   *
   * @param pflgFlag
   * @return
   */
  public String boolean2String(boolean pflgFlag) {

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

    String strRet = "true";
    if (pflgFlag == false) {
      strRet = "false";
    }

    return strRet;
  } // private String Bool2String

  /**
   *
   * \brief AddSingleQuotes
   *
   * \details
   *
   * \return String
   *
   * @param pstrS
   */
  public String AddSingleQuotes(final String pstrS) {

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

    final String strT = "'" + pstrS.replaceAll("'", "''") + "'";

    return strT;
  } // private String AddSingleQuotes

  /**
   *
   * \brief AddQuotes
   *
   * \details
   *
   * \return String
   *
   * @param pstrS
   * @return
   */
  protected String AddQuotes(final String pstrS) {

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

    return "\"" + Quotes2DoubleQuotes(pstrS) + "\"";

  } // private String AddQuotes

  /**
   *
   * \brief Quotes2DoubleQuotes
   *
   * \details
   *
   * \return String
   *
   * @param pstrS
   * @return
   */
  protected String Quotes2DoubleQuotes(final String pstrS) {

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

    final String strT = pstrS.replaceAll("\"", "\"\"");

    return strT;
  } // private String Quotes2DoubleQuotes

  /**
   *
   * \brief toDouble
   *
   * \details
   *
   * \return double
   *
   * @param pstrV
   * @return
   * @throws Exception
   */
  protected double toDouble(final String pstrV) throws Exception {
    final String conMethodName = conClassName + "::toDouble";
    double dblT = 0.0;
    String strT = pstrV.trim();
    final int intLen = strT.length();
    if (intLen > 0) {
      if (strT.endsWith("-")) {
        strT = "-" + strT.substring(0, intLen - 1);
      }
      else {
        if (strT.startsWith("+")) {
          strT = strT.substring(1, intLen);
        }
        else {
          if (strT.endsWith("+")) {
            strT = strT.substring(0, intLen - 1);
          }
        }
      }
      try {
        dblT = new Double(strT);
      } // try
      catch (final Exception objException) {
        try {
          // DecimalFormat decimalF = new DecimalFormat();
          // DecimalFormatSymbols objDFS = decimalF.getDecimalFormatSymbols();
          // objDFS.setDecimalSeparator('.');
          // objDFS.setGroupingSeparator(',');
          // System.out.println("DecimalSeparator = " + objDFS.getDecimalSeparator());
          // System.out.println("GroupingSeparator = " + objDFS.getGroupingSeparator());
          // numberF.setParseBigDecimal(true);
          final NumberFormat numberF = NumberFormat.getNumberInstance(Locale.GERMAN);
          numberF.setGroupingUsed(false);
          numberF.setParseIntegerOnly(false);
          numberF.setMaximumFractionDigits(5);
          numberF.setMinimumFractionDigits(0);
          final Number number = numberF.parse(strT);
          dblT = number instanceof Double ? number.doubleValue() : new Double(number.doubleValue());
        }
        catch (final ParseException e) {
          try {
            final NumberFormat numberF = NumberFormat.getNumberInstance(Locale.US);
            numberF.setGroupingUsed(false);
            numberF.setParseIntegerOnly(false);
            final Number number = numberF.parse(strT);
            dblT = number instanceof Double ? number.doubleValue() : new Double(number.doubleValue());
          }
          catch (final ParseException e1) {
            e1.printStackTrace();
            SignalError(conMethodName + ": could not convert '" + strT + "' to double");
          }
        }

        catch (final NumberFormatException e) {
          SignalError(conMethodName + ": could not convert '" + strT + "' to double");
          dblT = 0.0;
        }
      }
      finally {
        //
      } // finally
    }

    return dblT;
  }

  /**
   *
   * \brief CreationTimeStamp
   *
   * \details
   *
   * \return String
   *
   * @return
   */
  public String CreationTimeStamp() {
    return getISODate();
  }

  /**
   *
   * \brief CreationTimeStamp
   *
   * \details
   *
   * \return String
   *
   * @param pstrDate
   * @return
   * @throws Exception
   */
  public String CreationTimeStamp(final String pstrDate) throws Exception {
    return this.CreationTimeStamp(pstrDate, "010203");
  }

  /**
   * \brief Liefert einen Zeitstempel in der Form YYYY-MM-DDTHH:MM:SS
   *
   * F�r ein anzugebendes Datum und eine anzugebende Uhrzeit wird ein Zeitstempel im
   * ISO-Datumsformat geliefert.
   *
   * \return String - YYYY-MM-DDTHH:MM:SS
   *
   * @param pstrDate - Datum in der Form YYYYMMDD
   * @param pstrTime Zeit in der Form HHMMSS
   * @throws Exception
   */
  public String CreationTimeStamp(final String pstrDate, final String pstrTime) throws Exception {
    /*
     * pstrDate is YYYYMMDD
     */
    String strD = "";
    String strT = "";

    // ToDo: check for valid date and time values
    if (pstrDate.length() > 0 && pstrTime.length() > 0) {
      try {
        @SuppressWarnings("unused")
        long lngTemp = Integer.parseInt(pstrDate);
        lngTemp = Integer.parseInt(pstrTime);
        strD = pstrDate.substring(0, 3 + 1) + "-" + pstrDate.substring(4, 5 + 1) + "-" + pstrDate.substring(6, 7 + 1) + "T";
        strT = pstrTime.substring(0, 1 + 1) + ":" + pstrTime.substring(2, 3 + 1) + ":" + pstrTime.substring(4, 5 + 1);
      }
      catch (final RuntimeException e) {
        // nothing to do. Date and/or Time not valid?
        strD = pstrDate + "T";
        strT = "??:??:??";
      }
    }
    return strD + strT;
  }

  /**
   *
   * \brief getDateTimeFormatted
   *
   * \details
   *
   * \return
   *
   * @param pstrEditMask
   * @return
   */
  @Override
  public String getDateTimeFormatted(final String pstrEditMask) {
    final java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(pstrEditMask);
    final java.util.Calendar now = java.util.Calendar.getInstance();
    return formatter.format(now.getTime()).toString();
  }

  public Date Now() {

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

    final java.util.Calendar now = java.util.Calendar.getInstance();
    return now.getTime();
  } // public Date Now}

  /**
   *
   * \brief getISODate
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public String getISODate() {
    return getDateTimeFormatted("yyyy-MM-dd'T'HH:mm:ss");
  }

  /**
   *
   * \brief getTime
   *
   * \details
   *
   * \return
   *
   * @return
   */
  @Override
  public String getTime() {
    return getDateTimeFormatted("HH:mm:ss");
  }

  /**
   *
   * \brief getHHIISS
   *
   * \details
   *
   * \return String
   *
   * @return
   */
  public String getHHIISS() {
    return getDateTimeFormatted("HHmmss");
  }

  /**
   *
   * \brief getDate
   *
   * \details
   *
   * \return String
   *
   * @return
   */
  public String getDate() {
    return getDateTimeFormatted("yyyy-MM-dd");
  }

  /**
   *
   * \brief getYYYYMMDD
   *
   * \details
   *
   * \return String
   *
   * @return
   */
  public String getYYYYMMDD() {
    return getDateTimeFormatted("yyyyMMdd");
  }

  /**
   *
   * \brief Liefert den Wert einer Environment Variable
   *
   * \details
   *
   * \return String - der Wert der Variablen oder null
   *
   * @param pstrVariableName - Der Name der Variablen, deren Wert geliefert werden soll
   * @throws Exception
   */
  public String EnvironmentVariable(final String pstrVariableName) {

    final String conMethodName = conClassName + "::EnvironmentVariable";

    String strValue = null;

    if (isNotEmpty(pstrVariableName)) {
      strValue = System.getenv(pstrVariableName);
      if (isNotEmpty(strValue)) {
        SignalDebug(String.format("%s: %s = %s", conMethodName, pstrVariableName, strValue));
      }
      else {
        // SignalInfo(String.format("%s: '%s' is not assigned.", conMethodName, pstrVariableName));
      }
    }

    return strValue;
  }

  /**
   * \brief vereinfachter Stringvergleich
   *
   * \details
   * Liefert "true", wenn beide Strings einen unterschiedlichen Wert haben oder sich nicht
   * vergleichen lassen, weil null-pointer.
   *
   * Es wird immer mit equalsIgnoreCase verglichen.
   *
   * \return boolean true, wenn beide Strings einen unterschiedlichen Wert haben
   *
   * @param pstrActual
   * @param pstrNew
   */
  protected boolean isNotEqual(final String pstrActual, final String pstrNew) {
    Boolean flgT = false;
    if (pstrActual == null && pstrNew == null) {
      flgT = false;
    }
    else {
      if (pstrActual == null || !pstrActual.equalsIgnoreCase(pstrNew.toString())) {
        flgT = true;
      }
    }
    return flgT;
  }

  /**
   * @brief Helperfunktion - liefert true, wenn String weder null noch leer
   *
   * \details
   * StringObjekt auf null/empty pr�fen
   *
   * @param pstrValue zu pr�fendes Stringobjekt
   *
   * @return boolean true, wenn String sinnvollen Wert enth�lt.
   */

  protected boolean isNotEmpty(final String pstrValue) {
    return pstrValue != null && pstrValue.trim().length() > 0;
  }

  protected boolean isNotEmpty(final StringBuffer pstrS) {

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

    if (pstrS != null && pstrS.length() > 0) {
      return true;
    }
    return false;
  } // private boolean isNotEmpty

  /**
   * \brief Helperfunktion - liefert true, wenn String null oder leer ist
   *
   * StringObjekt auf null/empty pr�fen
   *
   * \return boolean
   *
   * @param pstrFileName
   */
  protected boolean isEmpty(final String pstrFileName) {
    return pstrFileName == null || pstrFileName.length() <= 0;
  }

  /**
   * \brief liefert den Wert eines String-Objekts oder ein leeres Objekt
   *
   * \return String
   *
   * @param pstrS
   */
  protected String notNull(final String pstrS) {
    if (pstrS == null) {
      return "";
    }
    else {
      return pstrS;
    }
  }

  public final static String repeatString(final String str, int length) {
    final StringBuffer sb = new StringBuffer();
    if (str != null) {
      while (length > 0) {
        sb.append(str);
        --length;
      }
    }
    return sb.toString();
  }

  public void raiseJSException(final String pstrExceptionText) throws Exception {
    throw new JobSchedulerException(pstrExceptionText);
  }

  /**
   *
   * \brief StackTrace2String
   *
   * \details
   * This Method creates a String with all infos from the stack as a trace.
   *
   * \return String
   *
   * @param e
   */
  public String StackTrace2String(final Exception e) {

    String strT = e.getMessage() + "\n";
    final StackTraceElement arrStack[] = e.getStackTrace();
    for (final StackTraceElement objS : arrStack) {
      strT += objS.toString() + "\n";
    }

    return strT;
  } // void ShowStackTrace (Exception e)

  public static void notImplemented() {
    throw new JobSchedulerException("Method/Functionality not implemented presently.");
  }

} // public class JSToolBox
TOP

Related Classes of com.sos.JSHelper.Basics.JSToolBox

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.