private FormType activate_form(Guid applicationId, Guid currentUserId, string id)
        {
            TemplateForm tempForm = !Forms.ContainsKey(id) ? null : Forms[id];

            if (tempForm == null)
            {
                return(null);
            }

            FormType form = new FormType()
            {
                FormID         = IDs.new_id(id),
                TemplateFormID = IDs.get_id_from_template(id),
                Title          = Base64.decode(tempForm.Title) + " " + PublicMethods.get_random_number().ToString(),
                Creator        = new User()
                {
                    UserID = currentUserId
                }
            };

            if (!FGController.create_form(applicationId, form))
            {
                return(null);
            }

            List <FormElement> elements = tempForm.Elements == null ? new List <FormElement>() : tempForm.Elements.Select(e =>
            {
                FormElement newElem = new FormElement()
                {
                    ElementID         = IDs.new_id(e.ID),
                    TemplateElementID = IDs.get_id_from_template(e.ID),
                    Name   = Base64.decode(e.Code),
                    Title  = Base64.decode(e.Title),
                    Weight = e.Weight,
                    Help   = e.Help
                };

                e.Info = PublicMethods.fromJSON(PublicMethods.toJSON_typed(e.Info));

                FormElementTypes tp = FormElementTypes.Text;
                if (!Enum.TryParse <FormElementTypes>(e.Type, out tp))
                {
                    return(null);
                }
                else
                {
                    newElem.Type = tp;
                }

                if (tp == FormElementTypes.MultiLevel && e.Info.ContainsKey("NodeType"))
                {
                    Dictionary <string, object> infoNt = PublicMethods.get_dic_value <Dictionary <string, object> >(e.Info, "NodeType");

                    string _id = infoNt == null || !infoNt.ContainsKey("ID") ? null : infoNt["ID"].ToString();

                    NodeType nodeType = string.IsNullOrEmpty(_id) ? null : (IDs.is_replaced(_id) ?
                                                                            CNController.get_node_type(applicationId, IDs.get_existing_id(_id).Value) :
                                                                            activate_node_type(applicationId, currentUserId, _id));

                    if (nodeType == null)
                    {
                        return(null);
                    }

                    ((Dictionary <string, object>)e.Info["NodeType"])["ID"]   = nodeType.NodeTypeID.Value.ToString();
                    ((Dictionary <string, object>)e.Info["NodeType"])["Name"] = Base64.encode(nodeType.Name);
                }
                else if (tp == FormElementTypes.Form && e.Info.ContainsKey("FormID"))
                {
                    string _id = e.Info["FormID"].ToString();

                    FormType elmForm = string.IsNullOrEmpty(_id) ? null : (IDs.is_replaced(_id) ?
                                                                           FGController.get_form(applicationId, IDs.get_existing_id(_id).Value) :
                                                                           activate_form(applicationId, currentUserId, _id));

                    if (elmForm == null)
                    {
                        return(null);
                    }

                    e.Info["FormID"]   = elmForm.FormID.Value.ToString();
                    e.Info["FormName"] = Base64.encode(elmForm.Title);
                }
                else if (tp == FormElementTypes.Node && e.Info.ContainsKey("NodeTypes"))
                {
                    ArrayList infoNts = PublicMethods.get_dic_value <ArrayList>(e.Info, "NodeTypes");

                    List <Dictionary <string, object> > newNts = (infoNts == null ? new ArrayList() : infoNts).ToArray().ToList()
                                                                 .Where(itm => itm.GetType() == typeof(Dictionary <string, object>))
                                                                 .Select(itm => (Dictionary <string, object>)itm)
                                                                 .Where(itm => itm.ContainsKey("NodeTypeID")).ToList()
                                                                 .Select(itm =>
                    {
                        string _id = itm["NodeTypeID"].ToString();

                        NodeType nodeType = string.IsNullOrEmpty(_id) ? null : (IDs.is_replaced(_id) ?
                                                                                CNController.get_node_type(applicationId, IDs.get_existing_id(_id).Value) :
                                                                                activate_node_type(applicationId, currentUserId, _id));

                        if (nodeType == null)
                        {
                            return(null);
                        }

                        itm["NodeTypeID"] = nodeType.NodeTypeID.Value;
                        itm["NodeType"]   = Base64.encode(nodeType.Name);

                        return(itm);
                    }).Where(itm => itm != null).ToList();

                    e.Info["NodeTypes"] = newNts;
                }

                newElem.Info = PublicMethods.toJSON(e.Info);

                return(newElem);
            }).Where(e => e != null).ToList();

            FGController.save_form_elements(applicationId, form.FormID.Value, null, null, null, elements, currentUserId);

            return(form);
        }
        private void add_form(FormType form, List <FormElement> elements)
        {
            //get new id
            string newFormId = IDs.resolve(form.FormID.Value);

            if (Forms.ContainsKey(newFormId))
            {
                return;
            }
            //end of get new id

            TemplateForm newForm = new TemplateForm()
            {
                ID    = newFormId,
                Title = Base64.encode(form.Title)
            };

            Forms[newFormId] = newForm;

            elements.Where(e => e.Type.HasValue).ToList().ForEach(elem => {
                TemplateFormElement newFormElement = new TemplateFormElement()
                {
                    ID     = IDs.resolve(elem.ElementID),
                    Code   = Base64.encode(elem.Name),
                    Title  = Base64.encode(elem.Title),
                    Type   = elem.Type.Value.ToString(),
                    Weight = !elem.Weight.HasValue ? 0 : elem.Weight.Value,
                    Info   = PublicMethods.fromJSON(elem.Info),
                    Help   = elem.Help
                };

                newForm.Elements.Add(newFormElement);

                if (elem.Type == FormElementTypes.MultiLevel && newFormElement.Info.ContainsKey("NodeType"))
                {
                    Dictionary <string, object> infoNt =
                        PublicMethods.get_dic_value <Dictionary <string, object> >(newFormElement.Info, "NodeType");

                    Guid?_id = infoNt == null || !infoNt.ContainsKey("ID") ? null : PublicMethods.parse_guid(infoNt["ID"].ToString());

                    if (_id.HasValue)
                    {
                        string strId = IDs.resolve(_id);
                        ((Dictionary <string, object>)newFormElement.Info["NodeType"])["ID"] = strId.ToString();

                        add_dependency(elem.ElementID, _id);
                        add_node_type(_id);
                    }
                }
                else if (elem.Type == FormElementTypes.Form && newFormElement.Info.ContainsKey("FormID"))
                {
                    Guid?_id = PublicMethods.parse_guid(newFormElement.Info["FormID"].ToString());

                    if (_id.HasValue)
                    {
                        string strId = IDs.resolve(_id);
                        newFormElement.Info["FormID"] = strId;

                        add_dependency(elem.ElementID, _id);
                        add_form(_id.Value);
                    }
                }
                else if (elem.Type == FormElementTypes.Node && newFormElement.Info.ContainsKey("NodeTypes"))
                {
                    ArrayList infoNts = PublicMethods.get_dic_value <ArrayList>(newFormElement.Info, "NodeTypes");

                    List <Dictionary <string, object> > newNts = (infoNts == null ? new ArrayList() : infoNts).ToArray().ToList()
                                                                 .Where(itm => itm.GetType() == typeof(Dictionary <string, object>))
                                                                 .Select(itm => (Dictionary <string, object>)itm)
                                                                 .Where(itm => itm.ContainsKey("NodeTypeID")).ToList()
                                                                 .Select(itm =>
                    {
                        Guid?ntId = PublicMethods.parse_guid(itm["NodeTypeID"].ToString());

                        if (!ntId.HasValue)
                        {
                            return(null);
                        }

                        string strId = IDs.resolve(ntId);

                        itm["NodeTypeID"] = strId;

                        add_dependency(elem.ElementID, ntId);
                        add_node_type(ntId);

                        return(itm);
                    }).Where(itm => itm != null).ToList();

                    newFormElement.Info["NodeTypes"] = newNts;
                }
            });
        }