Exemplo n.º 1
0
        /// <summary>
        /// Получает все данные из хранилища касаемые данного владельца с заданным типом.
        /// </summary>
        /// <param name="owner">Владелец данных. Обычно приложение или сервис.</param>
        /// <param name="type">Тип данных.</param>
        /// <param name="infos">Полученная информация из хранилища.</param>
        public void Get(StoreOwner owner, StoreType type, out List <StorageItemInfo> infos)
        {
            string header = "GetData";

            // Получаем атрибуты элементов перечислений.
            OwnerAttribute ownerAttribute = GetStoreOwnerTextAttribute(owner);
            TypeAttribute  typeAttribute  = GetStoreTypeTextAttribute(type);

            infos = new List <StorageItemInfo>();

            // Проверяем входные параметры.
            if (ownerAttribute == null || typeAttribute == null)
            {
                Event_Message(new MessageEventArgs(this, MessageEventArgsType.Error, header, "Incorrect input parameters."));
                return;
            }

            // Получаем специальное имя каталога и входящие в него файлы.
            DirectoryInfo di = new DirectoryInfo(StorageDirectory + "\\" + ownerAttribute.Name);

            if (di.Exists)
            {
                foreach (FileInfo fi in di.GetFiles("*.xml"))
                {
                    bool   result   = true;
                    string filename = fi.FullName;

                    try
                    {
                        XDocument xdoc = XDocument.Load(fi.FullName);
                        if (xdoc.Root.ExistAs("storage") && xdoc.Root.Element("data") != null)
                        {
                            string readedId    = xdoc.Root.Attribute("ID").GetXValue(null);
                            string readedTicks = xdoc.Root.Attribute("Saved").GetXValue(null);
                            string readedOwner = xdoc.Root.Attribute("Owner").GetXValue(null);
                            string readedType  = xdoc.Root.Attribute("Type").GetXValue(null);

                            if (readedOwner != ownerAttribute.Name)
                            {
                                Event_Message(new MessageEventArgs(this, MessageEventArgsType.Error, header, "Object:" + ownerAttribute.Name + "::" + typeAttribute.Name + ". Incorrect file structure: " + fi.FullName + ". Requested Owner = " + ownerAttribute.Name + "<> Readed Owner = " + readedOwner));
                                result = false;
                            }

                            if (readedType != typeAttribute.Name)
                            {
                                Event_Message(new MessageEventArgs(this, MessageEventArgsType.Error, header, "Object:" + ownerAttribute.Name + "::" + typeAttribute.Name + ". Incorrect file structure: " + fi.FullName + ". Requested Type = " + typeAttribute.Name + "<> Readed Type = " + readedType));
                                result = false;
                            }

                            if (readedTicks == null || !readedTicks.All(c => Char.IsDigit(c)))
                            {
                                Event_Message(new MessageEventArgs(this, MessageEventArgsType.Error, header, "Object:" + ownerAttribute.Name + "::" + typeAttribute.Name + ". Incorrect file structure: " + fi.FullName + ". Readed DateTime Stamp is not correct."));
                                result = false;
                            }

                            if (result)
                            {
                                StorageItemInfo info = new StorageItemInfo(owner, type, readedId, new DateTime(Convert.ToInt64(readedTicks)), true, xdoc.Root.Element("data"));
                                infos.Add(info);
                            }
                        }
                    }
                    catch
                    {
                        Event_Message(new MessageEventArgs(this, MessageEventArgsType.Error, header, "Object:" + ownerAttribute.Name + "::" + typeAttribute.Name + "::" + ". Can not to read file: " + fi.FullName + "."));
                    }
                }
            }
            else
            {
                Event_Message(new MessageEventArgs(this, MessageEventArgsType.Warning, header, "Object:" + ownerAttribute.Name + "::" + typeAttribute.Name + "::" + ". Folder not found: " + di.FullName + "."));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Получает данные из хранилища по конкретному идентификатору.
        /// </summary>
        /// <param name="owner">Владелец данных. Обычно приложение или сервис.</param>
        /// <param name="type">>Тип данных.</param>
        /// <param name="curr_id">Иденификатор данных.</param>
        /// <param name="info">Полученная информация из хранилища.</param>
        /// <returns></returns>
        public bool Get(StoreOwner owner, StoreType type, string id, out StorageItemInfo info)
        {
            string header  = "GetData";
            string curr_id = "";

            if (id != null && id.Trim() != "")
            {
                curr_id = id;
            }

            info = new StorageItemInfo(owner, type, curr_id, null, null, null);

            // Получаем атрибуты элементов перечислений.
            OwnerAttribute ownerAttribute = GetStoreOwnerTextAttribute(owner);
            TypeAttribute  typeAttribute  = GetStoreTypeTextAttribute(type);


            // Проверяем входные параметры.
            if (ownerAttribute == null || typeAttribute == null)
            {
                Event_Message(new MessageEventArgs(this, MessageEventArgsType.Error, header, "Incorrect input parameters."));
                return(false);
            }

            // Получаем специальное имя файла.
            string filename = GetFileName(type, curr_id);

            // Проверяем существует ли данный файл и проверяем его содержимое.
            FileInfo fi = new FileInfo(StorageDirectory + "\\" + ownerAttribute.Name + "\\" + filename);

            if (fi.Exists)
            {
                try
                {
                    XDocument xdoc = XDocument.Load(fi.FullName);
                    if (xdoc.Root.ExistAs("storage") && xdoc.Root.Element("data") != null)
                    {
                        string readedId    = xdoc.Root.Attribute("ID").GetXValue(null);
                        string readedTicks = xdoc.Root.Attribute("Saved").GetXValue(null);
                        string readedOwner = xdoc.Root.Attribute("Owner").GetXValue(null);
                        string readedType  = xdoc.Root.Attribute("Type").GetXValue(null);

                        if (readedOwner != ownerAttribute.Name)
                        {
                            Event_Message(new MessageEventArgs(this, MessageEventArgsType.Error, header, "Object:" + ownerAttribute.Name + "::" + typeAttribute.Name + "::" + curr_id + ". Incorrect file structure: " + fi.FullName + ". Requested Owner = " + ownerAttribute.Name + "<> Readed Owner = " + readedOwner));
                            return(false);
                        }

                        if (readedType != typeAttribute.Name)
                        {
                            Event_Message(new MessageEventArgs(this, MessageEventArgsType.Error, header, "Object:" + ownerAttribute.Name + "::" + typeAttribute.Name + "::" + curr_id + ". Incorrect file structure: " + fi.FullName + ". Requested Type = " + typeAttribute.Name + "<> Readed Type = " + readedType));
                            return(false);
                        }

                        if (readedTicks == null || !readedTicks.All(c => Char.IsDigit(c)))
                        {
                            Event_Message(new MessageEventArgs(this, MessageEventArgsType.Error, header, "Object:" + ownerAttribute.Name + "::" + typeAttribute.Name + "::" + curr_id + ". Incorrect file structure: " + fi.FullName + ". Readed DateTime Stamp is not correct."));
                            return(false);
                        }

                        if (curr_id != readedId)
                        {
                            Event_Message(new MessageEventArgs(this, MessageEventArgsType.Error, header, "Object:" + ownerAttribute.Name + "::" + typeAttribute.Name + "::" + curr_id + ". Incorrect file structure: " + fi.FullName + ". Requested ID = " + curr_id + "<> Readed ID = " + readedId));
                            return(false);
                        }

                        info = new StorageItemInfo(owner, type, curr_id, new DateTime(Convert.ToInt64(readedTicks)), true, xdoc.Root.Element("data"));
                        return(true);
                    }
                    else
                    {
                        Event_Message(new MessageEventArgs(this, MessageEventArgsType.Error, header, "Object:" + ownerAttribute.Name + "::" + typeAttribute.Name + "::" + curr_id + ". Incorrect file structure: " + fi.FullName + "."));
                        return(false);
                    }
                }
                catch
                {
                    Event_Message(new MessageEventArgs(this, MessageEventArgsType.Error, header, "Object:" + ownerAttribute.Name + "::" + typeAttribute.Name + "::" + curr_id + ". Can not to read file: " + fi.FullName + "."));
                    return(false);
                }
            }
            else
            {
                Event_Message(new MessageEventArgs(this, MessageEventArgsType.Warning, header, "Object:" + ownerAttribute.Name + "::" + typeAttribute.Name + "::" + curr_id + ". File not found: " + fi.FullName + "."));
                return(false);
            }
        }