Esempio n. 1
0
        public static void Run(RestController client, string RestHomeUri, string filter, int itemsPerPage, bool pauseBetweenPages, string repositoryName, bool printResult, string userName)
        {
            // get repository resource
            HomeDocument      home         = client.Get <HomeDocument>(RestHomeUri, null);
            Feed <Repository> repositories = home.GetRepositories <Repository>(new FeedGetOptions {
                Inline = true, Links = true
            });
            Repository repository = repositories.FindInlineEntry(repositoryName);

            // get a collection of users
            GetUsers(repository, filter, itemsPerPage, pauseBetweenPages, printResult);

            OperationsOnUser(repository, userName, itemsPerPage, pauseBetweenPages, printResult);
        }
        public static void Run(RestController client, string RestHomeUri, string repositoryName, bool printResult,
                               string groupName, string subGroupName, string userName)
        {
            // get repository resource
            HomeDocument      home         = client.Get <HomeDocument>(RestHomeUri, null);
            Feed <Repository> repositories = home.GetRepositories <Repository>(new FeedGetOptions {
                Inline = true, Links = true
            });
            Repository repository = repositories.FindInlineEntry(repositoryName);

            //create or get group
            Group testGroup = CreateOrGetGroup(repository, groupName, printResult);

            //create or get group
            Group testSubGroup = CreateOrGetGroup(repository, subGroupName, printResult);

            //create or get user
            User testUser = CreateOrGetUser(repository, userName, printResult);

            //add user/sub-group to the parent group
            AddMembersOperations(repository, testGroup, testSubGroup, testUser, printResult);
            //remove user/sub-group from the parent group
            RemoveMembersOperations(repository, testGroup, testSubGroup, testUser, printResult);
        }
        public static void Run(RestController client, string RestHomeUri, string filter, int itemsPerPage, bool pauseBetweenPages, string repositoryName, bool printResult)
        {
            HomeDocument      home         = client.Get <HomeDocument>(RestHomeUri, null);
            Feed <Repository> repositories = home.GetRepositories <Repository>(new FeedGetOptions {
                Inline = true, Links = true
            });
            Repository repository = repositories.FindInlineEntry(repositoryName);

            Console.WriteLine(String.Format("Getting formats for filter '{0}' on repository '{1}', with page size {2}", filter, repository.Name, itemsPerPage));

            // REST call to get the 1st page of formats for the specified filter
            FeedGetOptions options = new FeedGetOptions()
            {
                ItemsPerPage = itemsPerPage, Inline = true, IncludeTotal = true, Filter = String.IsNullOrEmpty(filter) ? null : filter
            };
            Feed <Format> formats = repository.GetFormats <Format>(options);

            if (formats != null)
            {
                int    totalResults = formats.Total;
                double totalPages   = formats.PageCount;
                int    docProcessed = 0;
                for (int i = 0; i < totalPages; i++)
                {
                    Console.WriteLine("**************************** PAGE " + (i + 1) + " *******************************");
                    foreach (Entry <Format> obj in formats.Entries)
                    {
                        StringBuilder values = new StringBuilder();
                        Console.WriteLine(String.Format("Name: {0}, \tExtension: {1}, \tMime: {2}",
                                                        obj.Content.GetPropertyString("name"),
                                                        obj.Content.GetPropertyString("dos_extension"),
                                                        obj.Content.GetPropertyString("mime_type")));
                        Console.WriteLine(values.ToString());
                        docProcessed++;
                    }

                    // REST call to get next page
                    if (totalResults != docProcessed)
                    {
                        formats = formats.NextPage();
                    }
                    Console.WriteLine("*******************************************************************");
                    Console.WriteLine("Page:" + (i + 1) + " Results: " + docProcessed + " out of " + totalResults + " Processed");
                    Console.WriteLine("*******************************************************************");
                    Console.WriteLine("\n\n");
                    if (pauseBetweenPages)
                    {
                        Console.WriteLine("Press 'q' to quit, 'g' to run to end, or any other key to run next page..");
                        ConsoleKeyInfo next = Console.ReadKey();
                        if (next.KeyChar.Equals('q'))
                        {
                            return;
                        }
                        if (next.KeyChar.Equals('g'))
                        {
                            pauseBetweenPages = false;
                        }
                    }
                }
            }
            if (printResult)
            {
                Console.WriteLine(formats == null ? "NULL" : formats.ToString());
            }
        }
        public static void Run(RestController client, string RestHomeUri, string query, int itemsPerPage, bool pauseBetweenPages, string repositoryName, bool printResult)
        {
            HomeDocument      home         = client.Get <HomeDocument>(RestHomeUri, null);
            Feed <Repository> repositories = home.GetRepositories <Repository>(new FeedGetOptions {
                Inline = true, Links = true
            });
            Repository repository = repositories.FindInlineEntry(repositoryName);

            Console.WriteLine(string.Format("Running DQL query '{0}' on repository '{1}', with page size {2}", query, repository.Name, itemsPerPage));

            // REST call to get the 1st page of the dql query
            Feed <PersistentObject> queryResult = repository.ExecuteDQL <PersistentObject>(query, new FeedGetOptions()
            {
                ItemsPerPage = itemsPerPage
            });

            if (queryResult != null)
            {
                int    totalResults = queryResult.Total;
                double totalPages   = queryResult.PageCount;
                int    docProcessed = 0;
                //int pageCount = queryResult.Entries.c
                for (int i = 0; i < totalPages && queryResult != null; i++)
                {
                    Console.WriteLine("**************************** PAGE " + (i + 1) + " *******************************");
                    foreach (Entry <PersistentObject> obj in queryResult.Entries)
                    {
                        StringBuilder values = new StringBuilder();
                        Console.WriteLine(string.Format("  ID: {0} \t\tName: {1}",
                                                        GetAttr(obj.Content, new string[] { "r_object_id" }),
                                                        GetAttr(obj.Content, new string[] { "object_name", "user_name", "group_name", "name" })));
                        Console.WriteLine(values.ToString());
                        docProcessed++;
                    }

                    // REST call to get next page of the dql query
                    if (totalResults != docProcessed)
                    {
                        queryResult = queryResult.NextPage();
                    }
                    Console.WriteLine("*******************************************************************");
                    Console.WriteLine("Page:" + (i + 1) + " Results: " + docProcessed + " out of " + totalResults + " Processed");
                    Console.WriteLine("*******************************************************************");
                    Console.WriteLine("\n\n");
                    if (pauseBetweenPages)
                    {
                        Console.WriteLine("Press 'q' to quit, 'g' to run to end, or any other key to run next page..");
                        ConsoleKeyInfo next = Console.ReadKey();
                        if (next.KeyChar.Equals('q'))
                        {
                            return;
                        }
                        if (next.KeyChar.Equals('g'))
                        {
                            pauseBetweenPages = false;
                        }
                    }
                }
            }
            if (printResult)
            {
                Console.WriteLine(queryResult == null ? "NULL" : queryResult.ToString());
            }
        }
        public static void Run(RestController client, string RestHomeUri, string parent, int itemsPerPage, bool pauseBetweenPages, string repositoryName, bool printResult)
        {
            HomeDocument      home         = client.Get <HomeDocument>(RestHomeUri, null);
            Feed <Repository> repositories = home.GetRepositories <Repository>(new FeedGetOptions {
                Inline = true, Links = true
            });
            Repository repository = repositories.FindInlineEntry(repositoryName);

            Console.WriteLine(String.Format("Getting types for parent '{0}' on repository '{1}', with page size {2}", parent, repository.Name, itemsPerPage));

            // REST call to get the 1st page of the types
            FeedGetOptions options = new FeedGetOptions()
            {
                ItemsPerPage = itemsPerPage, Inline = true, IncludeTotal = true
            };

            if (!String.IsNullOrEmpty(parent))
            {
                options.SetQuery("parent-type", parent);
            }
            options.SetQuery("inherited", false);
            Feed <DmType> types = repository.GetTypes(options);

            if (types != null)
            {
                int    totalResults = types.Total;
                double totalPages   = types.PageCount;
                int    docProcessed = 0;
                for (int i = 0; i < totalPages; i++)
                {
                    Console.WriteLine("**************************** PAGE " + (i + 1) + " *******************************");
                    foreach (Entry <DmType> obj in types.Entries)
                    {
                        StringBuilder values = new StringBuilder();
                        Console.WriteLine(String.Format("Name: {0}, \t\tParent: {1}, \tNon-inherited Attrs: {2}",
                                                        obj.Title,
                                                        String.IsNullOrEmpty(obj.Content.Parent) ? "" : obj.Content.Parent.Substring(obj.Content.Parent.LastIndexOf("/") + 1),
                                                        obj.Content.TypeProperties == null ? 0 : obj.Content.TypeProperties.Count));
                        Console.WriteLine(values.ToString());
                        docProcessed++;
                    }

                    // REST call to get next page of the types
                    if (totalResults != docProcessed)
                    {
                        types = types.NextPage();
                    }
                    Console.WriteLine("*******************************************************************");
                    Console.WriteLine("Page:" + (i + 1) + " Results: " + docProcessed + " out of " + totalResults + " Processed");
                    Console.WriteLine("*******************************************************************");
                    Console.WriteLine("\n\n");
                    if (pauseBetweenPages)
                    {
                        Console.WriteLine("Press 'q' to quit, 'g' to run to end, or any other key to run next page..");
                        ConsoleKeyInfo next = Console.ReadKey();
                        if (next.KeyChar.Equals('q'))
                        {
                            return;
                        }
                        if (next.KeyChar.Equals('g'))
                        {
                            pauseBetweenPages = false;
                        }
                    }
                }
            }
            if (printResult)
            {
                Console.WriteLine(types == null ? "NULL" : types.ToString());
            }
        }
        public static void Run(RestController client, string RestHomeUri, string repositoryName)
        {
            HomeDocument      home         = client.Get <HomeDocument>(RestHomeUri, null);
            Feed <Repository> repositories = home.GetRepositories <Repository>(new FeedGetOptions {
                Inline = true, Links = true
            });
            Repository repository = repositories.FindInlineEntry(repositoryName);

            BatchCapabilities batchCapabilities = repository.GetBatchCapabilities();

            Console.WriteLine("Batch capabilities for this repository:");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("  - transactional or non-transactional? {0}", batchCapabilities.Transactions);
            Console.WriteLine("  - sequential or non-sequential?       {0}", batchCapabilities.Sequence);
            Console.WriteLine("  - fail on error or continue on error? {0}", batchCapabilities.OnError);
            Console.WriteLine("  - all resources are batch-able except these [{0}] ", String.Join(",", batchCapabilities.NonBatchableResources.ToArray <string>()));
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Press any key to continue batch test..");
            Console.ReadKey();

            Console.WriteLine(String.Format("\r\nPreparing data for the batch.."));
            User currentUser = repository.GetCurrentUser(new SingleGetOptions());

            Console.WriteLine("\t - Fetched current user: "******"user_name"));
            Cabinet homeCabinet = currentUser.GetHomeCabinet(new SingleGetOptions());

            Console.WriteLine("\t - Fetched home cabinet: " + homeCabinet.GetPropertyString("object_name"));

            // start a batch - create 1 folder & 1 document under home cabinet + get dql + create a new relation
            Console.WriteLine("\r\nReady to create a batch with 5 operations:");
            string folderNamePrefix = "BatchTestFolder-";

            Console.WriteLine("\t - (1) create a new folder under home cabinet with name prefix: " + folderNamePrefix);
            string docNamePrefix = "BatchTestDoc-";

            Console.WriteLine("\t - (2) create a new document under home cabinet with name: " + docNamePrefix);
            string dql = "select * from dm_group";

            Console.WriteLine("\t - (3) execute a dql query: " + dql);
            Console.WriteLine("\t - (4) get all relations");
            Console.WriteLine("\t - (5) create a new cabinet with empty name (should fail)");
            Console.WriteLine("Press any key to run batch..");
            Console.ReadKey();

            List <KeyValuePair <string, object> > dqlParams = new List <KeyValuePair <string, object> >();

            dqlParams.Add(new KeyValuePair <string, object>("dql", dql));
            string dqlUri = UriUtil.BuildUri(LinkRelations.FindLinkAsString(repository.Links, LinkRelations.DQL.Rel), dqlParams);

            Batch batch = Batch.CreateFromBuilder()
                          .Description("a sample batch with 4 operations")
                          .BatchSpec(new BatchSpec {
                Transactional = false, Sequential = false, FailOnError = false, ReturnRequest = false
            })
                          .AddOperation <Folder>(
                "batch-opt-1", homeCabinet, LinkRelations.FOLDERS.Rel, "POST", ObjectUtil.NewRandomFolder(folderNamePrefix, "dm_folder")
                )
                          .AddOperation <Document>(
                "batch-opt-2", homeCabinet, LinkRelations.DOCUMENTS.Rel, "POST", ObjectUtil.NewRandomDocument(docNamePrefix, "dm_document")
                )
                          .AddOperation(
                "batch-opt-3", dqlUri, "GET"
                )
                          .AddOperation(
                "batch-opt-4", homeCabinet, LinkRelations.RELATIONS.Rel, "GET"
                )
                          .AddOperation(
                "batch-opt-5", repository, LinkRelations.CABINETS.Rel, "POST", new Cabinet()
                )
                          .Build();

            Batch result = repository.CreateBatch(batch);

            Console.WriteLine("\r\nPrinting batch result..");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine(String.Format(" - description [{0}]", result.Description));
            Console.WriteLine(String.Format(" - owner       [{0}]", result.Owner));
            Console.WriteLine(String.Format(" - state       [{0}]", result.State));
            Console.WriteLine(String.Format(" - submitted   [{0}]", result.Submitted));
            Console.WriteLine(String.Format(" - started     [{0}]", result.Started));
            Console.WriteLine(String.Format(" - finished    [{0}]", result.Finished));

            foreach (BatchOperation operation in result.Operations)
            {
                Console.WriteLine(String.Format("\r\n - operation    [{0}]", operation.Id));
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(String.Format("\t--> state        [{0}]", operation.State));
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine(String.Format("\t--> started      [{0}]", operation.Started));
                Console.WriteLine(String.Format("\t--> finished     [{0}]", operation.Finished));
                Console.WriteLine("\t--> response");
                Console.WriteLine(String.Format("\t-----------> status  [{0}]", operation.Response.Status));

                if (operation.Response.HasError())
                {
                    Error error = operation.Response.GetError();
                    Console.WriteLine(String.Format("\t-----------> entity  [{0}:{1}]", error.Code, error.Message));
                }
                else
                {
                    if (operation.Id.Equals("batch-opt-1") || operation.Id.Equals("batch-opt-2"))
                    {
                        PersistentObject po = operation.Response.GetObject <PersistentObject>();
                        Console.WriteLine(String.Format("\t-----------> entity  [object of URI {0}]", LinkRelations.FindLinkAsString(po.Links, LinkRelations.SELF.Rel)));
                    }
                    else if (operation.Id.Equals("batch-opt-3") || operation.Id.Equals("batch-opt-4"))
                    {
                        Feed <PersistentObject> feed = operation.Response.GetObject <Feed <PersistentObject> >();
                        Console.WriteLine(String.Format("\t-----------> entity  [feed with title {0} and entry count {1}]", feed.Title, feed.Entries.Count));
                    }
                }
            }
            Console.ForegroundColor = ConsoleColor.White;
        }
        public static void Run(RestController client, string RestHomeUri, string terms, int itemsPerPage, bool pauseBetweenPages, string repositoryName, bool printResult)
        {
            HomeDocument      home         = client.Get <HomeDocument>(RestHomeUri, null);
            Feed <Repository> repositories = home.GetRepositories <Repository>(new FeedGetOptions {
                Inline = true, Links = true
            });
            Repository repository = repositories.FindInlineEntry(repositoryName);

            Console.WriteLine(String.Format("Running full-text search for terms '{0}' on repository '{1}', with page size {2}", terms, repository.Name, itemsPerPage));

            // REST call to get the 1st page of the search result
            SearchOptions options = new SearchOptions()
            {
                ItemsPerPage = itemsPerPage, Inline = false, IncludeTotal = true, SearchQuery = terms
            };
            Feed <PersistentObject> searchResult = repository.ExecuteSimpleSearch <PersistentObject>(options);

            if (searchResult != null)
            {
                int    totalResults = searchResult.Total;
                double totalPages   = searchResult.PageCount;
                int    docProcessed = 0;
                for (int i = 0; i < totalPages; i++)
                {
                    Console.WriteLine("**************************** PAGE " + (i + 1) + " *******************************");
                    foreach (Entry <PersistentObject> obj in searchResult.Entries)
                    {
                        StringBuilder values = new StringBuilder();
                        Console.WriteLine(String.Format("Score: {0:0.000},   ID: {1},   Name: {2},    Highlight:\n {3}",
                                                        obj.Score,
                                                        obj.Id,
                                                        obj.Title,
                                                        obj.Summary));
                        Console.WriteLine(values.ToString());
                        docProcessed++;
                    }

                    // REST call to get next page of the search
                    if (totalResults != docProcessed)
                    {
                        searchResult = searchResult.NextPage();
                    }
                    Console.WriteLine("*******************************************************************");
                    Console.WriteLine("Page:" + (i + 1) + " Results: " + docProcessed + " out of " + totalResults + " Processed");
                    Console.WriteLine("*******************************************************************");
                    Console.WriteLine("\n\n");
                    if (pauseBetweenPages)
                    {
                        Console.WriteLine("Press 'q' to quit, 'g' to run to end, or any other key to run next page..");
                        ConsoleKeyInfo next = Console.ReadKey();
                        if (next.KeyChar.Equals('q'))
                        {
                            return;
                        }
                        if (next.KeyChar.Equals('g'))
                        {
                            pauseBetweenPages = false;
                        }
                    }
                }
            }
            if (printResult)
            {
                Console.WriteLine(searchResult == null ? "NULL" : searchResult.ToString());
            }
        }