Example #1
0
        //
        // List documents for a client
        //
        public void List(int clientID, int clientDocumentSetUID)
        {
            clientDocSetDocLink = new List <scClientDocSetDocLink>();

            using (var connection = new SqlConnection(ConnString.ConnectionString))
            {
                var commandString = string.Format(
                    " SELECT " +
                    "  [UID] " +
                    "  ,[FKClientUID] " +
                    "  ,[FKClientDocumentSetUID] " +
                    "  ,[FKDocumentUID] " +
                    "  ,[SequenceNumber] " +
                    "  ,[StartDate] " +
                    "  ,[EndDate] " +
                    "  ,[IsVoid] " +
                    "  ,[SourceLocation] " +
                    "  ,[SourceFileName] " +
                    "  ,[Location] " +
                    "  ,[FileName] " +
                    "  ,[SourceIssueNumber] " +
                    "  ,[Generated] " +
                    "  ,[RecordType] " +
                    "  ,[ParentUID] " +
                    "   FROM [ClientDocument] " +
                    "   WHERE [FKClientUID] = {0} " +
                    "   AND [FKClientDocumentSetUID] = {1} " +
                    "   ORDER BY ParentUID ASC, SequenceNumber ASC ",
                    clientID,
                    clientDocumentSetUID
                    );

                using (var command = new SqlCommand(
                           commandString, connection))
                {
                    connection.Open();
                    using (SqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            // Ignore voids
                            if (Convert.ToChar(reader["IsVoid"]) == 'Y')
                            {
                                continue;
                            }

                            var docItem = new scClientDocSetDocLink();

                            // Get document
                            //
                            docItem.document     = new Document.Document();
                            docItem.document.UID = Convert.ToInt32(reader["FKDocumentUID"]);
                            docItem.document.Read();

                            // Get Client Document Set
                            //
                            docItem.clientDocumentSet             = new ClientDocumentSet();
                            docItem.clientDocumentSet.UID         = Convert.ToInt32(reader["FKClientDocumentSetUID"].ToString());
                            docItem.clientDocumentSet.FKClientUID = Convert.ToInt32(reader["FKClientUID"].ToString());

                            // Set Client Document
                            //
                            docItem.clientDocument                        = new ClientDocument();
                            docItem.clientDocument.UID                    = Convert.ToInt32(reader["UID"].ToString());
                            docItem.clientDocument.FKDocumentUID          = Convert.ToInt32(reader["FKDocumentUID"].ToString());
                            docItem.clientDocument.SequenceNumber         = Convert.ToInt32(reader["SequenceNumber"].ToString());
                            docItem.clientDocument.FKClientDocumentSetUID = Convert.ToInt32(reader["FKClientDocumentSetUID"].ToString());
                            docItem.clientDocument.IsVoid                 = Convert.ToChar(reader["IsVoid"].ToString());
                            docItem.clientDocument.StartDate              = Convert.ToDateTime(reader["StartDate"].ToString());
                            docItem.clientDocument.SourceLocation         = reader["SourceLocation"].ToString();
                            docItem.clientDocument.SourceFileName         = reader["SourceFileName"].ToString();
                            docItem.clientDocument.Location               = reader["Location"].ToString();
                            docItem.clientDocument.FileName               = reader["FileName"].ToString();
                            docItem.clientDocument.SourceIssueNumber      = Convert.ToInt32(reader["SourceIssueNumber"].ToString());
                            docItem.clientDocument.Generated              = Convert.ToChar(reader["Generated"]);
                            docItem.clientDocument.RecordType             = reader["RecordType"].ToString();
                            docItem.clientDocument.ParentUID              = Convert.ToInt32(reader["ParentUID"].ToString());


                            try
                            {
                                docItem.clientDocument.EndDate = Convert.ToDateTime(reader["EndDate"].ToString());
                            }
                            catch
                            {
                                docItem.clientDocument.EndDate = System.DateTime.MaxValue;
                            }

                            this.clientDocSetDocLink.Add(docItem);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Generate Documents Controller (Word must be previously initialised
        /// </summary>
        private void GenerateDocumentsController(TreeNode documentsTreeNode)
        {
            scClientDocSetDocLink documentTN = new scClientDocSetDocLink();

            // Get List of documents for a client
            //
            if (documentsTreeNode.Tag.GetType().Name == "scClientDocSetDocLink")
            {
                documentTN = (scClientDocSetDocLink)documentsTreeNode.Tag;
            }
            else
            {
                if (documentsTreeNode.Tag.GetType().Name == "Document")
                {
                    if (documentTN.clientDocument == null)
                    {
                        if (uioutput != null)
                        {
                            uioutput.AddOutputMessage("Error CDISNULL019202 - client document is null.");
                        }
                        if (uioutput != null)
                        {
                            uioutput.AddErrorMessage("Error CDISNULL019202 - client document is null. Generation stopped.");
                        }
                        return;
                    }

                    documentTN.clientDocument.RecordType = Utils.RecordType.DOCUMENT;
                    documentTN.document = (Document.Document)documentsTreeNode.Tag;
                }
            }

            // If it is a document, generate
            if (documentTN.clientDocument.RecordType.Trim() != Utils.RecordType.FOLDER)
            {
                #region Generate Document
                // Generate

                // Retrieve updated file name from source
                Document.Document document = new Document.Document();
                document.UID = documentTN.clientDocument.FKDocumentUID;
                document.Read();

                if (uioutput != null)
                {
                    uioutput.AddOutputMessage("Inspecting file: " + document.UID + " === " + document.Name);
                }
                // if (iconMessage != null) iconMessage.Text = "Start time: " + System.DateTime.Now.ToString();

                // Update client records with new file name
                //
                // Instantiate client document
                ClientDocument cd = new ClientDocument();
                cd.UID            = documentTN.clientDocument.UID;
                cd.SourceFileName = document.FileName;
                // cd.Read();

                // Set comboIssueNumber and File Name
                //
                ClientDocument.SetClientDestinationFile(
                    clientDocument: cd,
                    clientUID: documentTN.clientDocument.FKClientUID,
                    documentCUID: document.CUID,
                    sourceFileName: document.FileName,
                    sourceVersionNumber: document.IssueNumber,
                    simpleFileName: document.SimpleFileName);


                cd.UpdateSourceFileName();

                // Update memory with latest file name
                documentTN.clientDocument.SourceFileName = cd.SourceFileName;

                string sourceFileLocationName = Utils.getFilePathName(
                    documentTN.clientDocument.SourceLocation,
                    documentTN.clientDocument.SourceFileName);

                // check if source folder/ file exists
                if (string.IsNullOrEmpty(documentTN.clientDocument.Location))
                {
                    if (uioutput != null)
                    {
                        uioutput.AddOutputMessage("Document Location is empty.");
                    }
                    return;
                }

                if (string.IsNullOrEmpty(documentTN.clientDocument.FileName))
                {
                    if (uioutput != null)
                    {
                        uioutput.AddOutputMessage("File Name is empty.");
                    }
                    return;
                }

                if (!File.Exists(sourceFileLocationName))
                {
                    string er = "File does not exist " +
                                sourceFileLocationName + " - File Name: " + documentTN.clientDocument.SourceFileName;

                    if (uioutput != null)
                    {
                        uioutput.AddOutputMessage(er);
                    }
                    if (uioutput != null)
                    {
                        uioutput.AddErrorMessage(er);
                    }
                    return;
                }


                // Check if destination folder exists
                //
                if (string.IsNullOrEmpty(cds.Folder))
                {
                    string er = "Destination folder not set. Generation stopped.";
                    if (uioutput != null)
                    {
                        uioutput.AddOutputMessage(er);
                    }
                    return;
                }
                string PhysicalCDSFolder = Utils.GetPathName(cds.Folder);
                if (!Directory.Exists(PhysicalCDSFolder))
                {
                    Directory.CreateDirectory(PhysicalCDSFolder);
                }

                // Generate one document (Folder will also be created in this call)
                //

                // Get time before file
                var previousTime = System.DateTime.Now;

                GenerateDocument(documentTN);

                // Get time after file
                var agora = System.DateTime.Now;

                fileprocessedcount++;
                valueForProgressBar = (fileprocessedcount / filecount) * 100;
                int leftToProcess = filecount - Convert.ToInt32(fileprocessedcount);

                // Get the time it took to process one file
                TimeSpan span = agora.Subtract(previousTime);

                // Average span in seconds
                acumulatedSpanInSec += span.Seconds;
                averageSpanInSec     = acumulatedSpanInSec / fileprocessedcount;

                // Calculate the estimated time to complete
                estimated = System.DateTime.Now.AddSeconds(averageSpanInSec * leftToProcess);

                if (uioutput != null)
                {
                    uioutput.UpdateProgressBar(valueForProgressBar, estimated, leftToProcess);
                }

                return;

                #endregion Generate Document

                // Processing for one file ends here
            }
            else
            {
                // If item imported is a FOLDER

                // This is the client destination folder (and name)
                //
                string clientDestinationFileLocation = documentTN.clientDocument.Location.Trim();

                if (!string.IsNullOrEmpty(clientDestinationFileLocation))
                {
                    string clientDestinationFileLocationName = Utils.getFilePathName(
                        clientDestinationFileLocation, documentTN.clientDocument.FileName.Trim());

                    // string PhysicalLocation = Utils.GetPathName(clientDestinationFileLocation);
                    string PhysicalLocation = clientDestinationFileLocationName;

                    if (uioutput != null)
                    {
                        uioutput.AddOutputMessage("Processing Folder: " + PhysicalLocation);
                    }

                    if (!string.IsNullOrEmpty(PhysicalLocation))
                    {
                        if (!Directory.Exists(PhysicalLocation))
                        {
                            if (uioutput != null)
                            {
                                uioutput.AddOutputMessage("Folder Created: " + clientDestinationFileLocationName);
                            }

                            Directory.CreateDirectory(PhysicalLocation);
                        }
                        else
                        {
                            if (uioutput != null)
                            {
                                uioutput.AddOutputMessage("Folder Already Exists: " + clientDestinationFileLocationName);
                            }
                        }
                    }
                }
                else
                {
                    if (uioutput != null)
                    {
                        uioutput.AddOutputMessage("Folder Ignored: " + documentTN.document.Name);
                    }
                }

                // Process each document in folder
                //
                foreach (TreeNode tn in documentsTreeNode.Nodes)
                {
                    scClientDocSetDocLink doco = (scClientDocSetDocLink)tn.Tag;

                    GenerateDocumentsController(tn);
                }
            }
        }
        /// <summary>
        /// Generate one document
        /// </summary>
        /// <param name="doco"></param>
        public void GenerateDocument(scClientDocSetDocLink doco)
        {
            // Retrieve latest version
            //
            Document.Document document = new Document.Document();
            document.UID = doco.clientDocument.FKDocumentUID;
            document.Read();

            string msg = ">>> Generating file: " + document.UID + " === " + document.SimpleFileName;

            if (uioutput != null)
            {
                uioutput.AddOutputMessage(msg);
            }
            // if (iconMessage != null) iconMessage.Text = ">>> Generating file: " + document.UID;


            // Locate source file
            //
            string sourceFileLocationName = Utils.getFilePathName(
                doco.clientDocument.SourceLocation,
                doco.clientDocument.SourceFileName);

            // Find the parent folder location
            //
            int            parentUID = doco.clientDocument.ParentUID;
            ClientDocument cdParent  = new ClientDocument();

            cdParent.UID = parentUID;
            cdParent.Read();

            // This is the client destination folder (and name)
            //
            //ResponseStatus destLocationDerivedClient = ClientDocument.GetDocumentPath(doco.clientDocument);
            //string destLocationDerived = destLocationDerivedClient.Contents.ToString();

            string clientDestinationFileLocation = doco.clientDocument.Location.Trim();

            string clientDestinationFileLocationName = Utils.getFilePathName(
                clientDestinationFileLocation, doco.clientDocument.FileName.Trim());

            // This is the source client file name
            //
            string clientSourceFileLocation = doco.clientDocument.Location.Trim();

            string clientSourceFileLocationName = Utils.getFilePathName(
                clientSourceFileLocation,
                doco.clientDocument.FileName.Trim());

            // Source location and destination may be different.
            // The destination of the file must be the one where it lives on the actual tree
            // To determine where the file should be located, we need to get the parent folder
            // The only way to determine the parent folder is walking through the entire tree from the root.
            // The root is the only thing that can be trusted. Every other location is dependent on the root.

            // The determination of the file location occurs in 2 parts. The one here is the second part.
            // At this point we are not going to use the source location of the original document, instead
            // we are going to use the client document location
            //

            // Check if destination folder directory exists
            //
            string PhysicalLocation = Utils.GetPathName(clientDestinationFileLocation);

            if (string.IsNullOrEmpty(PhysicalLocation))
            {
                string er = "Location is empty " + clientDestinationFileLocation + "\n" +
                            "File Name: " + doco.document.Name;

                uioutput.AddOutputMessage(er);
                return;
            }


            // 02/04/2011
            // This step should be done when the "FOLDER" record is created and not when the document
            // is generated
            //

            //if (!Directory.Exists( PhysicalLocation ))
            //    Directory.CreateDirectory( PhysicalLocation );


            // However the folder existence must be checked
            //
            if (!Directory.Exists(PhysicalLocation))
            {
                Directory.CreateDirectory(PhysicalLocation);

                string er = "Destination folder has been created with File! " + PhysicalLocation + "\n" +
                            "File Name: " + doco.document.Name;

                uioutput.AddOutputMessage(er);
                //return;
            }

            if (File.Exists(clientDestinationFileLocationName))
            {
                // Proceed but report in list
                //
                if (overrideDocuments == "Yes")
                {
                    // Delete file
                    try
                    {
                        File.Delete(clientDestinationFileLocationName);
                        uioutput.AddOutputMessage("File replaced: " +
                                                  document.SimpleFileName);
                    }
                    catch (Exception)
                    {
                        uioutput.AddOutputMessage("Error deleting file " +
                                                  document.SimpleFileName);
                        uioutput.AddErrorMessage("Error deleting file " +
                                                 document.SimpleFileName);

                        return;
                    }
                }
                else
                {
                    uioutput.AddOutputMessage("File already exists " +
                                              document.SimpleFileName);
                    return;
                }
            }

            // Copy and fix file
            //

            // Word Documents
            //
            if (doco.clientDocument.RecordType.Trim() == Utils.RecordType.FOLDER)
            {
                // Update file - set as GENERATED.
                //

                // This is the moment where the folder destination has to be created
                // and the folder db record has to be updated with the location
                //

                if (!Directory.Exists(PhysicalLocation))
                {
                    Directory.CreateDirectory(PhysicalLocation);
                }

                uioutput.AddOutputMessage("FOLDER: " + doco.clientDocument.SourceFileName);
            }
            else
            {
                // If is is not a folder, it must be a regular file.
                // Trying to copy it as well...
                //

                var currentDocumentPath = Path.GetExtension(doco.clientDocument.FileName);

                if (doco.clientDocument.DocumentType == Utils.DocumentType.WORD)
                {
                    #region Word
                    // ------------------------------------------------------------------------
                    // ------------------------------------------------------------------------
                    // Generate Document and replace tag values in new document generated
                    // ------------------------------------------------------------------------
                    // ------------------------------------------------------------------------
                    var results = WordDocumentTasks.CopyDocument(sourceFileLocationName, clientSourceFileLocationName, ts, vkWordApp, uioutput);
                    if (results.ReturnCode < 0)
                    {
                        // Error has occurred
                        //
                        var er = (System.Exception)results.Contents;
                        uioutput.AddOutputMessage("ERROR: " + er.ToString());
                        uioutput.AddErrorMessage("ERROR: " + er.ToString());

                        return;
                    }


                    #endregion Word
                }
                else if (doco.clientDocument.DocumentType == Utils.DocumentType.EXCEL)
                {
                    // ------------------------------------------------------------------------
                    // ------------------------------------------------------------------------
                    // Generate Document and replace tag values in new document generated
                    // ------------------------------------------------------------------------
                    // ------------------------------------------------------------------------

                    ExcelSpreadsheetTasks.CopyDocument(sourceFileLocationName, clientSourceFileLocationName, ts, vkExcelApp, uioutput);
                }
                else
                {
                    File.Copy(sourceFileLocationName, clientSourceFileLocationName);

                    uioutput.AddOutputMessage("File copied but not modified: " +
                                              Path.GetExtension(doco.clientDocument.FileName) + " == File: " + clientSourceFileLocationName);
                }

                //
                // Instantiate client document
                ClientDocument cd = new ClientDocument();
                cd.UID = doco.clientDocument.UID;

                // Update file - set as GENERATED.
                //
                cd.SetGeneratedFlagVersion('Y', document.IssueNumber);

                uioutput.AddOutputMessage("Document generated: " +
                                          clientDestinationFileLocationName);
            }
            return;
        }
Example #4
0
        private void WriteLineToRoSD(TreeNode tn, WordNet._Document oDoc, WordNet.Table oTable, string prefix = "", string parent = "", int seqnum = 0)
        {
            if (tn.Tag == null || tn.Tag.GetType().Name != "scClientDocSetDocLink")
            {
                // still need to check subnodes
            }
            else
            {
                int x = 0;
                foreach (TreeNode node in tn.Nodes)
                {
                    x++;
                    row++;

                    scClientDocSetDocLink documentClient = (scClientDocSetDocLink)node.Tag;

                    // First column

                    string currentParent = "";
                    if (string.IsNullOrEmpty(parent))
                    {
                        currentParent = x.ToString();
                    }
                    else
                    {
                        currentParent = parent + "." + x.ToString();
                    }

                    oTable.Cell(row, 1).Width      = 30;
                    oTable.Cell(row, 1).Range.Text = currentParent;

                    oTable.Cell(row, 2).Width = 30;

                    System.Drawing.Bitmap bitmap1 = Properties.Resource1.FolderIcon;

                    if (documentClient.document.RecordType.Trim() == Utils.RecordType.FOLDER)
                    {
                        bitmap1 = Properties.Resource1.FolderIcon;
                    }
                    else
                    {
                        bitmap1 = Properties.Resource1.WordIcon;

                        if (documentClient.document.DocumentType == Utils.DocumentType.EXCEL)
                        {
                            bitmap1 = Properties.Resource1.ExcelIcon;
                        }

                        if (documentClient.document.DocumentType == Utils.DocumentType.PDF)
                        {
                            bitmap1 = Properties.Resource1.PDFIcon;
                        }
                    }

                    Clipboard.SetImage(bitmap1);
                    oTable.Cell(row, 2).Range.Paste();


                    oTable.Cell(row, 3).Width      = 60;
                    oTable.Cell(row, 3).Range.Text = prefix + documentClient.document.CUID;

                    oTable.Cell(row, 4).Width      = 50;
                    oTable.Cell(row, 4).Range.Text = prefix + documentClient.document.IssueNumber.ToString("000");

                    oTable.Cell(row, 5).Width      = 300;
                    oTable.Cell(row, 5).Range.Text = prefix + documentClient.document.Name;

                    oTable.Cell(row, 6).Width      = 100;
                    oTable.Cell(row, 6).Range.Text = "???";

                    if (uioutput != null)
                    {
                        uioutput.AddOutputMessage(documentClient.document.Name);
                    }

                    if (node.Nodes.Count > 0)
                    {
                        WriteLineToRoSD(node, oDoc, oTable, prefix: "", parent: currentParent, seqnum: x);
                    }
                }
            }
        }