示例#1
0
 public void AddandGetTemplate_TwoTemplates_ShouldPass(Template template)
 {
     var dataStorage = new HospitalDAL(_dataProvider, _connectionString);
     dataStorage.AddTemplate(template);
     Template expected = dataStorage.GetTemplate(template.Title);
     Assert.NotEqual(expected, null);
 }
示例#2
0
        public static Analysis GetAnalysis(IEnumerable<string> analysisData, Template template, DateTime date)
        {
            if (template.Data.Count != analysisData.Count())
            {
                throw new ArgumentException("analysisData");
            }

            return new Analysis(analysisData.ToList(), template.Title, date);
        }
示例#3
0
        public void FillTemplate_HtmlTemplate_ShouldPass()
        {
            var template = new Template(new List<string> {"hemoglobin", "erythrocytes"}, "BloodTest");
            var person = new Person("Igor", "Shein", new DateTime(1994, 4, 20), "Yaroslavl", "123456789");
            var analysis = new Analysis(new List<string> {"20", "50"}, "Blood Test", new DateTime(2014, 08, 3));

            var templateFiller = new TemplateFiller("Blood Test.html");
            string actual = templateFiller.FillTemplate(person, analysis, template);
            //File.WriteAllText("expected.html", actual);
            string expected = File.ReadAllText("expected.html");
            Assert.Equal(expected, actual);
        }
示例#4
0
        public string FillTemplate(Person person, Analysis analysis, Template template)
        {
            InitSubstitutions(person);

            StringBuilder templateContent = GetTemplateContent();

            FillTemplateTitle(template, templateContent);
            FillPersonFields(templateContent);
            FillAnalysisFields(analysis, template, templateContent);

            return templateContent.ToString();
        }
示例#5
0
        private void FillAnalysisFields(Analysis analysis, Template template, StringBuilder templateContent)
        {
            for (int i = 0; i < analysis.Data.Count; ++i)
            {
                string fieldSubstitution = FieldSubstitition + i;
                string valueSubstitution = ValueSubstitition + i;

                CheckFillPosibility(templateContent, fieldSubstitution, valueSubstitution);

                templateContent.Replace(fieldSubstitution, template.Data[i]);
                templateContent.Replace(valueSubstitution, analysis.Data[i]);
            }
        }
示例#6
0
        public static Analysis GetAnalysis(IEnumerable<string> analysisData, Template template, DateTime date)
        {
            if (template.Data.Count != analysisData.Count())
            {
                //LOGGING
                Logger.Warn("Analyzes were not obtained");
                throw new ArgumentException("analysisData");
            }

            //LOGGING
            Logger.Info("Analyzes were obtained");
            return new Analysis(analysisData.ToList(), template.Title, date);
        }
示例#7
0
        private void LoadNewTemplateMenuItem_Click(object sender, RoutedEventArgs e)
        {
            ClearCanvas();

            var newOutputFormatOpenFileDialog = new OpenFileDialog
            {
                Filter = "txt files (*.txt)|*.txt",
                DefaultExt = ".txt",
                Title = "Please select txt file"
            };

            if (newOutputFormatOpenFileDialog.ShowDialog() == true)
            {
                Template template;

                using (var input = new StreamReader(newOutputFormatOpenFileDialog.FileName, Encoding.Default))
                {
                    string line;
                    var fields = new List<string>();

                    while ((line = input.ReadLine()) != null)
                    {
                        if (!string.IsNullOrEmpty(line))
                        {
                            fields.Add(line);
                        }
                    }

                    string fileName = newOutputFormatOpenFileDialog.FileName;
                    template = new Template(fields,
                        Path.GetFileNameWithoutExtension(fileName));
                }

                try
                {
                    _dataAccessLayer.AddTemplate(template);
                    //LOGGING
                    Logger.Info("New Template was successfully added to data access layer.");
                }
                catch (InvalidOperationException ex)
                {
                    //LOGGING
                    Logger.Info("New Template was not added to data access layer.");
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                MessageBox.Show("Template added successfully!", "Information", MessageBoxButton.OK,
                    MessageBoxImage.Information);
            }
        }
示例#8
0
        public Template GetTemplate(string title)
        {
            OpenConnection();

            using (DbCommand command = _dbProviderFactory.CreateCommand())
            {
                command.Connection = _sqlConnection;
                command.CommandText = "SELECT * FROM Templates WHERE Title = @Title";

                command.Parameters.Add(GetParam("@Title", title));

                Template template = null;
                var binFormatter = new ListBinaryFormatter<string>();

                try
                {
                    using (DbDataReader dataReader = command.ExecuteReader())
                    {
                        while (dataReader.Read())
                        {
                            template = new Template(binFormatter.Deserialize((byte[]) dataReader["Data"]), title);
                        }
                    }
                }
                catch (Exception)
                {
                    throw new InvalidOperationException("Can't get template!");
                }
                finally
                {
                    _sqlConnection.Close();
                }

                return template;
            }
        }
示例#9
0
        public void AddTemplate(Template template)
        {
            OpenConnection();

            using (DbCommand command = _dbProviderFactory.CreateCommand())
            {
                command.Connection = _sqlConnection;
                var binFormatter = new ListBinaryFormatter<string>();

                command.CommandText = "INSERT INTO Templates(Title, Data) VALUES(@Title, @Data)";

                command.Parameters.Add(GetParam("@Title", template.Title));
                command.Parameters.Add(GetParam("@Data", binFormatter.Serialize(template.Data)));

                try
                {
                    command.ExecuteNonQuery();
                }
                catch (SqlException)
                {
                    throw new InvalidOperationException("This template already exists!");
                }
                catch (Exception)
                {
                    throw new InvalidOperationException("Can't add template!");
                }
                finally
                {
                    _sqlConnection.Close();
                }
            }
        }
示例#10
0
 private void FillTemplateTitle(Template template, StringBuilder templateContent)
 {
     const string templateTitleSubstitution = "TemplateTitleValue";
     CheckFillPosibility(templateContent, templateTitleSubstitution);
     templateContent.Replace(templateTitleSubstitution, template.Title);
     //LOGGING
     Logger.Info("Template was filled");
 }
示例#11
0
        public Template GetTemplate(string title)
        {
            OpenConnection();
            _command.CommandText = "SELECT * FROM Templates WHERE Title = @Title";

            _command.Parameters.Add(GetParam("@Title", title));

            Template template = null;
            var binFormatter = new ListBinaryFormatter<string>();

            try
            {
                using (DbDataReader dataReader = _command.ExecuteReader())
                {
                    while (dataReader.Read())
                    {
                        template = new Template(binFormatter.Deserialize((byte[]) dataReader["Data"]), title);
                    }
                }
                //LOGGING
                Logger.Info("Template was obtained");
            }
            catch (Exception ex)
            {
                //LOGGING
                Logger.Error("Can't get template!",ex);
                throw new InvalidOperationException("Can't get template!");
            }
            finally
            {
                CloseConnection();
            }

            return template;
        }
示例#12
0
        public void AddTemplate(Template template)
        {
            OpenConnection();
            var binFormatter = new ListBinaryFormatter<string>();

            _command.CommandText = "INSERT INTO Templates(Title, Data) VALUES(@Title, @Data)";

            _command.Parameters.Add(GetParam("@Title", template.Title));
            _command.Parameters.Add(GetParam("@Data", binFormatter.Serialize(template.Data)));

            try
            {
                _command.ExecuteNonQuery();
                //LOGGING
                Logger.Info("Template was added");
            }
            catch (SqlException ex)
            {
                //LOGGING
                Logger.Error("This template already exists!",ex);
                throw new InvalidOperationException("This template already exists!");
            }
            catch (Exception ex)
            {
                //LOGGING
                Logger.Error("Can't add template!",ex);
                throw new InvalidOperationException("Can't add template!");
            }
            finally
            {
                CloseConnection();
            }
        }
示例#13
0
文件: Template.cs 项目: kamilion/WISP
        public static bool LoadTemplate(XPathDocument docNav)
        {
            Template t = new Template();
            bool isValidTemplate = false;
            try
            {
                // Create a navigator to query with XPath.
                XPathNavigator nav = docNav.CreateNavigator();

                XPathNavigator root = nav.SelectSingleNode("//Item");

                t.Description = root.GetAttribute("Name", "");
                t.Class = root.GetAttribute("Class", "");

                bool isStatic = true;
                if (!bool.TryParse(root.GetAttribute("IsStatic", ""), out isStatic))
                {
                    isStatic = true; // default to static item
                }
                t.IsStatic = isStatic;

                bool isTransient = true;
                if (!bool.TryParse(root.GetAttribute("IsTransient", ""), out isTransient))
                {
                    isTransient = true; // default to Transient, non-persisting item
                }
                t.IsTransient = isTransient;

                int stackCount = 1;
                if (!int.TryParse(root.GetAttribute("StackCount", ""), out stackCount))
                {
                    stackCount = 1; // default to Transient, non-persisting item
                }
                t.StackCount = stackCount;

                if (t.Class.Length < 1)
                {
                    Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Class (fully qualified type name) could not be determined. Make sure that the Class attribute is defined.]");
                    goto bailout;
                }

                GOT got = GOT.None;
                string gotStr = root.GetAttribute("GOT", "");
                try
                {
                    got = (GOT)Enum.Parse(typeof(GOT), gotStr);
                }
                catch { }

                if (got == GOT.None)
                {
                    Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [GOT (Game object type) could not be determined. Make sure that GOT value exists in GOT enum cs file.]");
                    goto bailout;
                }

                t.GameObjectType = got;

                string strExpression = "//Item/Properties/*";
                XPathNodeIterator NodeIter = nav.Select(strExpression);

                //Iterate through the results showing the element value.
                while (NodeIter.MoveNext())
                {
                    string name = NodeIter.Current.GetAttribute("Name", "");
                    if (name.Length < 1)
                    {
                        Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Property Name attribute is missing.]");
                        goto bailout;
                    }

                    string stringValue = NodeIter.Current.GetAttribute("StringValue", "");
                    string intValue = NodeIter.Current.GetAttribute("IntValue", "");
                    string floatValue = NodeIter.Current.GetAttribute("FloatValue", "");
                    string longValue = NodeIter.Current.GetAttribute("LongValue", "");

                    if (stringValue.Length < 1 && intValue.Length < 1 && floatValue.Length < 1 && longValue.Length < 1)
                    {
                        Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must set either StringValue, IntValue, FloatValue or LongValue for property " + name + ".]");
                        goto bailout;
                    }

                    if (stringValue.Length > 0)
                    {
                        t.Properties.SetProperty(name, stringValue);
                    }
                    else if (floatValue.Length > 1)
                    {
                        try
                        {
                            float val = float.Parse(floatValue);
                            t.Properties.SetProperty(name, val);
                        }
                        catch
                        {
                            Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must specify a valid number for FloatValue for property " + name + ".]");
                            goto bailout;
                        }
                    }
                    else if (intValue.Length > 0)
                    {
                        try
                        {
                            int val = int.Parse(intValue);
                            t.Properties.SetProperty(name, val);
                        }
                        catch
                        {
                            Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must specify a valid number for IntValue for property " + name + ".]");
                            goto bailout;
                        }
                    }
                    else if (longValue.Length > 0)
                    {
                        try
                        {
                            long val = long.Parse(longValue);
                            t.Properties.SetProperty(name, val);
                        }
                        catch
                        {
                            Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must specify a valid number for LongValue for property " + name + ".]");
                            goto bailout;
                        }
                    }
                }

                strExpression = "//Item/Stats/*";
                NodeIter = nav.Select(strExpression);
                //Iterate through the results showing the element value.
                while (NodeIter.MoveNext())
                {
                    string id = NodeIter.Current.GetAttribute("StatID", "");

                    string value = NodeIter.Current.GetAttribute("StartValue", "");
                    if (value.Length < 1)
                    {
                        value = "0";
                    }

                    float val = float.Parse(value);
                    Stat prototype = StatManager.Instance.AllStats.GetStat(id);
                    if (prototype == null)
                    {
                        Log.LogMsg("Failed to load stat [" + id + "] for template [" + t.Description + "]. Make sure stat is defiend in Stats.xml.");
                        continue;
                    }

                    Stat ns = new Stat(
                        prototype.StatID,
                        prototype.DisplayName,
                        prototype.Description,
                        prototype.Group,
                        prototype.CurrentValue,
                        prototype.MinValue,
                        prototype.MaxValue);
                    ns.ForceValue(val);
                    t.Stats.AddStat(ns);
                }

                strExpression = "//Item/Scripts/*";
                NodeIter = nav.Select(strExpression);
                //Iterate through the results showing the element value.
                while (NodeIter.MoveNext())
                {
                    string name = NodeIter.Current.GetAttribute("Name", "");
                    if(!name.StartsWith("Scripts."))
                    {
                        name = "Scripts." + name;
                    }

                    if (name.Length > 0)
                    {
                        GameObjectScript s = GameObjectScript.GetScript(Factory.GetStableHash(name));
                        if (s == null)
                        {
                            Log.LogMsg("Unable to instantiate script [" + name + "] on template [" + docNav.ToString() + "]. Factory couldn't create script. Was it registered using Factor.Register()?");
                        }
                        else
                        {
                            t.Scripts.Add(Factory.GetStableHash(name));
                        }
                    }
                }

                isValidTemplate = true;
            }
            catch (Exception e)
            {
                isValidTemplate = false;
                Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [" + e.Message + "]");
            }

            bailout:
            if (isValidTemplate)
            {
                t.Name = t.Description.ToLower();// Path.GetFileNameWithoutExtension(template).ToLower();
                Templates.Add(t.Name, t);
                //Log1.Logger("Server").Info("Loaded template [" + Path.GetFileNameWithoutExtension(template) + "]");
            }
            else
            {
                Log.LogMsg("Failed to load template [" + docNav.ToString() + "]");
            }

            return isValidTemplate;
        }
示例#14
0
 private void FillTemplateTitle(Template template, StringBuilder templateContent)
 {
     const string templateTitleSubstitution = "TemplateTitleValue";
     CheckFillPosibility(templateContent, templateTitleSubstitution);
     templateContent.Replace(templateTitleSubstitution, template.Title);
 }
示例#15
0
        public static bool LoadTemplate(XPathDocument docNav)
        {
            Template t = new Template();
            bool     isValidTemplate = false;

            try
            {
                // Create a navigator to query with XPath.
                XPathNavigator nav = docNav.CreateNavigator();

                XPathNavigator root = nav.SelectSingleNode("//Item");

                t.Description = root.GetAttribute("Name", "");
                t.Class       = root.GetAttribute("Class", "");

                bool isStatic = true;
                if (!bool.TryParse(root.GetAttribute("IsStatic", ""), out isStatic))
                {
                    isStatic = true; // default to static item
                }
                t.IsStatic = isStatic;

                bool isTransient = true;
                if (!bool.TryParse(root.GetAttribute("IsTransient", ""), out isTransient))
                {
                    isTransient = true; // default to Transient, non-persisting item
                }
                t.IsTransient = isTransient;

                int stackCount = 1;
                if (!int.TryParse(root.GetAttribute("StackCount", ""), out stackCount))
                {
                    stackCount = 1; // default to Transient, non-persisting item
                }
                t.StackCount = stackCount;

                if (t.Class.Length < 1)
                {
                    Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Class (fully qualified type name) could not be determined. Make sure that the Class attribute is defined.]");
                    goto bailout;
                }

                GOT    got    = GOT.None;
                string gotStr = root.GetAttribute("GOT", "");
                try
                {
                    got = (GOT)Enum.Parse(typeof(GOT), gotStr);
                }
                catch { }

                if (got == GOT.None)
                {
                    Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [GOT (Game object type) could not be determined. Make sure that GOT value exists in GOT enum cs file.]");
                    goto bailout;
                }

                t.GameObjectType = got;

                string            strExpression = "//Item/Properties/*";
                XPathNodeIterator NodeIter      = nav.Select(strExpression);

                //Iterate through the results showing the element value.
                while (NodeIter.MoveNext())
                {
                    string name = NodeIter.Current.GetAttribute("Name", "");
                    if (name.Length < 1)
                    {
                        Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Property Name attribute is missing.]");
                        goto bailout;
                    }

                    string stringValue = NodeIter.Current.GetAttribute("StringValue", "");
                    string intValue    = NodeIter.Current.GetAttribute("IntValue", "");
                    string floatValue  = NodeIter.Current.GetAttribute("FloatValue", "");
                    string longValue   = NodeIter.Current.GetAttribute("LongValue", "");

                    if (stringValue.Length < 1 && intValue.Length < 1 && floatValue.Length < 1 && longValue.Length < 1)
                    {
                        Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must set either StringValue, IntValue, FloatValue or LongValue for property " + name + ".]");
                        goto bailout;
                    }

                    if (stringValue.Length > 0)
                    {
                        t.Properties.SetProperty(name, stringValue);
                    }
                    else if (floatValue.Length > 1)
                    {
                        try
                        {
                            float val = float.Parse(floatValue);
                            t.Properties.SetProperty(name, val);
                        }
                        catch
                        {
                            Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must specify a valid number for FloatValue for property " + name + ".]");
                            goto bailout;
                        }
                    }
                    else if (intValue.Length > 0)
                    {
                        try
                        {
                            int val = int.Parse(intValue);
                            t.Properties.SetProperty(name, val);
                        }
                        catch
                        {
                            Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must specify a valid number for IntValue for property " + name + ".]");
                            goto bailout;
                        }
                    }
                    else if (longValue.Length > 0)
                    {
                        try
                        {
                            long val = long.Parse(longValue);
                            t.Properties.SetProperty(name, val);
                        }
                        catch
                        {
                            Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [Must specify a valid number for LongValue for property " + name + ".]");
                            goto bailout;
                        }
                    }
                }

                strExpression = "//Item/Stats/*";
                NodeIter      = nav.Select(strExpression);
                //Iterate through the results showing the element value.
                while (NodeIter.MoveNext())
                {
                    string id = NodeIter.Current.GetAttribute("StatID", "");

                    string value = NodeIter.Current.GetAttribute("StartValue", "");
                    if (value.Length < 1)
                    {
                        value = "0";
                    }

                    float val       = float.Parse(value);
                    Stat  prototype = StatManager.Instance.AllStats.GetStat(id);
                    if (prototype == null)
                    {
                        Log.LogMsg("Failed to load stat [" + id + "] for template [" + t.Description + "]. Make sure stat is defiend in Stats.xml.");
                        continue;
                    }

                    Stat ns = new Stat(
                        prototype.StatID,
                        prototype.DisplayName,
                        prototype.Description,
                        prototype.Group,
                        prototype.CurrentValue,
                        prototype.MinValue,
                        prototype.MaxValue);
                    ns.ForceValue(val);
                    t.Stats.AddStat(ns);
                }

                strExpression = "//Item/Scripts/*";
                NodeIter      = nav.Select(strExpression);
                //Iterate through the results showing the element value.
                while (NodeIter.MoveNext())
                {
                    string name = NodeIter.Current.GetAttribute("Name", "");
                    if (!name.StartsWith("Scripts."))
                    {
                        name = "Scripts." + name;
                    }

                    if (name.Length > 0)
                    {
                        GameObjectScript s = GameObjectScript.GetScript(Factory.GetStableHash(name));
                        if (s == null)
                        {
                            Log.LogMsg("Unable to instantiate script [" + name + "] on template [" + docNav.ToString() + "]. Factory couldn't create script. Was it registered using Factor.Register()?");
                        }
                        else
                        {
                            t.Scripts.Add(Factory.GetStableHash(name));
                        }
                    }
                }

                isValidTemplate = true;
            }
            catch (Exception e)
            {
                isValidTemplate = false;
                Log.LogMsg("Failed to load template [" + docNav.ToString() + "] - [" + e.Message + "]");
            }

bailout:
            if (isValidTemplate)
            {
                t.Name = t.Description.ToLower();// Path.GetFileNameWithoutExtension(template).ToLower();
                Templates.Add(t.Name, t);
                //Log1.Logger("Server").Info("Loaded template [" + Path.GetFileNameWithoutExtension(template) + "]");
            }
            else
            {
                Log.LogMsg("Failed to load template [" + docNav.ToString() + "]");
            }

            return(isValidTemplate);
        }