public static bool save_form_instance_element(Guid applicationId, FormElement element, Guid currentUserId) { List <FormElement> elems = new List <FormElement>(); elems.Add(element); return(save_form_instance_elements(applicationId, ref elems, new List <Guid>(), currentUserId)); }
private static string _find_text_value(FormElement refElement, ref List <FormElement> elements, List <string> strValidNames) { return(elements.Where(u => u.FormInstanceID.HasValue && u.Type == FormElementTypes.Text && (refElement == null || u.FormInstanceID == refElement.FormInstanceID) && field_name_match(u.Name, strValidNames) && !string.IsNullOrEmpty(u.TextValue)).Select(x => x.TextValue).FirstOrDefault()); }
public static bool set_element_value(string value, ref FormElement element) { if (string.IsNullOrEmpty(value)) { value = string.Empty; } FormElementTypes type = element.Type.HasValue ? element.Type.Value : FormElementTypes.Text; switch (type) { case FormElementTypes.Text: case FormElementTypes.Select: case FormElementTypes.Checkbox: element.TextValue = value; return(!string.IsNullOrEmpty(element.TextValue)); case FormElementTypes.Binary: element.BitValue = value.ToLower() == "true"; return(true); case FormElementTypes.Date: if (value.ToLower() == "now") { element.DateValue = DateTime.Now; } else { DateTime dt = new DateTime(); if (DateTime.TryParse(value, out dt)) { element.DateValue = dt; } } return(element.DateValue.HasValue); case FormElementTypes.Node: case FormElementTypes.User: SelectedGuidItemType tp = type == FormElementTypes.Node ? SelectedGuidItemType.Node : SelectedGuidItemType.User; Guid id = Guid.Empty; if (Guid.TryParse(value, out id) && id != Guid.Empty) { element.GuidItems.Add(new SelectedGuidItem(id, string.Empty, string.Empty, tp)); } return(id != Guid.Empty); case FormElementTypes.Numeric: float flt = 0; if (float.TryParse(value, out flt)) { element.FloatValue = flt; } return(element.FloatValue.HasValue); } return(false); }
private static bool is_match(FormElement elem, FormElement elemToCheck, ref List <FormElement> allElements, string nationalId, string firstName, string lastName, string fullName) { List <string> strAll = new List <string>(); strAll.AddRange(StrNationalID); strAll.AddRange(StrFirstName); strAll.AddRange(StrLastName); strAll.AddRange(StrFullName); List <string> firstAndLast = new List <string>(); firstAndLast.AddRange(StrFirstName); firstAndLast.AddRange(StrLastName); if (!elemToCheck.FormInstanceID.HasValue || elemToCheck.FormInstanceID == elem.FormInstanceID || elemToCheck.Type != FormElementTypes.Text || !field_name_match(elemToCheck.Name, strAll)) { return(false); } if (field_name_match(elemToCheck.Name, StrNationalID)) { return(!string.IsNullOrEmpty(nationalId) && elemToCheck.TextValue == nationalId); } else if (field_name_match(elemToCheck.Name, StrFullName)) { return((!string.IsNullOrEmpty(fullName) && elemToCheck.TextValue == fullName) || (!string.IsNullOrEmpty(firstName) && elemToCheck.TextValue == firstName + " " + lastName)); } else if (field_name_match(elemToCheck.Name, firstAndLast)) { string f = _find_text_value(elemToCheck, ref allElements, StrFirstName); string l = _find_text_value(elemToCheck, ref allElements, StrLastName); if (!string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName) && f == firstName && l == lastName) { return(true); } else if (!string.IsNullOrEmpty(fullName) && f + " " + l == fullName) { return(true); } else { return(false); } } return(false); }
public static List <FormElement> get_form_elements(string strElements, bool formDesignMode = false) { try { List <FormElement> retList = new List <FormElement>(); if (string.IsNullOrEmpty(strElements)) { return(retList); } Dictionary <string, object> dic = PublicMethods.fromJSON(Base64.decode(strElements)); if (!dic.ContainsKey("Elements") || dic["Elements"].GetType() != typeof(ArrayList)) { return(retList); } foreach (Dictionary <string, object> elem in (ArrayList)dic["Elements"]) { FormElement newElement = new FormElement() { ElementID = PublicMethods.parse_guid(PublicMethods.get_dic_value(elem, "ElementID")), RefElementID = PublicMethods.parse_guid(PublicMethods.get_dic_value(elem, "RefElementID")), FormInstanceID = PublicMethods.parse_guid(PublicMethods.get_dic_value(elem, "InstanceID")), Title = PublicMethods.parse_string(PublicMethods.get_dic_value(elem, "Title"), defaultValue: null), Name = formDesignMode ? Base64.decode(PublicMethods.get_dic_value(elem, "Name", defaultValue: null)) : PublicMethods.get_dic_value(elem, "Name", defaultValue: null), Filled = PublicMethods.parse_bool(PublicMethods.get_dic_value(elem, "Filled"), defaultValue: false), SequenceNumber = PublicMethods.parse_int(PublicMethods.get_dic_value(elem, "SequenceNumber"), defaultValue: -1), Necessary = PublicMethods.parse_bool(PublicMethods.get_dic_value(elem, "Necessary")), UniqueValue = PublicMethods.parse_bool(PublicMethods.get_dic_value(elem, "UniqueValue")), Help = PublicMethods.parse_string(PublicMethods.get_dic_value(elem, "Help"), defaultValue: null), Info = PublicMethods.parse_string(PublicMethods.get_dic_value(elem, "Info"), defaultValue: null), Weight = PublicMethods.parse_double(PublicMethods.get_dic_value(elem, "Weight")), TextValue = PublicMethods.parse_string(PublicMethods.get_dic_value(elem, "TextValue"), defaultValue: null), FloatValue = PublicMethods.parse_double(PublicMethods.get_dic_value(elem, "FloatValue")), BitValue = PublicMethods.parse_bool(PublicMethods.get_dic_value(elem, "BitValue")), DateValue = PublicMethods.parse_date(PublicMethods.get_dic_value(elem, "DateValue")), AttachedFiles = DocumentUtilities.get_files_info(PublicMethods.get_dic_value(elem, "Files")) }; if (newElement.Type == FormElementTypes.Separator) { newElement.Necessary = null; } if (newElement.Type != FormElementTypes.Text && newElement.Type != FormElementTypes.Numeric) { newElement.UniqueValue = null; } FormElementTypes fet = FormElementTypes.Text; if (Enum.TryParse <FormElementTypes>(PublicMethods.get_dic_value(elem, "Type"), out fet)) { newElement.Type = fet; } if (elem.ContainsKey("GuidItems") && elem["GuidItems"] != null && elem["GuidItems"].GetType() == typeof(ArrayList)) { SelectedGuidItemType tp = SelectedGuidItemType.None; if (newElement.Type == FormElementTypes.Node) { tp = SelectedGuidItemType.Node; } else if (newElement.Type == FormElementTypes.User) { tp = SelectedGuidItemType.User; } foreach (Dictionary <string, object> d in (ArrayList)elem["GuidItems"]) { newElement.GuidItems.Add(new SelectedGuidItem(Guid.Parse((string)d["ID"]), code: string.Empty, name: Base64.decode((string)d["Name"]), type: tp)); } } if (newElement.ElementID.HasValue && !formDesignMode && (!newElement.Filled.HasValue || !newElement.Filled.Value)) { newElement.RefElementID = newElement.ElementID; newElement.ElementID = Guid.NewGuid(); } if (newElement.AttachedFiles != null) { for (var i = 0; i < newElement.AttachedFiles.Count; ++i) { newElement.AttachedFiles[i].OwnerID = newElement.ElementID; newElement.AttachedFiles[i].OwnerType = FileOwnerTypes.FormElement; } } retList.Add(newElement); } return(retList); } catch { return(new List <FormElement>()); } }
public static bool modify_form_element(Guid applicationId, FormElement Info, ref string errorMessage) { return(DataProvider.ModifyFormElement(applicationId, Info, ref errorMessage)); }
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); } }
private static List <FormElement> _parse_imported_form(Guid applicationId, XmlNode parentNode, XmlNodeList nodeList, XmlNamespaceManager nsmgr, object mapEntry, List <FormElement> formElements, Dictionary <string, object> parentMap) { if (nodeList == null || nodeList.Count == 0) { return(new List <FormElement>()); } List <FormElement> retList = new List <FormElement>(); Type tp = mapEntry.GetType(); if (tp == typeof(Dictionary <string, object>)) { Dictionary <string, object> map = (Dictionary <string, object>)mapEntry; string target = map.ContainsKey("target") && formElements.Any(u => !string.IsNullOrEmpty(u.Name) && u.Name.ToLower() == map["target"].ToString().ToLower()) ? map["target"].ToString().ToLower() : string.Empty; if (!string.IsNullOrEmpty(target)) { FormElement elem = formElements.Where( u => !string.IsNullOrEmpty(u.Name) && u.Name.ToLower() == target).FirstOrDefault(); FormElement newElement = _extract_xml_node_data(applicationId, parentNode, nodeList, nsmgr, elem, map, parentMap); if (newElement != null) { retList.Add(newElement); } } else { Dictionary <string, object> subMap = map.ContainsKey("sub") && map["sub"].GetType() == typeof(Dictionary <string, object>) ? (Dictionary <string, object>)map["sub"] : null; if (subMap != null) { foreach (string name in subMap.Keys) { foreach (XmlNode nd in nodeList) { if (!string.IsNullOrEmpty(nd.Prefix)) { nsmgr.AddNamespace(nd.Prefix, nd.NamespaceURI); } List <FormElement> lst = _parse_imported_form(applicationId, nd, get_child_nodes(nd, name, nsmgr), nsmgr, subMap[name], formElements, subMap); if (lst != null && lst.Count > 0) { retList.AddRange(lst); } } } } } } else if (tp == typeof(string)) { FormElement elem = formElements .Where(u => !string.IsNullOrEmpty(u.Name) && u.Name.ToLower() == ((string)mapEntry).ToString().ToLower()).FirstOrDefault(); FormElement el = _extract_xml_node_data(applicationId, parentNode, nodeList, nsmgr, elem, null, parentMap); if (el != null) { retList.Add(el); } } return(retList); }
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); }