Example #1
0
        public void Set(object fromKey, IEnumerable toKeys)
        {
            // load
            var relationTbl = DalcManager.LoadAll(new Query(RelationSourceName, ComposeFromCondition(fromKey)));

            var data = ExtraKeys == null ? new Dictionary <string, object>() : new Dictionary <string, object>(ExtraKeys);

            data[FromFieldName] = fromKey;

            int pos = 1;

            foreach (var toKey in toKeys)
            {
                DataRow relationRow = relationTbl.Rows.Cast <DataRow>().Where(r => DalcRelationEditor.AreEqual(toKey, r[ToFieldName])).FirstOrDefault();
                if (relationRow == null)
                {
                    relationRow = relationTbl.NewRow();
                    relationRow[FromFieldName] = fromKey;
                    relationRow[ToFieldName]   = toKey;
                    if (ExtraKeys != null)
                    {
                        foreach (var extraKeyEntry in ExtraKeys)
                        {
                            relationRow[extraKeyEntry.Key] = extraKeyEntry.Value;
                        }
                    }
                    relationTbl.Rows.Add(relationRow);
                }

                if (PositionFieldName != null)
                {
                    relationRow[PositionFieldName] = pos++;
                }
            }

            // delete missed relations
            foreach (DataRow r in relationTbl.Rows)
            {
                if (!DalcRelationEditor.Contains(r[ToFieldName], toKeys))
                {
                    r.Delete();
                }
            }

            DalcManager.Update(relationTbl);
        }
Example #2
0
    public void ProcessRequest(HttpContext context)
    {
        var Request  = context.Request;
        var Response = context.Response;

        log.Write(LogEvent.Info, "Processing FlexBox ajax request: {0}", Request.Url.ToString());

        var dalcName       = Request["dalc"];
        var relex          = Request["relex"];
        var validationCode = Request["validate"];

        if (!ValidateRequest(validationCode, dalcName, relex))
        {
            log.Write(LogEvent.Error, "Validation failed for FlexBox ajax request: {0}", Request.Url.ToString());
            throw new Exception("Invalid FlexBox ajax request");
        }

        var relexByServiceName = WebManager.GetService <object>(relex);

        if (relexByServiceName != null)
        {
            relex = Convert.ToString(relexByServiceName);
        }

        var dalc          = WebManager.GetService <IDalc>(dalcName);
        var labelField    = Request["label"];
        var filterPrvName = Request["filter"];

        var relexParser  = new RelExQueryParser(false);
        var exprResolver = WebManager.GetService <NI.Common.Expressions.IExpressionResolver>("defaultExprResolver");
        var filterPrv    = filterPrvName != null?WebManager.GetService <IProvider <IDictionary <string, object>, IDictionary <string, object> > >(filterPrvName) : null;

        var qContext = new Hashtable();

        // directly passed query parameters
        foreach (string key in Request.Params.Keys)
        {
            if (key != null)
            {
                qContext[key] = Request.Params[key];
            }
        }
        // legacy context format (left for compatibility)
        if (Request["context"] != null)
        {
            var deserializedCtx = JsHelper.FromJsonString <IDictionary <string, object> >(Request["context"]);
            foreach (var item in deserializedCtx)
            {
                qContext[item.Key] = JsHelper.FromJsonString(Convert.ToString(item.Value));
            }
        }
        // actually used context
        if (Request["context_json"] != null)
        {
            var deserializedCtx = JsHelper.FromJsonString <IDictionary <string, object> >(Request["context_json"]);
            foreach (var item in deserializedCtx)
            {
                qContext[item.Key] = item.Value;
            }
        }

        Query q = (Query)relexParser.Parse(Convert.ToString(exprResolver.Evaluate(qContext, relex)));

        if (Request["action"] == "add" && Request["textfield"] != null && Request["valuefield"] != null)
        {
            var dbMgr       = new DalcManager(dalc, WebManager.GetService <IDataSetProvider>("dsFactory"));
            var newEntryRow = dbMgr.Create(q.SourceName);
            newEntryRow[Request["textfield"]] = Request["value"];
            dbMgr.Update(newEntryRow);

            Response.Write(JsHelper.ToJsonString(new Dictionary <string, object>(ConvertManager.ChangeType <IDictionary <string, object> >(newEntryRow))));
        }
        else
        {
            if (Request["p"] != null && Request["s"] != null)
            {
                var pageSize = Convert.ToInt32(Request["s"]);
                q.StartRecord = (Convert.ToInt32(Request["p"]) - 1) * pageSize;
                q.RecordCount = pageSize;
            }
            var ds = new DataSet();
            dalc.Load(ds, q);

            var res     = new Dictionary <string, object>();
            var results = new List <IDictionary <string, object> >();
            foreach (DataRow r in ds.Tables[q.SourceName].Rows)
            {
                IDictionary <string, object> data = new Dictionary <string, object>(new DataRowDictionaryWrapper(r));
                // process label field (if specified)
                if (!String.IsNullOrEmpty(labelField) && data.ContainsKey(labelField))
                {
                    data[labelField] = WebManager.GetLabel(Convert.ToString(data[labelField]), typeof(FlexBoxAjaxHandler).FullName);
                }

                // prevent security hole
                if (data.ContainsKey("password"))
                {
                    data["password"] = null;
                }

                // filter
                if (filterPrv != null)
                {
                    data = filterPrv.Provide(data);
                }
                if (data != null)
                {
                    results.Add(data);
                }
            }

            res["total"]   = dalc.RecordsCount(q.SourceName, q.Root);
            res["results"] = results;

            var json = new JavaScriptSerializer();
            Response.Write(json.Serialize(res));
        }
    }