Пример #1
0
        private static FormElement _extract_xml_node_data(Guid applicationId,
                                                          XmlNode parentNode, XmlNodeList nodeList, XmlNamespaceManager nsmgr, FormElement formElement,
                                                          Dictionary <string, object> map, Dictionary <string, object> parentMap)
        {
            if (formElement == null || !formElement.ElementID.HasValue ||
                nodeList == null || nodeList.Count == 0)
            {
                return(null);
            }

            FormElement retElement = new FormElement()
            {
                ElementID      = formElement.ElementID,
                RefElementID   = formElement.RefElementID,
                FormInstanceID = formElement.FormInstanceID,
                Type           = formElement.Type,
                SequenceNumber = formElement.SequenceNumber,
                Name           = formElement.Name
            };

            if (!retElement.RefElementID.HasValue || retElement.RefElementID == retElement.ElementID)
            {
                retElement.RefElementID = retElement.ElementID;
                retElement.ElementID    = Guid.NewGuid();
            }

            switch (formElement.Type)
            {
            case FormElementTypes.Text:
            case FormElementTypes.Select:
            case FormElementTypes.Node:
            case FormElementTypes.User:
            {
                string strValue = nodeList.Item(0).InnerText.Trim();
                int    intValue = 0;

                List <string> options = null;

                if (formElement.Type == FormElementTypes.Select &&
                    int.TryParse(strValue, out intValue) && intValue > 0)
                {
                    //if strValue is an integer, probably it is the index of selected option
                    Dictionary <string, object> dic = PublicMethods.fromJSON(formElement.Info);
                    if (dic.ContainsKey("Options") && dic["Options"].GetType() == typeof(ArrayList))
                    {
                        ArrayList arr = (ArrayList)dic["Options"];
                        options = new List <string>();
                        foreach (object obj in arr)
                        {
                            options.Add(Base64.decode((string)obj));
                        }

                        if (options.Count >= intValue && !options.Any(u => u == strValue))
                        {
                            strValue = options[intValue - 1];
                        }
                    }
                }

                if (!string.IsNullOrEmpty(strValue))
                {
                    retElement.TextValue = strValue;
                    return(retElement);
                }
                else
                {
                    return(null);
                }
            }

            case FormElementTypes.Checkbox:
            case FormElementTypes.MultiLevel:
            {
                List <string> strItems = new List <string>();
                foreach (XmlNode nd in nodeList)
                {
                    strItems.Add(nd.InnerText.Trim());
                }
                string strValue = string.Join(" ~ ", strItems.Where(u => !string.IsNullOrEmpty(u)));
                if (!string.IsNullOrEmpty(strValue))
                {
                    retElement.TextValue = strValue;
                    return(retElement);
                }
                else
                {
                    return(null);
                }
            }

            case FormElementTypes.Binary:
            {
                Dictionary <string, object> dic = PublicMethods.fromJSON(formElement.Info);

                string yes = dic.ContainsKey("Yes") ? Base64.decode((string)dic["Yes"]) : string.Empty;
                string no  = dic.ContainsKey("No") ? Base64.decode((string)dic["No"]) : string.Empty;

                string txt      = nodeList.Item(0).InnerText.Trim().ToLower();
                bool?  bitValue = null;
                if (!string.IsNullOrEmpty(txt) && "truefalse".IndexOf(txt) >= 0)
                {
                    bitValue = txt == "true";
                }
                if (bitValue.HasValue)
                {
                    retElement.BitValue  = bitValue;
                    retElement.TextValue = bitValue.Value ? (string.IsNullOrEmpty(yes) ? null : yes) :
                                           (string.IsNullOrEmpty(no) ? null : no);
                    return(retElement);
                }
                else
                {
                    return(null);
                }
            }

            case FormElementTypes.Numeric:
            {
                double dblValue = 0;
                if (double.TryParse(nodeList.Item(0).InnerText, out dblValue))
                {
                    retElement.FloatValue = dblValue;
                    return(retElement);
                }
                else
                {
                    return(null);
                }
            }

            case FormElementTypes.Date:
            {
                string calendarType = map != null && map.ContainsKey("calendar_type") ?
                                      map["calendar_type"].ToString() : string.Empty;
                DateTime?dateValue = null;

                if (string.IsNullOrEmpty(calendarType) || calendarType.ToLower() == "jalali")
                {
                    string[] parts = nodeList.Item(0).InnerText.Trim().Split('/');
                    int      first = 0, second = 0, third = 0;
                    if (parts.Length == 3 && int.TryParse(parts[0], out first) &&
                        int.TryParse(parts[1], out second) && int.TryParse(parts[2], out third))
                    {
                        dateValue = PublicMethods.persian_to_gregorian_date(first, second, third, null, null, null);
                    }
                }

                if (!dateValue.HasValue && parentMap != null)
                {
                    int year = 0;
                    if (int.TryParse(nodeList.Item(0).InnerText, out year) && year > 0)
                    {
                        dateValue = extract_date_from_parts(year, retElement.Name, parentNode, nsmgr, parentMap);
                    }
                }

                if (dateValue.HasValue)
                {
                    retElement.DateValue = dateValue;
                    retElement.TextValue = PublicMethods.get_local_date(dateValue);
                    return(retElement);
                }
                else
                {
                    string strValue = nodeList.Item(0).InnerText.Trim();
                    if (!string.IsNullOrEmpty(strValue))
                    {
                        retElement.TextValue = strValue;
                        return(retElement);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }

            case FormElementTypes.File:
            {
                string      strContent = nodeList.Item(0).InnerText.Trim();
                DocFileInfo doc        = DocumentUtilities.decode_base64_file_content(applicationId,
                                                                                      retElement.ElementID, strContent, FileOwnerTypes.FormElement);
                if (doc != null && !string.IsNullOrEmpty(doc.FileName) && doc.Size.HasValue && doc.Size > 0)
                {
                    retElement.AttachedFiles.Add(doc);
                }
                return(retElement.AttachedFiles.Count > 0 ? retElement : null);
            }

            case FormElementTypes.Form:
            {
                Dictionary <string, object> dic = PublicMethods.fromJSON(formElement.Info);

                Guid formId = Guid.Empty;
                if (!dic.ContainsKey("FormID") || map == null || !map.ContainsKey("sub") ||
                    map["sub"].GetType() != typeof(Dictionary <string, object>) ||
                    !Guid.TryParse(dic["FormID"].ToString(), out formId))
                {
                    return(null);
                }

                List <FormElement> elements = FGController.get_form_elements(applicationId, formId);

                if (elements == null || elements.Count == 0)
                {
                    return(null);
                }

                Dictionary <string, object> subMap = (Dictionary <string, object>)map["sub"];

                foreach (XmlNode node in nodeList)
                {
                    Guid formInstanceId = Guid.NewGuid();

                    List <FormElement> subElements = new List <FormElement>();

                    foreach (string name in subMap.Keys)
                    {
                        List <FormElement> newElements = _parse_imported_form(applicationId, node,
                                                                              get_child_nodes(node, name, nsmgr), nsmgr, subMap[name], elements, subMap);

                        if (newElements != null && newElements.Count > 0)
                        {
                            subElements.AddRange(newElements);
                        }
                    }

                    if (subElements.Count == 0)
                    {
                        continue;
                    }

                    //add [national_id], [first_name], [last_name] & [full_name] elements that are empty
                    List <string> strAll = new List <string>();
                    strAll.AddRange(StrNationalID);
                    strAll.AddRange(StrFirstName);
                    strAll.AddRange(StrLastName);
                    strAll.AddRange(StrFullName);

                    foreach (FormElement e in elements.Where(u => u.Type == FormElementTypes.Text &&
                                                             field_name_match(u.Name, strAll) && !subElements.Any(x => x.RefElementID == u.ElementID)))
                    {
                        subElements.Add(new FormElement()
                            {
                                ElementID      = Guid.NewGuid(),
                                RefElementID   = e.ElementID,
                                FormInstanceID = formInstanceId,
                                Type           = e.Type,
                                SequenceNumber = e.SequenceNumber,
                                Name           = e.Name
                            });
                    }
                    //end of add [national_id], [first_name], [last_name] & [full_name] elements that are empty

                    retElement.TableContent.Add(new FormType()
                        {
                            FormID     = formId,
                            InstanceID = formInstanceId,
                            OwnerID    = retElement.ElementID,
                            Elements   = subElements
                        });
                }

                return(retElement.TableContent.Count > 0 ? retElement : null);
            }

            default:
                return(null);
            }
        }
Пример #2
0
        public static bool extract_forms_from_xml(Guid applicationId, Guid?formId, XmlDocument xmlDoc,
                                                  Dictionary <string, object> map, Guid currentUserId, ref List <FormType> formsToBeSaved,
                                                  ref List <FormElement> elementsToBeSaved, ref Dictionary <Guid, object> nodes)
        {
            try
            {
                if (map.ContainsKey("sub"))
                {
                    map = (Dictionary <string, object>)map["sub"];
                }

                List <FormElement> refElements = !formId.HasValue ? null :
                                                 FGController.get_form_elements(applicationId, formId).OrderBy(u => u.SequenceNumber).ToList();

                XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

                foreach (XmlNode xmlNode in xmlDoc.DocumentElement.ChildNodes)
                {
                    XmlNodeList lstNodeName = get_child_nodes(xmlNode, "node_name", nsmgr);
                    XmlNodeList lstNodeId   = get_child_nodes(xmlNode, "node_id", nsmgr);
                    XmlNodeList lstParentId = get_child_nodes(xmlNode, "node_parent_id", nsmgr);
                    XmlNodeList lstTags     = get_child_nodes(xmlNode, "node_tags", nsmgr);
                    XmlNodeList lstAbstract = get_child_nodes(xmlNode, "node_abstract", nsmgr);

                    Guid   nodeId       = Guid.NewGuid();
                    string nodeName     = lstNodeName == null || lstNodeName.Count <= 0 ? null : lstNodeName.Item(0).InnerText;
                    string nodeAddId    = lstNodeId == null || lstNodeId.Count <= 0 ? null : lstNodeId.Item(0).InnerText;
                    string nodeParentId = lstParentId == null || lstParentId.Count <= 0 ? null : lstParentId.Item(0).InnerText;
                    string nodeTags     = lstTags == null || lstTags.Count <= 0 ? null : lstTags.Item(0).InnerText
                                          .Replace('-', '~').Replace(';', '~').Replace(',', '~');
                    string nodeAbstract = lstAbstract == null || lstAbstract.Count <= 0 ? null : lstAbstract.Item(0).InnerText;

                    if (string.IsNullOrEmpty(nodeName))
                    {
                        continue;
                    }

                    //Store the node
                    Dictionary <string, object> nd = new Dictionary <string, object>();

                    nd["Name"] = nodeName;
                    if (!string.IsNullOrEmpty(nodeAddId))
                    {
                        nd["AdditionalID"] = nodeAddId;
                    }
                    if (!string.IsNullOrEmpty(nodeParentId))
                    {
                        nd["ParentAdditionalID"] = nodeParentId;
                    }
                    if (!string.IsNullOrEmpty(nodeTags))
                    {
                        nd["Tags"] = nodeTags;
                    }
                    if (!string.IsNullOrEmpty(nodeAbstract))
                    {
                        nd["Abstract"] = nodeAbstract;
                    }

                    nodes[nodeId] = nd;
                    //end of Store the node

                    if (!formId.HasValue || refElements == null || refElements.Count == 0)
                    {
                        continue;
                    }

                    FormType formInstance = new FormType()
                    {
                        FormID     = formId,
                        InstanceID = Guid.NewGuid(),
                        OwnerID    = nodeId,
                        Creator    = new Users.User()
                        {
                            UserID = currentUserId
                        },
                        CreationDate = DateTime.Now
                    };

                    List <FormElement> formElements = new List <FormElement>();
                    foreach (FormElement e in refElements)
                    {
                        formElements.Add((FormElement)e.Clone());
                    }

                    List <FormElement> theElements = new List <FormElement>();

                    foreach (string name in map.Keys)
                    {
                        List <FormElement> newElems = _parse_imported_form(applicationId, xmlNode,
                                                                           get_child_nodes(xmlNode, name, nsmgr), nsmgr, map[name], formElements, map);
                        if (newElems != null && newElems.Count > 0)
                        {
                            theElements.AddRange(newElems);
                        }
                    }

                    List <FormType>    newFormInstances = new List <FormType>();
                    List <FormElement> newElements      = new List <FormElement>();
                    List <DocFileInfo> newFiles         = new List <DocFileInfo>();

                    get_save_items(formInstance, theElements, ref newFormInstances, ref newElements, ref newFiles);

                    //remove empty text elements
                    newElements = newElements
                                  .Where(u => u.Type != FormElementTypes.Text || !string.IsNullOrEmpty(u.TextValue)).ToList();
                    //end of remove empty text elements

                    if (theElements != null && newElements.Count > 0)
                    {
                        formsToBeSaved.Add(formInstance);
                        elementsToBeSaved.AddRange(newElements);
                    }
                }

                return(true);
            }
            catch (Exception ex) {
                LogController.save_error_log(applicationId, currentUserId,
                                             "ExtractFormsFromXML", ex, ModuleIdentifier.FG, LogLevel.Fatal);
                return(false);
            }
        }