Exemplo n.º 1
0
        // This functions takes a newer version of this document, and merges it with this one
        // acording to "Simple Merge Policy" given in slice-of-pie.pdf.
        // It also updated all other fields in the document according to the new version
        // and generates a log based on the changes.
        // MergeWith returns a bool as well, it returns false if the ID of the updated
        // document is not the same as this documents ID, otherwise it returns true
        public bool MergeWith(Document doc, User user)
        {
            if (this.id != doc.id)
                return false;
            List<string> changes = new List<string>();
            // Create original and latest arrays. ( step 1 )
            string[] original = this.GetTextAsArray();
            string[] latest = doc.GetTextAsArray();
            // Create merged array ( made as a list instead ). ( step 2 )
            List<string> merged = new List<string>();
            // Definer o and n index, which point to lines in the original and latest arrays. ( step 3 )
            int o = 0;
            int n = 0;
            bool done = false;

            Console.Out.WriteLine("Started merge.");
            // While loop that continues untill the end of both versions of the document have been reached.
                while (!done)
            {
                Console.Out.WriteLine("New round in while loop");
                // All remaining lines in latest are new. ( step 4 )

                if ((o == original.Length) && (n != latest.Length))
                {
                    Console.Out.WriteLine("Step 4");
                    for (int N=n; N < latest.Length; N++)
                    {
                        changes.Add("+L"+N+": "+latest[N]);
                        merged.Add(latest[N]);
                        n++;
                    }
                }
                // All remaining lines in original have been removed. ( step 5 )

                else if ((o != original.Length) && (n == latest.Length))
                {
                    Console.Out.WriteLine("Step 5");
                    for (int O=o; O < original.Length; O++)
                    {
                        changes.Add("-L"+O+": "+original[O]);
                    }
                    o = original.Length;
                }
                // No changes have occured in this line ( step 6 )
                else if (String.Compare(original[o], latest[n]) == 0)
                {
                    Console.Out.WriteLine("Step 6");
                    merged.Add(original[o]);
                    n++;
                    o++;
                }

                else if (String.Compare(original[o], latest[n]) != 0)
                {
                    List<string> temp = new List<string>();
                    bool found = false;
                    int t = 0;
                    for (int N = n; N < latest.Length; N++)
                    {
                        temp.Add(latest[N]);
                        if (String.Compare(original[o], latest[N]) == 0)
                        {
                            found = true;
                            t = N;
                            break;
                        }
                    }
                        // Line has been removed ( Step 7.b )
                        if (found == false)
                        {
                            Console.Out.WriteLine("Step 7.b");
                            changes.Add("-L"+o+": "+original[o]);
                            o++;
                        }
                        // All lines in between have been added ( step 7.c )
                        else
                        {
                            Console.Out.WriteLine("Step 7.c");
                            int counter = 0;
                            foreach (string s in temp)
                            {
                                if (!(counter == temp.Count-1))
                                changes.Add("+L"+n+": "+latest[n]);
                                merged.Add(s);
                                n++;
                                counter++;
                            }
                            o++;
                            n = t + 1;
                        }
                }
                if (((o == original.Length) && (n == latest.Length)))
                {
                    done = true;
                }
            }
            Console.Out.WriteLine("Merge complete");
            StringBuilder newTextBuilder = new StringBuilder();
            for (int i = 0; i < merged.Count; i++)
            {
                if (!(i == merged.Count-1))
                newTextBuilder.AppendFormat(merged[i] + "\n");
                else
                newTextBuilder.AppendFormat(merged[i]);
            }

            text = newTextBuilder.ToString();

            // Check and see if the document has been marked for deletion
            this.deleted = doc.deleted;

            // A log that will document what changes have been made to the document.
            List<string> changeLog = new List<string>();

            bool titleChanged = false;
            bool pathChanged = false;
            bool textChanged = (!(changes.Count==0));
            bool picturesAdded = false;
            bool picturesRemoved = false;

            List<Picture> imagesAdded = new List<Picture>();
            List<Picture> imagesRemoved = new List<Picture>();

            // Has title been changed?
            if (String.Compare(doc.Title, this.Title) != 0)
            {
                changeLog.Add("Title has been changed from '" + this.Title + "' to '" + doc.Title + "'");
                this.title = doc.Title;
                titleChanged = true;
            }

            // Has path been changed?
            if (String.Compare(doc.Path, this.Path) != 0)
            {
                changeLog.Add("Path has been changed from '" + this.Path + "' to '" + doc.Path + "'");
                this.path = doc.Path;
                pathChanged = true;
            }

            // Are there any new pictures?
            foreach (Picture pic in doc.Images)
            {
                if (!(this.Images.Contains(pic)))
                {
                    picturesAdded = true;
                    imagesAdded.Add(pic);
                }
            }

            // Are any of the old pictures removed?
            foreach (Picture pic in this.Images)
            {
                if (!(doc.Images.Contains(pic)))
                {
                    picturesRemoved = true;
                    imagesRemoved.Add(pic);
                }
            }
            foreach (Picture pic in imagesRemoved)
            {
                images.Remove(pic);
                pic.Image.Dispose();
            }

            foreach (Picture pic in imagesAdded)
            {
                images.Add(pic);
            }
            // If there were pictures added, add it to the changelog.
            if (picturesAdded)
            {
                StringBuilder imageLineBuilder = new StringBuilder();

                for (int i = 0; i < imagesAdded.Count; i++)
                {
                    if (i == imagesAdded.Count - 1)
                        imageLineBuilder.AppendFormat(imagesAdded[i].Id);
                    else
                        imageLineBuilder.AppendFormat(imagesAdded[i].Id + ",");
                }
                changeLog.Add("Following pictures were added: " + imageLineBuilder.ToString());
            }

            // If there were pictures removed, add it to the changelog.
            if (picturesRemoved)
            {
                StringBuilder imageLineBuilder = new StringBuilder();

                for (int i = 0; i < imagesRemoved.Count; i++)
                {
                    if (i == imagesRemoved.Count - 1)
                        imageLineBuilder.AppendFormat(imagesRemoved[i].Id);
                    else
                        imageLineBuilder.AppendFormat(imagesRemoved[i].Id + ", ");
                }
                changeLog.Add("Following pictures were removed: " + imageLineBuilder.ToString());
            }

            // Finally, add changes to the text to the changelog.
            if (textChanged)
                changeLog.Add("Changes to the documents text:");

            foreach (string change in changes)
            {
                changeLog.Add(change);
            }

            string titleString = "";
            string pathString = "";
            string textString = "";
            string pictureString = "";
            string masterString = "Changed the document's: ";

            if (titleChanged)
                titleString = "Title. ";

            if (textChanged)
                textString = "Text. ";

            if (pathChanged)
                pathString = "Path. ";

            if ((picturesAdded || picturesRemoved)&&textChanged)
                pictureString = " And changed attached pictures";
            else if ((picturesAdded || picturesRemoved) && textChanged == false)
            {
                masterString = "";
                pictureString = "Changed the attached pictures";
            }

            this.Log.AddEntry(new DocumentLog.Entry(user, masterString + titleString + textString + pathString + pictureString, changeLog));

            return true;
        }
Exemplo n.º 2
0
        // This functions takes a newer version of this document, and merges it with this one
        // acording to "Simple Merge Policy" given in slice-of-pie.pdf.
        // It also updated all other fields in the document according to the new version
        // and generates a log based on the changes.
        // MergeWith returns a bool as well, it returns false if the ID of the updated
        // document is not the same as this documents ID, otherwise it returns true
        public bool MergeWith(Document doc, User user)
        {
            if (this.id != doc.id)
            {
                return(false);
            }
            List <string> changes = new List <string>();

            // Create original and latest arrays. ( step 1 )
            string[] original = this.GetTextAsArray();
            string[] latest   = doc.GetTextAsArray();
            // Create merged array ( made as a list instead ). ( step 2 )
            List <string> merged = new List <string>();
            // Definer o and n index, which point to lines in the original and latest arrays. ( step 3 )
            int  o    = 0;
            int  n    = 0;
            bool done = false;

            Console.Out.WriteLine("Started merge.");
            // While loop that continues untill the end of both versions of the document have been reached.
            while (!done)
            {
                Console.Out.WriteLine("New round in while loop");
                // All remaining lines in latest are new. ( step 4 )

                if ((o == original.Length) && (n != latest.Length))
                {
                    Console.Out.WriteLine("Step 4");
                    for (int N = n; N < latest.Length; N++)
                    {
                        changes.Add("+L" + N + ": " + latest[N]);
                        merged.Add(latest[N]);
                        n++;
                    }
                }
                // All remaining lines in original have been removed. ( step 5 )

                else if ((o != original.Length) && (n == latest.Length))
                {
                    Console.Out.WriteLine("Step 5");
                    for (int O = o; O < original.Length; O++)
                    {
                        changes.Add("-L" + O + ": " + original[O]);
                    }
                    o = original.Length;
                }
                // No changes have occured in this line ( step 6 )
                else if (String.Compare(original[o], latest[n]) == 0)
                {
                    Console.Out.WriteLine("Step 6");
                    merged.Add(original[o]);
                    n++;
                    o++;
                }

                else if (String.Compare(original[o], latest[n]) != 0)
                {
                    List <string> temp  = new List <string>();
                    bool          found = false;
                    int           t     = 0;
                    for (int N = n; N < latest.Length; N++)
                    {
                        temp.Add(latest[N]);
                        if (String.Compare(original[o], latest[N]) == 0)
                        {
                            found = true;
                            t     = N;
                            break;
                        }
                    }
                    // Line has been removed ( Step 7.b )
                    if (found == false)
                    {
                        Console.Out.WriteLine("Step 7.b");
                        changes.Add("-L" + o + ": " + original[o]);
                        o++;
                    }
                    // All lines in between have been added ( step 7.c )
                    else
                    {
                        Console.Out.WriteLine("Step 7.c");
                        int counter = 0;
                        foreach (string s in temp)
                        {
                            if (!(counter == temp.Count - 1))
                            {
                                changes.Add("+L" + n + ": " + latest[n]);
                            }
                            merged.Add(s);
                            n++;
                            counter++;
                        }
                        o++;
                        n = t + 1;
                    }
                }
                if (((o == original.Length) && (n == latest.Length)))
                {
                    done = true;
                }
            }
            Console.Out.WriteLine("Merge complete");
            StringBuilder newTextBuilder = new StringBuilder();

            for (int i = 0; i < merged.Count; i++)
            {
                if (!(i == merged.Count - 1))
                {
                    newTextBuilder.AppendFormat(merged[i] + "\n");
                }
                else
                {
                    newTextBuilder.AppendFormat(merged[i]);
                }
            }

            text = newTextBuilder.ToString();

            // Check and see if the document has been marked for deletion
            this.deleted = doc.deleted;

            // A log that will document what changes have been made to the document.
            List <string> changeLog = new List <string>();

            bool titleChanged    = false;
            bool pathChanged     = false;
            bool textChanged     = (!(changes.Count == 0));
            bool picturesAdded   = false;
            bool picturesRemoved = false;

            List <Picture> imagesAdded   = new List <Picture>();
            List <Picture> imagesRemoved = new List <Picture>();

            // Has title been changed?
            if (String.Compare(doc.Title, this.Title) != 0)
            {
                changeLog.Add("Title has been changed from '" + this.Title + "' to '" + doc.Title + "'");
                this.title   = doc.Title;
                titleChanged = true;
            }

            // Has path been changed?
            if (String.Compare(doc.Path, this.Path) != 0)
            {
                changeLog.Add("Path has been changed from '" + this.Path + "' to '" + doc.Path + "'");
                this.path   = doc.Path;
                pathChanged = true;
            }

            // Are there any new pictures?
            foreach (Picture pic in doc.Images)
            {
                if (!(this.Images.Contains(pic)))
                {
                    picturesAdded = true;
                    imagesAdded.Add(pic);
                }
            }


            // Are any of the old pictures removed?
            foreach (Picture pic in this.Images)
            {
                if (!(doc.Images.Contains(pic)))
                {
                    picturesRemoved = true;
                    imagesRemoved.Add(pic);
                }
            }
            foreach (Picture pic in imagesRemoved)
            {
                images.Remove(pic);
                pic.Image.Dispose();
            }

            foreach (Picture pic in imagesAdded)
            {
                images.Add(pic);
            }
            // If there were pictures added, add it to the changelog.
            if (picturesAdded)
            {
                StringBuilder imageLineBuilder = new StringBuilder();

                for (int i = 0; i < imagesAdded.Count; i++)
                {
                    if (i == imagesAdded.Count - 1)
                    {
                        imageLineBuilder.AppendFormat(imagesAdded[i].Id);
                    }
                    else
                    {
                        imageLineBuilder.AppendFormat(imagesAdded[i].Id + ",");
                    }
                }
                changeLog.Add("Following pictures were added: " + imageLineBuilder.ToString());
            }

            // If there were pictures removed, add it to the changelog.
            if (picturesRemoved)
            {
                StringBuilder imageLineBuilder = new StringBuilder();

                for (int i = 0; i < imagesRemoved.Count; i++)
                {
                    if (i == imagesRemoved.Count - 1)
                    {
                        imageLineBuilder.AppendFormat(imagesRemoved[i].Id);
                    }
                    else
                    {
                        imageLineBuilder.AppendFormat(imagesRemoved[i].Id + ", ");
                    }
                }
                changeLog.Add("Following pictures were removed: " + imageLineBuilder.ToString());
            }

            // Finally, add changes to the text to the changelog.
            if (textChanged)
            {
                changeLog.Add("Changes to the documents text:");
            }

            foreach (string change in changes)
            {
                changeLog.Add(change);
            }

            string titleString   = "";
            string pathString    = "";
            string textString    = "";
            string pictureString = "";
            string masterString  = "Changed the document's: ";

            if (titleChanged)
            {
                titleString = "Title. ";
            }

            if (textChanged)
            {
                textString = "Text. ";
            }

            if (pathChanged)
            {
                pathString = "Path. ";
            }

            if ((picturesAdded || picturesRemoved) && textChanged)
            {
                pictureString = " And changed attached pictures";
            }
            else if ((picturesAdded || picturesRemoved) && textChanged == false)
            {
                masterString  = "";
                pictureString = "Changed the attached pictures";
            }



            this.Log.AddEntry(new DocumentLog.Entry(user, masterString + titleString + textString + pathString + pictureString, changeLog));

            return(true);
        }