/// <summary>
        /// Updates the UserStrings for a bunch of collections.
        /// At the moment, it only works on The Design and Development Control Form.
        /// </summary>
        /// <param name="DocumentName">The name of the document we are updating the UserStrings for.</param>
        /// <param name="LSProp">The Properties we wish to update.</param>
        internal void UpdateUserStrings(string DocumentName, dynamic LSProp)
        {
            switch (DocumentName)
            {
            case "DDC":
                LightSwitchApplication.DandDC dandDC = LSProp;     //this DandDC!
                if (dandDC.ClientStaffMemberValidationAttendees.Count() > 0)
                {
                    var cvalma = from c in dandDC.ClientStaffMemberValidationAttendees
                                 where c.ClientStaffMember.Name != null
                                 select c.ClientStaffMember.Name;
                    dandDC.ClientValidationMeetingAttendees = String.Join(", ", cvalma);
                }

                if (dandDC.MatrixStaffMemberValidationAttendees.Count() > 0)
                {
                    var mvalma = from c in dandDC.MatrixStaffMemberValidationAttendees
                                 where c.MatrixStaffMember.Name != null
                                 select c.MatrixStaffMember.Name;
                    dandDC.MatrixValidationMeetingAttendees = String.Join(",", mvalma);
                }

                if (dandDC.ClientStaffMemberVerificationAttendees.Count() > 0)
                {
                    var cvma = from c in dandDC.ClientStaffMemberVerificationAttendees
                               where c.ClientStaffMember.Name != null
                               select c.ClientStaffMember.Name;
                    dandDC.ClientVerificationMeetingAttendees = String.Join(", ", cvma);
                }
                if (dandDC.MatrixStaffMemberVerificationAttendees.Count() > 0)
                {
                    var mvma = from c in dandDC.MatrixStaffMemberVerificationAttendees
                               where c.MatrixStaffMember.Name != null
                               select c.MatrixStaffMember.Name;
                    dandDC.MatrixVerificationMeetingAttendees = String.Join(", ", mvma);
                }
                break;

            case "SomethingElse":
                break;
            }
        }
        /// <summary>
        /// Generates a new document based on the document name and associated properties.
        /// Uses entitysets for the documents/tasks/reviews required.
        /// TO DO:
        /// 1) Check whether files exist in the Vault and prompt user to check out/update as necessary.
        /// 2)
        /// </summary>
        /// <param name="DocumentName">The name of the document we are creating.</param>
        /// <param name="LSProp">The Properties we wish to publish</param>
        /// <param name="LSDocs">The list of documents/tasks/reviews we need to publish</param>
        /// <param name="IsProjectDoc">Defines whether the document is using project data or independant</param>
        /// <param name="ListByProjectOnly">Defines whether the document (Timesheet mainly) is using project data.</param>
        public void GenerateFromAddNew(string DocumentName, dynamic LSProp, dynamic LSDocs, Boolean IsProjectDoc = true, Boolean ListByProjectOnly = false)
        {
            List <ColumnMapping> mapContent = new List <ColumnMapping>();
            List <OfficeIntegration.ColumnMapping> mappings = new List <ColumnMapping>();
            string  wordDoc  = "";
            dynamic doc      = "";
            string  filename = "";

            if (IsProjectDoc) // project-specific!
            {
                mapContent.Add(new ColumnMapping("Customer", "Customer"));
                mapContent.Add(new ColumnMapping("ProjectTitle", "Project"));
                mapContent.Add(new ColumnMapping("MatrixSalesOrderNo", "MatrixSalesOrderNo"));
                mapContent.Add(new ColumnMapping("MatrixProjectCode", "MatrixProjectCode"));
                mapContent.Add(new ColumnMapping("CustomerProjectCode", "CustomerProjectCode"));
                mapContent.Add(new ColumnMapping("CustomerOrderNo", "CustomerOrderNo"));
                mapContent.Add(new ColumnMapping("UserName", "UserName"));
                mapContent.Add(new ColumnMapping("UserPosition", "UserPosition"));
            }
            else //non project-specific!
            {
                mapContent.Add(new ColumnMapping("Customer", "Customer"));
                mapContent.Add(new ColumnMapping("MatrixSalesOrderNo", "MatrixSalesOrderNo"));
                mapContent.Add(new ColumnMapping("UserName", "UserName"));
                mapContent.Add(new ColumnMapping("UserPosition", "UserPosition"));
            }
            dynamic word = null;

            #region "Document-specific"

            switch (DocumentName)
            {
            case "CofC":
                #region "CofC --WORKING! 2013-10-01 AF"
                LightSwitchApplication.CofC cofC = LSProp;
                //template name
                wordDoc = matrixDocs + "CofC QMF 10 Issue 06.dotx";
                //template-specifics
                mapContent.Add(new ColumnMapping("DeliveryNoteNo", "DeliveryNoteNo"));
                mapContent.Add(new ColumnMapping("CertificateNo", "CertificateNo"));
                mapContent.Add(new ColumnMapping("CofCDate", "CofCDate"));
                doc = OfficeIntegration.Word.GenerateDocument(wordDoc, cofC, mapContent);
                //template datatable-specific columns
                mappings.Add(new OfficeIntegration.ColumnMapping("", "Quantity"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DrawingNumber"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DrawingRevision"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DrawingTitle"));
                word = OfficeIntegration.Word.GetWord();
                Word.GetDocument(word, wordDoc);
                //export our list of documents:
                Microsoft.LightSwitch.Framework.EntitySet <CofCDoc> docs = LSDocs;
                var cofcresults = from a in docs
                                  where a.CofC.CertificateNo == cofC.CertificateNo
                                  select a;
                Word.Export(doc, "DataTable", 2, false, cofcresults, mappings);

                filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\CofCs\" + cofC.CertificateNo + ".docx";
                break;

                #endregion
            case "DT":
                #region "Transmittal --WORKING! 2013-12-03 AF"
                LightSwitchApplication.Transmittal trans = LSProp;     //this transimittal!
                //template name
                wordDoc = matrixDocs + "DT QMF39 Issue 02.dotx";
                //template-specifics
                mapContent.Add(new ColumnMapping("TransId", "TransmittalNumber"));
                mapContent.Add(new ColumnMapping("Month", "Month"));
                mapContent.Add(new ColumnMapping("UserName", "UserName"));
                mapContent.Add(new ColumnMapping("Recipient", "Recipient"));
                mapContent.Add(new ColumnMapping("QueryDate", "QueryDate"));
                doc = OfficeIntegration.Word.GenerateDocument(wordDoc, trans, mapContent);
                //template datatable-specific columns
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DocNo"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DocRevision"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "SheetSize"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "MPENumber"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DocTitle"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "Copies"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "TransmittalDocReasonsForIssue"));
                word = OfficeIntegration.Word.GetWord();
                Word.GetDocument(word, wordDoc);
                Microsoft.LightSwitch.Framework.EntitySet <TransmittalDoc> transDocs = LSDocs;    //all the documents!
                var transresults = from TransmittalDoc a in transDocs
                                   where a.Transmittal.TransmittalNumber == trans.TransmittalNumber
                                   select a;
                Word.Export(doc, "DataTable", 2, false, transresults, mappings);

                filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\Transmittals\" + trans.TransmittalNumber + ".docx";
                break;

                #endregion
            case "DDA":
                #region "Drawing and Document Approval Form --WORKING! 2013-10-02 AF"
                LightSwitchApplication.DandDA dandDA = LSProp;     //this DandDA!
                //template name
                wordDoc = matrixDocs + "DDA QMF47 ISsue 02.dotx";
                //template-specifics
                mapContent.Add(new ColumnMapping("DDAId", "DDANumber"));
                mapContent.Add(new ColumnMapping("DDADate", "DDADate"));
                doc = OfficeIntegration.Word.GenerateDocument(wordDoc, dandDA, mapContent);
                //template datatable-specific columns
                mappings.Add(new OfficeIntegration.ColumnMapping("", "DrawingNo"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "Title"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "Revision"));
                word = OfficeIntegration.Word.GetWord();
                Word.GetDocument(word, wordDoc);
                Microsoft.LightSwitch.Framework.EntitySet <DandADoc> DandDADocs = LSDocs;    //all the documents!
                var dandDAResults = from DandADoc a in DandDADocs
                                    where a.DandDA.DDANumber == dandDA.DDANumber
                                    select a;
                Word.Export(doc, "DataTable", 2, false, dandDAResults, mappings);

                filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\DDAs\" + dandDA.DDANumber + ".docx";
                break;

                #endregion
            case "DDC":
                #region "Drawing and Document Control Form --WORKING! 2013-10-11 AF"
                LightSwitchApplication.DandDC dandDC = LSProp; //this DandDC!
                filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\DDCs\" + dandDC.DDCNumber + ".docx";
                if (!File.Exists(filename))                    //new file
                {
                    //template name
                    wordDoc = matrixDocs + "DDC QMF25 Issue 02.dotx";
                    mapContent.Add(new ColumnMapping("DesignId", "DDCId"));
                    mapContent.Add(new ColumnMapping("Month", "Month"));
                    mapContent.Add(new ColumnMapping("BasedOn", "BasedOn"));
                    mapContent.Add(new ColumnMapping("Essentials", "Essentials"));
                    mapContent.Add(new ColumnMapping("StatsRegs", "StatsRegs"));
                    mapContent.Add(new ColumnMapping("SpecReqs", "SpecReqs"));
                    mapContent.Add(new ColumnMapping("DandDCStartDate", "DandDCStartDate"));
                    doc = OfficeIntegration.Word.GenerateDocument(wordDoc, dandDC, mapContent);
                }
                else     //updating the existing file.
                {
                    wordDoc = filename;
                    mapContent.Add(new ColumnMapping("Verification", "OutputsVerification"));
                    mapContent.Add(new ColumnMapping("Validation", "OutputsValidation"));
                    mapContent.Add(new ColumnMapping("OutputsValiMDate", "OutputsValiMDate"));
                    mapContent.Add(new ColumnMapping("OutputsVeriMDate", "OutputsVeriMDate"));
                    mapContent.Add(new ColumnMapping("OutputsVeriMMatrixAttendees", "MatrixVerificationMeetingAttendees"));
                    mapContent.Add(new ColumnMapping("OutputsVeriMClientAttendees", "ClientVerificationMeetingAttendees"));
                    mapContent.Add(new ColumnMapping("OutputsValiMMatrixAttendees", "MatrixValidationMeetingAttendees"));
                    mapContent.Add(new ColumnMapping("OutputsValiMClientAttendees", "ClientValidationMeetingAttendees"));
                    doc = OfficeIntegration.Word.GenerateDocument(wordDoc, dandDC, mapContent);
                    mappings.Add(new OfficeIntegration.ColumnMapping("", "ReviewDateStr"));
                    mappings.Add(new OfficeIntegration.ColumnMapping("", "Reviewer"));
                    mappings.Add(new OfficeIntegration.ColumnMapping("", "ReviewLocation"));
                    mappings.Add(new OfficeIntegration.ColumnMapping("", "ReviewComment"));

                    word = OfficeIntegration.Word.GetWord();
                    Word.GetDocument(word, wordDoc);
                    Microsoft.LightSwitch.Framework.EntitySet <Review> reviews = LSDocs;    //all the documents!
                    var dandDCResults = from Review a in reviews
                                        where a.Project.ProjectTitle == dandDC.Project.ProjectTitle
                                        select a;
                    Word.Export(doc, "Reviews", 3, false, dandDCResults, mappings);

                    filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\DDCs\" + dandDC.DDCNumber + ".docx";
                }
                #endregion
                break;

            case "COPT":
                #region "CAD Office Project Report --WORKING! 2013-12-03 AF"
                LightSwitchApplication.CADOfficeProjT copt = LSProp;
                //var myDocs = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
                wordDoc = matrixDocs + "COPT QMF41 Issue 02.dotx";
                //template-specifics
                mapContent.Add(new ColumnMapping("COPTId", "COPTNumber"));
                mapContent.Add(new ColumnMapping("Month", "Month"));
                mapContent.Add(new ColumnMapping("Year", "Year"));
                mapContent.Add(new ColumnMapping("UserName", "UserName"));
                doc = OfficeIntegration.Word.GenerateDocument(wordDoc, copt, mapContent);
                //template datatable-specific columns
                mappings.Add(new OfficeIntegration.ColumnMapping("", "TaskDate"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "TaskName"));
                mappings.Add(new OfficeIntegration.ColumnMapping("", "Hours"));
                word = OfficeIntegration.Word.GetWord();
                Word.GetDocument(word, wordDoc);
                //Project or month-specifics:
                Microsoft.LightSwitch.Framework.EntitySet <Task> tasks = LSDocs;
                //dynamic sortedGroups = getGroupedTasks(tasks);
                if (IsProjectDoc)     //project tasks
                {
                    var filteredTasks = from Task t in tasks
                                        where t.Project != null
                                        select t;
                    if (copt.EndDate == null)
                    {
                        if (!ListByProjectOnly)
                        {
                            coptresults = from Task b in filteredTasks
                                          where b.Project.ProjectTitle == copt.Project.ProjectTitle &&
                                          b.Day >= copt.StartDate &&
                                          b.User == copt.UserName
                                          select b;
                        }
                        else
                        {
                            coptresults = from Task b in filteredTasks
                                          where b.Project.ProjectTitle == copt.Project.ProjectTitle &&
                                          b.Day >= copt.StartDate
                                          select b;
                        }
                    }
                    else
                    {
                        if (!ListByProjectOnly)
                        {
                            coptresults = from Task b in filteredTasks
                                          where b.Project.ProjectTitle == copt.Project.ProjectTitle &&
                                          b.Day >= copt.StartDate &&
                                          b.Day <= copt.EndDate &&
                                          b.User == copt.UserName
                                          select b;
                        }
                        else
                        {
                            coptresults = from Task b in filteredTasks
                                          where b.Project.ProjectTitle == copt.Project.ProjectTitle &&
                                          b.Day >= copt.StartDate &&
                                          b.Day <= copt.EndDate
                                          select b;
                        }
                    }
                    filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\Project Timesheets\"
                               + copt.COPTNumber
                               + ".docx";
                }
                else     //monthly tasks
                {
                    if (copt.EndDate == null)
                    {
                        coptresults = from Task b in tasks
                                      where b.Day >= copt.StartDate &&
                                      b.User == copt.UserName
                                      orderby b.Day
                                      select b;
                    }
                    else
                    {
                        coptresults = from Task b in tasks
                                      where b.Day >= copt.StartDate &&
                                      b.Day <= copt.EndDate &&
                                      b.User == copt.UserName
                                      orderby b.Day
                                      select b;
                    }
                    filename = @"\\MATRIXSERVER\Data\01 - CAD SUPPORT FILES\Project Timesheets\"
                               + copt.COPTNumber
                               + ".docx";
                }

                //var coptresults = from Task t in tasks
                //                  select t;

                Word.Export(doc, "DataTable", 2, false, coptresults, mappings);


                break;

                #endregion
            case "":
                break;
            }
            #endregion

            dynamic _word = AutomationFactory.GetObject("Word.Application");
            var     _doc  = _word.ActiveDocument;
            if (DocumentName == "DDC")
            {
                #region "checkboxes"
                if (LSProp.ProtoTypeProduced == true)
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("PrototypeProducedYes");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                    ccs = _doc.SelectContentControlsByTitle("PrototypeProducedNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = false;
                    }
                }
                else
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("PrototypeProducedNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                }
                if (LSProp.MaterialsCosted == true)
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("MaterialsYes");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                    ccs = _doc.SelectContentControlsByTitle("MaterialsNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = false;
                    }
                }
                else
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("MaterialsNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                }
                if (LSProp.LabourCosted == true)
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("LabourCostedYes");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                    ccs = _doc.SelectContentControlsByTitle("LabourCostedNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = false;
                    }
                }
                else
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("LabourCostedNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                }
                if (LSProp.ClientApproved == true)
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("ClientApprovedYes");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                    ccs = _doc.SelectContentControlsByTitle("ClientApprovedNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = false;
                    }
                }
                else
                {
                    dynamic ccs = _doc.SelectContentControlsByTitle("ClientApprovedNo");
                    if (ccs != null)
                    {
                        dynamic cc = ccs[1];
                        cc.Checked = true;
                    }
                }
                #endregion
            }
            _doc.SaveAs(filename, 12);
            if (!File.Exists(filename))
            {
                MessageBox.Show("The created Word File didn't save for some reason, please do a manual saveas to preserve our template!");
            }
            mapContent = null;
            mappings   = null;
        }