Example #1
0
        /// <summary>
        ///
        /// This method will persist all data to files (metadata, rules, current data, report), zip the files, and then upload the .ZIP to IPFS
        ///
        /// <param name="poEngine">The instance of the Wonka engine that has just run</param>
        /// <param name="poRecord">This record holds the current snapshot of the data being addressed by the metadata (mentioned in WonkaRefEnvironment)</param>
        /// <param name="poReport">This report holds the verbose results of running the RuleTree</param>
        /// <param name="psUniqueChronoLogName">This will be the unique name of the ChronoLog entry</param>
        /// <param name="psTmpDirectory">The directory which will hold the files to be zipped and uploaded</param>
        /// <returns>IPFS Url of the Zip file</returns>
        /// </summary>
        public static string ZipInvocation(this WonkaBizRulesEngine poEngine, WonkaProduct poRecord, IRuleTreeReport poReport, string psUniqueChronoLogName, string psTmpDirectory = @"./tmp")
        {
            string sZipFile = "";

            DirectoryInfo tmpDirInfo = new DirectoryInfo(psTmpDirectory);

            if (!tmpDirInfo.Exists)
            {
                tmpDirInfo.Create();
            }

            var snapshotDirInfo = Directory.CreateDirectory(psTmpDirectory + "/snapshot-" + psUniqueChronoLogName);

            var rulesWriter = new WonkaBizRulesXmlWriter(poEngine);

            File.WriteAllText(snapshotDirInfo.FullName + "/" + psUniqueChronoLogName + ".rules.xml", rulesWriter.ExportXmlString());

            var prodMsgXml = new WonkaProductMsgWriter().WriteWonkaMsg(new WonkaProductMessage(poRecord, true));

            File.WriteAllText(snapshotDirInfo.FullName + "/" + psUniqueChronoLogName + ".prdmsg.xml", prodMsgXml);

            var reportXml = new WonkaRuleTreeReportWriter(poReport).ExportXmlString();

            File.WriteAllText(snapshotDirInfo.FullName + "/" + psUniqueChronoLogName + ".report.xml", reportXml);

            sZipFile = psTmpDirectory + "/" + psUniqueChronoLogName + ".zip";
            using (var zipOutputStream = new ZipOutputStream(File.Create(sZipFile)))
            {
                try
                {
                    zipOutputStream.SetLevel(5);

                    var snapshotFileList = snapshotDirInfo.GetFiles();
                    foreach (var tmpFile in snapshotFileList)
                    {
                        Int32 nSize = 0;

                        using (FileStream tmpFileStream = File.OpenRead(tmpFile.FullName))
                        {
                            ZipEntry tmpEntry = new ZipEntry(tmpFile.Name);

                            zipOutputStream.PutNextEntry(tmpEntry);
                            while (tmpFileStream.Position < tmpFileStream.Length)
                            {
                                byte[] buffer = new byte[16384];

                                nSize = tmpFileStream.Read(buffer, 0, buffer.Length);

                                zipOutputStream.Write(buffer, 0, nSize);
                                zipOutputStream.Flush();
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    // NOTE: What should be done here?
                    throw e;
                }
            }

            return(sZipFile);
        }
Example #2
0
 public WonkaRuleTreeReportWriter(IRuleTreeReport piReport)
 {
     miReport = piReport;
 }