示例#1
0
        public static List <FormFilter> get_filters_from_json(Dictionary <string, object> dic,
                                                              Guid?ownerId = null, Guid?formId = null, Guid?applicationId = null)
        {
            List <FormFilter> retFilters = new List <FormFilter>();

            Dictionary <string, Guid> namesDic = new Dictionary <string, Guid>();

            if (formId.HasValue && applicationId.HasValue)
            {
                Guid          tempId     = Guid.Empty;
                List <String> fieldNames = dic.Keys.Where(i => !Guid.TryParse(i, out tempId)).ToList();
                namesDic = FGController.get_form_element_ids(applicationId.Value, formId.Value, fieldNames);
            }

            foreach (string id in dic.Keys)
            {
                try
                {
                    Guid elementId = namesDic.ContainsKey(id.ToLower()) ? namesDic[id.ToLower()] : Guid.Empty;

                    if ((elementId == Guid.Empty && !Guid.TryParse(id, out elementId)) ||
                        (dic[id] != null && dic[id].GetType() != typeof(Dictionary <string, object>)))
                    {
                        continue;
                    }

                    Dictionary <string, object> elemDic = (Dictionary <string, object>)dic[id];

                    FormElementTypes type = FormElementTypes.Text;
                    //if (!Enum.TryParse<FormElementTypes>(elemDic.ContainsKey("Type") ?
                    //    elemDic["Type"].ToString() : string.Empty, out type)) continue;
                    if (!Enum.TryParse <FormElementTypes>(PublicMethods.get_dic_value(elemDic, "Type", string.Empty), out type))
                    {
                        type = FormElementTypes.Text;
                    }

                    if (type == FormElementTypes.Form)
                    {
                        List <FormFilter> subFilters = get_filters_from_json(elemDic, elementId);

                        if (subFilters != null && subFilters.Count > 0)
                        {
                            retFilters.Add(new FormFilter()
                            {
                                ElementID = elementId, OwnerID = ownerId
                            });                                                                            //just to say that this field has filters
                            retFilters.AddRange(subFilters);
                        }

                        continue;
                    }

                    FormFilter filter = new FormFilter()
                    {
                        ElementID = elementId, OwnerID = ownerId
                    };

                    filter.Exact      = PublicMethods.parse_bool(PublicMethods.get_dic_value(elemDic, "Exact"));
                    filter.Or         = PublicMethods.parse_bool(PublicMethods.get_dic_value(elemDic, "Or"));
                    filter.Bit        = PublicMethods.parse_bool(PublicMethods.get_dic_value(elemDic, "Bit"));
                    filter.FloatFrom  = PublicMethods.parse_double(PublicMethods.get_dic_value(elemDic, "FloatFrom"));
                    filter.FloatTo    = PublicMethods.parse_double(PublicMethods.get_dic_value(elemDic, "FloatTo"));
                    filter.DateFrom   = PublicMethods.parse_date(PublicMethods.get_dic_value(elemDic, "DateFrom"));
                    filter.DateTo     = PublicMethods.parse_date(PublicMethods.get_dic_value(elemDic, "DateTo"), 1);
                    filter.Compulsory = PublicMethods.parse_bool(PublicMethods.get_dic_value(elemDic, "Compulsory"));

                    if (elemDic.ContainsKey("TextItems") && elemDic["TextItems"] != null &&
                        elemDic["TextItems"].GetType() == typeof(ArrayList))
                    {
                        ArrayList lst = (ArrayList)elemDic["TextItems"];
                        foreach (object o in lst)
                        {
                            if (o != null)
                            {
                                filter.TextItems.Add(Base64.decode(o.ToString()));
                            }
                        }
                    }

                    if (elemDic.ContainsKey("GuidItems") && elemDic["GuidItems"] != null &&
                        elemDic["GuidItems"].GetType() == typeof(ArrayList))
                    {
                        ArrayList lst = (ArrayList)elemDic["GuidItems"];
                        foreach (object o in lst)
                        {
                            Guid val = Guid.Empty;
                            if (Guid.TryParse(o.ToString(), out val))
                            {
                                filter.GuidItems.Add(val);
                            }
                        }
                    }

                    retFilters.Add(filter);
                }
                catch { }
            }

            return(retFilters);
        }
示例#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);
            }
        }
示例#3
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);
            }
        }
示例#4
0
        public static bool import_form(Guid applicationId, Guid?instanceId, DocFileInfo uploadedFile,
                                       Dictionary <string, object> map, Guid currentUserId, ref List <FormElement> savedElements,
                                       List <FormElement> nodeElements, ref string errorMessage)
        {
            if (!instanceId.HasValue || uploadedFile == null || !uploadedFile.FileID.HasValue)
            {
                return(false);
            }

            if (!uploadedFile.exists(applicationId))
            {
                return(false);
            }

            if (map.ContainsKey("sub"))
            {
                map = (Dictionary <string, object>)map["sub"];
            }

            FormType formInstance = FGController.get_form_instance(applicationId, instanceId.Value);

            if (formInstance == null || !formInstance.InstanceID.HasValue || !formInstance.FormID.HasValue)
            {
                return(false);
            }

            List <FormElement> formElements = FGController.get_form_instance_elements(applicationId,
                                                                                      instanceId.Value).OrderBy(u => u.SequenceNumber).ToList();

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

            XmlDocument doc = new XmlDocument();

            try
            {
                using (MemoryStream stream = new MemoryStream(uploadedFile.toByteArray(applicationId)))
                    doc.Load(stream);
            }
            catch (Exception ex)
            {
                LogController.save_error_log(applicationId, currentUserId,
                                             "FG_ImportForm_LoadFile", ex, ModuleIdentifier.FG);
                errorMessage = Messages.OperationFailed.ToString();
                return(false);
            }

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

            if (!string.IsNullOrEmpty(doc.DocumentElement.GetNamespaceOfPrefix("")))
            {
                nsmgr.AddNamespace(DEFAULTPREFIX, doc.DocumentElement.GetNamespaceOfPrefix(""));
                nsmgr.AddNamespace("", doc.DocumentElement.GetNamespaceOfPrefix(""));
            }
            foreach (XmlAttribute attr in doc.SelectSingleNode("/*").Attributes)
            {
                if (attr.Prefix == "xmlns")
                {
                    nsmgr.AddNamespace(attr.LocalName, attr.Value);
                }
            }

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

            //add node elements
            if (nodeElements == null)
            {
                nodeElements = new List <FormElement>();
            }

            foreach (FormElement e in nodeElements)
            {
                e.Type = FormElementTypes.Text;
                formElements.Add(e);
            }
            //end of add node elements

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

            //remove node elements
            foreach (FormElement e in nodeElements)
            {
                FormElement elem = theElements.Where(
                    u => (u.ElementID == e.ElementID || u.RefElementID == e.ElementID) && u.Name == e.Name).FirstOrDefault();

                if (elem != null)
                {
                    e.TextValue = elem.TextValue;
                    theElements.Remove(elem);
                }
            }
            //end of remove node elements

            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);

            //set_national_ids(ref newElements);
            set_identity_values(ref newElements);

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

            if (newFiles != null)
            {
                newFiles.ForEach(f => f.move(applicationId, FolderNames.TemporaryFiles, FolderNames.Attachments));
            }

            bool result = newFormInstances == null || newFormInstances.Count == 0 ||
                          FGController.create_form_instances(applicationId, newFormInstances, currentUserId);

            result = result && FGController.save_form_instance_elements(applicationId,
                                                                        ref newElements, new List <Guid>(), currentUserId, ref errorMessage);

            if (!result && newFiles != null)
            {
                newFiles.ForEach(f => f.move(applicationId, FolderNames.Attachments, FolderNames.TemporaryFiles));
            }

            if (result)
            {
                savedElements = theElements;
            }

            return(result);
        }