Ejemplo n.º 1
0
        /// <summary>
        /// Generate documents selected for a client
        /// </summary>
        /// <param name="clientID"></param>
        /// <param name="clientDocSetID"></param>
        /// <param name="uioutput"></param>
        /// <param name="overrideDocuments"></param>
        private void TBD_GenerateDocumentsForClient(
            int clientID, int clientDocSetID,
            string overrideDocuments)
        {
            uioutput.AddOutputMessage("Start time: " + System.DateTime.Now.ToString());

            // Instantiate Word
            //
            object vkFalse = false;

            Word.Application vkWordApp =
                new Word.Application();

            // Make it not visible
            vkWordApp.Visible = false;

            Excel.Application vkExcelApp = new Excel.Application();

            // Make it not visible
            vkExcelApp.Visible = false;

            // Get Metadata for client

            ReportMetadataList clientMetadata = new ReportMetadataList();

            clientMetadata.ListMetadataForClient(clientID);

            var ts = new List <WordDocumentTasks.TagStructure>();

            // Load variables/ metadata into memory
            //
            foreach (ReportMetadata metadata in clientMetadata.reportMetadataList)
            {
                // Retrieve value for the field selected
                //
                string value = metadata.GetValue();

                // If the field is not enabled, the program has to replace the value with spaces.
                //
                var valueOfTag = metadata.Enabled == 'Y' ? value : string.Empty;

                // When the field is an image and it is not enable, do not include the "No image" icon in the list
                //
                if (metadata.InformationType == Utils.InformationType.IMAGE && metadata.Enabled == 'N')
                {
                    continue;
                }

                ts.Add(new WordDocumentTasks.TagStructure()
                {
                    TagType  = metadata.InformationType,
                    Tag      = metadata.FieldCode,
                    TagValue = valueOfTag
                });
            }

            // Get Client Document Set Details
            // To get the source and destination folders
            ClientDocumentSet cds = new ClientDocumentSet();

            cds.Get(clientID, clientDocSetID);

            // Get List of documents for a client
            //
            var cdl = new ClientDocument();

            cdl.List(Utils.ClientID, Utils.ClientSetID);


            bool fileNotFound = false;
            // ---------------------------------------------------------------------------
            //    Check if source files exist before generation starts
            // ---------------------------------------------------------------------------
            int filecount = 0;

            foreach (scClientDocSetDocLink doco in cdl.clientDocSetDocLink)
            {
                #region File Inspection
                filecount++;

                // Ignore for now
                //
                if (doco.clientDocument.RecordType.Trim() == Utils.RecordType.FOLDER)
                {
                    string er = "Folder " + doco.document.Name;

                    uioutput.AddOutputMessage(er);
                    continue;
                }


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

                uioutput.AddOutputMessage("Inspecting file: " + document.UID + " === " + document.Name);

                // Client Document.SourceFileName is the name for the FCM File
                // Client Document.FileName is the client file name

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

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

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

                // check if source folder/ file exists
                if (string.IsNullOrEmpty(doco.clientDocument.Location))
                {
                    MessageBox.Show("Document Location is empty.");
                    return;
                }

                if (string.IsNullOrEmpty(doco.clientDocument.FileName))
                {
                    MessageBox.Show("File Name is empty.");
                    return;
                }

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

                    uioutput.AddOutputMessage(er);
                    uioutput.AddErrorMessage(er);
                    fileNotFound = true;
                    continue;
                }
                #endregion File Inspection
            }


            // Can't proceed if file not found
            if (fileNotFound)
            {
                return;
            }

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


            // -----------------------------------------------------------------------
            //                          Generation starts here
            // -----------------------------------------------------------------------

            fileprocessedcount  = 0;
            valueForProgressBar = 0;
            startTime           = System.DateTime.Now.ToString();
            estimated           = System.DateTime.Now.AddSeconds(5 * filecount);

            var previousTime = System.DateTime.Now;
            var agora        = System.DateTime.Now;

            foreach (scClientDocSetDocLink doco in cdl.clientDocSetDocLink)
            {
                fileprocessedcount++;
                valueForProgressBar = (fileprocessedcount / filecount) * 100;

                // Get current time
                agora = System.DateTime.Now;

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

                // Calculate the estimated time to complete
                estimated = System.DateTime.Now.AddSeconds(span.TotalSeconds * filecount);

                uioutput.UpdateProgressBar(valueForProgressBar, estimated);

                previousTime = System.DateTime.Now;

                // Retrieve latest version
                //
                Document.Document document = new Document.Document();
                document.UID = doco.clientDocument.FKDocumentUID;
                document.Read();


                uioutput.AddOutputMessage(">>> Generating file: " + document.UID + " === " + document.SimpleFileName);


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

                // This is the client file name
                //
                string clientFileLocation = cds.Folder.Trim() +
                                            doco.clientDocument.Location.Trim();

                string clientFileLocationName = Utils.getFilePathName(
                    clientFileLocation,
                    doco.clientDocument.FileName.Trim());



                // Check if file destination directory exists
                //
                string PhysicalLocation = Utils.GetPathName(clientFileLocation);

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

                    uioutput.AddOutputMessage(er);
                    continue;
                }

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

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

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

                // Copy and fix file
                //

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

                    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, clientFileLocationName, 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());

                            continue;
                        }


                        //
                        // 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: " +
                                                  clientFileLocationName);

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

                        ExcelSpreadsheetTasks.CopyDocument(sourceFileLocationName, clientFileLocationName, ts, vkExcelApp, uioutput);

                        //
                        // 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: " +
                                                  clientFileLocationName);
                    }
                    else
                    {
                        File.Copy(sourceFileLocationName, clientFileLocationName);

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

            // close word application
            vkWordApp.Quit(ref vkFalse, ref vkFalse, ref vkFalse);
            vkExcelApp.Quit();

            uioutput.AddOutputMessage("End time: " + System.DateTime.Now.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="ClientID"></param>
        /// <param name="ClientDocSetID"></param>
        public DocumentGeneration(int ClientID, int ClientDocSetID, IOutputMessage UIoutput = null,
                                  string OverrideDocuments = null)
        {
            if (overrideDocuments == null)
            {
                overrideDocuments = "N";
            }

            // Assign to internal variables
            //
            // iconMessage = IconMessage;

            // Set private attributes
            clientID          = ClientID;
            clientDocSetID    = ClientDocSetID;
            uioutput          = UIoutput;
            overrideDocuments = OverrideDocuments;

            // Instantiate Word
            //
            vkFalse = false;

            vkWordApp = new Word.Application();

            // Make it not visible
            vkWordApp.Visible = false;

            vkExcelApp = new Excel.Application();

            // Make it not visible
            vkExcelApp.Visible = false;

            // Get Metadata for client

            clientMetadata = new ReportMetadataList();

            // Daniel 31/12/2011
            //
            // clientMetadata.ListMetadataForClient(clientID);
            // The intention is to always use the full set of variables.
            // There is need to use all in order to replace the tags not used.
            //
            clientMetadata.ListDefault();

            ts = new List <WordDocumentTasks.TagStructure>();

            // Load variables/ metadata into memory
            //
            #region ClientMetadata
            foreach (ReportMetadata metadata in clientMetadata.reportMetadataList)
            {
                // Add client ID
                metadata.ClientUID = this.clientID;

                // Retrieve value for the field selected
                //
                string value = metadata.GetValue();

                // If the field is not enabled, the program has to replace the value with spaces.
                // 01-Jan-2012 - No longer necessary.
                // All the variables have to be used

                // var valueOfTag = metadata.Enabled == 'Y' ? value : string.Empty;

                // Set to the value. If value is null, set to spaces.
                var valueOfTag = string.IsNullOrEmpty(value) ? string.Empty : value;

                // When the field is an image and it is not enable, do not include the "No image" icon in the list
                //
                //if (metadata.InformationType == Utils.InformationType.IMAGE && metadata.Enabled == 'N')
                //    continue;

                // If the field is an image but it has no value, no need to include.
                // Regular fields must be included because they need to be replaced.
                // Images uses bookmarks, no need to be replace. It is not displayed in the document.
                //
                if (metadata.InformationType == Utils.InformationType.IMAGE)
                {
                    if (string.IsNullOrEmpty(value))
                    {
                        continue;
                    }
                }

                // Add label before value to print.
                //
                if (metadata.UseAsLabel == 'Y')
                {
                    valueOfTag = metadata.Description + " " + valueOfTag;
                }

                ts.Add(new WordDocumentTasks.TagStructure()
                {
                    TagType  = metadata.InformationType,
                    Tag      = metadata.FieldCode,
                    TagValue = valueOfTag
                });
            }
            #endregion ClientMetadata

            // Get Client Document Set Details
            // To get the source and destination folders
            cds = new ClientDocumentSet();
            cds.Get(clientID, clientDocSetID);

            fileprocessedcount  = 0;
            valueForProgressBar = 0;
            startTime           = System.DateTime.Now.ToString();
        }
Ejemplo n.º 3
0
        public WordReport(int ClientID, int ClientDocSetID, IOutputMessage UIoutput = null,
                          string OverrideDocuments = null)
        {
            row = 1;

            // Set private attributes
            clientID       = ClientID;
            clientDocSetID = ClientDocSetID;
            uioutput       = UIoutput;

            // Instantiate Word
            //
            vkFalse = false;

            vkWordApp = new Word.Application();

            // Make it not visible
            vkWordApp.Visible = false;

            vkExcelApp = new Excel.Application();

            // Make it not visible
            vkExcelApp.Visible = false;

            // Get Metadata for client

            clientMetadata = new ReportMetadataList();
            clientMetadata.ListMetadataForClient(clientID);

            ts = new List <WordDocumentTasks.TagStructure>();

            // Load variables/ metadata into memory
            //
            #region ClientMetadata
            foreach (ReportMetadata metadata in clientMetadata.reportMetadataList)
            {
                // Retrieve value for the field selected
                //
                string value = metadata.GetValue();

                // If the field is not enabled, the program has to replace the value with spaces.
                //
                var valueOfTag = metadata.Enabled == 'Y' ? value : string.Empty;

                // When the field is an image and it is not enable, do not include the "No image" icon in the list
                //
                if (metadata.InformationType == Utils.InformationType.IMAGE && metadata.Enabled == 'N')
                {
                    continue;
                }

                ts.Add(new WordDocumentTasks.TagStructure()
                {
                    TagType  = metadata.InformationType,
                    Tag      = metadata.FieldCode,
                    TagValue = valueOfTag
                });
            }
            #endregion ClientMetadata

            // Get Client Document Set Details
            // To get the source and destination folders
            cds = new ClientDocumentSet();
            cds.Get(clientID, clientDocSetID);

            valueForProgressBar = 0;
            startTime           = System.DateTime.Now.ToString();
        }