/** 
 * Project    : KANT (LMT)
 * Source Name: Util.java
 * Copyright  : Copyright (c) LTI, Carnegie Mellon University
 * Description: Class for providing utility functions to the developers
 * @version     1.0 (created by jko - 10/01/01)
 **/

package lmt;

import java.sql.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class Util {
	
  // repleace single quote with two single quotes for Oracle representation
  public static String replaceSingleQuote(String str) {	
    String SINGLE_QUOTE = "'";
    String DOUBLE_QUOTE = "''";
    String newStr = "";
		
    // replace ' with '' (two single quotes)
    int idx = str.indexOf(SINGLE_QUOTE);
    //if ( idx >=0 )
    //	Debug.debug("** has single quote:" + str);
    while (idx >= 0) {
      newStr += str.substring(0, idx) + DOUBLE_QUOTE;
      str = str.substring(idx + SINGLE_QUOTE.length()) ;
      idx = str.indexOf(SINGLE_QUOTE) ;
    }
    newStr += str;
    return newStr;
  }
	
  public static String replaceString(String str, String from, String to) {
    String newStr = "";
    int idx = str.indexOf(from);
    while (idx >= 0) {
      newStr += str.substring(0, idx) + to;
      str = str.substring(idx + from.length()) ;
      idx = str.indexOf(from) ;
    }
    newStr += str;
    return newStr;
  }
	
  public static String getTimeExpression(String strTime) {
    if ( strTime == null)
      return "";
    SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
    java.util.Date dtTime;
    try {
      dtTime = df.parse(strTime);
      return dtTime.toString();
    } catch (Exception e) {
      Debug.debug("++ getTime error");
      return "";
    }			
  }
	
  public static String getTime(){
    SimpleDateFormat formatter = new SimpleDateFormat ("d-MMM-yyyy HH:mm:ss");  //, Locale.getDefault());
    Date currentDate = new Date();
    String str = formatter.format(currentDate);
    return str.toUpperCase();
  }
		
}

