using System;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
/**
  $Id: DataConverter.cs,v 1.20 2009/01/23 00:35:16 nli Exp $
  $Revision: 1.20 $
  $Date: 2009/01/23 00:35:16 $
  */
namespace edu.jhu.Cas.Util {
    public class DataConverter {

        public static Filter[] dataSet2Filter(DataSet dataSet) {
            Filter[] filters = new Filter[dataSet.Tables["Table"].Rows.Count];
            int currentRow = 0;
            foreach (DataTable table in dataSet.Tables) {
                foreach (DataRow r in table.Rows) {
                    Filter filter = new Filter();
                    foreach (DataColumn c in table.Columns) {
                        if (r[c] != null) {
                            if (r[c].ToString().Length > 0) {
                                string name = c.ColumnName.ToLower();
                                switch (name) {
                                    case "matchthis":
                                        filter.MatchThis = r[c].ToString();
                                        break;
                                    case "replacewith":
                                        filter.ReplaceWith = r[c].ToString();
                                        break;
                                    case "target":
                                        filter.Target = r[c].ToString();
                                        break;
                                    case "queue":
                                        filter.Queue = int.Parse(r[c].ToString());
                                        break;
                                    case "caseinsensitive":
                                        filter.CaseInsensitive = bool.Parse(r[c].ToString());
                                        break;
                                    case "failonmatch":
                                        filter.FailOnMatch = bool.Parse(r[c].ToString());
                                        break;
                                }
                            }
                        }
                    }
                    filters[currentRow++] = filter;
                }
            }
            return filters;
        }
        public static Server[] dataSet2Server(DataSet dataSet) {
            Server[] servers = new Server[dataSet.Tables["Table"].Rows.Count];
            int currentRow = 0;
            foreach (DataTable table in dataSet.Tables) {
                foreach (DataRow r in table.Rows) {
                    Server server = new Server();
                    foreach (DataColumn c in table.Columns) {
                        if (r[c] != null) {
                            if (r[c].ToString().Length > 0) {
                                string name = c.ColumnName.ToLower();
                                switch (name) {
                                    case "cstringextra":
                                        server.CStringExtra = r[c].ToString();
                                        break;
                                    case "password":
                                        server.Pass = r[c].ToString();
                                        break;
                                    case "queue":
                                        server.Queue = int.Parse(r[c].ToString());
                                        break;
                                    case "target":
                                        server.Target = r[c].ToString();
                                        break;
                                    case "usr":
                                        server.User = r[c].ToString();
                                        break;
                                    case "host":
                                        server.Host = r[c].ToString();
                                        break;
                                    case "cat":
                                        server.Catalog = r[c].ToString();
                                        break;
                                    case "name":
                                        server.Name = r[c].ToString();
                                        break;
                                    case "execlimit":
                                        server.ExecLimit = int.Parse(r[c].ToString());
                                        break;
                                    case "rank":
                                        server.Rank = int.Parse(r[c].ToString());
                                        break;
                                    case "type":
                                        server.Type = int.Parse(r[c].ToString());
                                        break;
                                    case "privs":
                                        server.Priv = r[c].ToString();
                                        break;
                                }
                            }
                        }
                    }
                    servers[currentRow++] = server;
                }
            }
            return servers;
        }
        public static User[] dataSet2User(DataSet dataSet) {
            User[] users = new User[dataSet.Tables["Table"].Rows.Count];
            int currentRow = 0;
            foreach (DataTable table in dataSet.Tables) {
                foreach (DataRow r in table.Rows) {
                    User user = new User();
                    foreach (DataColumn c in table.Columns) {
                        if (r[c] != null) {
                            if (r[c].ToString().Length > 0) {
                                string name = c.ColumnName.ToLower();
                                switch (name) {
                                    case "gvisible":
                                        user.GVisible = (bool)r[c];
                                        break;
                                    case "userid":
                                        user.UserID = r[c].ToString();
                                        break;
                                    case "name":
                                        user.Name = r[c].ToString();
                                        break;
                                    case "email":
                                        user.Email = r[c].ToString();
                                        break;
                                    case "pwencrypted":
                                        user.PWEncrypted = r[c].ToString();
                                        break;
                                    case "webservicesid":
                                        user.WebServicesID = long.Parse(
                                            r[c].ToString());
                                        break;
                                    case "privileges":
                                        user.Privileges = r[c].ToString();
                                        break;
                                    case "timecreated":
                                        user.TimeCreated = DateTime.Parse(r[c].ToString());
                                        break;
                                    case "createdby":
                                        user.CreatedBy = long.Parse(
                                            r[c].ToString());
                                        break;
                                    case "emaillevel":
                                        user.EmailLevel = int.Parse(r[c].ToString());
                                        break;
                                    case "mydbhost":
                                        user.MyDBHost = r[c].ToString();
                                        break;
                                    case "mydbname":
                                        user.MyDBName = r[c].ToString();
                                        break;
                                    case "mydbminsize":
                                        user.MyDBMinSize = int.Parse(r[c].ToString());
                                        break;
                                    case "mydbmaxsize":
                                        user.MyDBMaxSize = int.Parse(r[c].ToString());
                                        break;
                                    case "lastmodified":
                                        user.LastModified = DateTime.Parse(r[c].ToString());
                                        break;
                                }
                            }
                        }
                    }
                    users[currentRow++] = user;
                }
            }
            return users;
        }
        public static CJUser[] dataSet2CJUser(DataSet dataSet) {
            CJUser[] users = new CJUser[dataSet.Tables["Table"].Rows.Count];
            int currentRow = 0;
            foreach (DataTable table in dataSet.Tables) {
                foreach (DataRow r in table.Rows) {
                    CJUser user = new CJUser();
                    foreach (DataColumn c in table.Columns) {
                        if (r[c] != null) {
                            if (r[c].ToString().Length > 0) {
                                string name = c.ColumnName.ToLower();
                                switch (name) {
                                    case "gvisible":
                                        user.GVisible = (bool)r[c];
                                        break;
                                    case "userid":
                                        user.UserID = r[c].ToString();
                                        break;
                                    case "name":
                                        user.Name = r[c].ToString();
                                        break;
                                    case "email":
                                        user.Email = r[c].ToString();
                                        break;
                                    case "webservicesid":
                                        user.WebServicesID = long.Parse(
                                            r[c].ToString());
                                        break;
                                    case "privileges":
                                        user.Privileges = r[c].ToString();
                                        break;
                                    case "timecreated":
                                        user.TimeCreated = DateTime.Parse(r[c].ToString());
                                        break;
                                    case "emaillevel":
                                        user.EmailLevel = int.Parse(r[c].ToString());
                                        break;
                                }
                            }
                        }
                    }
                    users[currentRow++] = user;
                }
            }
            return users;
        }
        public static void dataSet2CSVFile(DataSet dataSet, string file) {
            StreamWriter writer = File.CreateText(file);
            try {
                dataSet2CSVwriter(dataSet, writer);
            }
            finally {
                try { writer.BaseStream.Close(); }
                catch { }
                try { writer.Close(); }
                catch { }
            }
        }
        public static void dataSet2CSVwriter(DataSet dataSet, TextWriter writer) {
            bool first = true;
            foreach (DataTable table in dataSet.Tables) {
                first = true;
                writer.Write("# ");
                foreach (DataColumn c in table.Columns) {
                    if (first)
                        first = false;
                    else
                        writer.Write(",");
                    writer.Write(c.Caption);
                }
                foreach (DataRow r in table.Rows) {
                    first = true;
                    writer.Write("\n");
                    foreach (DataColumn c in table.Columns) {
                        if (r[c] != null) {
                            if (first)
                                first = false;
                            else
                                writer.Write(",");
                            string outVal = null;
                            if (c.DataType == typeof(Byte[])) {
                                outVal = HexEncoding.ToString((byte[])r[c]);
                            }
                            else {
                                outVal = r[c].ToString();
                            }
                            if (outVal.Length == 0) outVal = SYSTEM.NULL;
                            writer.Write(outVal);
                        }
                    }
                }
                writer.Write("\n");
            }
            writer.Write("\n  ");
        }

        public static void reader2CSVFile(SqlDataReader reader, string file) {
            StreamWriter writer = File.CreateText(file);
            bool doHeader = true;
            while (reader.Read()) {
                if (doHeader) {
                    for (int x = 0; x < reader.FieldCount; x++) {
                        string outVal = reader.GetName(x).ToString();
                        if (outVal.Length == 0) outVal = SYSTEM.NULL;
                        writer.Write(outVal);
                        if (x < (reader.FieldCount - 1))
                            writer.Write(",");
                    }
                    doHeader = false;

                }
                writer.Write("\n");
                for (int x = 0; x < reader.FieldCount; x++) {
                    string outVal = null;
                    if (reader.GetDataTypeName(x).ToLower() == "binary") {
                        outVal = HexEncoding.ToString((byte[])reader.GetValue(x));
                    }
                    else {
                        outVal = reader.GetValue(x).ToString();
                    }

                    if (outVal.Length == 0) outVal = SYSTEM.NULL;
                    writer.Write(outVal);
                    if (x < (reader.FieldCount - 1))
                        writer.Write(",");
                }
            }
            reader.Close();
            writer.Close();
        }
        public void dataSet2Response(DataSet dataSet, HttpResponse res) {

            dataSet2CSVwriter(dataSet, res.Output);
        }
        public static Job[] dataTable2Job(DataTable table) {
            Job[] jobs = new Job[table.Rows.Count];
            int currentRow = 0;
            foreach (DataRow r in table.Rows) {
                Job job = new Job();
                foreach (DataColumn c in table.Columns) {
                    if (r[c] != null) {
                        if (r[c].ToString().Length > 0) {
                            string name = c.ColumnName.ToLower();
                            switch (name) {
                                case "jobid":
                                    job.JobID = long.Parse(r[c].ToString());
                                    break;
                                case "estimate":
                                    job.Estimate = int.Parse(r[c].ToString());
                                    break;
                                case "rows":
                                    job.Rows = long.Parse(r[c].ToString());
                                    break;
                                case "webservicesid":
                                    job.WebServicesID = long.Parse(r[c].ToString());
                                    break;
                                case "timesubmit":
                                    job.TimeSubmit =
                                        DateTime.Parse(r[c].ToString());
                                    break;
                                case "timestart":
                                    job.TimeStart =
                                        DateTime.Parse(r[c].ToString());
                                    break;
                                case "timeend":
                                    job.TimeEnd =
                                        DateTime.Parse(r[c].ToString());
                                    break;
                                case "sendemail":
                                    job.SendEmail = (int)r[c];
                                    break;
                                case "status":
                                    job.Status = (int)r[c];
                                    break;
                                case "autocomplete":
                                    job.AutoComplete =
                                        int.Parse(r[c].ToString());
                                    break;
                                case "taskname":
                                    job.TaskName = r[c].ToString();
                                    break;
                                case "outputloc":
                                    job.OutputLoc = r[c].ToString();
                                    break;
                                case "target":
                                    job.Target = r[c].ToString();
                                    break;
                                case "hostip":
                                    job.HostIP = r[c].ToString();
                                    break;
                                case "message":
                                    job.Message = r[c].ToString();
                                    break;
                                case "query":
                                    job.Query = r[c].ToString();
                                    break;
                                case "modifiedquery":
                                    job.ModifiedQuery = r[c].ToString();
                                    break;
                                case "outputtype":
                                    job.OutputType = r[c].ToString();
                                    break;
                                case "params":
                                    job.Params = r[c].ToString();
                                    break;
                                case "created_table":
                                    job.Created_Table = r[c].ToString();
                                    break;
                            }
                        }
                    }
                }
                jobs[currentRow++] = job;
            }
            return jobs;
        }
        public static CJServer[] dataTable2CJServer(DataTable table) {
            CJServer[] servers = new CJServer[table.Rows.Count];
            int currentRow = 0;
            foreach (DataRow r in table.Rows) {
                CJServer server = new CJServer();
                foreach (DataColumn c in table.Columns) {
                    if (r[c] != null) {
                        if (r[c].ToString().Length > 0) {
                            string name = c.ColumnName.ToLower();
                            switch (name) {
                                case "name":
                                    server.Name = r[c].ToString();
                                    break;
                                case "timeout":
                                    server.TimeOut = int.Parse(r[c].ToString());
                                    break;
                                case "context":
                                    server.Context = r[c].ToString();
                                    break;
                                case "dbuser":
                                    server.DBUser = r[c].ToString();
                                    break;
                                case "dbmachine":
                                    server.DBMachine = r[c].ToString();
                                    break;
                                case "dbcatalog":
                                    server.DBCatalog = r[c].ToString();
                                    break;
                                case "execcap":
                                    server.ExecCap = int.Parse(r[c].ToString());
                                    break;
                                case "gpriv":
                                    server.GPriv = r[c] as string;
                                    break;
                                case "dbpw":
                                    server.DBPw = r[c].ToString();
                                    break;
                            }
                        }
                    }
                }
                servers[currentRow++] = server;
            }
            return servers;
        }
        public static CJJob[] dataTable2CJJob(DataTable table) {
            CJJob[] jobs = new CJJob[table.Rows.Count];
            int currentRow = 0;
            foreach (DataRow r in table.Rows) {
                CJJob job = new CJJob();
                foreach (DataColumn c in table.Columns) {
                    if (r[c] != null) {
                        if (r[c].ToString().Length > 0) {
                            string name = c.ColumnName.ToLower();
                            switch (name) {
                                case "jobid":
                                    job.JobID = long.Parse(r[c].ToString());
                                    break;
                                case "queue":
                                    job.Queue = int.Parse(r[c].ToString());
                                    break;
                                case "rows":
                                    job.Rows = long.Parse(r[c].ToString());
                                    break;
                                case "webservicesid":
                                    job.WebServicesID = long.Parse(r[c].ToString());
                                    break;
                                case "timesubmit":
                                    job.TimeSubmit =
                                        DateTime.Parse(r[c].ToString());
                                    break;
                                case "timestart":
                                    job.TimeStart =
                                        DateTime.Parse(r[c].ToString());
                                    break;
                                case "timeend":
                                    job.TimeEnd =
                                        DateTime.Parse(r[c].ToString());
                                    break;
                                case "status":
                                    job.Status = (int)r[c];
                                    break;
                                case "taskname":
                                    job.TaskName = r[c].ToString();
                                    break;
                                case "outputloc":
                                    job.OutputLoc = r[c].ToString();
                                    break;
                                case "context":
                                    job.Context = r[c].ToString();
                                    break;
                                case "error":
                                    job.Error = r[c].ToString();
                                    break;
                                case "query":
                                    job.Query = r[c].ToString();
                                    break;
                                case "type":
                                    job.Type = r[c].ToString();
                                    break;
                            }
                        }
                    }
                }
                jobs[currentRow++] = job;
            }
            return jobs;
        }
        public static Job[] dataSet2Job(DataSet dataSet) {
            return dataTable2Job(dataSet.Tables[0]);
        }
        public static Group[] dataSet2Group(DataSet dataSet) {
            Group[] groups = new Group[dataSet.Tables["Table"].Rows.Count];
            int currentRow = 0;
            foreach (DataTable table in dataSet.Tables) {
                foreach (DataRow r in table.Rows) {
                    Group group = new Group();
                    foreach (DataColumn c in table.Columns) {
                        if (r[c] != null) {
                            if (r[c].ToString().Length > 0) {
                                string name = c.ColumnName.ToLower();
                                switch (name) {
                                    case "name":
                                        group.Name = r[c].ToString();
                                        break;
                                    case "description":
                                        group.Description = r[c].ToString();
                                        break;
                                    case "owner":
                                        group.Owner = long.Parse(r[c].ToString());
                                        break;
                                    case "gid":
                                        group.GID = long.Parse(r[c].ToString());
                                        break;
                                }
                            }
                        }
                    }
                    groups[currentRow++] = group;
                }
            }
            return groups;
        }
        public static Table[] dataSet2Table(DataSet dataSet) {
            Table[] tables = new Table[dataSet.Tables["Table"].Rows.Count];
            int currentRow = 0;
            foreach (DataTable table in dataSet.Tables) {
                foreach (DataRow r in table.Rows) {
                    Table t = new Table();
                    foreach (DataColumn c in table.Columns) {
                        if (r[c] != null) {
                            if (r[c].ToString().Length > 0) {
                                string name = c.ColumnName.ToLower();
                                switch (name) {
                                    case "tablename":
                                        t.Name = r[c].ToString();
                                        break;
                                    case "mydbname":
                                        t.MyDBName = r[c].ToString();
                                        break;
                                    case "userid":
                                        t.UserID = r[c].ToString();
                                        break;
                                    case "webservicesid":
                                        t.OwnerWSID = long.Parse(r[c].ToString());
                                        break;
                                    case "groupname":
                                        t.GroupName = r[c].ToString();
                                        break;
                                }
                            }
                        }
                    }
                    tables[currentRow++] = t;
                }
            }
            return tables;
        }

        public static CJType[] dataTable2CJType(DataTable table) {
            CJType[] jobs = new CJType[table.Rows.Count];
            int currentRow = 0;
            foreach (DataRow r in table.Rows) {
                CJType job = new CJType();
                foreach (DataColumn c in table.Columns) {
                    if (r[c] != null) {
                        if (r[c].ToString().Length > 0) {
                            string name = c.ColumnName.ToLower();
                            switch (name) {
                                case "description":
                                    job.Description = r[c] as string;
                                    break;
                                case "type":
                                    job.Type = r[c] as string;
                                    break;
                            }
                        }
                    }
                }
                jobs[currentRow++] = job;
            }
            return jobs;
        }
        public const string VERSION = "$Revision: 1.20 $";
    }
}
