Example #1
0
        private void saveEntity()
        {
            string id = context.Request["Id"];
            string where = context.Request["filter"];
            string entityName = context.Request["entityName"];
            int mid = 0;
            if (!Int32.TryParse(id, out mid))
            {
                context.Response.Write("{success:false, errorMessage:'ID geçersiz!'}");
                return;
            }

            // bu istisnai durum, sadece konfigürasyon veritabanına kaydedilmiyor
            if (entityName == "Configuration")
            {
                Provider.Configuration.SetFieldsByPostData(context.Request.Form);
                Provider.Configuration.Save();
                context.Response.Write("{success:true}");
                return;
            }

            // entitiyi kaydedelim
            BaseEntity entity = null;
            if (mid > 0)
                entity = (BaseEntity)Provider.Database.Read(Provider.GetEntityType(entityName), mid);
            else
                entity = Provider.CreateEntity(entityName);
            entity.SetFieldsByPostData(context.Request.Form);
            if (!string.IsNullOrEmpty(where))
            {
                FilterParser filterParser = new FilterParser(where, entityName);
                where = filterParser.GetWhere();
                foreach (var item in filterParser.GetNameValuePairs())
                    if(string.IsNullOrEmpty(context.Request.Form[item.Key]))
                        entity.SetMemberValue(item.Key, item.Value);
            }
            entity.Save();

            // entitiye ait Lang kayıtlarını kaydedelim
            Type langEntityType = Provider.GetEntityType(entityName + "Lang");
            if (langEntityType != null)
            {
                Dictionary<int, NameValueCollection> langEntities = new Dictionary<int, NameValueCollection>();
                Dictionary<int, string> langEntityFieldSum = new Dictionary<int, string>();
                for (int i = 0; i < context.Request.Form.Count; i++)
                {
                    string key = context.Request.Form.GetKey(i);
                    if (key.Contains("_lang_"))
                    {
                        string[] parts = key.Split(new string[] { "_lang_" }, StringSplitOptions.None);
                        int langId = int.Parse(parts[1]);
                        string fieldName = parts[0];
                        string fieldVal = context.Request.Form[key];

                        if (!langEntities.ContainsKey(langId))
                            langEntities.Add(langId, new NameValueCollection());
                        langEntities[langId].Add(fieldName, fieldVal);

                        if (!langEntityFieldSum.ContainsKey(langId))
                            langEntityFieldSum.Add(langId, "");
                        langEntityFieldSum[langId] += fieldVal;
                    }
                }
                // içi boş olan langEntitileri kaldıralım
                foreach (var item in langEntityFieldSum)
                    if (item.Value.Trim() == "")
                        langEntities.Remove(item.Key);

                foreach (var item in langEntities)
                {
                    BaseEntity langEntity = (BaseEntity)Provider.Database.Read(langEntityType, entityName + "Id = {0} AND LangId={1}", entity.Id, item.Key);
                    if (langEntity == null)
                        langEntity = Provider.CreateEntity(langEntityType);
                    langEntity.SetFieldsByPostData(item.Value);
                    langEntity.SetMemberValue(entityName + "Id", entity.Id);
                    langEntity.SetMemberValue("LangId", item.Key);
                    langEntity.Save();
                }
            }

            context.Response.Write("{success:true}");
        }
Example #2
0
        private void newEntity()
        {
            string error = "";
            string where = context.Request["filter"] ?? "";
            string entityName = context.Request["entityName"];
            Type entityType = Provider.GetEntityType(entityName);
            if (entityType != null)
            {
                IDatabaseEntity entity = Provider.CreateEntity(entityType);
                if (!string.IsNullOrEmpty(where))
                {
                    FilterParser filterParser = new FilterParser(where, entityName);
                    where = filterParser.GetWhere();
                    foreach (var item in filterParser.GetNameValuePairs())
                        entity.SetMemberValue(item.Key, item.Value);
                }
                context.Response.Write(@"{ success: true, data: " + entity.ToJSON() + "}");
                return;
            }
            else
                error = "Entity tipi bulunamadı.";

            context.Response.Write(@"{ success: false, errorMessage: " + error.ToJS() + "}");
        }