Esempio n. 1
0
        //Make async
        private void updateDirectoryFile()
        {
            //updates the directory file with the new values
            string filePath = folderRoot + "directory.json";

            if (!File.Exists(filePath))
            {
                //FBackups are when things go badly, not normal backups
                Debug.WriteLine("Directory file doesn't exist, backing up our current settings.");
                if (File.Exists(folderRoot + "directory.Fbackup.json"))
                {
                    File.Copy(folderRoot + "directory.Fbackup.json", folderRoot + "directory.Fbackup.json.old");
                    File.Delete(folderRoot + "directory.Fbackup.json");
                }
                //File.WriteAllText(folderRoot + "directory.Fbackup.json", JsonConvert.SerializeObject(ticketDirectory));
                File.WriteAllText(folderRoot + "directory.Fbackup.json", TicketDirectory.Serialize(this.ticketDirectory));
                throw new Exception("Can't update directory file, doesn't exist");
                //Maybe backup the current directory to file?
            }
            else
            {
                File.Copy(folderRoot + "directory.json", folderRoot + "directory.json.backup", true);
                File.Delete(folderRoot + "directory.json");
                //File.WriteAllText(folderRoot + "directory.json", JsonConvert.SerializeObject(ticketDirectory));
                File.WriteAllText(folderRoot + "directory.json", TicketDirectory.Serialize(this.ticketDirectory));
            }
            addedTicket   = false;
            removedTicket = false;
        }
Esempio n. 2
0
        public static string Serialize(TicketDirectory tick)
        {
            JObject obj = new JObject(new JProperty("ticketCount", tick.ticketCount));
            JArray  arr = new JArray();

            foreach (KeyValuePair <int, StoredDetails> entry in tick.tickets)
            {
                try {
                    JObject entryObj = new JObject(new JProperty("id", entry.Value.id),
                                                   new JProperty("folderLocation", entry.Value.folderLocation),
                                                   new JProperty("statusLocation", entry.Value.statusLocation),
                                                   new JProperty("ticketLocation", entry.Value.ticketLocation));
                    arr.Add(entryObj);
                }
                catch (Exception e) {
                    string message = e.Message;
                }
            }
            obj.Add(new JProperty("tickets", arr));
            return(obj.ToString());
        }
Esempio n. 3
0
        private bool isValidDirectory(TicketDirectory ticketDirectory)
        {
            int count = 0;  //How many tickets are actually in directory.json versus its ticketCount

            //Currently not verifying status.json or ticket.json
            //http://stackoverflow.com/a/141098
            this.tickets = new Dictionary <int, Tuple <Ticket, TicketStatus> >(ticketDirectory.ticketCount);
            foreach (KeyValuePair <int, StoredDetails> entry in ticketDirectory.tickets)
            {
                if (!File.Exists(getAbsoluteFileLocation(entry.Value.ticketLocation)))
                {
                    Console.WriteLine("{0} does not exist -  isValidDirectory ticketLocation", getAbsoluteFileLocation(entry.Value.ticketLocation));
                    return(false);
                }
                if (!Directory.Exists(getAbsoluteFolderLocation(entry.Value.folderLocation)))
                {
                    Console.WriteLine("{0} does not exist -  isValidDirectory folderLocation", getAbsoluteFolderLocation(entry.Value.folderLocation));
                    return(false);
                }
                if (!File.Exists(getAbsoluteFileLocation(entry.Value.statusLocation)))
                {
                    Console.WriteLine("{0} does not exist -  isValidDirectory statusLocation", getAbsoluteFileLocation(entry.Value.statusLocation));
                    return(false);
                }
                if (!LoadTicket(getAbsoluteFileLocation(entry.Value.ticketLocation), getAbsoluteFileLocation(entry.Value.statusLocation)))
                {
                    return(false);
                }
                count++;
            }

            if (count != ticketDirectory.ticketCount)
            {
                Debug.WriteLine("Ticket Count: {0} does not match count supplied by directory.json: {1}", count, ticketDirectory.ticketCount);
                return(false);
            }
            return(true);
        }
Esempio n. 4
0
        //public StoredDetails[] tickets { get; set; }
        public static TicketDirectory Deserialize(string text)
        {
            JObject jobj;

            try {
                jobj = JObject.Parse(text);
            }
            catch (Exception e) {
                Debug.WriteLine("TicketDirectory failed to deserialize with Error: " + e.Message);
                throw new Exception("Could not deserialize TicketDirectory");
            }

            TicketDirectory direct = new TicketDirectory(jobj.Property("ticketCount").Value.ToObject <int>());

            foreach (JToken token in jobj.Property("tickets").Values())
            {
                //Create StoredDetail and add to Dictionary
                //broken
                StoredDetails stored = token.ToObject <StoredDetails>();
                direct.tickets.Add(stored.id, stored);
            }
            return(direct);
        }
Esempio n. 5
0
        /// <summary>
        /// Main Constructor
        /// </summary>
        /// <param name="path">Place where the Tickets are currently stored or will be stored</param>
        /// <remarks>Path must contain a valid directory.json</remarks>
        public TicketStorage(string path)
        {
            bool isRightSlash = false;

            if (path.Contains('\\'))
            {
                isRightSlash = false;
                if (path.Last <char>() != '\\')
                {
                    path = path + '\\';
                }
            }
            else if (path.Contains('/'))
            {
                isRightSlash = true;
                if (path.Last <char>() != '/')
                {
                    path = path + '/';
                }
            }

            if (Directory.Exists(path))
            {
                //try and load directory.json
                Debug.WriteLine("Directory exists");
                string fileDirectory;
                if (isRightSlash)
                {
                    fileDirectory = path + @"directory.json";
                }
                else
                {
                    fileDirectory = path + @"directory.json";
                }
                if (!File.Exists(fileDirectory))
                {
                    Console.WriteLine("Attempted to create TicketStorage with an invalid path");
                    throw new Exception("Attempted to create TicketStorage with an invalid path");
                }
                else
                {
                    TicketStorage.folderRoot = path;
                    using (StreamReader reader = new StreamReader(fileDirectory)) {
                        //this.ticketDirectory = JsonConvert.DeserializeObject<TicketDirectory>(reader.ReadToEnd());
                        string readToEnd = reader.ReadToEnd();
                        Console.WriteLine(readToEnd);
                        this.ticketDirectory = TicketDirectory.Deserialize(readToEnd);
                    }
                    //Validate directory
                    if (!isValidDirectory(this.ticketDirectory))
                    {
                        throw new Exception("Loaded an invalid Ticket Directory");
                    }
                    //Load Tickets from stored json format into a dictionary
                }
            }
            else
            {
                //create path
                Console.WriteLine("Directory does not exist, path=" + path);
                TicketStorage.folderRoot = path;
                if (Extensions.IsLinux)
                {
                    Console.WriteLine("Creating Directory at: {0}", System.Environment.CurrentDirectory + @"/Tickets");
                    Directory.CreateDirectory(System.Environment.CurrentDirectory + @"/Tickets");
                }
                else
                {
                    Console.WriteLine("Creating Directory at: {0}", System.Environment.CurrentDirectory + @"\Tickets");
                    Directory.CreateDirectory(System.Environment.CurrentDirectory + @"\Tickets");
                }

                //create directory.json
                Debug.WriteLine("Creating TicketDirectory Object");
                TicketDirectory directory     = new TicketDirectory(0);
                string          jsonData      = JsonConvert.SerializeObject(directory);
                string          directoryPath = String.Empty;
                directoryPath = path + "directory.json";

                /*if (Extensions.IsLinux) {
                 *  Console.WriteLine("Is Linux");
                 *  directoryPath = path + @"/directory.json";
                 * }
                 * else {
                 *  Console.WriteLine("Is NOT Linux");
                 *  directoryPath = path + @"\directory.json";
                 * }*/
                Console.WriteLine("Directory path: " + directoryPath);
                using (StreamWriter writer = File.CreateText(directoryPath)) {
                    writer.Write(jsonData);
                }
                //create TicketDirectory!!
                ticketDirectory = new TicketDirectory(0);
                this.tickets    = new Dictionary <int, Tuple <Ticket, TicketStatus> >(ticketDirectory.ticketCount);
            }
            Debug.WriteLine("Starting IOWorker thread");
            WorkerThread = new Thread(() => IOWorker());
            WorkerThread.Start();
        }