예제 #1
0
        public MainWindow()
        {
            InitializeComponent();

            _places = new PlaceContainer();
            foreach (var pers in Persons.Users)
            {
                _places.AddPerson(pers.Schedule[Time.T0]);
            }

            _layout = new Layout(cvs.Width, cvs.Height, _places.GetPlaces);
            cvs.Children.Add(_layout.Visuals);

            _way = new Way(_places.GetWay(0, 0));
            _layout.Drawables.Add(_way);
            _layout.Refresh();

            foreach (var i in Places.Names)
            {
                FromBox.Items.Add(i);
                ToBox.Items.Add(i);
            }

            FromBox.SelectedIndex = 0;
            ToBox.SelectedIndex   = 0;
        }
        public virtual PlaceDataObject Save(
            PlaceContainer theDataObjectToSave,
            LambdaExpression securityFilterExpression = null,
            List <string> includes = null,
            bool skipSecurity      = false,
            Parameters parameters  = null)
        {
            var uri     = new Uri(_serviceUrl);
            var request = WebRequest.Create(uri);

            request.ContentType = "application/x-www-form-urlencoded";
            request.Method      = "POST";
            request.Timeout     = GetRequestTimeout(parameters);

            using (var stream = request.GetRequestStream())
            {
                string objectToSaveAsString = JsonConvert.SerializeObject(theDataObjectToSave, JsonSerializerSettings);

                var byteArray = Encoding.UTF8.GetBytes("entity=" + HttpUtility.UrlEncode(objectToSaveAsString));
                stream.Write(byteArray, 0, byteArray.Length);

                if (includes != null && includes.Any())
                {
                    byteArray = Encoding.UTF8.GetBytes("&include=" + HttpUtility.UrlEncode(String.Join(",", includes)));
                    stream.Write(byteArray, 0, byteArray.Length);
                }

                // get user token for currently authenticated user if any
                var userToken = UserIdentity.UserToken;

                if (!String.IsNullOrEmpty(userToken))
                {
                    byteArray = Encoding.UTF8.GetBytes("&_user_token=" + userToken);
                    stream.Write(byteArray, 0, byteArray.Length);
                }

                // Add custom query params
                WriteQueryParams(stream);

                try
                {
                    using (var response = request.GetResponse())
                    {
                        if (response == null)
                        {
                            throw new PulpException("Unable to get the response from " + uri.ToString());
                        }

                        using (var responseStream = response.GetResponseStream())
                        {
                            if (responseStream == null)
                            {
                                throw new PulpException("Unable to get the response stream from " + uri.ToString());
                            }

                            var sr        = new StreamReader(responseStream);
                            var container = JsonConvert.DeserializeObject <PlaceContainer>(sr.ReadToEnd(), JsonSerializerSettings);

                            if (container == null)
                            {
                                if (theDataObjectToSave.ExtractPlace().IsMarkedForDeletion)
                                {
                                    return(null);
                                }
                                else
                                {
                                    throw new GOServerException("No Place in the response to SDK DataProvider Save(). Not expecting a null response here!");
                                }
                            }
                            else
                            {
                                container.ObjectsDataSet.EnsureInitialized();
                                container.ObjectsDataSet.ReconstructIndexes();

                                response.Close();
                                return(container.ExtractPlace());
                            }
                        }
                    }
                }
                catch (WebException we)
                {
                    // See if we can decode GOServerException
                    var error = GOServerException.FromWebResponse(we);

                    if (error != null)
                    {
                        throw error;
                    }

                    throw;
                }
            }
        }