/*
 * 
 * Created on Jun 9, 2005
 * 
 * Gy�rgy S�veges
 * Etixpert Kft.
 * Copyright(c) 2005
 * 
 */
package com.etixpert.evolution;

import java.sql.Connection;
import java.util.*;

import javax.servlet.http.HttpSession;

import java.io.*;
/**
 * @author Suveges Gyorgy
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Tools {
	public final static char ESCAPE='#';
    /**
     * 
     */
    public Tools() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public static boolean getRight(Connection conn, PersistableObject rights, String field, Integer loggedId, Integer distrId) throws Exception {
    	String right = rights.getString(field, "X");
    	if (right.equals("") || right.equalsIgnoreCase("x"))
    		return false;
    	
        ObjectSearch os = new ObjectSearch();
        List l = os.search(conn, "select (? in (select * from "+right.toLowerCase()+"(?))) as allowed", new Object[]{distrId, loggedId});
        if (l.size()==0)
        	return false;
        
        PersistableObject po = (PersistableObject) l.get(0);
        Boolean b = (Boolean) po.get("allowed");
        if (b==null) 
        	return false;
        return b.booleanValue();
        
    	
    }
    public static boolean equals(Map webMap, String key1, String key2) {
        String[] a1 = (String[]) webMap.get(key1);
        String[] a2 = (String[]) webMap.get(key2);
        if (a1 == null && a2 == null) return true;
        if (a1 == null || a2 == null) return false;
        if (a1.length==0 && a2.length == 0) return true;
        if (a1.length==0 || a2.length == 0) return false;
        String v1 = a1[0].trim();
        String v2 = a2[0].trim();
        return v1.equals(v2);
    }

    public static String idsToSql(Set orders, boolean prepared) {
        StringBuffer sb = new StringBuffer();
        if (orders.size()==0) return ("(null)");
        sb.append("(");
        for (Iterator it=orders.iterator(); it.hasNext(); ) {
            Object orderId =  it.next();
            sb.append(prepared?"?":orderId.toString());
            if (it.hasNext()) sb.append(", ");
        }
        sb.append(")");
        return sb.toString();
    }
    
    public static String arrayToSql(String attr, String[] values, boolean quota) {
        if (values==null || values.length==0) return "";
        StringBuffer sb = new StringBuffer();
        sb.append("(");
        for (int i = 0; i<values.length; i++) {
            if (i>0) sb.append(" OR ");
            sb.append(attr).append("=");
            if (quota) sb.append("'");
            sb.append(values[i]);
            if (quota) sb.append("'");
        }
        sb.append(")");
        return sb.toString();
             
        
    }
    
    
    public static String getVal(HttpSession session, String whichMap, String name) {
		Map m = (Map)session.getAttribute(whichMap);
		if (m == null) return "";
		String[] vals = (String[])m.get(name);
		if (vals != null && vals.length>0 && vals[0] != null) return vals[0];
		return "";    
    }
    
    public static String getVal(Map m, String name, String defaultNull) {
    	
    	if (m == null) return defaultNull;
    	String[] vals = (String[])m.get(name);
    	if (vals != null && vals.length>0 && vals[0] != null) return vals[0];
    	return defaultNull;
    }
    
    public static String parse(HttpSession session, String toParse) {
        if (toParse==null) return "";
        if (toParse.equals("")) return "";
        StringBuffer parsed=new StringBuffer();

        int i=0;
        while (i<toParse.length()) {
          char c = toParse.charAt(i);
          if (c!=ESCAPE) parsed.append(c);
          else {
            if (i==toParse.length()-1) parsed.append(c);
            else {
              i++;
              int end = toParse.indexOf(ESCAPE, i);
              if (end==-1) {
                parsed.append(toParse.substring(i-1)); break; }
              String value = session.getAttribute(toParse.substring(i, end)).toString();
              if (value == null) value="";
              //if (value == null) parsed.append(filename.substring(i-1, end+1));
              parsed.append(value);
              i=end;
            }
          }
          i++;
        }
        return parsed.toString();
      }

    public static String getFileName(String path) {
        if (path==null) return "";
        String n = new File(path).getName();
        int pos =0;
        if ((pos=n.lastIndexOf('\\'))!=-1) {
            if (pos==n.length()-1) return "";
            return n.substring(pos+1);
        } else
            return n;
        
    }
   /* public static String localize(PageContext pageContext, String key) throws JspTagException, JspException {
        return localize(pageContext, key, null);
        
    }
    
    public static String localize(PageContext pageContext, String key, Object[] args) throws JspTagException, JspException {
        MessageTag tag = new MessageTag();
        tag.setPageContext(pageContext);
        tag.setParent(null);
        tag.setKey(key);
        tag.setVar("__evolution_temp__");
        if (args!=null) {
	        for (int i=0; i<args.length; i++)
	            tag.addParam(args[i]);
	     }
        tag.doEndTag();
        
        return (String) pageContext.getAttribute("__evolution_temp__");
        
        
        
        
     }*/
    
}
