Example #1
0
        /// <summary>
        /// Add node object to the database and Nodes collection.
        /// </summary>
        /// <param name="newToDoItem">Node object</param>
        public void AddNode(Node node)
        {
            // Add the node object to the data context.
            appDB.Nodes.InsertOnSubmit(node);

            // Save changes to the database.
            appDB.SubmitChanges();

            // Add the node object to the Nodes observable collection.
            Nodes.Add(node);
        }
Example #2
0
        /// <summary>
        /// Remove a node object from the database and Node collection.
        /// </summary>
        /// <param name="toDoForDelete"></param>
        public void DeleteNode(Node node)
        {
            // Remove the node object from the Nodes observable collection.
            Nodes.Remove(node);

            // Remove the node object from the data context.
            appDB.Nodes.DeleteOnSubmit(node);

            // Save changes to the database.
            appDB.SubmitChanges();
        }
Example #3
0
        /// <summary>
        /// Reads node data through web service.
        /// 
        /// Incomplete!
        /// </summary>
        /// <param name="nid"></param>
        /// <param name="onFinish"></param>
        /// <param name="onError"></param>
        public void GetNode(int nid, Action<Node> onFinish = null, Action<Exception> onError = null)
        {
            // Check network availability.
            if(!DeviceNetworkInformation.IsNetworkAvailable)
            {
                onError(new Exception("Network is unavailable!"));
            }

            WebClient webClient = new WebClient();

            String requestString = String.Format("{0}node/{1}.xml", this.serviceUri, nid);

            webClient.OpenReadCompleted += delegate (object sender, OpenReadCompletedEventArgs e)
            {
                try
                {
                    if (e.Error != null)
                    {
                        if (onError != null)
                        {
                            onError(e.Error);
                        }
                    }

                    // TODO: Extract other node fields.
                    XElement resultXml = XElement.Load(e.Result);
                    XElement xEl = resultXml.Element("title");

                    Node node = new Node();
                    node.Title = xEl.Value;

                    if (onFinish != null)
                    {
                        onFinish(node);
                    }
                }
                catch (Exception ex)
                {
                    if (onError != null)
                    {
                        onError(ex);
                    }
                }
            };

            // Call the OpenReadAsyc to make a GET request.
            webClient.OpenReadAsync(new Uri(requestString));
        }
Example #4
0
        void onFileCreatedRemotely(Model.File file)
        {
            App.ViewModel.AddFile(file);

            Node node = new Node();
            node.Title = file.FileName;
            node.Type = AppResources.DrupalContentType;
            node.File = file;

            RESTService service = new RESTService(AppResources.RESTServiceUri);
            service.CreateNode(node, onNodeCreatedRemotely, onError);
        }
Example #5
0
        /// <summary>
        /// Creates a node in Drupal remotelly.
        /// 
        /// To allow creating nodes through Services and if you don't use authentication 
        /// make sure that anonymous user has necessary Drupal permission.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="onFinish"></param>
        /// <param name="onError"></param>
        public void CreateNode(Node node, Action<Node> onFinish = null, Action<Exception> onError = null)
        {
            // Check network availability.
            if (!DeviceNetworkInformation.IsNetworkAvailable)
            {
                onError(new Exception("Network is unavailable!"));
            }

            WebClient webClient = new WebClient();

            webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            webClient.Headers[HttpRequestHeader.Accept] = "application/xml";

            String requestString = String.Format("{0}node", this.serviceUri);

            String requestQuery = String.Format("title={0}&type={1}&catchsmile_image[und][][fid]={2}&catchsmile_image[und][][list]=1", Uri.EscapeDataString(node.Title), node.Type, node.File.Fid);

            webClient.UploadStringCompleted += delegate(object sender, UploadStringCompletedEventArgs e)
            {
                try
                {
                    if (e.Error != null)
                    {
                        if (onError != null)
                        {
                            onError(e.Error);
                        }
                    }

                    /* Response example:
                       <?xml version="1.0" encoding="utf-8"?>
                       <result>
                          <nid>26</nid>
                          <uri>http://drupal7/endpoint1/node/26</uri>
                       </result>
                     */

                    XElement resultXml = XElement.Parse(e.Result);

                    node.Nid = int.Parse(resultXml.Element("nid").Value);
                    node.Uri = resultXml.Element("uri").Value;

                    if (onFinish != null)
                    {
                        onFinish(node);
                    }
                }
                catch (Exception ex)
                {
                    if (onError != null)
                    {
                        onError(ex);
                    }
                }
            };

            // Call the UploadStringAsync to make a POST request.
            webClient.UploadStringAsync(new Uri(requestString), "POST", requestQuery);
        }