public override void UpdateItems(List<DataCompareItem> items, IDataSynchronizationStatus status) { if (items != null && items.Count > 0) { int currentItem = 0; //Pass an array of ints to DataCompareItemInvariant to identify identifier fields to copy foreach (var item in items.Select(p => new DataCompareItemInvariant(p))) { if (!status.ContinueProcessing) break; try { //Example: Get the item ID from the Target Identifier Store var item_id = item.GetTargetIdentifier<int>(); //Call the Automation BeforeUpdateItem if (Automation != null) Automation.BeforeUpdateItem(this, item, item_id); if (item.Sync) { #region Update Item foreach (DataCompareColumnItem dcci in item.Row) { if (!Mapping.ColumnMapsToDestination(dcci)) continue; string columnB = Mapping.MapColumnToDestination(dcci); object sourceValue = dcci.AfterColumnValue; //TODO: Update the Item in the Target //Call the Automation AfterUpdateItem if (Automation != null) Automation.AfterUpdateItem(this, item, item_id); } #endregion } } catch (Exception) { //TODO: Handle Error if (status.FailOnError) throw; } finally { status.Progress(items.Count, ++currentItem); } } } }
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 } } } }
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; } var itemInvariant = new DataCompareItemInvariant(item); //Example: Get the item ID from the Target Identifier Store var item_id = itemInvariant.GetTargetIdentifier <int>(); try { //Call the Automation BeforeUpdateItem Automation?.BeforeUpdateItem(this, itemInvariant, item_id); if (itemInvariant.Sync) { #region Update Item //Get the Target Item Data var targetItem = UpdateItemToDictionary(Mapping, itemInvariant); var targetItemToSend = new Dictionary <string, object>(); foreach (var k in targetItem.Keys) { var val = targetItem[k]; var pdsi = DataSchema[k]; targetItemToSend[pdsi.Key] = pdsi.Parser.ConvertValue(val); } if (targetItemToSend.Any()) { var json = JsonConvert.SerializeObject(targetItemToSend, Formatting.None); var result = WebRequestHelper.PutRequestAsJson(json, DatasourceInfo.GetPipedriveItemEndpointUrl(item_id)); //Call the Automation AfterUpdateItem Automation?.AfterUpdateItem(this, itemInvariant, item_id); } #endregion } ClearSyncStatus(item); //Clear the Sync Flag on Processed Rows } catch (WebException e) { Automation?.ErrorItem(this, itemInvariant, item_id, e); HandleError(status, e); } catch (SystemException e) { Automation?.ErrorItem(this, itemInvariant, item_id, e); HandleError(status, e); } finally { status.Progress(items.Count, ++currentItem); //Update the Sync Progress } } } }
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; } var itemInvariant = new DataCompareItemInvariant(item); // Get the item ID from the Target Identifier Store var item_id = itemInvariant.GetTargetIdentifier <string>(); try { //Call the Automation BeforeUpdateItem (Optional only required if your supporting Automation Item Events) Automation?.BeforeUpdateItem(this, itemInvariant, item_id); if (itemInvariant.Sync) { #region Update Item //Get the Target Item Data var targetItem = UpdateItemToDictionary(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 - get the original list of tags and work out which ones to add/remove var source_item = itemInvariant.Row.First(p => Mapping.MapColumnToDestination(p) == k); var existing = GetHashSet <string>(source_item.BeforeColumnValue); var toadd = GetHashSet <string>(source_item.AfterColumnValue); var arrayOfDictionary = new List <Dictionary <string, object> >(); // Tags to Add foreach (var v in toadd.Except(existing)) { arrayOfDictionary.Add(new Dictionary <string, object>() { { "name", v }, { "status", "active" } }); } // Tags to Remove foreach (var v in existing.Except(toadd)) { arrayOfDictionary.Add(new Dictionary <string, object>() { { "name", v }, { "status", "inactive" } }); } var tags_json = JsonConvert.SerializeObject(new { tags = arrayOfDictionary }, Formatting.None); var tags_result = WebRequestHelper.PostRequestAsJson(tags_json, $"{ListServiceUrl}/{DataSourceReader.ListId}/members/{item_id}/tags"); } } 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]; } } if (targetItemToSend.Any()) { var json = JsonConvert.SerializeObject(targetItemToSend, Formatting.None); var result = WebRequestHelper.PutRequestAsJson(json, $"{ListServiceUrl}/{DataSourceReader.ListId}/members/{item_id}"); } //Call the Automation AfterUpdateItem Automation?.AfterUpdateItem(this, itemInvariant, item_id); #endregion } ClearSyncStatus(item); //Clear the Sync Flag on Processed Rows } catch (WebException e) { Automation?.ErrorItem(this, itemInvariant, item_id, e); HandleError(status, e); } catch (SystemException e) { Automation?.ErrorItem(this, itemInvariant, item_id, e); HandleError(status, e); } finally { status.Progress(items.Count, ++currentItem); //Update the Sync Progress } } } }