Exemplo n.º 1
0
        public Stream CreateData(string tablename, string data)
        {
            var request    = WebOperationContext.Current.IncomingRequest;
            var headers    = request.Headers;
            var JSONString = string.Empty;

            try
            {
                var entity = JObject.Parse(data);
                //var entity2 = JsonConvert.DeserializeObject(data);

                var httpToken = headers["Authorization"].Trim().Replace("Bearer ", "");
                using (SecureData sd = new SecureData(false, httpToken))
                {
                    var dataset = sd.GetData("table_mob_" + tablename, ":" + tablename.Replace("_", "") + "id=0", 0, 0, "");
                    var table   = dataset.Tables["table_mob_" + tablename];
                    var row     = table.NewRow();

                    foreach (DataColumn c in table.Columns)
                    {
                        JToken prop = entity[c.ColumnName];
                        if (prop != null && prop.Type != JTokenType.Null && !prop.HasValues)
                        {
                            row.SetField(c, prop);
                        }
                    }
                    table.Rows.Add(row);

                    DataSet dsChanges = new DataSet();
                    dsChanges.Tables.Add(table.GetChanges());
                    var dsError = sd.SaveData(dsChanges, true, "");
                    if (dsError.Tables.Contains("table_mob_" + tablename))
                    {
                        WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK; //200
                        JSONString = JsonConvert.SerializeObject(dsError.Tables["table_mob_" + tablename].Rows[0]["NewPrimaryKeyID"]);
                    }
                    else
                    {
                        WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.InternalServerError; //500
                        JSONString = JsonConvert.SerializeObject(dsError.Tables["ExceptionTable"]);
                    }
                }
            }
            catch (Exception e)
            {
                WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.InternalServerError; //500
                JSONString = JsonConvert.SerializeObject(e.Message);
            }
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
            return(new MemoryStream(Encoding.UTF8.GetBytes(JSONString)));
        }
Exemplo n.º 2
0
        public Stream DeleteData(string tablename, string id)
        {
            var request    = WebOperationContext.Current.IncomingRequest;
            var headers    = request.Headers;
            var JSONString = string.Empty;

            try
            {
                var httpToken = headers["Authorization"].Trim().Replace("Bearer ", "");
                using (SecureData sd = new SecureData(false, httpToken))
                {
                    var dataset = sd.GetData("table_mob_" + tablename, ":" + tablename + "id=" + id, 0, 0, "");

                    if (dataset.Tables["table_mob_" + tablename].Rows.Count > 0)
                    {
                        var table = dataset.Tables["table_mob_" + tablename];
                        var nr    = table.Rows[0];
                        nr.Delete();

                        var dsError = sd.SaveData(dataset, true, "");
                        if (dsError.Tables.Contains("table_mob_" + tablename))
                        {
                            WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                            JSONString = string.Empty;
                        }
                        else
                        {
                            WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.InternalServerError;
                            JSONString = JsonConvert.SerializeObject(dsError.Tables["ExceptionTable"]);
                        }
                    }
                    else
                    {
                        WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
                        JSONString = JsonConvert.SerializeObject("Resource Not Found");
                    }
                }
            }
            catch (Exception e)
            {
                WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.InternalServerError; //500
                JSONString = JsonConvert.SerializeObject(e.Message);
            }
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
            return(new MemoryStream(Encoding.UTF8.GetBytes(JSONString)));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Saves data to the database using unique key fields as the primary key (i.e. _id fields not needed)
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public DataSet SaveData(DataSet ds, int altLanguageID, bool insertOnlyLanguageData, int ownerID, BackgroundWorker worker)
        {
            var options = "useuniquekeys=true;booldefaultfalse=true;onlyifallrequiredfieldsexist=true;safeupdatesanddeletes=false;skipsearchengineupdates=true;rowprogressinterval=10;insertonlylanguagedata=" + insertOnlyLanguageData + ";altlanguageid=" + altLanguageID;

            if (IsAdministrator)
            {
                options += ";ownerid=" + ownerID;
            }
            if (_conn.UseWebService)
            {
                return(_gui.SaveDataSet(true, _conn.GrinGlobalUserName, _conn.GrinGlobalPassword, ds, options));
            }
            else
            {
                using (var sd = new SecureData(true, _conn.GrinGlobalLoginToken, _conn.GenerateDataConnectionSpec())) {
                    return(sd.SaveData(ds, false, options, worker));
                }
            }
        }
Exemplo n.º 4
0
 public DataSet SaveData(bool suppressExceptions, string userName, string password, DataSet ds, string options)
 {
     using (SecureData sd = new SecureData(suppressExceptions, Login(userName, password))) {
         return(sd.SaveData(ds, true, options));
     }
 }