public static void UpdateResource(Resource resource)
        {
            string commaSeparatedListWithKeyWords;
            bool   updateOfResourceFileNeeded = false;
            List <ResourcePresentationInSearch> listWithResourcePresentationInSearch;

            commaSeparatedListWithKeyWords = KeyWordUtility.ReturnCommaSeparatedListWithKeyWords(resource.KeyWords);

            listWithResourcePresentationInSearch = ReturnListWithAllResourcePresentationInSearch();

            if (listWithResourcePresentationInSearch[resource.Id - 1].KeyWords != commaSeparatedListWithKeyWords)
            {
                updateOfResourceFileNeeded = true;
                listWithResourcePresentationInSearch[resource.Id - 1].KeyWords = commaSeparatedListWithKeyWords;
            }

            if (listWithResourcePresentationInSearch[resource.Id - 1].Title != resource.Title)
            {
                updateOfResourceFileNeeded = true;
                listWithResourcePresentationInSearch[resource.Id - 1].Title = resource.Title;
            }

            if (updateOfResourceFileNeeded)
            {
                SaveListWithResourcePresentationInSearch(listWithResourcePresentationInSearch);
            }
        }
        public static void RegenerateResourceFile(out string message)
        {
            List <Resource> listWithAllResources;
            ResourcePresentationInSearch resourcePresentationInSearch;
            string        commaSeparatedListWithKeyWords, serializedResourcePresentationInSearch, errorMessage;
            DateTime      created;
            StringBuilder sb;
            int           i;

            message = null;

            try
            {
                listWithAllResources = ResourceUtility.ReturnListWithResources(ResourcesType.All, out errorMessage);

                if (errorMessage != null)
                {
                    message = errorMessage;
                    return;
                }

                sb = new StringBuilder();

                for (i = 0; i < listWithAllResources.Count; i++)
                {
                    created = Utility.ReturnDateTimeFromString(listWithAllResources[i].Created);
                    commaSeparatedListWithKeyWords         = KeyWordUtility.ReturnCommaSeparatedListWithKeyWords(listWithAllResources[i].KeyWords);
                    resourcePresentationInSearch           = new ResourcePresentationInSearch(listWithAllResources[i].Id, listWithAllResources[i].ResourcesType, created, listWithAllResources[i].Title, commaSeparatedListWithKeyWords);
                    serializedResourcePresentationInSearch = SerializeResourcePresentationInSearch(resourcePresentationInSearch);

                    if (i > 0)
                    {
                        sb.Append(string.Format("{0}{1}", "\r\n\r\n---------- New resource ----------\r\n\r\n", serializedResourcePresentationInSearch));
                    }
                    else
                    {
                        sb.Append(serializedResourcePresentationInSearch);
                    }
                }

                Utility.CreateNewFile(_fileNameFullPathToResources, sb.ToString());

                message = string.Format("Resources.txt was successfully regenerated with {0} resources.", listWithAllResources.Count.ToString());
            }
            catch (Exception e)
            {
                message = string.Format("ERROR!! An Exception occured in method RegenerateResourceFile! e.Message:\r\n{0}", e.Message);
                return;
            }
        }
        public static void AddResource(Resource resource)
        {
            string commaSeparatedListWithKeyWords, serializedResourcePresentationInSearch;
            ResourcePresentationInSearch resourcePresentationInSearch;
            DateTime created;

            created = Utility.ReturnDateTimeFromString(resource.Created);
            commaSeparatedListWithKeyWords = KeyWordUtility.ReturnCommaSeparatedListWithKeyWords(resource.KeyWords);

            resourcePresentationInSearch           = new ResourcePresentationInSearch(resource.Id, resource.ResourcesType, created, resource.Title, commaSeparatedListWithKeyWords);
            serializedResourcePresentationInSearch = SerializeResourcePresentationInSearch(resourcePresentationInSearch);

            if (resource.Id > 1)
            {
                Utility.AppendToFile(_fileNameFullPathToResources, string.Format("{0}{1}", "\r\n\r\n---------- New resource ----------\r\n\r\n", serializedResourcePresentationInSearch));
            }
            else
            {
                Utility.AppendToFile(_fileNameFullPathToResources, serializedResourcePresentationInSearch);
            }
        }
Exemplo n.º 4
0
        public static Resource GetResource(int id, out string errorMessage)
        {
            string resourceDirectory, firstRow, firstRowTemplate, fileNameFullPath, resourceSerialized;

            string[]  filesInResourceDirectoryOrInFoldersInResourceDirectory, u, v;
            ArrayList tmp, fileNamesShort, directoryNames, fileCreationDate, fileUpdatedDate, href, hrefText;
            Resource  resourceDeserialized;
            int       i, j, index, ifw, ifh, tw, th;

            errorMessage = null;

            resourceDirectory = ReturnResourceDirectory(id);
            fileNameFullPath  = string.Format("{0}\\R{1}.txt", resourceDirectory, id.ToString());

            if (!File.Exists(fileNameFullPath))
            {
                errorMessage = string.Format("ERROR!! The resource R{0} does not exist!", id.ToString());
                return(null);
            }

            resourceSerialized   = Utility.ReturnFileContents(fileNameFullPath);
            resourceDeserialized = DeserializeResource(resourceSerialized);

            if (resourceDeserialized.ResourcesType == ResourcesType.Html)
            {
                resourceDeserialized.HtmlFileText = Utility.ReturnFileContents(_basePath + resourceDeserialized.HtmlFile);
                Utility.ReturnTextExceptFirstRow(resourceDeserialized.HtmlFileText, out firstRow);

                if (!Utility.CheckFirstRowInHtmlResource(firstRow, false, out firstRowTemplate, out ifw, out ifh, out tw, out th, out errorMessage))
                {
                    return(null);
                }
                else
                {
                    resourceDeserialized.WidthIframe    = ifw;
                    resourceDeserialized.HeightIframe   = ifh;
                    resourceDeserialized.WidthTextarea  = tw;
                    resourceDeserialized.HeightTextarea = th;
                }
            }
            else if (resourceDeserialized.ResourcesType == ResourcesType.Self)
            {
                tmp              = new ArrayList();
                fileNamesShort   = new ArrayList();
                directoryNames   = new ArrayList();
                fileCreationDate = new ArrayList();
                fileUpdatedDate  = new ArrayList();
                href             = new ArrayList();
                hrefText         = new ArrayList();

                resourceDeserialized.KeyWordPhrases = string.Format("({0})", KeyWordUtility.ReturnCommaSeparatedListWithKeyWords(resourceDeserialized.KeyWords).Replace(",", ", "));

                filesInResourceDirectoryOrInFoldersInResourceDirectory = GetFilesInDirectoryAndInFoldersInDirectory(resourceDirectory);

                if ((filesInResourceDirectoryOrInFoldersInResourceDirectory.Length > 1) || !string.IsNullOrEmpty(resourceDeserialized.FilesFolders))
                {
                    resourceDeserialized.FileNamesShort   = new List <string>();
                    resourceDeserialized.DirectoryNames   = new List <string>();
                    resourceDeserialized.FileCreationDate = new List <string>();
                    resourceDeserialized.FileUpdatedDate  = new List <string>();

                    if (filesInResourceDirectoryOrInFoldersInResourceDirectory.Length > 1)
                    {
                        for (i = 0; i < filesInResourceDirectoryOrInFoldersInResourceDirectory.Length; i++)
                        {
                            if ((new FileInfo(filesInResourceDirectoryOrInFoldersInResourceDirectory[i])).Name != string.Format("R{0}.txt", resourceDeserialized.Id))
                            {
                                tmp.Add(filesInResourceDirectoryOrInFoldersInResourceDirectory[i].Trim().ToLower());
                                Utility.AddFileInfo(filesInResourceDirectoryOrInFoldersInResourceDirectory[i], fileNamesShort, directoryNames, fileCreationDate, fileUpdatedDate);
                            }
                        }
                    }

                    if (!string.IsNullOrEmpty(resourceDeserialized.FilesFolders))
                    {
                        u = resourceDeserialized.FilesFolders.Split('\n');

                        for (i = 0; i < u.Length; i++)
                        {
                            if (!System.IO.File.Exists(u[i]) && !System.IO.Directory.Exists(u[i]))
                            {
                                errorMessage = string.Format("ERROR!! A name, \"{0}\" is found in list of files/folders that is neither an existing file nor a directory!!", u[i]);
                                return(null);
                            }

                            if (System.IO.File.Exists(u[i]))
                            {
                                if ((tmp.IndexOf(u[i].Trim().ToLower()) == -1))
                                {
                                    tmp.Add(u[i].Trim().ToLower());
                                    Utility.AddFileInfo(u[i], fileNamesShort, directoryNames, fileCreationDate, fileUpdatedDate);
                                }
                            }
                            else
                            {
                                v = GetFilesInDirectoryAndInFoldersInDirectory(u[i]);

                                for (j = 0; j < v.Length; j++)
                                {
                                    if (tmp.IndexOf(v[j].Trim().ToLower()) == -1)
                                    {
                                        tmp.Add(v[j].Trim().ToLower());
                                        Utility.AddFileInfo(v[j], fileNamesShort, directoryNames, fileCreationDate, fileUpdatedDate);
                                    }
                                }
                            }
                        }
                    }

                    if (KeyWordUtility.ResourceHasKeyWordId(resourceDeserialized.KeyWords, 12)) //It is a Task (KeyWordId = 12 is Task). Put "Task???.txt" and "To Test.txt" first if they exists
                    {
                        index = ReturnIndexForTaskFile(fileNamesShort);

                        if (index >= 0)
                        {
                            resourceDeserialized.FileNamesShort.Add((string)fileNamesShort[index]);
                            resourceDeserialized.DirectoryNames.Add("Folder: " + (string)directoryNames[index]);
                            resourceDeserialized.FileCreationDate.Add((string)fileCreationDate[index]);
                            resourceDeserialized.FileUpdatedDate.Add((string)fileUpdatedDate[index]);
                            fileNamesShort.RemoveAt(index);
                            directoryNames.RemoveAt(index);
                            fileCreationDate.RemoveAt(index);
                            fileUpdatedDate.RemoveAt(index);
                        }

                        index = ReturnIndexForToTestFile(fileNamesShort);

                        if (index >= 0)
                        {
                            resourceDeserialized.FileNamesShort.Add((string)fileNamesShort[index]);
                            resourceDeserialized.DirectoryNames.Add("Folder: " + (string)directoryNames[index]);
                            resourceDeserialized.FileCreationDate.Add((string)fileCreationDate[index]);
                            resourceDeserialized.FileUpdatedDate.Add((string)fileUpdatedDate[index]);
                            fileNamesShort.RemoveAt(index);
                            directoryNames.RemoveAt(index);
                            fileCreationDate.RemoveAt(index);
                            fileUpdatedDate.RemoveAt(index);
                        }
                    }

                    Utility.Sort(fileNamesShort, directoryNames, fileCreationDate, fileCreationDate);

                    for (i = 0; i < fileNamesShort.Count; i++)
                    {
                        resourceDeserialized.FileNamesShort.Add((string)fileNamesShort[i]);
                        resourceDeserialized.DirectoryNames.Add("Folder: " + (string)directoryNames[i]);
                        resourceDeserialized.FileCreationDate.Add((string)fileCreationDate[i]);
                        resourceDeserialized.FileUpdatedDate.Add((string)fileUpdatedDate[i]);
                    }
                }
                else
                {
                    resourceDeserialized.FileNamesShort   = null;
                    resourceDeserialized.DirectoryNames   = null;
                    resourceDeserialized.FileCreationDate = null;
                    resourceDeserialized.FileUpdatedDate  = null;
                }

                resourceDeserialized.ListWithImages = ProcessImagesForASelfResource(resourceDeserialized.FileNamesShort, resourceDeserialized.DirectoryNames);

                if (!string.IsNullOrEmpty(resourceDeserialized.Links))
                {
                    resourceDeserialized.Href     = new List <string>();
                    resourceDeserialized.HrefText = new List <string>();

                    u = resourceDeserialized.Links.Split('\n');

                    for (i = 0; i < u.Length; i++)
                    {
                        v = u[i].Split(new string[] { "###" }, StringSplitOptions.None);

                        if (v.Length != 2)
                        {
                            errorMessage = string.Format("ERROR!! Incorrect link found: {0}!", u[i]);
                            return(null);
                        }
                        else
                        {
                            resourceDeserialized.Href.Add(v[0]);
                            resourceDeserialized.HrefText.Add(v[1]);
                        }
                    }
                }
                else
                {
                    resourceDeserialized.Href     = null;
                    resourceDeserialized.HrefText = null;
                }
            }

            return(resourceDeserialized);
        }
        public static void CheckResourceFile(out string message)
        {
            string          commaSeparatedListWithKeyWords, errorMessage;
            List <Resource> listWithAllResources;
            List <ResourcePresentationInSearch> listWithResourcePresentationInSearch;
            int id;

            message = null;

            try
            {
                listWithAllResources = ResourceUtility.ReturnListWithResources(ResourcesType.All, out errorMessage);

                if (errorMessage != null)
                {
                    message = errorMessage;
                    return;
                }

                listWithResourcePresentationInSearch = ReturnListWithAllResourcePresentationInSearch();

                if (listWithResourcePresentationInSearch.Count != listWithAllResources.Count)
                {
                    message = string.Format("Number of resource in Resources.txt, {0} resources, is not the same as the actual number of resources, {1} resources!", listWithResourcePresentationInSearch.Count.ToString(), listWithAllResources.Count.ToString());
                    return;
                }

                id = 1;

                while ((id <= listWithResourcePresentationInSearch.Count) && (message == null))
                {
                    if (listWithResourcePresentationInSearch[id - 1].Id != id)
                    {
                        message = string.Format("ResourcePresentationInSearch number {0} in file Resources.txt does not have id = {1} as expected", id.ToString(), id.ToString());
                    }
                    else if (listWithResourcePresentationInSearch[id - 1].ResourcesType != listWithAllResources[id - 1].ResourcesType)
                    {
                        message = string.Format("ResourcePresentationInSearch number {0} in file Resources.txt does not have resource type = {1} as expected", id.ToString(), listWithAllResources[id - 1].ResourcesType.ToString());
                    }
                    else if (listWithResourcePresentationInSearch[id - 1].Created.ToString("yyyy-MM-dd HH:mm:ss") != listWithAllResources[id - 1].Created)
                    {
                        message = string.Format("ResourcePresentationInSearch number {0} in file Resources.txt does not have created date = {1} as expected", id.ToString(), listWithAllResources[id - 1].Created);
                    }
                    else if (listWithResourcePresentationInSearch[id - 1].Title != listWithAllResources[id - 1].Title)
                    {
                        message = string.Format("ResourcePresentationInSearch number {0} in file Resources.txt does not have title = {1} as expected", id.ToString(), listWithAllResources[id - 1].Title);
                    }

                    if (message == null)
                    {
                        commaSeparatedListWithKeyWords = KeyWordUtility.ReturnCommaSeparatedListWithKeyWords(listWithAllResources[id - 1].KeyWords);

                        if (commaSeparatedListWithKeyWords != listWithResourcePresentationInSearch[id - 1].KeyWords)
                        {
                            message = string.Format("ResourcePresentationInSearch number {0} in file Resources.txt does not have key words = {1} as expected", id.ToString(), commaSeparatedListWithKeyWords);
                        }
                    }

                    id++;
                }

                if (message == null)
                {
                    message = string.Format("All {0} ResourcePresentationInSearch in file Resources.txt are correct!", listWithResourcePresentationInSearch.Count.ToString());
                }
            }
            catch (Exception e)
            {
                message = string.Format("ERROR!! An Exception occured in method CheckResourceFile! e.Message:\r\n{0}", e.Message);
                return;
            }
        }