package com.connor.erp.processresource.EquipmentAcquiredFromERP;

import com.teamcenter.rac.kernel.TCPreferenceService;
import com.teamcenter.rac.kernel.TCSession;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;

public class TCPreferenceUitl {
  public String getEnv(String name) {
    String str = "";
    Iterator<Entry<String, String>> it = System.getenv().entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry entry = it.next();
      if (entry.getKey().toString().equals(name)) {
        str = (String)entry.getValue();
        if (!str.endsWith("\\"))
          str = String.valueOf(str) + "\\"; 
        break;
      } 
    } 
    return str;
  }
  
  public static boolean isSelectExsitToTCPreferenceValues(TCSession tcsession, String preferenceName, String isSelectName) {
    boolean flag = false;
    TCPreferenceService preferenceService = tcsession.getPreferenceService();
    String[] preferenceValues = preferenceService.getStringArray(4, preferenceName);
    if (preferenceValues == null)
      return false; 
    if (preferenceValues != null && preferenceValues.length > 0)
      for (int i = 0; i < preferenceValues.length; i++) {
        if (preferenceValues[i].trim().equals(isSelectName)) {
          flag = true;
          return flag;
        } 
      }  
    return flag;
  }
  
  private static boolean isWriteExpire = false;
  
  public static synchronized boolean isWriteExpire() {
    return isWriteExpire;
  }
  
  public static synchronized void setWriteExpire(boolean isWriteExpire) {
    TCPreferenceUitl.isWriteExpire = isWriteExpire;
  }
  
  public static boolean isTrueTCPreferenceValue(TCSession tcsession, String preferenceName) {
    boolean flag = false;
    TCPreferenceService preferenceService = tcsession.getPreferenceService();
    String value = preferenceService.getString(4, preferenceName);
    if (value == null)
      return false; 
    if (value.equalsIgnoreCase("true"))
      flag = true; 
    return flag;
  }
  
  public static String getTCPreferenceValue(TCSession tcsession, String preferenceName) {
    TCPreferenceService preferenceService = tcsession.getPreferenceService();
    String value = preferenceService.getString(4, preferenceName);
    return value;
  }
  
  public static String[] getListPreference(TCSession session, int scope, String pid) {
    try {
      String[] options = session.getPreferenceService().getStringArray(scope, pid);
      return options;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    } 
  }
  
  public static HashMap<String, String> getPreferenceHashMap(TCSession pSession, int pScope, String pid) {
    HashMap<String, String> hm = new HashMap<String, String>();
    Vector<String> preferencekeyNames = new Vector<String>();
    try {
      String[] options = getPreference(pSession, pScope, pid);
      if (options == null)
        return hm; 
      if (options == null || options.length == 0)
        return null; 
      String key = "";
      String value = "";
      for (int i = 0; i < options.length; i++) {
        if (options[i].indexOf("=") > 0) {
          key = options[i].substring(0, options[i].indexOf("="));
          value = options[i].substring(options[i].indexOf("=") + 1);
          hm.put(key, value);
          preferencekeyNames.add(key);
        } else {
          hm.put(i+"", options[i]);
        } 
      } 
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    } 
    return hm;
  }
  
  public static String[] getPreference(TCSession session, int scope, String pid) {
    try {
      String[] options = session.getPreferenceService().getStringArray(scope, pid);
      return options;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    } 
  }
}
