private void clearTemplateButton_Click_1(object sender, EventArgs e)
 {
     List<string> genericList = new List<string>(templateStorage.Componentlist);
     DocumentFactory df = new DocumentFactory();
     outputPanel.Controls.Clear();
     outputPanel.Controls.Add(df.createDocument(templateStorage.Name, templateStorage.Version, genericList));
 }
        /// <summary>
        /// Gets the document's data and the associated template and then generates the document for the user to view.
        /// </summary>
        private void getTemplate()
        {
            if (projectTreeView.SelectedNode.Nodes.Count == 0 && projectTreeView.SelectedNode.Text != "Documents" && projectTreeView.SelectedNode.Level != 0)
            {
                newDocument = false;
                Serialization saveDocument = new Serialization();

                string getVersionIDText = "SELECT Version.versionID, Project.projectID FROM Version, Project WHERE Version.versionNumber=" + projectTreeView.SelectedNode.Parent.Parent.Text.Substring(7) + " AND Project.projectName='" + projectTreeView.SelectedNode.Parent.Parent.Parent.Text + "' AND Version.project = Project.projectID;";
                DBCommand getVersionIDCmd = DBConnection.makeCommand(getVersionIDText);
                SqlCeDataReader getVersionIDReader = getVersionIDCmd.Start();
                int versionID;
                int projectID;
                getVersionIDReader.Read();
                versionID = getVersionIDReader.GetInt32(0);
                projectID = getVersionIDReader.GetInt32(1);

                string getTemplateTypeSqlText = "SELECT documentType FROM Document WHERE documentName='" + projectTreeView.SelectedNode.Text + "' AND versionID=" + versionID + ";";
                DBCommand getTemplateTypeSqlCmd = DBConnection.makeCommand(getTemplateTypeSqlText);
                SqlCeDataReader getTemplateTypeSqlReader = getTemplateTypeSqlCmd.Start();
                String templateType = "";
                while (getTemplateTypeSqlReader.Read())
                {
                    templateType += getTemplateTypeSqlReader.GetString(0);
                }

                string getTemplateSqlText = "SELECT templateData FROM Template WHERE templateName='" + templateType + "';";
                DBCommand getTemplateSqlCmd = DBConnection.makeCommand(getTemplateSqlText);
                SqlCeDataReader getTemplateSqlReader = getTemplateSqlCmd.Start();
                String templateInput = "";
                while (getTemplateSqlReader.Read())
                {
                    templateInput += getTemplateSqlReader.GetString(0);
                }
                //Testing the deSerialize method.
                TemplateStorage templateStorage = new TemplateStorage();
                templateStorage = (TemplateStorage)saveDocument.deSerialize(templateStorage, templateInput);
                this.templateStorage = templateStorage;
                List<string> genericList = new List<string>(templateStorage.Componentlist);

                DocumentFactory df = new DocumentFactory();
                outputPanel.Controls.Clear();
                outputPanel.Controls.Add(df.createDocument(templateStorage.Name, templateStorage.Version, genericList));
                templateControlToolStrip.Enabled = true;

                string getDocSqlText = "SELECT documentID, data FROM Document, Version WHERE documentName='" + projectTreeView.SelectedNode.Text + "' AND Document.versionID=" + versionID + ";";
                DBCommand getDocSqlCmd = DBConnection.makeCommand(getDocSqlText);
                SqlCeDataReader getDocSqlReader = getDocSqlCmd.Start();
                String documentData = "";

                getDocSqlReader.Read();
                this.documentID = (int)getDocSqlReader.GetSqlInt32(0);
                documentData += getDocSqlReader.GetString(1);

                while (getDocSqlReader.Read())
                {

                }
                DocumentStorage documentStorage = new DocumentStorage();
                documentStorage = (DocumentStorage)saveDocument.deSerialize(documentStorage, documentData);

                string[] controlData = documentStorage.DocumentData;
                if (controlData != null)
                {
                    int counter = 0;
                    for (int i = 0; i < outputPanel.Controls[0].Controls[2].Controls.Count; i++)
                    {
                        if (i % 2 != 0)
                        {
                            outputPanel.Controls[0].Controls[2].Controls[i].Text = controlData[counter];
                            counter++;
                        }
                    }
                }
            }
        }
        private void useTemplateButton_Click(object sender, EventArgs e)
        {
            exampleOutputPanel.Controls.Clear();
            if (templateListView.SelectedItems.Count > 0)
            {
                newDocument = true;
                string nextSqlText = "SELECT templateData FROM Template WHERE templateName='" + templateListView.SelectedItems[0].Text + "';";
                DBCommand nextSqlCmd = DBConnection.makeCommand(nextSqlText);
                SqlCeDataReader nextSqlReader = nextSqlCmd.Start();
                String input = "";
                while (nextSqlReader.Read())
                {
                    input += nextSqlReader.GetString(0);
                }

                nextSqlCmd.Stop(); //Stops command.
                //Testing the deSerialize method.
                TemplateStorage templateStorage = new TemplateStorage();
                Serialization saveDocument = new Serialization();
                templateStorage = (TemplateStorage)saveDocument.deSerialize(templateStorage, input);
                this.templateStorage = templateStorage;
                List<string> genericList = new List<string>(templateStorage.Componentlist);

                DocumentFactory df = new DocumentFactory();
                outputPanel.Controls.Clear();
                outputPanel.Controls.Add(df.createDocument(templateStorage.Name, templateStorage.Description, genericList));
                templateControlToolStrip.Enabled = true;

                string next1SqlText = "SELECT documentID FROM Document WHERE documentType='" + templateListView.SelectedItems[0].Text + "';";
                DBCommand next1SqlCmd = DBConnection.makeCommand(next1SqlText);
                SqlCeDataReader next1SqlReader = next1SqlCmd.Start();
                int input1=0;
                if (next1SqlReader.Read())
                {
                    input1 = next1SqlReader.GetInt32(0);
                }
                                nextSqlCmd.Stop(); //Stops command.
                if (input1 == 0)
                {
                    Label noTemp = new Label();
                    noTemp.AutoSize = true;
                    noTemp.Text = "The Template has not been used.";
                    exampleOutputPanel.Controls.Add(noTemp);
                }
                else
                {
                    getExample(input1, templateListView.SelectedItems[0].Text);
                }
            }
        }
        private void getExample(int documentID, String docuType)
        {
            Serialization saveDocument = new Serialization();

                string getTemplateSqlText = "SELECT templateData FROM Template WHERE templateName='" + docuType + "';";
                DBCommand getTemplateSqlCmd = DBConnection.makeCommand(getTemplateSqlText);
                SqlCeDataReader getTemplateSqlReader = getTemplateSqlCmd.Start();
                String templateInput = "";
                while (getTemplateSqlReader.Read())
                {
                    templateInput += getTemplateSqlReader.GetString(0);
                }
                //Testing the deSerialize method.
                TemplateStorage templateStorage = new TemplateStorage();
                templateStorage = (TemplateStorage)saveDocument.deSerialize(templateStorage, templateInput);
                this.templateStorage = templateStorage;
                List<string> genericList = new List<string>(templateStorage.Componentlist);

                DocumentFactory df = new DocumentFactory();
                exampleOutputPanel.Controls.Clear();
                exampleOutputPanel.Controls.Add(df.createDocument(templateStorage.Name, templateStorage.Version, genericList));

                string getDocSqlText = "SELECT data FROM Document WHERE documentID='" + documentID + "';";
                DBCommand getDocSqlCmd = DBConnection.makeCommand(getDocSqlText);
                SqlCeDataReader getDocSqlReader = getDocSqlCmd.Start();
                String documentData = "";

                getDocSqlReader.Read();
                documentData += getDocSqlReader.GetString(0);

                DocumentStorage documentStorage = new DocumentStorage();
                documentStorage = (DocumentStorage)saveDocument.deSerialize(documentStorage, documentData);

                string[] controlData = documentStorage.DocumentData;
                if (controlData != null)
                {
                    int counter = 0;
                    for (int i = 0; i < outputPanel.Controls[0].Controls[2].Controls.Count; i++)
                    {
                        if (i % 2 != 0)
                        {
                            exampleOutputPanel.Controls[0].Controls[2].Controls[i].Text = controlData[counter];
                            counter++;
                        }
                    }
                }
        }