Пример #1
0
        /// <summary>
        /// Send sushi request and convert to csv
        /// </summary>
        /// <param name="reportType"></param>
        /// <param name="fields"></param>
        private static void ProcessSushiRequest(string reportType, string[] fields)
        {
            string fileName = string.Format("{0}_{1}_{2}_{3}_{4}", fields[1], fields[0], StartDate.ToString("yyyyMM"), EndDate.ToString("yyyyMM"), reportType);

            fileName = XmlMode ? fileName + ".xml" : fileName + ".csv";

            string startDateStr = StartDate.Date.ToString("yyyy-MM-dd");
            string endDateStr = EndDate.Date.ToString("yyyy-MM-dd");

            XmlDocument reqDoc = new XmlDocument();
            if (fields.Length == 16 && !string.IsNullOrEmpty(fields[14]) && !string.IsNullOrEmpty(fields[15]))
            {
                // Load WSSE fields for Proquest
                FileStream wsSecurityFile = new FileStream("WSSecurityPlainText.xml", FileMode.Open, FileAccess.Read);
                StreamReader reader = new StreamReader(wsSecurityFile);
                string wsSecuritySnippet = string.Format(reader.ReadToEnd(), fields[14], fields[15]);
                reader.Close();
                reqDoc.LoadXml(
                    string.Format(RequestTemplate, fields[4], fields[5], fields[6], fields[7],
                                  fields[8], reportType, fields[2], startDateStr, endDateStr, wsSecuritySnippet));
            }
            else
            {
                reqDoc.LoadXml(
                    string.Format(RequestTemplate, fields[4], fields[5], fields[6], fields[7],
                                  fields[8], reportType, fields[2], startDateStr, endDateStr, string.Empty));
            }

            string resonseString = CallSushiServer(reqDoc, fields[3]);
            if (XmlMode)
            {
                StreamWriter sw = new StreamWriter(fileName);
                sw.Write(resonseString);
                sw.Flush();
                sw.Close();
                return; // parsing and conversion to csv is unnecessary
            }

            XmlDocument sushiDoc = new XmlDocument();
            sushiDoc.LoadXml(resonseString);

            XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(sushiDoc.NameTable);
            // Proquest Error
            xmlnsManager.AddNamespace("s", "http://www.niso.org/schemas/sushi");
            XmlNode exception = sushiDoc.SelectSingleNode("//s:Exception", xmlnsManager);

            if (exception != null && exception.HasChildNodes)
            {
                Console.WriteLine(string.Format("Exception detected for report of type {0} for Provider: {1}\nPlease see error log for more details.", reportType, fields[1]));
                throw new XmlException(
                    string.Format("Report returned Exception: Number: {0}, Severity: {1}, Message: {2}",
                    exception.SelectSingleNode("s:Number", xmlnsManager).InnerText, exception.SelectSingleNode("s:Severity", xmlnsManager).InnerText, exception.SelectSingleNode("s:Message", xmlnsManager).InnerText));
            }

            SushiReport sushiReport = ReportLoader.LoadCounterReport(sushiDoc);

            if (ValidateMode || StrictValidateMode)
            {
                IsValid = true;

                MemoryStream ms = new MemoryStream();
                sushiDoc.Save(ms);
                ms.Position = 0;
                using (XmlReader xmlReader = XmlReader.Create(new XmlTextReader(ms), XmlReaderSettings))
                {

                    // Read XML to the end
                    while (xmlReader.Read())
                    {
                        // just read through file to trigger any validation errors
                    }
                }

                if (StrictValidateMode)
                {
                    var validator = new CounterValidator();
                    if (!validator.Validate(sushiReport))
                    {
                        IsValid = false;

                        foreach (var error in validator.ErrorMessage)
                        {
                            Console.WriteLine(error);
                        }
                    }
                }

                Console.WriteLine(string.Format("Finished validation Counter report of type {0} for Provider: {1}", reportType, fields[1]));
                if (IsValid)
                {
                    Console.WriteLine("Document is valid");
                }
                else
                {
                    Console.WriteLine("Document is invalid");
                }
            }

            TextWriter tw = new StreamWriter(fileName);
            StringBuilder header;

            Console.WriteLine("Parsing report of type: " + reportType);

            switch(reportType)
            {
                case "JR1":
                    tw.WriteLine("Journal Report 1 (R2),Number of Successful Full-Text Article Requests By Month and Journal");
                    tw.WriteLine(fields[0]);
                    tw.WriteLine("Date run:");
                    tw.WriteLine(DateTime.Now.ToString("yyyy-M-d"));

                    // construct header
                    header = new StringBuilder(",Publisher,Platform,Print ISSN,Online ISSN");
                    for (DateTime currMonth = StartDate; currMonth <= EndDate; currMonth = currMonth.AddMonths(1))
                    {
                        header.Append(",");
                        header.Append(currMonth.ToString("MMM-yy"));
                    }

                    header.Append(",YTD Total,YTD HTML,YTD PDF");
                    tw.WriteLine(header);

                    ParseJR1v3(sushiReport, tw);

                    tw.Close();

                    break;
                case "DB1":
                    tw.WriteLine("Database Report 1 (R2),Total Searches and Sessions by Month and Database");
                    tw.WriteLine(fields[0]);
                    tw.WriteLine("Date run:");
                    tw.WriteLine(DateTime.Now.ToString("yyyy-M-d"));

                    // construct header
                    header = new StringBuilder(",Publisher,Platform,");
                    for (DateTime currMonth = StartDate; currMonth <= EndDate; currMonth = currMonth.AddMonths(1))
                    {
                        header.Append(",");
                        header.Append(currMonth.ToString("MMM-yy"));
                    }

                    header.Append(",YTD Total");
                    tw.WriteLine(header);

                    ParseDB1v3(sushiDoc, tw);

                    tw.Close();
                    break;
                case "DB3":
                    tw.WriteLine("Database Report 3 (R2),Total Searches and Sessions by Month and Service");
                    tw.WriteLine(fields[0]);
                    tw.WriteLine("Date run:");
                    tw.WriteLine(DateTime.Now.ToString("yyyy-M-d"));

                    // construct header
                    header = new StringBuilder(",Platform,");
                    for (DateTime currMonth = StartDate; currMonth <= EndDate; currMonth = currMonth.AddMonths(1))
                    {
                        header.Append(",");
                        header.Append(currMonth.ToString("MMM-yy"));
                    }

                    header.Append(",YTD Total");
                    tw.WriteLine(header);

                    ParseDB3v3(sushiDoc, tw);

                    tw.Close();
                    break;
                default:
                    ErrorFile.WriteLine(string.Format("{0}: Report Type {1} currently not supported.", ErrorDate, reportType));
                    break;
            }
        }
Пример #2
0
        static void Main(string[] args)
        {
            #region Process Arguements
            string validateFile = string.Empty;
            bool specifiedLibCodes = false;
            string start = string.Empty;
            string end = string.Empty;
            string libCodeStr = string.Empty;

            try
            {

                for (int i = 0; i < args.Length; i++)
                {
                    switch (args[i])
                    {

                        case "-v":
                            ValidateMode = true;
                            if (i + 1 < args.Length)
                            {
                                validateFile = args[i + 1];
                            }
                            break;
                        case "-d":
                            if (i + 1 < args.Length)
                            {
                                start = args[i + 1];
                            }
                            if (i + 2 < args.Length)
                            {
                                end = args[i + 2];
                            }
                            break;
                        case "-l":
                            specifiedLibCodes = true;
                            if (i + 1 >= args.Length)
                            {
                                throw new ArgumentException("File not specified for validation mode");
                            }
                            libCodeStr = args[i + 1];
                            break;
                        case "-x":
                            XmlMode = true;
                            break;
                        case "-h":
                            Console.WriteLine(CommandParameterMessage);
                            System.Environment.Exit(-1);
                            break;
                        case "-s":
                            StrictValidateMode = true;
                            if (i + 1 < args.Length)
                            {
                                validateFile = args[i + 1];
                            }
                            break;
                    }
                }
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
            #endregion

            FileStream sushiConfig = new FileStream("sushiconfig.csv", FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(sushiConfig);

            //initiate validation
            XmlSchema CounterSushiSchema = XmlSchema.Read(new XmlTextReader(CounterSchemaURL), new ValidationEventHandler(CounterV3ValidationEventHandler));
            XmlReaderSettings.ValidationType = ValidationType.Schema;
            XmlReaderSettings.Schemas.Add(CounterSushiSchema);
            XmlReaderSettings.ValidationEventHandler += new ValidationEventHandler(CounterV3ValidationEventHandler);

            try
            {

            #region Initialize
                // validate file mode
                if ((ValidateMode || StrictValidateMode) && !string.IsNullOrEmpty(validateFile))
                {
                    using (XmlReader xmlReader = XmlReader.Create(new XmlTextReader(validateFile), XmlReaderSettings))
                    {

                        // Read XML to the end
                        try
                        {
                            while (xmlReader.Read())
                            {
                                // just read through file to trigger any validation errors
                            }

                            Console.WriteLine("\nFinished validating XML file....");
                        }
                        catch (FileNotFoundException e)
                        {
                            Console.WriteLine(e.Message);
                            System.Environment.Exit(-1);
                        }
                    }

                    if (StrictValidateMode)
                    {
                        var file = new FileStream(validateFile, FileMode.Open, FileAccess.Read);
                        var xmlFile = new XmlDocument();
                        xmlFile.Load(file);

                        SushiReport report = ReportLoader.LoadCounterReport(xmlFile);

                        var validator = new CounterValidator();
                        if (!validator.Validate(report))
                        {
                            IsValid = false;

                            foreach (var error in validator.ErrorMessage)
                            {
                                Console.WriteLine(error);
                            }
                        }

                    }

                    if (IsValid)
                    {
                        Console.WriteLine("Document is valid");
                    }
                    else
                    {
                        Console.WriteLine("Document is invalid");
                    }
                }

                else
                {

                    string[] header = sr.ReadLine().Split(DELIM);

                    DateTime startMonth = DateTime.Now.AddMonths(-1);
                    DateTime endMonth = DateTime.Now.AddMonths(-1);

                    if (!string.IsNullOrEmpty(start))
                    {
                        startMonth = DateTime.ParseExact(start, DateTimeFormats, null,
                                                                  DateTimeStyles.None);
                    }
                    if (!string.IsNullOrEmpty(end))
                    {
                        endMonth = DateTime.ParseExact(end, DateTimeFormats, null,
                                                                DateTimeStyles.None);
                    }

                    if (endMonth < startMonth)
                    {
                        throw new ArgumentException("End date is before start date.");
                    }

                    StartDate = new DateTime(startMonth.Year, startMonth.Month, 1);
                    EndDate = new DateTime(endMonth.Year, endMonth.Month,
                                           DateTime.DaysInMonth(endMonth.Year, endMonth.Month));

                    FileStream requestTemplate = new FileStream("SushiSoapEnv.xml", FileMode.Open, FileAccess.Read);
                    StreamReader reader = new StreamReader(requestTemplate);
                    RequestTemplate = reader.ReadToEnd();
                    reader.Close();

                    #endregion

                    Dictionary<string, string> libCodeMap = null;
                    if (specifiedLibCodes)
                    {
                        libCodeMap = new Dictionary<string, string>();

                        string[] libCodes = libCodeStr.Split(DELIM);
                        foreach (string libCode in libCodes)
                        {
                            libCodeMap.Add(libCode.ToUpper(), string.Empty);
                        }
                    }

                    string buffer;
                    for (int lineNum = 1; (buffer = sr.ReadLine()) != null; lineNum++)
                    {
                        string[] fields = buffer.Split(DELIM);

                        if (libCodeMap == null || libCodeMap.ContainsKey(fields[0].ToUpper()))
                        {
                            if (fields.Length < 13)
                            {
                                ErrorFile.WriteLine(string.Format("{0}: Line {1} has insufficient data", ErrorDate,
                                                                  lineNum));
                            }
                            else
                            {
                                //loop through report types in header
                                for (int i = 9; i < 14; i++)
                                {
                                    try
                                    {
                                        if (fields[i].ToLower().StartsWith("y"))
                                        {
                                            ProcessSushiRequest(header[i], fields);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        ErrorFile.WriteLine(
                                            string.Format(
                                                "{0}: Exception occurred processing line {1} for report type {2}",
                                                ErrorDate,
                                                lineNum, header[i]));
                                        ErrorFile.WriteLine(ex.Message);
                                        ErrorFile.Write(ex.StackTrace);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (FormatException ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                sr.Close();
                sushiConfig.Close();
                if (_errorFile != null)
                {
                    _errorFile.Close();
                }
            }
        }