/// <summary>
        /// Get the application data from Ramsell in their XML format.
        /// Nearly identical to the XML format that is sent to Ramsell
        /// </summary>
        /// <param name="oauthToken">Oauth 2 security token</param>
        /// <param name="fromMemberId">Ramsell MemberId</param>
        /// <returns></returns>
        public int ImportApplication(string oauthToken, string fromMemberId)
        {
            string decodedXml = Services.Api.Ramsell.GetApplicationXml(oauthToken, fromMemberId);

            if (String.IsNullOrWhiteSpace(decodedXml))
            {
                throw new Exception("received empty response from Ramsell for memberId \"" + fromMemberId + "\"");
            }

            // Process the XML returned from Ramsell
            List <string> checkedBoxes = new List <string>();
            Dictionary <string, string> specialCaseValuesByTagname = new Dictionary <string, string>();

            using (System.Xml.XmlReader reader = XmlReader.Create(new StringReader(decodedXml)))
            {
                //iterate through each element node in the file
                while (reader.Read())
                {
                    //tags "string" and "Patient" are boilerplate, they can be ignored (this doesn't skip their children)
                    if ((reader.NodeType != XmlNodeType.Element) || (reader.Name == "string") || (reader.Name == "Patient"))
                    {
                        continue;
                    }

                    //update DEF responses based on tagname + contents
                    ImportXmlNodeFromRamsell(reader, checkedBoxes, specialCaseValuesByTagname);
                }
            }

            //for any checkboxes that are included in the "ramsellIdentifierMap" in RamsellExport.cs,
            //uncheck the ones that weren't included in the xml
            List <string> checkboxIvIdentifiers = GetGroupedCheckboxItemVarIdentifiers();

            foreach (string checkboxIvIdent in checkboxIvIdentifiers)
            {
                if (checkedBoxes.Contains(checkboxIvIdent))
                {
                    continue;
                }
                def_ItemVariables checkBoxIv = formsRepo.GetItemVariableByIdentifier(checkboxIvIdent);
                def_ItemResults   ir         = userData.SaveItemResult(toFormResultId, checkBoxIv.itemId);
                userData.SaveResponseVariable(ir, checkBoxIv, "0");
            }

            //handle any special-case ramsell tags that require on-off transformations
            RamsellTransformations.ImportSpecialCasesNoSave(specialCaseValuesByTagname, toFormResultId, formsRepo);

            formsRepo.Save();

            return(0);
        }
Beispiel #2
0
        public static void TransformAndAppendResponseNodesToXML(
            XmlNode appendTo, XmlDocument doc, DefResponseElement rspElement, int formResultId, IFormsRepository formsRepo)
        {
            string elementContent = null;

            //super-special case for Enrollment_Type, which is based on the formStatus, rather than responses
            if (rspElement.tagName == "Enrollment_Type")
            {
                #region check the formResult.formStatus field to determin enrollment type

                def_FormResults  fr           = formsRepo.GetFormResultById(formResultId);
                def_StatusMaster statusMaster = formsRepo.GetStatusMasterByFormId(fr.formId);
                def_StatusDetail statusDetail = formsRepo.GetStatusDetailBySortOrder(statusMaster.statusMasterId, fr.formStatus);
                switch (statusDetail.identifier)
                {
                case "NEEDS_INFORMATION":
                    elementContent = "2";
                    break;

                case "APPROVED":
                    elementContent = "2";
                    break;

                default:
                    elementContent = "1";
                    break;
                }

                #endregion
            }

            if (rspElement.tagName == "Email")
            {
                #region check uas tables for email
                using (UASEntities context = Data.Concrete.DataContext.getUasDbContext())
                {
                    def_FormResults fr   = formsRepo.GetFormResultById(formResultId);
                    var             data = from ue in context.uas_UserEmail
                                           where ue.UserID == fr.subject &&
                                           ue.EmailAddress != null &&
                                           ue.MayContact
                                           orderby ue.SortOrder
                                           select ue.EmailAddress;
                    elementContent = data.FirstOrDefault();
                }
                #endregion
            }

            if (rspElement.tagName == "Mailing_Address" || rspElement.tagName == "Mailing_City" ||
                rspElement.tagName == "Mailing_State" || rspElement.tagName == "Mailing_Zip")
            {
                def_ResponseVariables rv = formsRepo.GetResponseVariablesByFormResultIdentifier(formResultId, "ADAP_C2_SameAsMailing");
                if (rv.rspInt != null && rv.rspInt == 1)
                {
                    return;
                }
            }

            //assign a special-case transformation value if applicable
            if (elementContent == null)
            {
                elementContent = RamsellTransformations.GetExportValueForRamsellTag(rspElement.tagName, formResultId, formsRepo);
            }

            //if elementContent has been assigned a non-null value,
            //it must have been assigned to handle a one-off special case (above),
            //so append one node with elementContent and terminate this function
            if (elementContent != null)
            {
                AppendContentNodeToXML(appendTo, doc, rspElement.tagName, elementContent);
                return;
            }

            #region normal case: append an xml node for each associated itemvariable identifier in ramsellIdentifierMap

            List <string> ivIdentList = GetItemVariableIdentifiersForRamsellTagName(rspElement.tagName);
            foreach (string ivIdent in ivIdentList)
            {
                def_ResponseVariables rv = formsRepo.GetResponseVariablesByFormResultIdentifier(formResultId, ivIdent);
                if ((rv != null) && !String.IsNullOrWhiteSpace(rv.rspValue))
                {
                    elementContent = GetFormattedResponse(rv, rspElement.xmlType, formsRepo);
                }
                else
                {
                    GetDefaultValue(rspElement.xmlType);  // to pass validation
                }

                //if there are multiple itemVariables, assume this is one out of a set of checkboxes
                //in which case zeroes represent unchecked checkboxes which should be ignored
                if ((elementContent == "0") && (ivIdentList.Count > 1))
                {
                    continue;
                }

                // if no output and the tag is optional, don't write it out
                //   *** For some reason Ramsell system doesn't seem process empty tags or recognize as valid
                //   *** Even though they pass XML / XSD validation
                if (String.IsNullOrWhiteSpace(elementContent) && rspElement.minOccurs.Equals(0.0m))
                {
                    continue;
                }

                AppendContentNodeToXML(appendTo, doc, rspElement.tagName, elementContent);
            }

            #endregion
        }