예제 #1
0
        /// <summary>
        /// Creates a new store based on the given template
        /// </summary>
        /// <param name="template">Template</param>
        /// <param name="callback">Callback</param>
        /// <param name="state">State to pass to the callback</param>
        /// <remarks>
        /// <para>
        /// Template must inherit from <see cref="BaseSesameTemplate"/>
        /// </para>
        /// </remarks>
        public virtual void CreateStore(IStoreTemplate template, AsyncStorageCallback callback, object state)
        {
            if (template is BaseSesameTemplate)
            {
                // First we need to store the template as a new context in the SYSTEM repository
                Dictionary <String, String> createParams   = new Dictionary <string, string>();
                BaseSesameTemplate          sesameTemplate = (BaseSesameTemplate)template;

                if (template.Validate().Any())
                {
                    callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, new RdfStorageException("Template is not valid, call Validate() on the template to see the list of errors")), state);
                    return;
                }

                IGraph g = sesameTemplate.GetTemplateGraph();
                createParams.Add("context", sesameTemplate.ContextNode.ToString());
                HttpWebRequest request = this.CreateRequest(this._repositoriesPrefix + SesameServer.SystemRepositoryID + "/statements", "*/*", "POST", createParams);

                request.ContentType = MimeTypesHelper.NTriples[0];
                NTriplesWriter ntwriter = new NTriplesWriter();

                this.EnsureSystemConnection();
                this._sysConnection.SaveGraphAsync(request, ntwriter, g, (sender, args, st) =>
                {
                    if (args.WasSuccessful)
                    {
                        // Then we need to declare that said Context is of type rep:RepositoryContext
                        Triple repoType = new Triple(sesameTemplate.ContextNode, g.CreateUriNode("rdf:type"), g.CreateUriNode("rep:RepositoryContext"));
                        this._sysConnection.UpdateGraph(String.Empty, repoType.AsEnumerable(), null, (sender2, args2, st2) =>
                        {
                            if (args.WasSuccessful)
                            {
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, template), state);
                            }
                            else
                            {
                                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, StorageHelper.HandleError(args.Error, "creating a new Store in")), state);
                            }
                        }, st);
                    }
                    else
                    {
                        callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, template, StorageHelper.HandleError(args.Error, "creating a new Store in")), state);
                    }
                }, state);
            }
            else
            {
                callback(this, new AsyncStorageCallbackArgs(AsyncStorageOperation.CreateStore, template.ID, template, new RdfStorageException("Invalid template, templates must derive from BaseSesameTemplate")), state);
            }
        }
예제 #2
0
        /// <summary>
        /// Creates a new Store based on the given template
        /// </summary>
        /// <param name="template">Template</param>
        /// <returns></returns>
        /// <remarks>
        /// <para>
        /// Templates must inherit from <see cref="BaseSesameTemplate"/>
        /// </para>
        /// </remarks>
        public virtual bool CreateStore(IStoreTemplate template)
        {
            if (template is BaseSesameTemplate)
            {
                try
                {
                    Dictionary <String, String> createParams   = new Dictionary <string, string>();
                    BaseSesameTemplate          sesameTemplate = (BaseSesameTemplate)template;
                    if (template.Validate().Any())
                    {
                        throw new RdfStorageException("Template is not valid, call Validate() on the template to see the list of errors");
                    }
                    IGraph g = sesameTemplate.GetTemplateGraph();

                    // Firstly we need to save the Repository Template as a new Context to Sesame
                    createParams.Add("context", sesameTemplate.ContextNode.ToString());
                    HttpWebRequest request = this.CreateRequest(this._repositoriesPrefix + SesameServer.SystemRepositoryID + "/statements", "*/*", "POST", createParams);

                    request.ContentType = MimeTypesHelper.NTriples[0];
                    NTriplesWriter ntwriter = new NTriplesWriter();
                    ntwriter.Save(g, new StreamWriter(request.GetRequestStream()));

                    Tools.HttpDebugRequest(request);

                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        Tools.HttpDebugResponse(response);
                        // If we get then it was OK
                        response.Close();
                    }

                    // Then we need to declare that said Context is of type rep:RepositoryContext
                    Triple repoType = new Triple(sesameTemplate.ContextNode, g.CreateUriNode("rdf:type"), g.CreateUriNode("rep:RepositoryContext"));
                    this.EnsureSystemConnection();
                    this._sysConnection.UpdateGraph(String.Empty, repoType.AsEnumerable(), null);

                    return(true);
                }
                catch (WebException webEx)
                {
                    throw StorageHelper.HandleHttpError(webEx, "creating a new Store in");
                }
            }
            else
            {
                throw new RdfStorageException("Invalid template, templates must derive from BaseSesameTemplate");
            }
        }