Exemple #1
0
        /// <summary>
        /// Attempt to add the given Word to the Dictionary via FormPost
        /// </summary>
        /// <param name="word"></param>
        /// <returns>A Payload containing server response</returns>
        private Payload addWordFormPost(string word)
        {
            try {
                FormPost formPost = new FormPost();
                formPost.id       = 2; //
                formPost.formId   = 2;
                formPost.authorId = User.Identity.Name;
                //formPost.label = "Word";
                formPost.status           = 1;
                formPost.dateCreated      = DateTime.UtcNow.ToLocalTime();
                formPost.dateLastModified = DateTime.UtcNow.ToLocalTime();
                formPost.results          = new List <FormValue>();
                formPost.results.Add(new FormValue("value", word));
                formPost.resultsToXml();

                getObjectDbContext().FormPosts.Add(formPost);
                int success = getObjectDbContext().SaveChanges();

                return(new Payload(formPost.id, "Successfully added Word '" + word + "' as a FormPost (FormId:2, new WordId: " + formPost.id + ")"));
            } catch (Exception e) {
                return(new Payload(
                           0, e
                           , "Unknown exception for Word '" + word + "'<br><br>" + e.Message.ToString()));
            }
        }
        /// <summary>
        /// Create a FORMPOST using the given posted values
        /// https://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-format
        /// </summary>
        /// <param name="formPost"></param>
        /// <returns></returns>
        public override async Task <ActionResult> Create(FormPost formPost)
        {
            // Set formPost attributes where applicable
            formPost.formId           = formPost.id;
            formPost.id               = 0; // Id will be appended
            formPost.authorId         = User.Identity.Name;
            formPost.dateCreated      = DateTime.UtcNow.ToLocalTime();
            formPost.dateLastModified = DateTime.UtcNow.ToLocalTime();
            formPost.resultsToXml();

            // Attempt to save the form to the database
            try {
                // Save the object
                getObjectDbContext().FormPosts.Add(formPost);
                int success = getObjectDbContext().SaveChanges();
                // Return the success response
                return(Json(new Payload(
                                1, "FORMPOST", formPost,
                                formPost.getMessage()
                                )));
            } catch (Exception e) {
                var message = e.Message;
                return(Json(new Payload(0, e, "FormPostController.submit(" + e.GetType() + ")" + formPost.getMessage() + "\n\n" + e.Message)));
            }
        }
        /// <summary>
        /// Sets the value of the target object based on the given formPost values
        /// </summary>
        /// <param name="formPost">FormPost</param>
        /// <returns>A Json Object</returns>
        public override async Task <ActionResult> Set(FormPost formPost)
        {
            try {
                // Extract values from FormPost
                formPost.resultsToXml();
                int id = formPost.parseInt("id", -1);

                // Retrieve the record from the database
                ObjectDBContext ctx   = getObjectDbContext();
                var             model = select(ctx, id);

                // Set new values
                int result = 0;
                if (model != null)
                {
                    if (model.authorId == User.Identity.Name || model.shared == 1)
                    {
                        model.status = 1;

                        model.updateContainerModel(formPost);
                        model.dateLastModified = DateTime.UtcNow.ToLocalTime();

                        // Save the object
                        db.dbSets[this.className].Add(model);                             // ctx.Containers.Add(model);
                        ctx.Entry(model).State = System.Data.Entity.EntityState.Modified; // Critical
                        result = ctx.SaveChanges();

                        // Return the success response along with the message body
                        return(Json(new Payload(
                                        1, this.className, model
                                        //"Successfully set " + this.className + " (" + model.id + "," + id + ")"
                                        ), JsonRequestBehavior.AllowGet));
                    }
                    else
                    {
                        return(Json(new Payload(
                                        0, "You are not authorized to modify this " + this.className + "(" + id + ")"
                                        ), JsonRequestBehavior.AllowGet));
                    }
                }
                else
                {
                    return(Json(new Payload(
                                    0, "Failed to retrieve " + this.className + "(" + id + ").  The request returned null."
                                    ), JsonRequestBehavior.AllowGet));
                }
            } catch (Exception e) {
                return(Json(new Payload(
                                0, e,
                                "Unknown exception for " + this.className + "<br><br>"
                                + e.Message.ToString()), JsonRequestBehavior.AllowGet
                            ));
            }
        }
Exemple #4
0
 /// <summary>
 /// Sets Container Model values based on the given FormPost
 /// </summary>
 /// <param name="formPost"></param>
 public void updateContainerModel(FormPost formPost)
 {
     formPost.resultsToXml();
     this.subsections      = formPost.parseString("subsections", "0");
     this.label            = formPost.parseString("label");
     this.id               = formPost.parseInt("id", -1);
     this.attributesId     = formPost.parseInt("attributesId");
     this.dataId           = formPost.parseInt("dataId");
     this.metaId           = formPost.parseInt("metaId");
     this.shared           = formPost.parseInt("shared");
     this.isPublic         = formPost.parseInt("isPublic");
     this.dateLastModified = DateTime.UtcNow.ToLocalTime();
 }
        /// <summary>
        /// Sets the value of the target object based on the given formPost values
        /// </summary>
        /// <param name="formPost"></param>
        /// <returns></returns>
        public override async Task <ActionResult> Set(FormPost formPost)
        {
            try {
                // Extract values from FormPost
                formPost.resultsToXml();
                int id       = formPost.parseInt("id", -1);
                int shared   = formPost.parseInt("shared", 0);
                int isPublic = formPost.parseInt("isPublic", 0);

                // Retrieve the record from the database
                ObjectDBContext ctx   = getObjectDbContext();
                FormPost        model = ctx.FormPosts.Single(m =>
                                                             m.id == id && (m.authorId == User.Identity.Name || m.shared == 1)
                                                             );

                formPost.setXml();
                formPost.resultsToXml();

                // Set new values
                int result = 0;
                model.xmlResults  = formPost.xmlResults;
                model.jsonResults = formPost.jsonResults;
                model.formId      = formPost.formId;
                model.shared      = shared;
                model.isPublic    = isPublic;

                // Save the object
                ctx.FormPosts.Add(model);
                ctx.Entry(model).State = System.Data.Entity.EntityState.Modified; // Critical
                result = ctx.SaveChanges();

                // Return the success response along with the message body
                return(Json(new Payload(1, "FORMPOST", model)));                                                  //, JsonRequestBehavior.AllowGet // "Successfully set FORMPOST (" + model.id + "," + id + ")"
            } catch (Exception e) {
                return(Json(new Payload(0, e, "Unknown exception for FORMPOST<br><br>" + e.Message.ToString()))); // JsonRequestBehavior.AllowGet
            }
        }
        /// <summary>
        /// Creates an Empty Formpost and returns the model
        /// </summary>
        /// <returns></returns>
        public FormPost createEmptyFormPost()
        {
            FormPost formPost = new FormPost();

            formPost.id               = 0;
            formPost.formId           = 0;
            formPost.authorId         = User.Identity.Name;
            formPost.status           = 1;
            formPost.dateCreated      = DateTime.UtcNow.ToLocalTime();
            formPost.dateLastModified = DateTime.UtcNow.ToLocalTime();
            formPost.results          = new List <FormValue>();
            formPost.xmlResults       = "<root></root>";
            formPost.resultsToXml();
            return(formPost);
        }
Exemple #7
0
        public async Task <ActionResult> Talk(FormPost formPost)
        {
            // Set formPost attributes where applicable
            formPost.formId   = 1; // Unique to Chat - Represents a sentence
            formPost.authorId = User.Identity.Name;
            //formPost.label = "Sentence";
            // https://stackoverflow.com/questions/114983/given-a-datetime-object-how-do-i-get-an-iso-8601-date-in-string-format
            formPost.dateCreated      = DateTime.UtcNow.ToLocalTime();
            formPost.dateLastModified = DateTime.UtcNow.ToLocalTime();

            // Lets add some metadata to the post
            var message = new StringBuilder();

            try {
                Regex rgx = new Regex("[^a-zA-Z -]");

                var statement = Regex.Replace(formPost.results[0].value.Trim(), @"[\d-]", "");
                statement = rgx.Replace(formPost.results[0].value.Trim(), "");
                var statementArray = statement.Split(' ');

                // Get word id and map/reduce
                formPost.results.Add(new FormValue("statement-array", "ARRAY", string.Join(",", statementArray)));
                formPost.results.Add(new FormValue("Count", statementArray.Count().ToString()));



                List <FormValue> uniqueWords = new List <FormValue>();

                /*
                 * Map-Reduce
                 *
                 * Each word is reduced down to its associated wordId.
                 * The question is how this will scale on the database.
                 * I may need to create some sort of cache for the dictionary
                 *
                 */
                foreach (var word in statementArray)
                {
                    // Generate a query to find this word
                    var words = from wrd in statementArray
                                where wrd.ToLower() == word.ToLower()
                                select wrd.ToLower();

                    // Loop through and check if exists
                    var hasWord = false;
                    for (var w = 0; w < formPost.results.Count; w++)
                    {
                        if (formPost.results[w].name == word.ToLower())
                        {
                            hasWord = true;
                            break;
                        }
                    }

                    // If the word does not exist, add to the array
                    if (hasWord == false)
                    {
                        uniqueWords.Add(
                            new FormValue(word.ToLower(), words.Count().ToString())
                            );
                    }
                }

                /**
                 * Now that an array of unique words exists,
                 * iterate over each unique word and perform
                 * any required tasks
                 */
                message.Append(
                    "<h2>Summary</h2>"
                    + "<cite>Statement has " + statementArray.Count() + " words and "
                    + uniqueWords.Count() + " unique words</cite>"
                    + "<ul class=\"nav-pills dropup\">"
                    );

                // Get or Set each word on the database and update message accordingly
                for (var uw = 0; uw < uniqueWords.Count(); uw++)
                {
                    Payload results = this.updateDictionary(uniqueWords[uw].name);
                    uniqueWords[uw].value = results.result.ToString();
                    message.Append(
                        "<li>"
                        + "<a class=\"dropdown toggle\" data-toggle=\"dropdown\" href=\"#\">" + uniqueWords[uw].name + "</a>"
                        + "<ul class=\"word-summary\">"
                        + "<li><a href=\"http://www.dictionary.com/browse/" + uniqueWords[uw].name
                        + "?s=t\" target=\"_blank\">" + uniqueWords[uw].name + "[" + results.result + "]</a>"
                        + "</li>"
                        //+ "<cite style=\"font-size:small;\">"
                        + "<li><a href=\"https://www.dictionaryapi.com/api/v1/references/collegiate/xml/" + uniqueWords[uw].name + "?key=7f0d3743-cd0f-4c39-a11e-429184a376d1\" target=\"_blank\">Dictionary</a></li>"
                        //+ "&nbsp;|&nbsp;"
                        + "<li><a href=\"https://www.dictionaryapi.com/api/v1/references/thesaurus/xml/" + uniqueWords[uw].name + "?key=7f1eee46a-0757-408b-a42e-7da3dcaf394a\" target=\"_blank\">Thesaurus</a></li>"
                        //+ "</cite>"
                        + "</ul>"
                        + "</li>"
                        );
                }
                message.Append("</ul>");

                // This will cause an exception if given values are not XML friendly
                formPost.resultsToXml();
                formPost.jsonResults = "";

                // Attempt to save the form to the database
                try {
                    getObjectDbContext().FormPosts.Add(formPost);
                    int success = getObjectDbContext().SaveChanges();
                    return(Json(new {
                        text = "Successfully added the FormPost (FormId: "
                               + formPost.formId + ") with a response of " + success,
                        message = message.ToString(),
                        formPost = formPost
                    }));
                } catch (Exception e) {
                    return(Json(new {
                        text = "Failed to add the FormPost.  Returning Form object for debugging.",
                        message = e.Message, form = formPost, exception = e.ToString()
                    }));
                }
            } catch (Exception ee) {
                return(Json(new Payload(1,
                                        ee
                                        ,
                                        "I'm sorry, but I don't understand. :(")));
            }
        }