Пример #1
0
        public void GeteqAllValid()
        {
            DbxReset();

            // Prepare
            DbxObject o;
            var os = new DbxObjects();
            var output = new ArrayList();
            for (var i = 0; i < 10; i++) {
                os.Add(o = Dbx.Create("test", "system", new Dictionary<string, object>() { { "string", Util.GenerateSalt(16) }, { "number", 1 }, { "boolean", true }, { "stringenum", "a" } }));
                output.Add(new Dictionary<string, object>() { { "result", "ok" }, { "object", o.Output("system") } });
            }

            var expected = Util.JsonSerialize(output);

            // Execute
            var target = new Geteq();
            target.type = "test";
            target.locate = new Dictionary<string, ArrayList>();
            var actual = Util.JsonSerialize(target.Execute());

            // Check
            Assert.AreEqual(expected, actual);

            DbxReset();
        }
Пример #2
0
        public DbxObjects LocateEq(string type, List<Locate> locate, int skip = 0, int take = 100, string order = null)
        {
            // TODO: Migrate functionality into QueryEq
            // Must be fast!
            /* Broken!
            var hash = type + "|" + order + "|";
            foreach (var item in locate) {
                hash += item.Hash();
            }
            */
            // If we don't have it yet - load it from the database
            //var objs = (DbxObjects)CacheQ[hash]; // Done like this to handle thread sync issues
            //if (null == objs) {
            //    lock (CacheQ) {
            // Find appropriate objects
            var uids = Index.UidsMatching(type, locate); // TODO: Stop insanely large queries

            // Load each object
            var objs = new DbxObjects();
            foreach (var uid in uids) objs.Add(Uid(uid));

            // Sort objects
            if (null != order) objs.SortBy(order); // TODO: Untested - Unit tests

            // Store in cache
            var expires = new CacheItemPolicy();
            expires.AbsoluteExpiration = DateTime.Now.AddMinutes(2);
            //        CacheQ.Add(hash, objs, expires);
            //    }
            //}

            // Duplicate
            objs = new DbxObjects(objs);

            // Limit to skip
            if (skip > 0 && skip < objs.Count) { // TODO: Untested - Unit tests
                objs.RemoveRange(0, skip - 1);
            } else if (skip > 0) {
                return new DbxObjects();
            }

            // Limit to take
            if (objs.Count > take) objs.RemoveRange(take, objs.Count - take); // TODO: Untested - unit tests

            return objs;
        }
Пример #3
0
        private object CustomFunctionForkChild(Dictionary<string, object> parameters, string sourceuid)
        {
            if (string.IsNullOrEmpty(sourceuid)) throw new UserErrorException("auth.prerequisite", "Not yet authenticated");
            if (!parameters.ContainsKey("source")) throw new UserErrorException("parameter.missing", "source; Required field missing");
            var sourceChildUid = (string)parameters["source"];

            // Check target is valid
            var demoChildUids = new List<string>() {
                "child$aF~vwOR9kGH4~uKihTHEbswx8z" // Zoe Quinn
            };
            if (!demoChildUids.Contains(sourceChildUid)) throw new UserErrorException("parameter.invalid", "sourceUid; Unapproved target object");

            // Child
            var sourceChild = instance.Uid(sourceChildUid);
            var properties = sourceChild.GetAllProps("system");
            var previousAdminUserUID = ((ArrayList)properties["admins"])[0];
            properties["admins"] = new ArrayList() { sourceuid }; // Set admin to current user
            properties["members"] = new ArrayList(); // Wipe all member access
            properties["lastProgressReport"] = DateTime.UtcNow.ToString("o"); // Reset progress report counter
            properties["subscriptionStartDate"] = DateTime.UtcNow.ToString("o"); // Starting today
            properties["subscriptionEndDate"] = DateTime.UtcNow.AddMonths(+1).ToString("o"); // 1 month free
            properties["subscriptionTokens"] = new ArrayList(); // No tokens used
            properties["subscriptionAutoBill"] = false; // Disable auto-bill
            properties["subscriptionCrn"] = 0; // Reset any CRN value
            properties["demo"] = true; // Reset any CRN value
            var cloneChild = instance.Create("child", "system", properties);

            // Extras (the order below is important)
            var collection = new DbxObjects();
            collection.AddRange(instance.LocateEq("member", Locate.GenerateSimple("child", sourceChildUid)));
            collection.AddRange(instance.LocateEq("education", Locate.GenerateSimple("child", sourceChildUid)));
            collection.AddRange(instance.LocateEq("strength", Locate.GenerateSimple("child", sourceChildUid)));
            collection.AddRange(instance.LocateEq("weakness", Locate.GenerateSimple("child", sourceChildUid)));
            collection.AddRange(instance.LocateEq("diagnosis", Locate.GenerateSimple("child", sourceChildUid)));
            collection.AddRange(instance.LocateEq("journey", Locate.GenerateSimple("child", sourceChildUid)));
            collection.AddRange(instance.LocateEq("management", Locate.GenerateSimple("child", sourceChildUid)));
            collection.AddRange(instance.LocateEq("focus", Locate.GenerateSimple("child", sourceChildUid)));
            //collection.AddRange(instance.LocateEq("library", Locate.GenerateSimple("child", sourceChildUid))); // Do not clone library
            //DbxObjects posts;
            //collection.AddRange(posts = instance.LocateEq("post", Locate.GenerateSimple("child", sourceChildUid)));
            //var postList = new List<object>();
            // foreach (var post in posts) postList.Add(post.Uid);
            //collection.AddRange(instance.LocateEq("comment", new List<Locate>() { new Locate("post", postList, "1") }));

            // Create conversion table
            var conversion = new Dictionary<string, Dictionary<object, object>>();
            conversion["user"] = new Dictionary<object, object>() { { previousAdminUserUID, sourceuid } };
            conversion["child"] = new Dictionary<object, object>() { { sourceChild.Uid, cloneChild.Uid } };
            conversion["invited"] = new Dictionary<object, object>() { { true, false } };

            // Clone
            foreach (var sourceItem in collection) {
                // Load properties
                properties = sourceItem.GetAllProps("system");

                // For each conversion property...
                foreach (var conversionField in conversion) {
                    // If object has property to be converted...
                    if (properties.ContainsKey(conversionField.Key)) {
                        // And the property has a value that is to be converted... (skipping nulls of course)
                        if (null != properties[conversionField.Key] && conversionField.Value.ContainsKey(properties[conversionField.Key])) {
                            // Convert it!
                            properties[conversionField.Key] = conversionField.Value[properties[conversionField.Key]];
                        }
                    }
                }

                // Create clone
                try {
                    var cloneItem = instance.Create(sourceItem.Type, "system", properties);

                    // Add uid conversion
                    if (!conversion.ContainsKey(sourceItem.Type)) conversion[sourceItem.Type] = new Dictionary<object, object>();
                    conversion[sourceItem.Type][sourceItem.Uid] = cloneItem.Uid;
                } catch (UserErrorException) { }// If a target object no longer matches validation rules - skip it
            }

            return cloneChild.Uid;
        }
Пример #4
0
        public DbxObjects Older(string type, DateTime datetime)
        {
            // Find appropriate objects
            var uids = Index.UidsOlder(type, datetime);

            // Load objects
            var objs = new DbxObjects();
            foreach (var uid in uids) objs.Add(Uid(uid));

            return objs;
        }