Example #1
0
        private void generate_path_map
        (

            mmria.common.metadata.app p_metadata, string p_path,
            string p_file_name,
            string p_form_path,
            System.Collections.Generic.Dictionary <string, int> p_path_to_int_map,
            System.Collections.Generic.Dictionary <string, string> p_path_to_file_name_map,
            System.Collections.Generic.Dictionary <string, mmria.common.metadata.node> p_path_to_node_map,
            System.Collections.Generic.Dictionary <string, string> p_path_to_grid_map,
            System.Collections.Generic.Dictionary <string, string> p_path_to_multi_form_map,
            bool p_is_multiform_context,
            System.Collections.Generic.Dictionary <string, string> p_multi_form_to_grid_map,
            bool p_is_grid_context,
            System.Collections.Generic.HashSet <string> p_path_to_flat_map

        )
        {
            p_path_to_int_map.Add(p_path, p_path_to_int_map.Count);



            if (p_metadata.children != null)
            {
                IList <mmria.common.metadata.node> children = p_metadata.children as IList <mmria.common.metadata.node>;

                for (var i = 0; i < children.Count; i++)
                {
                    var child = children[i];

                    generate_path_map(child, child.name, p_file_name, p_form_path, p_path_to_int_map, p_path_to_file_name_map, p_path_to_node_map, p_path_to_grid_map, p_path_to_multi_form_map, false, p_multi_form_to_grid_map, false, p_path_to_flat_map);
                }
            }
        }
Example #2
0
        public mmria.common.metadata.app get_metadata()
        {
            mmria.common.metadata.app result = null;

            string URL = "http://test.mmria.org/api/metadata";
            //string urlParameters = "?api_key=123";
            string urlParameters = "";

            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(URL);

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            // List data response.
            HttpResponseMessage response = client.GetAsync(urlParameters).Result;              // Blocking call!

            if (response.IsSuccessStatusCode)
            {
                // Parse the response body. Blocking!
                //result = response.Content.ReadAsAsync<mmria.common.metadata.app>().Result;
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }

            return(result);
        }
Example #3
0
        public async System.Threading.Tasks.Task <mmria.common.model.couchdb.document_put_response> Post
        (
            [FromBody] mmria.common.metadata.app metadata
        )
        {
            string object_string = null;

            mmria.common.model.couchdb.document_put_response result = new mmria.common.model.couchdb.document_put_response();

            try
            {
                Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings();
                settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
                object_string = Newtonsoft.Json.JsonConvert.SerializeObject(metadata, settings);

                string metadata_url = Program.config_couchdb_url + "/metadata/" + metadata._id;

                var metadata_curl = new cURL("PUT", null, metadata_url, object_string, Program.config_timer_user_name, Program.config_timer_password);


                string responseFromServer = await metadata_curl.executeAsync();

                result = Newtonsoft.Json.JsonConvert.DeserializeObject <mmria.common.model.couchdb.document_put_response>(responseFromServer);

                if (!result.ok)
                {
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return(result);
        }
Example #4
0
        public IDictionary <string, object> create_default_object(mmria.common.metadata.app p_metadata, IDictionary <string, object> p_parent)
        {
            p_parent.Add("_id", Guid.NewGuid().ToString());
            for (var i = 0; i < p_metadata.children.Length; i++)
            {
                mmria.common.metadata.node child = p_metadata.children[i];
                create_default_object(child, p_parent);
            }

            return(p_parent);
        }
Example #5
0
        public void process_grid
        (
            mmria.common.metadata.app metadata,
            Case_Maker case_maker,
            IDictionary <string, object> case_data,
            System.Data.DataRow grid_row,
            System.Data.DataTable mapping_view_table,
            int?index      = null,
            int?grid_index = null
        )
        {
            foreach (System.Data.DataRow row in mapping_view_table.Rows)
            {
                if (row["mmria_path"] != DBNull.Value && !string.IsNullOrWhiteSpace(row["mmria_path"].ToString()))
                {
                    string path = row["mmria_path"].ToString().Trim();

                    /*
                     * if (row["table"].ToString().Contains("AutopsyReport12AR_COD_grid"))
                     * {
                     *      System.Console.Write("break");
                     * }*/

                    string[] path_array = row["mmria_path"].ToString().Trim().Split('/');

                    if (index != null && index.HasValue)
                    {
                        path = case_maker.AppendFormIndexToPath(index.Value, path);
                    }

                    if (grid_index != null && grid_index.HasValue)
                    {
                        path = case_maker.AppendGridIndexToPath(grid_index.Value, path);
                    }

                    int check_index = row[0].ToString().IndexOf("/");
                    if (check_index > -1)
                    {
                        string table_name = row[0].ToString().Trim().Substring(0, check_index);
                        case_maker.set_value(metadata, case_data, path, grid_row[row["field"].ToString().Trim()], table_name + "." + row[2].ToString().Trim(), null, row[0].ToString().Trim() + "." + row[2].ToString().Trim());
                    }
                    else
                    {
                        case_maker.set_value(metadata, case_data, path, grid_row[row["field"].ToString().Trim()], row[0].ToString().Trim() + "." + row[2].ToString().Trim(), null, row[0].ToString().Trim() + "." + row[2].ToString().Trim());
                    }
                    Console.WriteLine(string.Format("{0}", path));
                    Console.WriteLine(string.Format("{0}, {1}, \"\"", row[0].ToString().Replace(".", ""), row["prompt"].ToString().Replace(",", "")));
                }
            }
        }
Example #6
0
        public mmria.common.metadata.node get_metadata_node(mmria.common.metadata.app p_object, string p_path)
        {
            mmria.common.metadata.node           result       = null;
            System.Text.RegularExpressions.Regex number_regex = new System.Text.RegularExpressions.Regex(@"^\d+$");
            try
            {
                string[] path = p_path.Split('/');

                for (int i = 0; i < path.Length; i++)
                {
                    if (i == 0)
                    {
                        result = p_object.children.Where(c => c.name.Equals(path[i], StringComparison.OrdinalIgnoreCase)).First();
                    }
                    else if (number_regex.IsMatch(path[i]))
                    {
                        continue;
                    }
                    else
                    {
                        switch (result.type.ToLower())
                        {
                        case "form":
                        case "grid":
                        case "group":
                            foreach (mmria.common.metadata.node child in result.children)
                            {
                                if (child.name.Equals(path[i], StringComparison.OrdinalIgnoreCase))
                                {
                                    result = child;
                                    break;
                                }
                            }

                            break;

                        default:
                            System.Console.WriteLine("get_metadata_object: {0} - {1} - {2}", path, result.type, path[i]);
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("case_maker.set_value bad mapping {0}\n {1}", p_path, ex);
            }

            return(result);
        }
Example #7
0
        public void process_view
        (
            mmria.common.metadata.app metadata,
            Case_Maker case_maker,
            IDictionary <string, object> case_data,
            System.Data.DataRow grid_row,
            System.Data.DataTable mapping_view_table,
            int?index = null
        )
        {
            //var view_data_table = get_view_data_table(mmrds_data, "DeathCertificate");

            //var mapping_view_table = get_view_mapping(mmrds_data, "DeathCertificate", mapping_data, main_mapping_file_name);

            //grid_table = view_data_table.Select(string.Format("FKEY='{0}'", global_record_id));

            foreach (System.Data.DataRow row in mapping_view_table.Rows)
            {
                if (row["MMRIA Path"] != DBNull.Value && !string.IsNullOrWhiteSpace(row["MMRIA Path"].ToString()) && row[5].ToString().ToLower() != "grid")
                {
                    //List<string> path = row["MMRIA Path"].ToString();
                    string path = row["MMRIA Path"].ToString().Trim();

                    string[] path_array = row["MMRIA Path"].ToString().Trim().Split('/');

                    if (index != null && index.HasValue)
                    {
                        path = case_maker.AppendFormIndexToPath(index.Value, path);
                    }


                    if (path == "home_record/case_progress_report/death_certificate")
                    {
                        System.Console.Write("break\n");
                    }

                    if (row["DataType"].ToString().ToLower() == "boolean")
                    {
                        case_maker.set_value(metadata, case_data, path, grid_row[row["f#Name"].ToString().Trim()], row[0].ToString().Trim(), row["prompttext"].ToString().Trim(), row[0].ToString().Trim());
                    }
                    else
                    {
                        case_maker.set_value(metadata, case_data, path, grid_row[row["f#Name"].ToString().Trim()], row[0].ToString().Trim(), null, row[0].ToString().Trim());
                    }
                    Console.WriteLine(string.Format("{0}", path));
                    Console.WriteLine(string.Format("{0}, {1}, \"\"", row[0].ToString().Replace(".", ""), row["prompttext"].ToString().Trim().Replace(",", "")));
                }
            }
        }
Example #8
0
        // POST api/values
        public async System.Threading.Tasks.Task Post([FromBody] mmria.common.metadata.app metadata)
        {
            bool valid_login = false;

            try
            {
                string request_string         = Program.config_couchdb_url + "/_session";
                System.Net.WebRequest request = System.Net.WebRequest.Create(new System.Uri(request_string));

                request.PreAuthenticate = false;

                if (!string.IsNullOrWhiteSpace(this.Request.Cookies["AuthSession"]))
                {
                    string auth_session_value = this.Request.Cookies["AuthSession"];
                    request.Headers.Add("Cookie", "AuthSession=" + auth_session_value);
                    request.Headers.Add("X-CouchDB-WWW-Authenticate", auth_session_value);
                }

                System.Net.WebResponse response = await request.GetResponseAsync();

                System.IO.Stream       dataStream   = response.GetResponseStream();
                System.IO.StreamReader reader       = new System.IO.StreamReader(dataStream);
                string           responseFromServer = reader.ReadToEnd();
                session_response json_result        = Newtonsoft.Json.JsonConvert.DeserializeObject <session_response>(responseFromServer);

                valid_login = json_result.userCTX.name != null;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            if (valid_login)
            {
                Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings();
                settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
                string object_string = Newtonsoft.Json.JsonConvert.SerializeObject(metadata, settings);
                string hash          = GetHash(object_string);
                if (current_edit_list ["metadata"].id != hash)
                {
                    Current_Edit current = new Current_Edit();
                    current.id        = hash;
                    current.metadata  = object_string;
                    current.edit_type = "json";

                    current_edit_list ["metadata"] = current;
                }
            }
        }
Example #9
0
        static async Task <NJsonSchema.JsonSchema> GetSchema(mmria.common.metadata.app p_app)
        {
            //https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Schema_JsonSchema.htm

            var schema = new NJsonSchema.JsonSchema();

            schema.Type        = NJsonSchema.JsonObjectType.Object;
            schema.Title       = "mmria_case";
            schema.Description = "Here is a case for your ...!";

            schema.SchemaVersion = "http://json-schema.org/draft-06/schema#";

            schema.Properties.Add("_id", new NJsonSchema.JsonSchemaProperty()
            {
                Type = NJsonSchema.JsonObjectType.String
            });
            //schema.Properties.Add("name", new NJsonSchema.JsonSchemaProperty(){ Type = NJsonSchema.JsonObjectType.String});
            //schema.Properties.Add("prompt", new NJsonSchema.JsonSchemaProperty(){ Type = NJsonSchema.JsonObjectType.String});



            try
            {
                foreach (var child in p_app.lookup)
                {
                    Set_LookUp(schema.Definitions, child);
                }
            }
            catch (Exception ex)
            {
                System.Console.Write($"Set_LookUp Exception: {ex}");
            }


            try
            {
                foreach (var child in p_app.children)
                {
                    await GetSchema(schema.Definitions, schema, child);
                }
            }
            catch (Exception ex)
            {
                System.Console.Write($"GetSchema Exception: {ex}");
            }
            return(schema);
        }
Example #10
0
        //https://duckduckgo.com/?q=c%23+generate+dynamic+class&t=ironbrowser&ia=qa


        public static Type CreateObjectFromAppMetadata(mmria.common.metadata.app app_metadata)
        {
            Type result = null;

            foreach (var node in app_metadata.children)
            {
                CreateObjectFromMetadata(node);

                switch (node.type.ToLower())
                {
                default:
                    Console.WriteLine("CreateObjectFromMetadata unimplemented (0}", node.type);
                    break;
                }
            }

            return(result);
        }
Example #11
0
        public mmria.common.metadata.app get_metadata()
        {
            mmria.common.metadata.app result = null;

            string URL = this.mmria_url + "/api/metadata";
            //string urlParameters = "?api_key=123";
            string urlParameters = "";

            var curl = new cURL("GET", null, URL, null, null);

            try
            {
                string json_result = curl.execute();
                result = Newtonsoft.Json.JsonConvert.DeserializeObject <mmria.common.metadata.app> (json_result);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}", ex);
            }

            return(result);
        }
Example #12
0
        //http://json-schema.org/
        //http://swagger.io/specification/

        public string generate(mmria.common.metadata.app app)
        {
            System.Text.StringBuilder result = new System.Text.StringBuilder();

            result.Append(pre_fix);
            result.Append("\"case_item\":{");
            result.Append("\"title\": \"case_item schema\",");
            result.Append("\"type\": \"object\",");
            result.Append("\"properties\": {");
            result.Append(@" 
				""_id"": {
                      ""description"": ""id"",
                        ""type"": ""string""
                },
                ""_rev"": {
                  ""description"": ""revision"",
                  ""type"": ""string""
                },");

            for (int i = 0; i < app.children.Length; i++)
            {
                var child = app.children [i];
                generate(result, child);
                if (i < app.children.Length - 1)
                {
                    result.Append(",");
                }
            }


            result.Append(@",
              ""required"": [""_id""]}}
  }
}");
            //result.Append (post_fix);
            return(result.ToString());
        }
Example #13
0
        // POST api/values
        public async System.Threading.Tasks.Task Post()
        {
            bool valid_login = false;

            mmria.common.metadata.app metadata = null;

            try
            {
                System.IO.Stream dataStream0 = await this.Request.Content.ReadAsStreamAsync();

                // Open the stream using a StreamReader for easy access.
                //dataStream0.Seek(0, System.IO.SeekOrigin.Begin);
                System.IO.StreamReader reader0 = new System.IO.StreamReader(dataStream0);
                // Read the content.
                string temp = reader0.ReadToEnd();

                metadata = Newtonsoft.Json.JsonConvert.DeserializeObject <mmria.common.metadata.app>(temp);

                //string metadata = DecodeUrlString(temp);
            }
            catch (Exception ex)
            {
            }


            try
            {
                string request_string         = Program.config_couchdb_url + "/_session";
                System.Net.WebRequest request = System.Net.WebRequest.Create(new System.Uri(request_string));

                request.PreAuthenticate = false;

                if (this.Request.Headers.Contains("Cookie") && this.Request.Headers.GetValues("Cookie").Count() > 0)
                {
                    string[] cookie_set = this.Request.Headers.GetValues("Cookie").First().Split(';');
                    for (int i = 0; i < cookie_set.Length; i++)
                    {
                        string[] auth_session_token = cookie_set[i].Split('=');
                        if (auth_session_token[0].Trim() == "AuthSession")
                        {
                            request.Headers.Add("Cookie", "AuthSession=" + auth_session_token[1]);
                            request.Headers.Add("X-CouchDB-WWW-Authenticate", auth_session_token[1]);
                            break;
                        }
                    }
                }

                System.Net.WebResponse response = await request.GetResponseAsync();

                System.IO.Stream       dataStream   = response.GetResponseStream();
                System.IO.StreamReader reader       = new System.IO.StreamReader(dataStream);
                string           responseFromServer = reader.ReadToEnd();
                session_response json_result        = Newtonsoft.Json.JsonConvert.DeserializeObject <session_response>(responseFromServer);

                valid_login = json_result.userCTX.name != null;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            if (valid_login)
            {
                Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings();
                settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
                string object_string = Newtonsoft.Json.JsonConvert.SerializeObject(metadata, settings);
                string hash          = GetHash(object_string);
                if (current_edit_list ["metadata"].id != hash)
                {
                    Current_Edit current = new Current_Edit();
                    current.id        = hash;
                    current.metadata  = object_string;
                    current.edit_type = "json";

                    current_edit_list ["metadata"] = current;
                }
            }
        }
Example #14
0
        public async System.Threading.Tasks.Task <mmria.common.model.couchdb.document_put_response> Post()
        {
            //bool valid_login = false;
            mmria.common.metadata.app metadata = null;
            string object_string = null;

            mmria.common.model.couchdb.document_put_response result = new mmria.common.model.couchdb.document_put_response();

            try
            {
                System.IO.Stream dataStream0 = await this.Request.Content.ReadAsStreamAsync();

                // Open the stream using a StreamReader for easy access.
                //dataStream0.Seek(0, System.IO.SeekOrigin.Begin);
                System.IO.StreamReader reader0 = new System.IO.StreamReader(dataStream0);
                // Read the content.
                string temp = reader0.ReadToEnd();

                metadata = Newtonsoft.Json.JsonConvert.DeserializeObject <mmria.common.metadata.app>(temp);
                //System.Dynamic.ExpandoObject json_result = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Dynamic.ExpandoObject>(result, new  Newtonsoft.Json.Converters.ExpandoObjectConverter());



                //string metadata = DecodeUrlString(temp);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            try
            {
                Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings();
                settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
                object_string = Newtonsoft.Json.JsonConvert.SerializeObject(metadata, settings);

                string metadata_url = this.get_couch_db_url() + "/metadata/" + metadata._id;

                System.Net.WebRequest request = System.Net.WebRequest.Create(new System.Uri(metadata_url));
                request.Method          = "PUT";
                request.ContentType     = "application/json";
                request.ContentLength   = object_string.Length;
                request.PreAuthenticate = false;

                if (this.Request.Headers.Contains("Cookie") && this.Request.Headers.GetValues("Cookie").Count() > 0)
                {
                    string[] cookie_set = this.Request.Headers.GetValues("Cookie").First().Split(';');
                    for (int i = 0; i < cookie_set.Length; i++)
                    {
                        string[] auth_session_token = cookie_set[i].Split('=');
                        if (auth_session_token[0].Trim() == "AuthSession")
                        {
                            request.Headers.Add("Cookie", "AuthSession=" + auth_session_token[1]);
                            request.Headers.Add("X-CouchDB-WWW-Authenticate", auth_session_token[1]);
                            break;
                        }
                    }
                }

                using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(request.GetRequestStream()))
                {
                    try
                    {
                        streamWriter.Write(object_string);
                        streamWriter.Flush();
                        streamWriter.Close();


                        System.Net.WebResponse response = await request.GetResponseAsync();

                        System.IO.Stream       dataStream = response.GetResponseStream();
                        System.IO.StreamReader reader     = new System.IO.StreamReader(dataStream);
                        string responseFromServer         = reader.ReadToEnd();

                        result = Newtonsoft.Json.JsonConvert.DeserializeObject <mmria.common.model.couchdb.document_put_response>(responseFromServer);

                        if (response.Headers["Set-Cookie"] != null)
                        {
                            string[] set_cookie = response.Headers["Set-Cookie"].Split(';');
                            string[] auth_array = set_cookie[0].Split('=');
                            if (auth_array.Length > 1)
                            {
                                string auth_session_token = auth_array[1];
                                result.auth_session = auth_session_token;
                            }
                            else
                            {
                                result.auth_session = "";
                            }
                        }



                        //System.Threading.Tasks.Task.Run( new Action(()=> { var f = new GenerateSwaggerFile(); System.IO.File.WriteAllText(Program.config_file_root_folder + "/api-docs/api.json", f.generate(metadata)); }));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }

                if (!result.ok)
                {
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return(result);
        }
Example #15
0
        public void Execute(string[] args)
        {
            if (args.Length > 1)
            {
                for (var i = 1; i < args.Length; i++)
                {
                    string arg   = args[i];
                    int    index = arg.IndexOf(':');
                    string val   = arg.Substring(index + 1, arg.Length - (index + 1)).Trim(new char[] { '\"' });

                    if (arg.ToLower().StartsWith("auth_token"))
                    {
                        this.auth_token = val;
                    }
                    else if (arg.ToLower().StartsWith("user_name"))
                    {
                        this.user_name = val;
                    }
                    else if (arg.ToLower().StartsWith("password"))
                    {
                        this.password = val;
                    }
                    else if (arg.ToLower().StartsWith("database_url"))
                    {
                        this.database_url = val;
                    }
                    else if (arg.ToLower().StartsWith("database"))
                    {
                        this.database_path = val;
                    }
                    else if (arg.ToLower().StartsWith("item_file_name"))
                    {
                        this.item_file_name      = val;
                        this.item_directory_name = this.item_file_name.Substring(0, this.item_file_name.LastIndexOf("."));
                    }
                    else if (arg.ToLower().StartsWith("item_id"))
                    {
                        this.item_id = val;
                    }
                }
            }

            string core_file_name = "core_mmria_export.csv";

            if (string.IsNullOrWhiteSpace(this.database_url))
            {
                this.database_url = System.Configuration.ConfigurationManager.AppSettings["couchdb_url"];

                if (string.IsNullOrWhiteSpace(this.database_url))
                {
                    System.Console.WriteLine("missing database_url");
                    System.Console.WriteLine(" form database:[file path]");
                    System.Console.WriteLine(" example database:http://localhost:5984");
                    System.Console.WriteLine(" mmria.exe export user_name:user1 password:secret url:http://localhost:12345 database_url:http://localhost:5984");
                    return;
                }
            }



            if (string.IsNullOrWhiteSpace(this.user_name))
            {
                System.Console.WriteLine("missing user_name");
                System.Console.WriteLine(" form user_name:[user_name]");
                System.Console.WriteLine(" example user_name:user1");
                System.Console.WriteLine(" mmria.exe export user_name:user1 password:secret url:http://localhost:12345");
                return;
            }

            if (string.IsNullOrWhiteSpace(this.password))
            {
                System.Console.WriteLine("missing password");
                System.Console.WriteLine(" form password:[password]");
                System.Console.WriteLine(" example password:secret");
                System.Console.WriteLine(" mmria.exe export user_name:user1 password:secret url:http://localhost:12345");
                return;
            }

            string export_directory = System.IO.Path.Combine(System.Configuration.ConfigurationManager.AppSettings["export_directory"], this.item_directory_name);

            if (!System.IO.Directory.Exists(export_directory))
            {
                System.IO.Directory.CreateDirectory(export_directory);
            }


            string  URL           = this.database_url + "/mmrds/_all_docs";
            string  urlParameters = "?include_docs=true";
            cURL    document_curl = new cURL("GET", null, URL + urlParameters, null, this.user_name, this.password);
            dynamic all_cases     = Newtonsoft.Json.JsonConvert.DeserializeObject <System.Dynamic.ExpandoObject>(document_curl.execute());

            string metadata_url  = this.database_url + "/metadata/2016-06-12T13:49:24.759Z";
            cURL   metadata_curl = new cURL("GET", null, metadata_url, null, this.user_name, this.password);

            mmria.common.metadata.app metadata = Newtonsoft.Json.JsonConvert.DeserializeObject <mmria.common.metadata.app>(metadata_curl.execute());



            /*
             * foreach (KeyValuePair<string, object> kvp in all_cases)
             * {
             *      System.Console.WriteLine(kvp.Key);
             * }
             */

            System.Collections.Generic.Dictionary <string, int>    path_to_int_map       = new Dictionary <string, int>();
            System.Collections.Generic.Dictionary <string, string> path_to_file_name_map = new Dictionary <string, string>();
            System.Collections.Generic.Dictionary <string, mmria.common.metadata.node> path_to_node_map = new Dictionary <string, mmria.common.metadata.node>();
            System.Collections.Generic.Dictionary <string, string> path_to_grid_map       = new Dictionary <string, string>();
            System.Collections.Generic.Dictionary <string, string> path_to_multi_form_map = new Dictionary <string, string>();
            System.Collections.Generic.Dictionary <string, string> grid_to_multi_form_map = new Dictionary <string, string>();

            System.Collections.Generic.HashSet <string> path_to_flat_map = new System.Collections.Generic.HashSet <string>();

            System.Collections.Generic.Dictionary <string, WriteCSV> path_to_csv_writer = new Dictionary <string, WriteCSV>();

            generate_path_map
                (metadata, "", core_file_name, "",
                path_to_int_map,
                path_to_file_name_map,
                path_to_node_map,
                path_to_grid_map,
                path_to_multi_form_map,
                false,
                grid_to_multi_form_map,
                false,
                path_to_flat_map
                );


            System.Collections.Generic.HashSet <string> flat_grid_set     = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            System.Collections.Generic.HashSet <string> mutiform_grid_set = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            foreach (KeyValuePair <string, string> kvp in path_to_grid_map)
            {
                if (grid_to_multi_form_map.ContainsKey(kvp.Key))
                {
                    mutiform_grid_set.Add(kvp.Value);
                }
            }

            foreach (KeyValuePair <string, string> kvp in path_to_grid_map)
            {
                if (
                    !mutiform_grid_set.Contains(kvp.Value) &&
                    !flat_grid_set.Contains(kvp.Value)
                    )
                {
                    flat_grid_set.Add(kvp.Value);
                }
            }

            /*
             * System.Collections.Generic.HashSet<string> mutiform_set = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
             * foreach (KeyValuePair<string, string> kvp in path_to_multi_form_map)
             * {
             *      if (!mutiform_set.Contains(kvp.Value))
             *      {
             *              mutiform_set.Add(kvp.Value);
             *      }
             * }*/

            path_to_csv_writer.Add(core_file_name, new WriteCSV(core_file_name, this.item_directory_name));

            int stream_file_count = 0;

            /*
             * foreach (string file_name in path_to_file_name_map.Select(kvp => kvp.Value).Distinct())
             * {
             *      path_to_csv_writer.Add(file_name, new WriteCSV(file_name));
             *      Console.WriteLine(file_name);
             *      stream_file_count++;
             * }*/
            //Console.WriteLine("stream_file_count: {0}", stream_file_count);

            create_header_row
            (
                path_to_int_map,
                path_to_flat_map,
                path_to_node_map,
                path_to_csv_writer[core_file_name].Table,
                true,
                false,
                false
            );

            dynamic all_cases_rows;

            if (this.is_offline_mode)
            {
                all_cases_rows = all_cases;
            }
            else
            {
                all_cases_rows = all_cases.rows;
            }


            foreach (System.Dynamic.ExpandoObject case_row in all_cases_rows)
            {
                IDictionary <string, object> case_doc;
                if (this.is_offline_mode)
                {
                    case_doc = case_row as IDictionary <string, object>;
                }
                else
                {
                    case_doc = ((IDictionary <string, object>)case_row)["doc"] as IDictionary <string, object>;
                }

                //IDictionary<string, object> case_doc = ((IDictionary<string, object>)case_row)["doc"] as IDictionary<string, object>;
                //IDictionary<string, object> case_doc = case_row as IDictionary<string, object>;
                if (case_doc["_id"].ToString().StartsWith("_design", StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }
                System.Data.DataRow row = path_to_csv_writer[core_file_name].Table.NewRow();
                string mmria_case_id    = case_doc["_id"].ToString();
                row["_id"] = mmria_case_id;

                List <string> ordered_column_list = get_core_element_list();

                foreach (string path in ordered_column_list)
                {
                    if (
                        !path_to_node_map.ContainsKey(path) ||
                        !row.Table.Columns.Contains(convert_path_to_field_name(path)) ||
                        path_to_node_map[path].type.ToLower() == "app" ||
                        path_to_node_map[path].type.ToLower() == "form" ||
                        path_to_node_map[path].type.ToLower() == "group"

                        )
                    {
                        continue;
                    }

                    //System.Console.WriteLine("path {0}", path);

                    dynamic val = get_value(case_doc as IDictionary <string, object>, path);

                    /*
                     * if (path_to_int_map[path].ToString("X") == "41")
                     * {
                     *      System.Console.Write("pause");
                     * }
                     */

                    try
                    {
                        switch (path_to_node_map [path].type.ToLower())
                        {
                        case "number":
                            if (val != null && (!string.IsNullOrWhiteSpace(val.ToString())))
                            {
                                row [convert_path_to_field_name(path)] = val;
                            }
                            break;

                        case "list":

                            if
                            (
                                path_to_node_map [path].is_multiselect != null &&
                                path_to_node_map [path].is_multiselect == true

                            )
                            {
                                IList <object> temp = val as IList <object>;
                                if (temp != null && temp.Count > 0)
                                {
                                    row [convert_path_to_field_name(path)] = string.Join("|", temp);
                                }
                            }
                            else
                            {
                                if (val != null)
                                {
                                    row [convert_path_to_field_name(path)] = val;
                                }
                            }

                            break;

                        //case "date":
                        case "datetime":
                        case "time":
                            if (val != null)
                            {
                                row [convert_path_to_field_name(path)] = val;
                            }
                            break;

                        default:
                            if (val != null)
                            {
                                row [convert_path_to_field_name(path)] = val;
                            }
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
                path_to_csv_writer[core_file_name].Table.Rows.Add(row);
            }


            Dictionary <string, string> int_to_path_map = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (KeyValuePair <string, int> ptn in path_to_int_map)
            {
                //int_to_path_map.Add(ptn.Value.ToString("X"), ptn.Key);
                string key = convert_path_to_field_name(ptn.Key);
                if (int_to_path_map.ContainsKey(key))
                {
                    int_to_path_map.Add("_" + ptn.Value.ToString("X"), ptn.Key);
                }
                else
                {
                    int_to_path_map.Add(key, ptn.Key);
                }
            }

            WriteCSV mapping_document = new WriteCSV("core_field_mapping.csv", this.item_directory_name);

            System.Data.DataColumn column = null;

            column = new System.Data.DataColumn("file_name", typeof(string));
            mapping_document.Table.Columns.Add(column);

            column = new System.Data.DataColumn("mmria_path", typeof(string));
            mapping_document.Table.Columns.Add(column);

            column = new System.Data.DataColumn("mmria_prompt", typeof(string));
            mapping_document.Table.Columns.Add(column);

            column = new System.Data.DataColumn("column_name", typeof(string));
            mapping_document.Table.Columns.Add(column);


            foreach (KeyValuePair <string, WriteCSV> kvp in path_to_csv_writer)
            {
                foreach (System.Data.DataColumn table_column in kvp.Value.Table.Columns)
                {
                    System.Data.DataRow mapping_row = mapping_document.Table.NewRow();
                    mapping_row["file_name"] = kvp.Key;

                    if (int_to_path_map.ContainsKey(table_column.ColumnName))
                    {
                        string path = int_to_path_map [table_column.ColumnName];
                        mapping_row["mmria_path"]    = path;
                        mapping_row ["mmria_prompt"] = path_to_node_map [path].prompt;
                    }
                    else
                    {
                        switch (table_column.ColumnName)
                        {
                        case "_record_index":
                        case "_parent_index":
                        default:
                            mapping_row["mmria_path"] = table_column.ColumnName;
                            break;
                        }
                    }

                    mapping_row["column_name"] = table_column.ColumnName;


                    mapping_document.Table.Rows.Add(mapping_row);
                }



                kvp.Value.WriteToStream();
            }

            mapping_document.WriteToStream();


            mmria.server.util.cFolderCompressor folder_compressor = new mmria.server.util.cFolderCompressor();

            folder_compressor.Compress
            (
                System.IO.Path.Combine(System.Configuration.ConfigurationManager.AppSettings["export_directory"], this.item_file_name),
                null,                // string password
                System.IO.Path.Combine(System.Configuration.ConfigurationManager.AppSettings["export_directory"], this.item_directory_name)
            );



            var               get_item_curl      = new cURL("GET", null, Program.config_couchdb_url + "/export_queue/" + this.item_id, null, this.user_name, this.password);
            string            responseFromServer = get_item_curl.execute();
            export_queue_item export_queue_item  = Newtonsoft.Json.JsonConvert.DeserializeObject <export_queue_item> (responseFromServer);

            export_queue_item.status = "Download";

            Newtonsoft.Json.JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings();
            settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            string object_string = Newtonsoft.Json.JsonConvert.SerializeObject(export_queue_item, settings);
            var    set_item_curl = new cURL("PUT", null, Program.config_couchdb_url + "/export_queue/" + export_queue_item._id, object_string, this.user_name, this.password);

            responseFromServer = set_item_curl.execute();


            Console.WriteLine("{0} Export Finished.", System.DateTime.Now);
        }
Example #16
0
        public bool set_value(mmria.common.metadata.app p_metadata, IDictionary <string, object> p_object, string p_path, object p_value, string p_mmrds_path, string p_prompt = null, string p_source_path = null)
        {
            bool result = false;

            try
            {
                string[]      path                 = p_path.Split('/');
                List <string> built_path           = new List <string>();
                List <string> dictionary_path_list = new List <string>();

                System.Text.RegularExpressions.Regex number_regex = new System.Text.RegularExpressions.Regex(@"^\d+$");

                //IDictionary<string, object> index = p_object;
                dynamic index = p_object;

                //if (path.Length > 3 && path[3].Trim() == "time_of_injury")
                //if(p_path == "er_visit_and_hospital_medical_records/internal_transfers/date_and_time"
                //if(p_path.IndexOf("committee_review/pmss_mm") > -1 && p_object["_id"].ToString() == "d0e08da8-d306-4a9a-a5ff-9f1d54702091")

                for (int i = 0; i < path.Length; i++)
                {
                    built_path.Add(path[i]);


                    if (!number_regex.IsMatch(path[i]))
                    {
                        dictionary_path_list.Add(path[i]);
                    }

                    if (number_regex.IsMatch(path[i]))
                    {
                        IList <object> temp_list = index as IList <object>;
                        if (!(temp_list.Count > int.Parse(path[i])))
                        {
                            var node = get_metadata_node(p_metadata, string.Join("/", built_path.ToArray()));
                            Dictionary <string, object> temp = new Dictionary <string, object>();
                            create_default_object(node, temp);
                            ((IList <object>)index).Add(((IList <object>)temp[path[i - 1]])[0]);

                            /*
                             * if (node.type.ToLower() == "grid")
                             * {
                             *      ((IList<object>)index).Add(((IList<object>)temp[path[i - 1]])[0]);
                             * }
                             * else
                             * {
                             *      ((IList<object>)index).Add(temp[path[i - 1]]);
                             * }*/
                        }

                        index = index[int.Parse(path[i])] as IDictionary <string, object>;
                    }
                    else if (index[path[i]] is IList <object> )
                    {
                        index = index[path[i]] as IList <object>;
                    }
                    else if (index[path[i]] is IDictionary <string, object> && !index.ContainsKey(path[i]))
                    {
                        System.Console.WriteLine("Index not found. This should not happen. {0}", p_path);
                    }

                    else if (index[path[i]] is IDictionary <string, object> )
                    {
                        index = index[path[i]] as IDictionary <string, object>;
                    }
                    else if (i == path.Length - 1)
                    {
                        //System.Console.WriteLine("Set Type: {0}", index[path[i]].GetType());
                        string metadata_path = string.Join("/", built_path.ToArray());
                        var    node          = get_metadata_node(p_metadata, metadata_path);

                        string dictionary_path = null;

                        if (p_source_path != null)
                        {
                            dictionary_path = p_source_path + "|" + string.Join("/", dictionary_path_list.ToArray());
                        }
                        else
                        {
                            dictionary_path = string.Join("/", dictionary_path_list.ToArray()).Trim();
                        }

                        string key_check = null;
                        if (p_value != null)
                        {
                            key_check = System.Text.RegularExpressions.Regex.Replace(p_value.ToString().Trim(), @"[^\u0000-\u007F]+", string.Empty);
                        }

                        /*
                         * if (
                         *              p_path.IndexOf ("autopsy_report/causes_of_death/type") > -1 //&&
                         *              //p_value != null &&
                         *              //p_value.ToString ().IndexOf ("20.1") > -1
                         *
                         * )
                         * {
                         *
                         *      //birth_certificate_infant_fetal_section/method_of_delivery/fetal_delivery
                         *      //birth_certificate_infant_fetal_section/congenital_anomalies
                         *
                         *      key_check = System.Text.RegularExpressions.Regex.Replace (p_value.ToString ().Trim (), @"[^\u0000-\u007F]+", string.Empty);
                         *
                         *      if (this.lookup_value1.ContainsKey (dictionary_path))
                         *      {
                         *
                         *              if (this.lookup_value1 [dictionary_path].ContainsKey (key_check))
                         *              {
                         *                      System.Console.WriteLine ("break");
                         *              }
                         *              else
                         *              {
                         *                      System.Console.WriteLine ("break");
                         *              }
                         *
                         *      }
                         *      else
                         *      {
                         *              System.Console.WriteLine ("break");
                         *      }
                         *
                         *
                         *      if (this.lookup_value2.ContainsKey (dictionary_path))
                         *      {
                         *
                         *              if (this.lookup_value2 [dictionary_path].ContainsKey (key_check))
                         *              {
                         *                      System.Console.WriteLine ("break");
                         *              }
                         *              else
                         *              {
                         *                      System.Console.WriteLine ("break");
                         *              }
                         *
                         *      }
                         *      else
                         *      {
                         *              System.Console.WriteLine ("break");
                         *      }
                         *
                         *
                         *      System.Console.WriteLine ("break");
                         * }*/



                        if (
                            node.type.Equals("list", StringComparison.OrdinalIgnoreCase) &&
                            node.is_multiselect != null &&
                            node.is_multiselect == true
                            )
                        {
                            if (p_value is bool)
                            {
                                if ((bool)p_value)
                                {
                                    if (
                                        this.lookup_value1.ContainsKey(dictionary_path) &&
                                        this.lookup_value1[dictionary_path].ContainsKey(p_prompt)
                                        )
                                    {
                                        ((List <string>)index[path[i]]).Add(this.lookup_value1[dictionary_path][p_prompt]);
                                        result = true;
                                    }
                                    else if (
                                        this.lookup_value2.ContainsKey(dictionary_path) &&
                                        this.lookup_value2[dictionary_path].ContainsKey(p_prompt)

                                        )
                                    {
                                        ((List <string>)index[path[i]]).Add(this.lookup_value2[dictionary_path][p_prompt]);
                                        result = true;
                                    }
                                    else
                                    {
                                        ((List <string>)index[path[i]]).Add(p_prompt);
                                        result = true;
                                    }
                                }
                            }
                            else
                            {
                                if (
                                    this.lookup_value1.ContainsKey(dictionary_path) &&
                                    this.lookup_value1[dictionary_path].ContainsKey(p_value.ToString().Trim().Replace(char.ConvertFromUtf32(8211), "-"))
                                    )
                                {
                                    index[path[i]] = this.lookup_value1[dictionary_path][p_value.ToString().Trim().Replace(char.ConvertFromUtf32(8211), "-")];
                                    result         = true;
                                }
                                else if (
                                    this.lookup_value2.ContainsKey(dictionary_path) &&
                                    this.lookup_value2[dictionary_path].ContainsKey(p_value.ToString().Trim().Replace(char.ConvertFromUtf32(8211), "-"))

                                    )
                                {
                                    index[path[i]] = this.lookup_value2[dictionary_path][p_value.ToString().Trim().Replace(char.ConvertFromUtf32(8211), "-")];
                                    result         = true;
                                }
                                else
                                {
                                    if (p_value is string)
                                    {
                                        index[path[i]] = p_value.ToString().Trim();
                                    }
                                    else
                                    {
                                        index[path[i]] = p_value;
                                    }
                                    result = true;
                                }
                            }
                        }
                        else
                        {
                            if
                            (
                                node.type.ToLower() == "list" &&
                                p_value is bool
                            )
                            {
                                if ((bool)p_value)
                                {
                                    if (
                                        this.lookup_value1.ContainsKey(dictionary_path) &&
                                        this.lookup_value1[dictionary_path].ContainsKey(p_prompt)
                                        )
                                    {
                                        index[path[i]] = this.lookup_value1[dictionary_path][p_prompt];
                                        result         = true;
                                    }
                                    else if (
                                        this.lookup_value2.ContainsKey(dictionary_path) &&
                                        this.lookup_value2[dictionary_path].ContainsKey(p_prompt)

                                        )
                                    {
                                        index[path[i]] = this.lookup_value2[dictionary_path][p_prompt];
                                        result         = true;
                                    }
                                    else
                                    {
                                        index[path[i]] = p_prompt;
                                        result         = true;
                                    }
                                }
                            }
                            else if (
                                this.lookup_value1.ContainsKey(dictionary_path) &&
                                this.lookup_value1[dictionary_path].ContainsKey(key_check)
                                )
                            {
                                index[path[i]] = this.lookup_value1[dictionary_path][key_check];
                            }
                            else if (
                                this.lookup_value2.ContainsKey(dictionary_path) &&
                                this.lookup_value2[dictionary_path].ContainsKey(key_check)

                                )
                            {
                                index[path[i]] = this.lookup_value2[dictionary_path][key_check];
                            }
                            else
                            {
                                if (p_value is string)
                                {
                                    index [path [i]] = p_value.ToString().Trim();
                                }
                                else if (node.type.Equals("datetime", StringComparison.OrdinalIgnoreCase))
                                {
                                    if (p_value == DBNull.Value)
                                    {
                                        index [path [i]] = p_value;
                                    }
                                    else
                                    {
                                        index [path [i]] = ((DateTime)p_value).ToUniversalTime();
                                    }
                                }
                                else if (node.type.Equals("date", StringComparison.OrdinalIgnoreCase))
                                {
                                    if (p_value == DBNull.Value)
                                    {
                                        index [path [i]] = p_value;
                                    }
                                    else
                                    {
                                        DateTime temp = (DateTime)p_value;

                                        temp = temp.AddHours(-temp.Hour);
                                        temp = temp.AddMinutes(-temp.Minute);
                                        temp = temp.AddSeconds(-temp.Second);
                                        temp = temp.AddMilliseconds(-temp.Millisecond);

                                        index [path [i]] = temp.ToUniversalTime();
                                    }
                                }
                                else if (node.type.Equals("time", StringComparison.OrdinalIgnoreCase))
                                {
                                    if (p_value == DBNull.Value)
                                    {
                                        index [path [i]] = p_value;
                                    }
                                    else
                                    {
                                        DateTime temp  = new DateTime(1900, 01, 01);
                                        DateTime temp2 = (DateTime)p_value;
                                        temp = temp.Add(temp2.TimeOfDay);
                                        //index [path [i]] = temp.ToUniversalTime ();
                                        index [path [i]] = temp.ToShortTimeString();
                                    }
                                }
                                else if (p_value is List <object> )
                                {
                                    List <object> temp = p_value as List <object>;
                                    if (temp != null && temp.Count > 0)
                                    {
                                        index [path [i]] = string.Join("|", temp);
                                    }
                                    else
                                    {
                                        index[path[i]] = p_value;
                                    }
                                }
                                else if (p_value == DBNull.Value)
                                {
                                    index [path [i]] = p_value;
                                }
                                else
                                {
                                    index[path[i]] = p_value;
                                }
                            }
                            result = true;
                        }
                    }
                    else
                    {
                        System.Console.WriteLine("This should not happen. {0}", p_path);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Text.RegularExpressions.Regex index_path_regex = new System.Text.RegularExpressions.Regex(@"\d+/");

                string key = string.Format("bad mmria path: {0} {1}", p_mmrds_path, index_path_regex.Replace(p_path, ""));
                if (!FoundPaths.Contains(key))
                {
                    FoundPaths.Add(key);
                    BadMappingLog.WriteLine(key);
                }
                //System.Console.WriteLine("case_maker.set_value bad mapping {0}\n {1}", p_path, ex);
            }

            return(result);
        }
Example #17
0
        public void Execute(string[] args)
        {
            string import_directory = System.Configuration.ConfigurationManager.AppSettings["import_directory"];

            this.is_offline_mode = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["is_offline_mode"]);

            if (!System.IO.Directory.Exists(import_directory))
            {
                System.IO.Directory.CreateDirectory(import_directory);
            }


            if (args.Length > 1)
            {
                for (var i = 1; i < args.Length; i++)
                {
                    string arg   = args[i];
                    int    index = arg.IndexOf(':');
                    string val   = arg.Substring(index + 1, arg.Length - (index + 1)).Trim(new char[] { '\"' });

                    if (arg.ToLower().StartsWith("auth_token"))
                    {
                        this.auth_token = val;
                    }
                    else if (arg.ToLower().StartsWith("user_name"))
                    {
                        this.user_name = val;
                    }
                    else if (arg.ToLower().StartsWith("password"))
                    {
                        this.password = val;
                    }
                    else if (arg.ToLower().StartsWith("database_file_path"))
                    {
                        this.database_path = val;
                    }
                    else if (arg.ToLower().StartsWith("url"))
                    {
                        this.mmria_url = val;
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(this.database_path))
            {
                System.Console.WriteLine("missing database_path");
                System.Console.WriteLine(" form database:[file path]");
                System.Console.WriteLine(" example 1 database_file_path:c:\\temp\\maternal_mortality.mdb");
                System.Console.WriteLine(" example 2 database_file_path:\"c:\\temp folder\\maternal_mortality.mdb\"");
                System.Console.WriteLine(" example 3 database_file_path:mapping-file-set\\maternal_mortality.mdb\"");
                System.Console.WriteLine(" mmria.exe import user_name:user1 password:secret url:http://localhost:12345 database_file_path:\"c:\\temp folder\\maternal_mortality.mdb\"");

                return;
            }

            if (string.IsNullOrWhiteSpace(this.mmria_url))
            {
                this.mmria_url = System.Configuration.ConfigurationManager.AppSettings["web_site_url"];


                if (string.IsNullOrWhiteSpace(this.mmria_url))
                {
                    System.Console.WriteLine("missing url");
                    System.Console.WriteLine(" form url:[website_url]");
                    System.Console.WriteLine(" example url:http://localhost:12345");

                    return;
                }
            }

            if (string.IsNullOrWhiteSpace(this.user_name))
            {
                System.Console.WriteLine("missing user_name");
                System.Console.WriteLine(" form user_name:[user_name]");
                System.Console.WriteLine(" example user_name:user1");
                return;
            }

            if (string.IsNullOrWhiteSpace(this.password))
            {
                System.Console.WriteLine("missing password");
                System.Console.WriteLine(" form password:[password]");
                System.Console.WriteLine(" example password:secret");
                return;
            }

            var mmria_server = new mmria_server_api_client(this.mmria_url);

            mmria_server.login(this.user_name, this.password);

            mmria.common.metadata.app metadata = mmria_server.get_metadata();

            var mmrds_data = new cData(get_mdb_connection_string(this.database_path));

            var directory_path         = @"mapping-file-set";
            var main_mapping_file_name = @"MMRDS-Mapping-NO-GRIDS-test.csv";
            var mapping_data           = new cData(get_csv_connection_string(directory_path));

            var grid_mapping_file_name = @"grid-mapping-merge.csv";


            var lookup_mapping_file_name = @"MMRDS-Mapping-NO-GRIDS-lookup-values.csv";
            var lookup_mapping_table     = get_look_up_mappings(mapping_data, lookup_mapping_file_name);


            System.Collections.Generic.Dictionary <string, Dictionary <string, string> > lookup_value1 = new Dictionary <string, Dictionary <string, string> >(StringComparer.InvariantCultureIgnoreCase);
            System.Collections.Generic.Dictionary <string, Dictionary <string, string> > lookup_value2 = new Dictionary <string, Dictionary <string, string> >(StringComparer.InvariantCultureIgnoreCase);


            var case_maker = new Case_Maker(import_directory, lookup_value1, lookup_value2);

            foreach (System.Data.DataRow row in lookup_mapping_table.Rows)
            {
                //mmria_path value1  value2 mmria_value
                //string mmria_path = row["mmria_path"].ToString().Trim();
                string mmria_path  = row["source_path"].ToString().Trim() + "|" + row["mmria_path"].ToString().Trim();
                string value1      = null;
                string value2      = null;
                string mmria_value = null;

                /*
                 * if (
                 *      //row["mmria_path"].ToString().Trim() == "home_record/state_of_death_record" ||
                 *      //row["source_path"].ToString().Trim() == "MaternalMortality1.MM_MBC" ||
                 *      //row["mmria_path"].ToString ().IndexOf("other significant") > -1 &&
                 *      //	row ["value1"] != DBNull.Value &&
                 *              row ["value1"].ToString ().IndexOf ("ther significant") > -1
                 * )
                 * {
                 *      System.Console.Write ("break");
                 *
                 * }*/

                if (row["value1"] != DBNull.Value)
                {
                    value1 = System.Text.RegularExpressions.Regex.Replace(row ["value1"].ToString().Trim(), @"[^\u0000-\u007F]+", string.Empty);
                }

                if (row["value2"] != DBNull.Value)
                {
                    value2 = System.Text.RegularExpressions.Regex.Replace(row["value2"].ToString().Trim(), @"[^\u0000-\u007F]+", string.Empty);
                }

                if (row["mmria_value"] != DBNull.Value)
                {
                    if (row["mmria_value"].ToString() == "(blank)")
                    {
                        mmria_value = "";
                    }
                    else
                    {
                        mmria_value = row["mmria_value"].ToString().Trim();
                    }
                }

                if (value1 == null && mmria_value != null && mmria_value == "")
                {
                    value1 = "";
                }


                if (value2 == null && mmria_value != null && mmria_value == "")
                {
                    value2 = "";
                }

                if (!lookup_value1.ContainsKey(mmria_path))
                {
                    lookup_value1[mmria_path] = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);
                }

                if (!lookup_value2.ContainsKey(mmria_path))
                {
                    lookup_value2[mmria_path] = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);
                }

                if (mmria_value != null && value1 != null && !lookup_value1[mmria_path].ContainsKey(value1))
                {
                    lookup_value1[mmria_path].Add(value1, mmria_value);
                }
                else if (mmria_value != null && value1 != null && mmria_value != "" && value1 != "")
                {
                    case_maker.add_bad_mapping(string.Format("duplicate lookup: {0} - {1}", mmria_path, value1));
                }

                if (mmria_value != null && value2 != null && !lookup_value2[mmria_path].ContainsKey(value2))
                {
                    lookup_value2[mmria_path].Add(value2, mmria_value);
                }
                else if (mmria_value != null && value2 != null && mmria_value != "" && value2 != "")
                {
                    case_maker.add_bad_mapping(string.Format("duplicate lookup: {0} - {1}", mmria_path, value2));
                }
            }


            var case_data_list = new List <dynamic>();


            var view_name_list = new string[] {
                "MaternalMortality",
                "DeathCertificate",
                "MaternalBirthCertificate",
                "ChildBirthCertificate",
                "AutopsyReport",
                "PrenatalCareRecord",
                "SocialServicesRecord",
                "Hospitalization",
                "OfficeVisits",
                "CommitteeReview",
                "Interviews"
            };


            var view_name_to_name_map = new Dictionary <string, string> {
                { "MaternalMortality", "home_record" },
                { "DeathCertificate", "death_certificate" },
                { "MaternalBirthCertificate", "birth_fetal_death_certificate_parent" },
                { "ChildBirthCertificate", "birth_certificate_infant_fetal_section" },
                { "AutopsyReport", "autopsy_report" },
                { "PrenatalCareRecord", "prenatal" },
                { "SocialServicesRecord", "social_and_environmental_profile" },
                { "Hospitalization", "er_visit_and_hospital_medical_records" },
                { "OfficeVisits", "other_medical_office_visits" },
                { "CommitteeReview", "committe_review" },
                { "Interviews", "informant_interviews" }
            };

            var view_name_cardinality_map = new Dictionary <string, bool> {
                { "MaternalMortality", false },
                { "DeathCertificate", false },
                { "MaternalBirthCertificate", false },
                { "ChildBirthCertificate", true },
                { "AutopsyReport", false },
                { "PrenatalCareRecord", false },
                { "SocialServicesRecord", false },
                { "Hospitalization", true },
                { "OfficeVisits", true },
                { "CommitteeReview", false },
                { "Interviews", true }
            };

            var           id_record_set = mmrds_data.GetDataTable("Select Distinct GlobalRecordId From MaternalMortality");
            string        json_string   = null;
            List <string> id_list       = new List <string>();

            foreach (System.Data.DataRow row in id_record_set.Rows)
            {
                /*
                 * if
                 * (
                 *      row[0].ToString() != "d0e08da8-d306-4a9a-a5ff-9f1d54702091" &&
                 *      row[0].ToString() != "244da20f-41cc-4300-ad94-618004a51917" &&
                 *      row[0].ToString() != "e98ce2be-4446-439a-bb63-d9b4e690e3c3" &&
                 *      row[0].ToString() != "b5003bc5-1ab3-4ba2-8aea-9f3717c9682a"
                 * )
                 * {
                 *      continue;
                 * }*/


                id_list.Add(row[0].ToString());
            }

            foreach (string global_record_id in id_list)
            {
                dynamic case_data = case_maker.create_default_object(metadata, new Dictionary <string, object>());

                json_string = Newtonsoft.Json.JsonConvert.SerializeObject(case_data, new Newtonsoft.Json.JsonSerializerSettings()
                {
                    Formatting           = Newtonsoft.Json.Formatting.Indented,
                    DateFormatHandling   = Newtonsoft.Json.DateFormatHandling.IsoDateFormat,
                    DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc
                });
                System.Console.WriteLine("json\n{0}", json_string);

                foreach (string view_name in view_name_list)
                {
                    System.Data.DataRow[] view_record_data = null;
                    System.Data.DataRow[] grid_record_data = null;
                    System.Data.DataTable view_data_table  = get_view_data_table(mmrds_data, view_name);

                    var mapping_view_table = get_view_mapping(mapping_data, view_name, main_mapping_file_name);

                    var grid_table_name_list = get_grid_table_name_list(mapping_data, view_name, grid_mapping_file_name);

                    if (view_name == "MaternalMortality")
                    {
                        view_record_data = view_data_table.Select(string.Format("MaternalMortality.GlobalRecordId='{0}'", global_record_id));

                        IDictionary <string, object> updater = case_data as IDictionary <string, object>;
                        updater["_id"]               = global_record_id;
                        updater["date_created"]      = view_record_data[0]["FirstSaveTime"] != DBNull.Value ? ((DateTime)view_record_data[0]["FirstSaveTime"]).ToString("s") + "Z" : null;
                        updater["created_by"]        = view_record_data[0]["FirstSaveLogonName"];
                        updater["date_last_updated"] = view_record_data[0]["LastSaveTime"] != DBNull.Value ? ((DateTime)view_record_data[0]["LastSaveTime"]).ToString("s") + "Z" : null;
                        updater["last_updated_by"]   = view_record_data[0]["LastSaveLogonName"];
                    }
                    else
                    {
                        view_record_data = view_data_table.Select(string.Format("FKEY='{0}'", global_record_id));
                    }


                    if (view_record_data.Length > 1)
                    {
                        System.Console.WriteLine("multi rows: {0}\t{1}", view_name, view_record_data.Length);
                    }
                    //mmria.common.metadata.node form_metadata = metadata.children.Where(c => c.type == "form" && c.name == view_name_to_name_map[view_name]).First();
                    if (view_name_cardinality_map[view_name] == true)
                    {
                        for (int i = 0; i < view_record_data.Length; i++)
                        {
                            System.Data.DataRow row = view_record_data[i];

                            process_view
                            (
                                metadata,
                                case_maker,
                                case_data,
                                row,
                                mapping_view_table,
                                i
                            );

                            int column_index = -1;
                            for (int column_index_i = 0; column_index_i < view_data_table.Columns.Count; column_index_i++)
                            {
                                if (view_data_table.Columns[column_index_i].ColumnName.ToLower() == (view_name + ".globalrecordid").ToLower())
                                {
                                    column_index = column_index_i;
                                }
                            }

                            foreach (string grid_name in grid_table_name_list)
                            {
                                System.Data.DataTable grid_data = mmrds_data.GetDataTable(string.Format("Select * From [{0}] Where FKey='{1}'", grid_name, row[column_index]));
                                var grid_mapping = get_grid_mapping(mapping_data, grid_name, grid_mapping_file_name);

                                if (grid_data.Rows.Count > 0)
                                {
                                }

                                for (int grid_row_index = 0; grid_row_index < grid_data.Rows.Count; grid_row_index++)
                                {
                                    System.Data.DataRow grid_row = grid_data.Rows[grid_row_index];

                                    process_grid
                                    (
                                        metadata,
                                        case_maker,
                                        case_data,
                                        grid_row,
                                        grid_mapping,
                                        i,
                                        grid_row_index
                                    );
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (System.Data.DataRow row in view_record_data)
                        {
                            process_view
                            (
                                metadata,
                                case_maker,
                                case_data,
                                row,
                                mapping_view_table
                            );


                            int column_index = -1;
                            for (int column_index_i = 0; column_index_i < view_data_table.Columns.Count; column_index_i++)
                            {
                                if (view_data_table.Columns[column_index_i].ColumnName.ToLower() == (view_name + ".globalrecordid").ToLower())
                                {
                                    column_index = column_index_i;
                                }
                            }

                            foreach (string grid_name in grid_table_name_list)
                            {
                                System.Data.DataTable grid_data = null;
                                int check_index = grid_name.IndexOf("/");
                                if (check_index > 0)
                                {
                                    grid_data = mmrds_data.GetDataTable(string.Format("Select * From [{0}] Where FKey='{1}'", grid_name.Substring(0, check_index), row[column_index]));
                                }
                                else
                                {
                                    grid_data = mmrds_data.GetDataTable(string.Format("Select * From [{0}] Where FKey='{1}'", grid_name, row[column_index]));
                                }

                                var grid_mapping = get_grid_mapping(mapping_data, grid_name, grid_mapping_file_name);

                                if (grid_data.Rows.Count > 0)
                                {
                                }

                                if (check_index > 0)
                                {
                                    int grid_row_index = int.Parse(grid_name.Substring(check_index + 1, grid_name.Length - (check_index + 1)));
                                    if (grid_data.Rows.Count > grid_row_index)
                                    {
                                        process_grid
                                        (
                                            metadata,
                                            case_maker,
                                            case_data,
                                            grid_data.Rows[grid_row_index],
                                            grid_mapping,
                                            null,
                                            null
                                        );
                                    }
                                }
                                else
                                {
                                    for (int grid_row_index = 0; grid_row_index < grid_data.Rows.Count; grid_row_index++)
                                    {
                                        System.Data.DataRow grid_row = grid_data.Rows[grid_row_index];

                                        process_grid
                                        (
                                            metadata,
                                            case_maker,
                                            case_data,
                                            grid_row,
                                            grid_mapping,
                                            null,
                                            grid_row_index
                                        );
                                    }
                                }
                            }
                        }
                    }
                }



                /*
                 * if (case_data_list.Count == 0)
                 * {
                 *      var result = mmria_server.set_case(json_string);
                 * }
                 */
                try
                {
                    // check if doc exists
                    string document_url  = System.Configuration.ConfigurationManager.AppSettings["couchdb_url"] + "/mmrds/" + global_record_id;
                    var    document_curl = new cURL("GET", null, document_url, null, this.user_name, this.password);
                    string document_json = null;

                    document_json = document_curl.execute();
                    var result = Newtonsoft.Json.JsonConvert.DeserializeObject <System.Dynamic.ExpandoObject>(document_json);
                    IDictionary <string, object> updater           = case_data as IDictionary <string, object>;
                    IDictionary <string, object> result_dictionary = result as IDictionary <string, object>;
                    if (result_dictionary.ContainsKey("_rev"))
                    {
                        updater ["_rev"] = result_dictionary ["_rev"];


                        json_string = Newtonsoft.Json.JsonConvert.SerializeObject(case_data, new Newtonsoft.Json.JsonSerializerSettings()
                        {
                            Formatting           = Newtonsoft.Json.Formatting.Indented,
                            DateFormatHandling   = Newtonsoft.Json.DateFormatHandling.IsoDateFormat,
                            DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc
                        });
                        System.Console.WriteLine("json\n{0}", json_string);
                    }


                    IDictionary <string, object> home_record = result_dictionary["home_record"] as IDictionary <string, object>;

                    if (home_record.ContainsKey("jurisdiction_id"))
                    {
                        home_record["jurisdiction_id"] = "/";
                    }
                    else
                    {
                        home_record.Add("jurisdiction_id", "/");
                    }

                    var update_curl = new cURL("PUT", null, document_url, json_string, this.user_name, this.password);
                    try
                    {
                        string de_id_result = update_curl.execute();
                        System.Console.WriteLine("update id");
                        System.Console.WriteLine(de_id_result);
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine("sync de_id");
                        System.Console.WriteLine(ex);
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine("Get case");
                    System.Console.WriteLine(ex);

                    IDictionary <string, object> home_record = case_data["home_record"] as IDictionary <string, object>;

                    if (home_record.ContainsKey("jurisdiction_id"))
                    {
                        home_record["jurisdiction_id"] = "/";
                    }
                    else
                    {
                        home_record.Add("jurisdiction_id", "/");
                    }


                    json_string = Newtonsoft.Json.JsonConvert.SerializeObject(case_data, new Newtonsoft.Json.JsonSerializerSettings()
                    {
                        Formatting           = Newtonsoft.Json.Formatting.Indented,
                        DateFormatHandling   = Newtonsoft.Json.DateFormatHandling.IsoDateFormat,
                        DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc
                    });

                    string document_url = System.Configuration.ConfigurationManager.AppSettings["couchdb_url"] + "/mmrds/" + global_record_id;
                    var    update_curl  = new cURL("PUT", null, document_url, json_string, this.user_name, this.password);
                    try
                    {
                        string de_id_result = update_curl.execute();
                        System.Console.WriteLine("update id");
                        System.Console.WriteLine(de_id_result);
                    }
                    catch (Exception ex2)
                    {
                        System.Console.WriteLine("sync de_id");
                        System.Console.WriteLine(ex2);
                    }
                    System.Console.WriteLine("json\n{0}", json_string);
                }



                //return;
                System.IO.File.WriteAllText(import_directory + "/" + global_record_id + ".json", json_string);

                //break;
                case_data_list.Add(case_data);
            }
            case_maker.flush_bad_mapping();

            Console.WriteLine("Hello World!");

            System.IO.File.WriteAllText(import_directory + "/output.json", json_string);
        }