public object ParseValue(JToken token)
        {
            var val = token?.ToObject <object>();

            if (val != null)
            {
                var option = DataSchemaTypeConverter.ConvertTo <string>(val);

                if (Options.TryGetValue(option, out string value))
                {
                    return(value);
                }

                // Pipedrive seems to return true/false as an enum except the values are 0/1 in the option list and true/false in the data.
                if (string.Equals("true", DataSchemaTypeConverter.ConvertTo <string>(val), StringComparison.OrdinalIgnoreCase))
                {
                    option = 1.ToString();
                }

                if (string.Equals("false", DataSchemaTypeConverter.ConvertTo <string>(val), StringComparison.OrdinalIgnoreCase))
                {
                    option = 0.ToString();
                }

                if (Options.TryGetValue(option, out string boolValue))
                {
                    return(boolValue);
                }
            }
            return(null);
        }
Exemple #2
0
        public DataTableStore GetLookupTable(DataLookupSource source, List <string> columns)
        {
            var reader = new PipedriveDatasourceReader
            {
                APIToken = source.Config.ContainsKey("APIToken") ? source.Config["APIToken"] : APIToken,
                PageSize = source.Config.ContainsKey("PageSize") ? DataSchemaTypeConverter.ConvertTo <int>(source.Config["PageSize"]) : PageSize,
                List     = source.Config.ContainsKey("List") ? source.Config["List"] : source.Name
            };

            reader.Initialize(SecurityService);

            var defaultSchema = reader.GetDefaultDataSchema();

            reader.SchemaMap = new DataSchema();

            foreach (var dsi in defaultSchema.Map)
            {
                foreach (var column in columns)
                {
                    if (dsi.ColumnName.Equals(column, StringComparison.OrdinalIgnoreCase))
                    {
                        reader.SchemaMap.Map.Add(dsi.Copy());
                    }
                }
            }

            return(reader.GetDataTable());
        }
        private static T ConvertTo <T>(object value, T defaultValue)
        {
            if (value == null)
            {
                return(defaultValue);
            }

            return(DataSchemaTypeConverter.ConvertTo <T>(value));
        }
        public override DataTableStore GetDataTable(DataTableStore dt)
        {
            //Create a Schema Mapping Helper
            DataSchemaMapping mapping = new DataSchemaMapping(SchemaMap, Side);
            //Get the Rates from Yahoo
            Dictionary <string, double> rates = GetFXRates();

            //Calculate the Conversion Rate from other base currency
            double conversion_rate = 1 / rates[BaseCurrency];

            //Create a Sorted List of Rates
            var rateList = new List <string>();

            foreach (string k in rates.Keys)
            {
                rateList.Add(k);
            }
            rateList.Sort();

            //Populate the Rates Table
            foreach (string k in rateList)
            {
                var newRow = dt.NewRow();
                foreach (DataSchemaItem item in SchemaMap.GetIncludedColumns())
                {
                    string columnName = mapping.MapColumnToDestination(item);
                    switch (columnName)
                    {
                    case "Currency":
                    {
                        newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(k, item.DataType);
                        break;
                    }

                    case "Rate":
                    {
                        newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(Math.Round(rates[k] * conversion_rate, 4), item.DataType);
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                }

                if (dt.Rows.Add(newRow) == DataTableStore.ABORT)
                {
                    break;
                }
            }

            return(dt);
        }
        private static string ProcessRow(DataSchemaMapping mapping, Dictionary <string, MailChimpDataSchemaItem> schema, List <DataSchemaItem> included_columns, JToken result, DataTableStoreRow newRow)
        {
            foreach (DataSchemaItem item in included_columns)
            {
                string columnName = mapping.MapColumnToDestination(item);

                var schemaItem = schema[columnName];

                if (schemaItem.IsSubValue)
                {
                    if (result[schemaItem.ObjectName] != null)
                    {
                        if (schemaItem.IsArray)
                        {
                            var array = result[schemaItem.ObjectName] as JArray;
                            if (array != null)
                            {
                                var list = new List <string>();
                                foreach (var i in array)
                                {
                                    var o = i[schemaItem.FieldName]?.ToObject <string>();
                                    if (o != null)
                                    {
                                        list.Add(o);
                                    }
                                }

                                list.Sort();
                                newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(list.ToArray(), item.DataType);
                            }
                        }
                        else
                        {
                            foreach (var sub_item in result[schemaItem.ObjectName].Children <JProperty>())
                            {
                                if (sub_item.Name.Equals(schemaItem.FieldName))
                                {
                                    newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(sub_item.Value, item.DataType);
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (result[schemaItem.FieldName] != null)
                    {
                        newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(result[schemaItem.FieldName].ToObject <object>(), item.DataType);
                    }
                }
            }

            return(DataSchemaTypeConverter.ConvertTo <string>(result["id"].ToObject <object>()));
        }
Exemple #6
0
        public override DataTableStore GetDataTable(DataTableStore dt)
        {
            dt.AddIdentifierColumn(typeof(string));

            var uriHelper = new MailChimpUriHelper(APIKey);
            var mapping   = new DataSchemaMapping(SchemaMap, Side);
            var schema    = MailChimpDataSchema.MailChimpListSchema();

            helper.SetAuthorizationHeader(APIKey);
            var result = helper.GetRequestAsJson(uriHelper.ListServiceUrl);

            if (result["lists"] != null)
            {
                foreach (var item_row in result["lists"])
                {
                    var newRow = dt.NewRow();

                    foreach (DataSchemaItem item in SchemaMap.GetIncludedColumns())
                    {
                        string columnName = mapping.MapColumnToDestination(item);

                        var schemaItem = schema[columnName];

                        if (schemaItem.IsSubValue)
                        {
                            foreach (var six in item_row[schemaItem.ObjectName])
                            {
                                foreach (var sub_item in item_row[schemaItem.ObjectName].Children <JProperty>())
                                {
                                    if (sub_item.Name.Equals(schemaItem.FieldName))
                                    {
                                        newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(sub_item.Value, item.DataType);
                                    }
                                }
                            }
                        }
                        else
                        {
                            newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(item_row[schemaItem.FieldName], item.DataType);
                        }
                    }

                    if (dt.Rows.AddWithIdentifier(newRow, DataSchemaTypeConverter.ConvertTo <string>(item_row["id"])) == DataTableStore.ABORT)
                    {
                        break;
                    }
                }
            }

            return(dt);
        }
        public override DataTableStore GetDataTable(DataTableStore dt, DataTableKeySet keyset)
        {
            dt.AddIdentifierColumn(typeof(string));

            var uriHelper        = new MailChimpUriHelper(APIKey);
            var mapping          = new DataSchemaMapping(SchemaMap, Side);
            var schema           = MailChimpDataSchema.MailChimpMemberSchema();
            var hash_helper      = new HashHelper(HashHelper.HashType.MD5);
            var included_columns = SchemaMap.GetIncludedColumns();

            helper.SetAuthorizationHeader(APIKey);

            var target_index = mapping.MapColumnToDestination(keyset.KeyColumn);

            foreach (var key in keyset.KeyValues)
            {
                var index = key;
                if (target_index.Equals("email_address"))
                {
                    index = hash_helper.GetHashAsString(DataSchemaTypeConverter.ConvertTo <string>(key)).ToLower();
                }

                try
                {
                    var result = helper.GetRequestAsJson($"{uriHelper.ListServiceUrl}/{ListId}/members/{index}");

                    var newRow = dt.NewRow();
                    var id     = ProcessRow(mapping, schema, included_columns, result, newRow);

                    if (dt.Rows.AddWithIdentifier(newRow, id) == DataTableStore.ABORT)
                    {
                        break;
                    }
                }
                catch (WebException e)
                {
                    var response = e.Response as HttpWebResponse;
                    if (response.StatusCode == HttpStatusCode.NotFound)
                    {
                        continue;
                    }

                    throw;
                }
            }


            return(dt);
        }
        public override void UpdateItems(List <DataCompareItem> items, IDataSynchronizationStatus status)
        {
            if (items != null && items.Count > 0)
            {
                int currentItem = 0;

                foreach (var item in items)
                {
                    if (!status.ContinueProcessing)
                    {
                        break;
                    }

                    try
                    {
                        var itemInvariant = new DataCompareItemInvariant(item);
                        var filename      = itemInvariant.GetTargetIdentifier <string>();

                        Automation?.BeforeUpdateItem(this, itemInvariant, filename);

                        if (itemInvariant.Sync)
                        {
                            #region Update Item

                            //Get the Target Item Data
                            Dictionary <string, object> targetItem = UpdateItemToDictionary(Mapping, itemInvariant);

                            string tmpFile = System.IO.Path.Combine(Utility.GetTempPath(), $"{Guid.NewGuid()}.tmp");

                            try
                            {
                                System.IO.File.WriteAllBytes(tmpFile, SourceReader.GetBlobData(itemInvariant.ToDataCompareItem(), 0));
                                if (targetItem.TryGetValue("DateModified", out object value))
                                {
                                    System.IO.File.SetLastWriteTimeUtc(tmpFile, DataSchemaTypeConverter.ConvertTo <DateTime>(value).ToUniversalTime());
                                }

                                Session.PutFiles(tmpFile, filename, false, new TransferOptions {
                                    OverwriteMode = OverwriteMode.Overwrite, PreserveTimestamp = true, TransferMode = TransferMode.Binary
                                }).Check();
                            }
                            catch (Exception e)
                            {
                                Automation?.ErrorItem(this, itemInvariant, filename, e);
                                throw;
                            }
                            finally
                            {
                                if (System.IO.File.Exists(tmpFile))
                                {
                                    System.IO.File.Delete(tmpFile);
                                }
                            }

                            Automation?.AfterUpdateItem(this, itemInvariant, filename);
                            #endregion
                        }

                        ClearSyncStatus(item); //Clear the Sync Flag on Processed Rows
                    }
                    catch (SystemException e)
                    {
                        HandleError(status, e);
                    }
                    finally
                    {
                        status.Progress(items.Count, ++currentItem); //Update the Sync Progress
                    }
                }
            }
        }
        private HashSet <T> GetHashSet <T>(object value)
        {
            var result = DataSchemaTypeConverter.ConvertTo <T[]>(value);

            return(result != null ? new HashSet <T>(result) : new HashSet <T>());
        }
        public override void AddItems(List <DataCompareItem> items, IDataSynchronizationStatus status)
        {
            if (items != null && items.Count > 0)
            {
                int currentItem = 0;

                foreach (var item in items)
                {
                    if (!status.ContinueProcessing)
                    {
                        break;
                    }

                    var itemInvariant = new DataCompareItemInvariant(item);

                    try
                    {
                        //Call the Automation BeforeAddItem (Optional only required if your supporting Automation Item Events)
                        Automation?.BeforeAddItem(this, itemInvariant, null);

                        if (itemInvariant.Sync)
                        {
                            #region Add Item

                            //Get the Target Item Data
                            var targetItem       = AddItemToDictionary(Mapping, itemInvariant);
                            var targetItemToSend = new Dictionary <string, object>();

                            foreach (var k in targetItem.Keys)
                            {
                                if (!MailChimpDataSchema.ContainsKey(k))
                                {
                                    continue;
                                }

                                var mc = MailChimpDataSchema[k];

                                if (mc.ReadOnly)
                                {
                                    continue;
                                }

                                if (mc.IsSubValue)
                                {
                                    if (mc.IsArray)
                                    {
                                        if (mc.ObjectName == "tags")
                                        {
                                            // If Tags - create an array of Tags to add.
                                            targetItemToSend[mc.ObjectName] = DataSchemaTypeConverter.ConvertTo <string[]>(targetItem[k]);
                                        }
                                    }
                                    else
                                    {
                                        if (!targetItemToSend.ContainsKey(mc.ObjectName))
                                        {
                                            targetItemToSend[mc.ObjectName] = new Dictionary <string, object>();
                                        }

                                        var subValue = targetItemToSend[mc.ObjectName] as Dictionary <string, object>;

                                        subValue[mc.FieldName] = targetItem[k];
                                    }
                                }
                                else
                                {
                                    targetItemToSend[mc.FieldName] = targetItem[k];
                                }
                            }

                            var json    = JsonConvert.SerializeObject(targetItemToSend, Formatting.None);
                            var result  = WebRequestHelper.PostRequestAsJson(json, $"{ListServiceUrl}/{DataSourceReader.ListId}/members");
                            var item_id = result["id"].ToObject <string>();

                            //Call the Automation AfterAddItem (pass the created item identifier if possible)
                            Automation?.AfterAddItem(this, itemInvariant, item_id);
                        }

                        #endregion

                        ClearSyncStatus(item); //Clear the Sync Flag on Processed Rows
                    }
                    catch (WebException e)
                    {
                        Automation?.ErrorItem(this, itemInvariant, null, e);
                        HandleError(status, e);
                    }
                    catch (SystemException e)
                    {
                        Automation?.ErrorItem(this, itemInvariant, null, e);
                        HandleError(status, e);
                    }
                    finally
                    {
                        status.Progress(items.Count, ++currentItem); //Update the Sync Progress
                    }
                }
            }
        }
Exemple #11
0
        private void GetFiles(Session session, RemoteDirectoryInfo directoryInfo, bool recuirse, DataTableStore dt, DataSchemaMapping mapping, IList <DataSchemaItem> columns, DateTime?modifiedSince)
        {
            if (directoryInfo == null)
            {
                return;
            }
            if (dt.ContinueLoad != null && !dt.ContinueLoad(0))
            {
                return;
            }

            foreach (RemoteFileInfo file in directoryInfo.Files)
            {
                if (file.IsDirectory)
                {
                    if (recuirse)
                    {
                        if (file.IsParentDirectory == false && file.IsThisDirectory == false)
                        {
                            var folder = session.ListDirectory(file.FullName);
                            if (folder != null)
                            {
                                GetFiles(session, folder, recuirse, dt, mapping, columns, modifiedSince);
                            }
                        }
                    }
                }
                else
                {
                    var fileName     = Utility.StripStartSlash(file.FullName.Substring(Path.Length));
                    var fileModified = file.LastWriteTime.ToUniversalTime();

                    var newRow = dt.NewRow();

                    foreach (DataSchemaItem item in columns)
                    {
                        string columnName = mapping.MapColumnToDestination(item);

                        switch (columnName)
                        {
                        case "FullFileName":
                        {
                            if (WebFriendlyPaths)
                            {
                                newRow[item.ColumnName] = fileName != null?DataSchemaTypeConverter.ConvertTo <string>(fileName).Replace("\\", "/") : fileName;
                            }
                            else
                            {
                                newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(fileName, item.DataType);
                            }

                            break;
                        }

                        case "Path":
                        {
                            string path = Utility.StripStartSlash(System.IO.Path.GetDirectoryName(file.FullName).Substring(Path.Length));

                            if (WebFriendlyPaths)
                            {
                                newRow[item.ColumnName] = path != null?DataSchemaTypeConverter.ConvertTo <string>(path).Replace("\\", "/") : path;
                            }
                            else
                            {
                                newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(path, item.DataType);
                            }

                            break;
                        }

                        case "FileName":
                        {
                            newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(file.Name, item.DataType);
                            break;
                        }

                        case "DateCreated":
                        case "DateModified":
                        {
                            newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(fileModified, item.DataType);
                            break;
                        }

                        case "Length":
                        {
                            newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(file.Length, item.DataType);
                            break;
                        }

                        default:
                        {
                            break;
                        }
                        }
                    }

                    if (LikeOperator.LikeString(fileName, FilterPattern, Microsoft.VisualBasic.CompareMethod.Binary))
                    {
                        if (modifiedSince != null && modifiedSince.HasValue)
                        {
                            if (fileModified >= modifiedSince)
                            {
                                dt.Rows.AddWithIdentifier(newRow, file.FullName);
                            }
                        }
                        else
                        {
                            dt.Rows.AddWithIdentifier(newRow, file.FullName);
                        }
                    }
                }
            }
        }