Пример #1
0
        internal static Document PostDocument(FullSyncPolicy policy, Database database, IDictionary <string, object> newProperties)
        {
            Init();
            Func <Database, IDictionary <string, object>, Document> postDocumentOp = null;

            if (fullSyncPolicies.TryGetValue(policy, out postDocumentOp))
            {
                return(postDocumentOp(database, newProperties));
            }
            else
            {
                throw new ArgumentException(C8oExceptionMessage.UnknownFullSyncPolicy(policy));
            }
        }
Пример #2
0
 public static FullSyncPolicy GetFullSyncPolicy(String value)
 {
     if (value != null)
     {
         FullSyncPolicy[] fullSyncPolicyValues = FullSyncPolicy.Values();
         foreach (FullSyncPolicy fullSyncPolicy in fullSyncPolicyValues)
         {
             if (fullSyncPolicy.value.Equals(value))
             {
                 return(fullSyncPolicy);
             }
         }
     }
     return(NONE);
 }
Пример #3
0
        //*** PostDocument ***//

        public async override Task <object> HandlePostDocumentRequest(string databaseName, FullSyncPolicy fullSyncPolicy, IDictionary <string, object> parameters)
        {
            var fullSyncDatabase = await GetOrCreateFullSyncDatabase(databaseName);

            // Gets the subkey separator parameter
            string subkeySeparatorParameterValue = C8oUtils.GetParameterStringValue(parameters, FullSyncPostDocumentParameter.SUBKEY_SEPARATOR.name, false);

            if (subkeySeparatorParameterValue == null)
            {
                subkeySeparatorParameterValue = ".";
            }

            // Filters and modifies wrong properties
            var newProperties = new Dictionary <string, object>();

            foreach (var parameter in parameters)
            {
                string parameterName = parameter.Key;

                if (parameterName.Equals(C8oFullSync.FULL_SYNC__REV))
                {
                    newProperties[parameterName] = parameter.Value;
                }
                else if (!parameterName.StartsWith("__") && !parameterName.StartsWith("_use_"))
                {
                    // Retrieves ???
                    var objectParameterValue = C8oUtils.GetParameterJsonValue(parameter);
                    //Manager.SharedInstance. ow = null;

                    if (objectParameterValue is JObject)
                    {
                        objectParameterValue = (objectParameterValue as JObject).ToObject <Dictionary <string, object> > ();
                    }

                    // Checks if the parameter name is splittable
                    var paths = parameterName.Split(new String[] { subkeySeparatorParameterValue }, StringSplitOptions.None); // Regex.Split(parameterName, subkeySeparatorParameterValue);
                    if (paths.Length > 1)
                    {
                        // The first substring becomes the key
                        parameterName = paths[0];
                        // Next substrings create a hierarchy which will becomes json subkeys
                        int count = paths.Length - 1;
                        while (count > 0)
                        {
                            var tmpObject = new Dictionary <string, object>();
                            tmpObject[paths[count]] = objectParameterValue;
                            objectParameterValue    = tmpObject;
                            count--;
                        }
                        if (newProperties.ContainsKey(parameterName) && newProperties[parameterName] is IDictionary <string, object> )
                        {
                            FullSyncUtils.MergeProperties(objectParameterValue as IDictionary <string, object>, newProperties[parameterName] as IDictionary <string, object>);
                        }
                    }

                    newProperties[parameterName] = objectParameterValue;
                }
            }

            var    createdDocument = C8oFullSyncCblEnum.PostDocument(fullSyncPolicy, fullSyncDatabase.Database, newProperties);
            string documentId      = createdDocument.Id;
            string currentRevision = createdDocument.CurrentRevisionId;

            return(new FullSyncDocumentOperationResponse(documentId, currentRevision, true));
        }
Пример #4
0
        private async Task <JObject> ApplyPolicy(string fullSyncDatatbaseName, JObject document, FullSyncPolicy fullSyncPolicy)
        {
            if (fullSyncPolicy == FullSyncPolicy.NONE)
            {
            }
            else if (fullSyncPolicy == FullSyncPolicy.CREATE)
            {
                document.Remove("_id");
                document.Remove("_rev");
            }
            else
            {
                string docid = document["_id"].ToString();

                if (docid != null)
                {
                    if (fullSyncPolicy == FullSyncPolicy.OVERRIDE)
                    {
                        string rev = await GetDocumentRev(fullSyncDatatbaseName, docid);

                        if (rev != null)
                        {
                            document["_rev"] = rev;
                        }
                    }
                    else if (fullSyncPolicy == FullSyncPolicy.MERGE)
                    {
                        var dbDocument = await HandleGetDocumentRequest(fullSyncDatatbaseName, docid) as JObject;

                        if (dbDocument["_id"] != null)
                        {
                            document.Remove("_rev");
                            Merge(dbDocument, document);
                            document = dbDocument;
                        }
                    }
                }
            }

            document.Remove("_c8oMeta");

            return(document);
        }
Пример #5
0
        public async override Task <object> HandlePostDocumentRequest(string fullSyncDatatbaseName, FullSyncPolicy fullSyncPolicy, IDictionary <string, object> parameters)
        {
            await CheckDatabase(fullSyncDatatbaseName);

            var options = new Dictionary <string, object>();

            foreach (var parameter in parameters)
            {
                var isUse = RE_FS_USE.Match(parameter.Key);
                if (isUse.Success)
                {
                    if (isUse.Groups[1].Success)
                    {
                        options[isUse.Groups[1].Value] = parameter.Value;
                    }
                    parameters.Remove(parameter.Key);
                }
            }

            string uri = HandleQuery(GetDatabaseUrl(fullSyncDatatbaseName), options);

            var request = HttpWebRequest.CreateHttp(uri);

            request.Method = "POST";

            // Gets the subkey separator parameter
            string subkeySeparatorParameterValue = C8oUtils.PeekParameterStringValue(parameters, FullSyncPostDocumentParameter.SUBKEY_SEPARATOR.name, false);

            if (subkeySeparatorParameterValue == null)
            {
                subkeySeparatorParameterValue = ".";
            }

            var postData = new JObject();

            foreach (KeyValuePair <string, object> kvp in parameters)
            {
                var      obj   = postData;
                string   key   = kvp.Key;
                String[] paths = key.Split(subkeySeparatorParameterValue.ToCharArray());

                if (paths.Length > 1)
                {
                    for (int i = 0; i < paths.Length - 1; i++)
                    {
                        string path = paths[i];
                        if (obj[path] is JObject)
                        {
                            obj = obj[path] as JObject;
                        }
                        else
                        {
                            obj = (obj[path] = new JObject()) as JObject;
                        }
                    }

                    key = paths[paths.Length - 1];
                }
                obj[key] = JToken.FromObject(kvp.Value);
            }

            postData = await ApplyPolicy(fullSyncDatatbaseName, postData, fullSyncPolicy);

            return(await Execute(request, postData));
        }
Пример #6
0
        //*** PostDocument ***//

        public abstract Task <object> HandlePostDocumentRequest(string fullSyncDatatbaseName, FullSyncPolicy fullSyncPolicy, IDictionary <string, object> parameters);