public async Task <VersionedDocument <T> > InsertDocumentAsync(
            ObjectId objectId,
            T doc)
        {
            VersionedDocument <T> currentDocument = null;

            if (objectId != ObjectId.Empty)
            {
                currentDocument = await(await Collection.FindAsync(x => x.Id == objectId)).FirstOrDefaultAsync();
            }

            if (currentDocument == null)
            {
                var versionedDoc = new VersionedDocument <T>(doc);
                Collection.InsertOne(versionedDoc);
                return(versionedDoc);
            }

            var    previousVersionNumber = currentDocument.Version;
            JToken jtokenCurrentDoc      = currentDocument.Current == null
                ? JToken.Parse("{}")
                : JToken.FromObject(currentDocument.Current);

            JToken diff = _jdp.Diff(jtokenCurrentDoc, JToken.FromObject(doc));

            if (diff == null)
            {
                return(null);              // no change
            }
            currentDocument.Current = doc;
            VersionHistory versionHistory = new VersionHistory(diff.ToString(Formatting.None))
            {
                Version = previousVersionNumber,
                UtcDate = currentDocument.UtcDate,
                Type    = currentDocument.Type
            };

            currentDocument.UtcDate = DateTime.UtcNow;
            currentDocument.Prev.Add(versionHistory);
            currentDocument.Version   = previousVersionNumber + 1;
            currentDocument.IsDeleted = false;
            currentDocument.Type      = doc.GetType().ToString();
            var result = Collection.ReplaceOne(item => item.Id == objectId, currentDocument);

            if (result.ModifiedCount != 1) // the number of modified documents
            {
                // print("Someone must have gotten there first, re-fetch the new document, try again");
                // todo - är det här något som kan uppstå?
            }

            return(null);
        }
Пример #2
0
        public ActionResult Index(FormViewModel model)
        {
            if (ModelState.IsValid)
            {
                XDocument document = model.toXml();

                XmlSchemaSet schemas = new XmlSchemaSet();
                schemas.Add("http://www.filogix.com/Schema/FCXAPI/1", Server.MapPath("~/Models/XSD/referralApplication_1_0_1.xsd"));

                Console.WriteLine("Attempting to validate");

                bool errors = false;
                document.Validate(schemas, (o, e) =>
                {
                    ModelState.AddModelError("", e.Message);
                    errors = true;
                });
                Console.WriteLine("document {0}", errors ? "did not validate" : "validated");
                Console.WriteLine();

                if (!errors)
                {
                    try
                    {
                        //setMessage
                        FxLinkMessageSubmission fxlinkMessageSubmission = new FxLinkMessageSubmission(
                            ConfigurationManager.AppSettings["wsdl"],
                            ConfigurationManager.AppSettings["username"],
                            ConfigurationManager.AppSettings["password"],
                            ConfigurationManager.AppSettings["sendingChanel"],
                            ConfigurationManager.AppSettings["recevingChanel"] + model.firmCode.ToUpper() + "." + model.userName.ToUpper(),
                            "Referral Submission"
                            );

                        VersionedDocument vd = new VersionedDocument();
                        //Convert.ToBase64String(document.ToString());

                        vd.content = Encoding.UTF8.GetBytes(document.ToString());
                        vd.docname = "Referral Application";

                        List <VersionedDocument> docs = new List <VersionedDocument>();
                        docs.Add(vd);

                        Response resp = fxlinkMessageSubmission.submitMessage(docs);
                        Response.Write(resp.status + " - " + resp.messageId);

                        try
                        {
                            string fileId = DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss");
                            document.Save(Server.MapPath("~/Log/" + fileId + ".xml"));
                        }
                        catch
                        {
                        }


                        //Console.WriteLine("Response: " + resp.status);

                        //getMessage
                        //VersionedMessage vm1 = fxlinkMessageSubmission.getMessage();

                        ////ackonwledge
                        //Response response = new Response();
                        //response.messageId = vm1.messageId;
                        //response.status = "OK";
                        //fxlinkMessageSubmission.acknowledge(response);
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError("", e.Message);
                    }
                }
            }
            if (ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(model.url))
                {
                    TempData["success"] = "Form successfully submitted!";
                }
                else
                {
                    return(Redirect(model.url));
                }
            }

            return(View(model));
        }