//===============================================================================
// Locator_Title web application
//===============================================================================
// Author: Karen Chakhmakhchyan
// Date: 07/19/2007
//===============================================================================
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using ACS.I2.Engine.Models;
/// <summary>
/// Summary description for ServiceLocator
/// </summary>
///
namespace ACS.I2.Engine.ModelProcessor
{
public static class ServiceLocator
{
static string fn1 = "\\ServiceStore0.js";
static string fn2 = "\\ServiceStore1.js";
static string msEOL = Environment.NewLine;
const string xmlStatePath = "//State";
const string County = "//County";
const string AttrName = "Name";
const string AttrFips = "Fipscode";
const string stAttrName = "StateName";
const string stAttrFips = "stFipscode";
const string stAttrCode = "code";
const string sSearchGate = "//Searchgate";
private static string msURLCurrentJSfile;
private static IList<State> states;
public static IList<State> States
{
get { return states; }
}
public static string JSStorage
{
get { return msURLCurrentJSfile; }
}
public static bool GenerateJS(string mappath, string xmlFileName)
{
// NOTE;!!!
//--------------------------------------------------------------------------------
///in case where generated exception last catch will be in Locator.ascx.cs file
///-------------------------------------------------------------------------------
string jspath1 = mappath + fn1;
string storeDir = mappath.Substring(mappath.LastIndexOf("\\") + 1);
string CurrentJSfile;
try
{
if (File.Exists(jspath1))
{
File.Delete(jspath1);
CurrentJSfile = mappath + fn2;
msURLCurrentJSfile = storeDir + "/" + fn2.Substring(fn2.LastIndexOf("\\") + 1);
//File.Create(msCurrentJSfile);
}
else
{
if (File.Exists(mappath + fn2))
File.Delete(mappath + fn2);
CurrentJSfile = mappath + fn1;
msURLCurrentJSfile = storeDir + "/" + fn1.Substring(fn1.LastIndexOf("\\") + 1);
//File.Create(msCurrentJSfile);
}
states = ServiceArray(xmlFileName);
CreateJS(CurrentJSfile, states);
}
catch (Exception ex)
{
throw ex;
}
return true;
}
private static SortedList<string, string> SortStates(IList<State> states,
ref SortedList<string, string> sortedCountiesNames,
ref SortedList<string, string> sortedCountiesNamesServices,
ref SortedList<string, string> sortedCountiesFips,
ref SortedList<string, string> sortedCountiesFipsServices)
{
SortedList<string, string> sortedStates = new SortedList<string, string>();
foreach (State st in states)
{
sortedStates.Add(st.StateName, st.StateCode);
foreach (County ct in st.Counties)
{
if (sortedCountiesNames.ContainsKey(ct.CountyName + " " + ct.StateCode) == false)
{
sortedCountiesNames.Add(ct.CountyName + " " + ct.StateCode, string.Empty);
sortedCountiesNamesServices.Add(ct.CountyName + " " + ct.StateCode,
ct.Service);
}
if (sortedCountiesFips.ContainsKey(ct.CountyFipsCode + " " + ct.StateCode) == false)
{
sortedCountiesFips.Add(ct.CountyFipsCode + " " + ct.StateCode, string.Empty);
sortedCountiesFipsServices.Add(ct.CountyFipsCode + " " + ct.StateCode,
ct.Service);
}
}
}
return sortedStates;
}
private static bool CreateJS(string jsFileName, IList<State> states)
{
const string StatesArrName = "states";
const string StatesNameCodesArr = "state_codes";
const string StatesNameCodesArrComment = "// states and states code associated array";
const string CountiesArrName = "count";
const string CountyFipsCodesArr = "cfipses";
const string CountyNameServicesArr = "name_services";
const string CountyNameServicesArrComment = "//counties/services assotiative array";
const string CountyFipsServicesArr = "fips_services";
const string CountyFipsServicesArrComment = "// countyfipses/service assotiative array";
SortedList<string, string> sortedCountiesNames = new SortedList<string, string>();
SortedList<string, string> sortedCountiesNamesServices = new SortedList<string, string>();
SortedList<string, string> sortedCountiesFips = new SortedList<string, string>();
SortedList<string, string> sortedCountiesFipsServices = new SortedList<string, string>();
//sorting states and counties
SortedList<string, string> sortStates = SortStates(states
, ref sortedCountiesNames, ref sortedCountiesNamesServices
, ref sortedCountiesFips, ref sortedCountiesFipsServices);
using (StreamWriter sw = new StreamWriter(jsFileName))
{
sw.WriteLine("// This part generated by ServiceLocator");
sw.WriteLine("//-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("//The date was: ");
sw.WriteLine(DateTime.Now);
// create states array
DoArray(sw, StatesArrName, sortStates);
// create statescode assoc array
DoAssocArray(sw, StatesNameCodesArrComment, StatesNameCodesArr, sortStates);
// create array for countiew
DoArray(sw, CountiesArrName, sortedCountiesNames);
// create array for counties' fips codes
DoArray(sw, CountyFipsCodesArr, sortedCountiesFips);
// create county/service associative array
DoAssocArray(sw, CountyNameServicesArrComment, CountyNameServicesArr,
sortedCountiesNamesServices);
// create counyFips/Services associative array
DoAssocArray(sw, CountyFipsServicesArrComment, CountyFipsServicesArr, sortedCountiesFipsServices);
}
return true;
}
private static void DoAssocArray(StreamWriter sw, string comment, string arName, SortedList<string, string> slist)
{
sw.WriteLine("");
sw.WriteLine(comment);
sw.WriteLine(string.Format("var {0} = new Array();", arName));
for (int i = 0; i < slist.Count; i++)
{
sw.WriteLine(string.Format("{0}[\"{1}\"] = \"{2}\";", arName, slist.Keys[i], slist.Values[i]));
}
}
private static void DoArray(StreamWriter sw, string arName, SortedList<string, string> slist)
{
sw.WriteLine("");
sw.WriteLine(string.Format("// {0} array", arName));
sw.WriteLine(string.Format("var {0} = new Array();", arName));
for (int i = 0; i < slist.Count; i++)
{
sw.WriteLine(string.Format("{0}[{1}] = \"{2}\";", arName, i, slist.Keys[i]));
}
}
private static IList<State> ServiceArray(string xmlFileName)
{
IList<State> states = new List<State>();
try
{
XMLFinder xfinder = new XMLFinder(xmlFileName);
int stcount = xfinder.GetItemsCount(xmlStatePath);
for (int i = 1; i <= stcount; i++)
{
IList<County> counties = new List<County>();
string statePath, stName, stcode, stfips;
// combine path for "State[i]" node
statePath = xmlStatePath + '[' + i.ToString() + ']';
XPathNodeIterator stit = xfinder.GetNodeIterator(statePath);
// get State attributes - StateName , stFipscode and code
stName = xfinder.GetAttributeValue(stit, stAttrName);
stcode = xfinder.GetAttributeValue(stit, stAttrCode);
stfips = xfinder.GetAttributeValue(stit, stAttrFips);
// create new instance for State
State state = new State(stName, stfips, stcode);
int countyCount = xfinder.GetItemsCount(statePath + County);
for (int j = 1; j <= countyCount; j++)
{
string cpath = statePath + County + '[' + j.ToString() + ']';
XPathNodeIterator it = xfinder.GetNodeIterator(cpath);
string name = xfinder.GetAttributeValue(it, AttrName);
string fipscode = xfinder.GetAttributeValue(it, AttrFips);
string service = xfinder.GetTextNode(cpath + sSearchGate);
counties.Add(new County(fipscode, name, stfips, service, stcode));
}
state.Counties = counties;
states.Add(state);
}
return states;
}
catch (Exception ex)
{
throw ex;
}
}
}
}