Beispiel #1
0
        /**
         * Loads parameter values from a json object specifying the offset
         * @param params
         * @param value
         * @param Offset
         * @return bool
         */
        public static bool LoadParametersValues(TParams parameters,
                                                TJSONObject value, int Offset)
        {
            JArray       parValue;
            JObject      jvalue = (JObject)value.getInternalObject();
            DBXParameter par;

            if (parameters.size() <= 0)
            {
                return(false);
            }
            for (int i = 0; i < parameters.size(); i++)
            {
                par      = parameters.getParameter(i);
                parValue = jvalue.Value <JArray>(par.getName());
                if (parValue.Count < Offset + 1)
                {
                    return(false);
                }
                if (parValue[Offset] is JArray)
                {
                    DBXJSONTools.JSONtoDBX(parValue[Offset].Value <JArray>(), par.getValue(), "");
                }
                else
                {
                    DBXJSONTools.JSONtoDBX(parValue[Offset].Value <object>(), par.getValue(), "");
                }
            }
            return(true);
        }
Beispiel #2
0
        public TJSONObject asJSONObject()
        {
            TJSONObject result       = null;
            long        lastPosition = currentPosition;

            try {
                reset();
                result = DBXJSONTools.DBXReaderToJSONObject(this);
            } finally {
                currentPosition = lastPosition;
            }
            return(result);
        }
Beispiel #3
0
        /**
         * Returns a new TParams created by the metadata represented in the specified {@link JSONArray}
         * @param paramsMetadata
         * @return TParams
         */
        public static TParams CreateParametersFromMetadata(TJSONArray parametersMetadata)
        {
            TParams      o = new TParams();
            JArray       paramMetadata;
            JArray       paramsMetadata = parametersMetadata.asJSONArray();
            DBXParameter parameter;

            try {
                for (int i = 0; i < paramsMetadata.Count; i++)
                {
                    paramMetadata = paramsMetadata.Value <JArray>(i);
                    parameter     = new DBXParameter();
                    DBXJSONTools.JSONToValueType(new TJSONArray(paramMetadata), parameter);
                    o.addParameter(parameter);
                }
            } catch (Exception e) {
                throw new DBXException(e.Message);
            }
            return(o);
        }
Beispiel #4
0
        /**
         * Convert a Object to DBXValue
         * @param o
         * @param value
         * @param DBXTypeName
         */
        public static void JSONtoDBX(Object o, DBXValue value, String DBXTypeName)
        {
            try
            {
                if (DBXTypeName.StartsWith("TDBX") && DBXTypeName.EndsWith("Value") &&
                    !(DBXTypeName.IndexOf("Stream") > 0) && !(DBXTypeName.IndexOf("Reader") > 0))
                {
                    if (JTokenToObject(o) == null)
                    {
                        value.GetAsDBXValue().setNull();
                        return;
                    }
                }

                if (!((o == null) && (DBXTypeName.Equals(""))))
                {
                    switch (value.getDBXType())
                    {
                    case DBXDataTypes.Int8Type:
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if ((DBXTypeName.Equals("TDBXInt8Value")))
                        {
                            value.GetAsDBXValue()
                            .SetAsInt8(Int16.Parse(o.ToString()));
                        }
                        else
                        {
                            value.SetAsInt8(Int16.Parse(o.ToString()));
                        }
                        break;

                    case DBXDataTypes.Int16Type:
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if ((DBXTypeName.Equals("TDBXInt16Value")))
                        {
                            value.GetAsDBXValue().SetAsInt16(
                                (Int16.Parse(o.ToString())));
                        }
                        else
                        {
                            value.SetAsInt16(Int16.Parse(o.ToString()));
                        }
                        break;

                    case DBXDataTypes.Int32Type:
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if ((DBXTypeName.Equals("TDBXInt32Value")))
                        {
                            value.GetAsDBXValue().SetAsInt32(
                                (Int32.Parse(o.ToString())));
                        }
                        else
                        {
                            value.SetAsInt32(Int32.Parse(o.ToString()));
                        }
                        break;

                    case DBXDataTypes.Int64Type:
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if ((DBXTypeName.Equals("TDBXInt64Value")))
                        {
                            value.GetAsDBXValue().SetAsInt64(
                                (Int64.Parse(o.ToString())));
                        }
                        else
                        {
                            value.SetAsInt64(Int64.Parse(o.ToString()));
                        }
                        break;

                    case DBXDataTypes.BcdType:
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if ((DBXTypeName.Equals("TDBXBcdValue")))
                        {
                            value.GetAsDBXValue().SetAsBcd(
                                DBXDefaultFormatter.getInstance().StringToDouble(o.ToString()));
                        }
                        else
                        {
                            value.SetAsBcd((DBXDefaultFormatter.getInstance()
                                            .StringToDouble((o.ToString()))));
                        }
                        break;

                    case DBXDataTypes.UInt8Type:
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if ((DBXTypeName.Equals("TDBXUInt8Value")))
                        {
                            value.GetAsDBXValue().SetAsUInt8(
                                (UInt16.Parse(o.ToString())));
                        }
                        else
                        {
                            value.SetAsUInt8(UInt16.Parse(o.ToString()));
                        }
                        break;

                    case DBXDataTypes.UInt16Type:
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if ((DBXTypeName.Equals("TDBXUInt16Value")))
                        {
                            value.GetAsDBXValue().SetAsUInt16(
                                (UInt16.Parse(o.ToString())));
                        }
                        else
                        {
                            value.SetAsUInt16(UInt16.Parse(o.ToString()));
                        }
                        break;

                    case DBXDataTypes.UInt32Type:
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if ((DBXTypeName.Equals("TDBXUInt32Value")))
                        {
                            value.GetAsDBXValue().SetAsUInt32(
                                UInt32.Parse((o.ToString())));
                        }
                        else
                        {
                            value.SetAsUInt32(UInt32.Parse(o.ToString()));
                        }
                        break;

                    case DBXDataTypes.UInt64Type:
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if ((DBXTypeName.Equals("TDBXUInt64Value")))
                        {
                            value.GetAsDBXValue().SetAsUInt64(
                                int.Parse(o.ToString()));
                        }
                        else
                        {
                            value.SetAsUInt64(int.Parse(o.ToString()));
                        }
                        break;

                    case DBXDataTypes.DoubleType:
                    {
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if (DBXTypeName.Equals("TDBXDoubleValue"))
                        {
                            value.GetAsDBXValue().SetAsDouble(
                                (Convert.ToDouble(o.ToString())));
                        }
                        else
                        {
                            value.SetAsDouble(Convert.ToDouble(o.ToString()));
                        }
                        break;
                    }

                    case DBXDataTypes.SingleType:
                    {
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if (DBXTypeName.Equals("TDBXSingleValue"))
                        {
                            value.GetAsDBXValue().SetAsSingle(
                                Convert.ToSingle(o.ToString()));
                        }
                        else
                        {
                            value.SetAsSingle(Convert.ToSingle(o.ToString()));
                        }
                        break;
                    }

                    case DBXDataTypes.CurrencyType:
                    {
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        value.SetAsCurrency(Convert.ToDouble(o));
                        break;
                    }

                    case DBXDataTypes.JsonValueType:
                    {
                        value.SetAsJSONValue((TJSONValue)DBXJSONTools
                                             .JSONToJSONValue(o));
                        break;
                    }

                    case DBXDataTypes.BinaryBlobType:
                    {
                        if (DBXTypeName.Equals("TDBXStreamValue"))
                        {
                            value.GetAsDBXValue().SetAsStream(DBXJSONTools.JSONToStream(new TJSONArray((JArray)o)));
                        }
                        else
                        {
                            value.SetAsStream(DBXJSONTools.JSONToStream(new TJSONArray((JArray)o)));
                        }
                        break;
                    }

                    case DBXDataTypes.BlobType:
                    {
                        value.SetAsBlob(DBXJSONTools.JSONToStream(new TJSONArray((JArray)o)));
                        break;
                    }

                    case DBXDataTypes.TimeStampType:
                    {
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        DateTime d = DBXDefaultFormatter.getInstance()
                                     .StringToDateTime((String)o);
                        if (d == null)
                        {
                            throw new DBXException("Invalid date");
                        }
                        if (DBXTypeName.Equals("TDBXTimeStampValue"))
                        {
                            value.GetAsDBXValue().SetAsTimeStamp(d);
                        }
                        else
                        {
                            value.SetAsTimeStamp(d);
                        }
                        break;
                    }

                    case DBXDataTypes.AnsiStringType:
                    {
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if ((DBXTypeName.Equals("TDBXAnsiStringValue")) ||
                            (DBXTypeName.Equals("TDBXStringValue")) ||
                            (DBXTypeName.Equals("TDBXAnsiCharsValue")))
                        {
                            value.GetAsDBXValue().SetAsAnsiString((String)o);
                        }
                        else
                        {
                            value.SetAsString((string)o);
                        }
                        break;
                    }

                    case DBXDataTypes.TableType:
                    {
                        if (DBXTypeName.Equals("TDBXReaderValue"))
                        {
                            value.GetAsDBXValue().SetAsTable(
                                (TableType)DBXJSONTools.JSONToTableType(o, DBXTypeName));
                        }
                        else
                        {
                            value.SetAsTable((TableType)DBXJSONTools.JSONToTableType(
                                                 o, DBXTypeName));
                        }
                        break;
                    }

                    case DBXDataTypes.WideStringType:
                    {
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if ((DBXTypeName.Equals("TDBXAnsiStringValue")) ||
                            (DBXTypeName.Equals("TDBXStringValue")) ||
                            (DBXTypeName.Equals("TDBXAnsiCharsValue")) ||
                            (DBXTypeName.Equals("TDBXWideStringValue")))
                        {
                            value.GetAsDBXValue().SetAsString((String)o);
                        }
                        else
                        {
                            value.SetAsString((string)o);
                        }
                        break;
                    }

                    case DBXDataTypes.TimeType:
                    {
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        int t = DBXDefaultFormatter.getInstance().StringToTDBXTime(
                            (String)o);
                        if (DBXTypeName.Equals("TDBXTimeValue"))
                        {
                            value.GetAsDBXValue().SetAsTDBXTime(t);
                        }
                        else
                        {
                            value.SetAsTDBXTime(t);
                        }
                        break;
                    }

                    case DBXDataTypes.DateType:
                    {
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        int t = DBXDefaultFormatter.getInstance().StringToTDBXDate(
                            (String)o);
                        if (DBXTypeName.Equals("TDBXDateValue"))
                        {
                            value.GetAsDBXValue().SetAsTDBXDate(t);
                        }
                        else
                        {
                            value.SetAsTDBXDate(t);
                        }
                        break;
                    }

                    case DBXDataTypes.DateTimeType:
                    {
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        DateTime d = DBXDefaultFormatter.getInstance()
                                     .StringToDateTime((String)o);
                        if (d == null)
                        {
                            throw new DBXException("Invalid date");
                        }
                        value.SetAsDateTime(d);
                        break;
                    }

                    case DBXDataTypes.BooleanType:
                    {
                        if (o is JToken)
                        {
                            o = ((JToken)o).Value <Object>();
                        }
                        if (DBXTypeName.Equals("TDBXBooleanValue"))
                        {
                            value.GetAsDBXValue().SetAsBoolean(Convert.ToBoolean(o.ToString()));
                        }
                        else
                        {
                            value.SetAsBoolean(Convert.ToBoolean(o.ToString()));
                        }
                        break;
                    }

                    default:
                        throw new DBXException("Cannot convert datatype "
                                               + value.getDBXType().ToString());
                    } // switch
                }
            }
            catch (Exception ex)
            {
                throw new DBXException(ex.Message);
            }
        }
Beispiel #5
0
 /**
  * Returns this Object like a {@link JSONObject}
  */
 public TJSONObject asJSONObject()
 {
     return(DBXJSONTools.DBXParametersToJSONObject(this));
 }
            public override void execute()
            {
                if (_parameters == null)
                {
                    throw new DBXException(
                              "Parameters cannot be null in a POST request");
                }
                TJSONObject body = new TJSONObject();

                body.addPairs("_parameters", _parameters);
                Client.BeginGetRequestStream((IAsyncResult asynchronousResult) =>
                {
                    Stream postStream = Client.EndGetRequestStream(asynchronousResult);
                    byte[] postBytes  = Encoding.UTF8.GetBytes(body.ToString());
                    postStream.Write(postBytes, 0, postBytes.Length);
                    postStream.Close();
                    Client.BeginGetResponse((IAsyncResult asynchResult) =>
                    {
                        HttpWebRequest request   = (HttpWebRequest)asynchResult.AsyncState;
                        HttpWebResponse response = null;
                        try
                        {
                            response = (HttpWebResponse)request.EndGetResponse(asynchResult);
                        }
                        catch (WebException e)
                        {
                            try
                            {
                                Stream s       = ((HttpWebResponse)e.Response).GetResponseStream();
                                TextReader txt = new StreamReader(s);
                                if (s.Length > 0)
                                {
                                    connection.throwExceptionIfNeeded(JObject.Parse(txt.ReadToEnd()));
                                }
                                else
                                {
                                    throw new DBXException(e.Message);
                                }
                            }
                            catch (Exception ex)
                            {
                                if (EXCallback != null)
                                {
                                    connection.syncContext.Send(new SendOrPostCallback(x => EXCallback.DynamicInvoke(ex)), null);
                                }
                                else
                                if (Sender.BaseExCal != null)
                                {
                                    connection.syncContext.Send(new SendOrPostCallback(x => Sender.BaseExCal.DynamicInvoke(ex)), null);
                                }
                                else
                                {
                                    throw ex;
                                }
                                return;
                            }
                        }
                        finally
                        {
                            syncStop();
                        }
                        connection.setSessionIdentifier(response);
                        connection.throwExceptionIfNeeded(response);
                        if (!isThereOnlyOneStreamInOutput(command.getParameters()))
                        {
                            string resultString = null;
                            using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
                            {
                                resultString = rdr.ReadToEnd();
                                rdr.Close();
                            }
                            response.Close();
                            try
                            {
                                JObject obj        = JObject.Parse(resultString);
                                JArray arr         = obj.Value <JArray>("result");
                                int returnParIndex = 0;
                                foreach (DSRESTParameter param in command.getParameters())
                                {
                                    if ((param.Direction == DSRESTParamDirection.ReturnValue) ||
                                        (param.Direction == DSRESTParamDirection.InputOutput) ||
                                        (param.Direction == DSRESTParamDirection.Output))
                                    {
                                        DBXJSONTools.JSONtoDBX(arr[returnParIndex],
                                                               param.getValue(), param.TypeName);
                                        returnParIndex++;
                                    } // if
                                }     // for
                            }
                            catch (DBXException e)
                            {
                                throw new DBXException(e.Message);
                            }
                            if (!(_TimedOut))
                            {
                                connection.syncContext.Send(new SendOrPostCallback(x => callback.DynamicInvoke()), null);
                            }
                        }
                        else
                        {
                            Stream inputstream = response.GetResponseStream();
                            byte[] b1          = DBXTools.streamToByteArray(inputstream);
                            inputstream.Close();
                            response.Close();
                            TStream ins = new TStream(b1);
                            foreach (DSRESTParameter param in command.getParameters())
                            {
                                if ((param.Direction == DSRESTParamDirection.ReturnValue) ||
                                    (param.Direction == DSRESTParamDirection.InputOutput) ||
                                    (param.Direction == DSRESTParamDirection.Output))
                                {
                                    if (param.TypeName.StartsWith("TDBX") && param.TypeName.EndsWith("Value"))
                                    {
                                        param.getValue().GetAsDBXValue().SetAsStream(ins);
                                    }
                                    else
                                    {
                                        param.getValue().SetAsStream(ins);
                                    }
                                    if (!(_TimedOut))
                                    {
                                        connection.syncContext.Send(new SendOrPostCallback(x => callback.DynamicInvoke()), null);
                                    }
                                }
                            }
                        };
                    }, Client);
                }, Client);
                start();
            }