Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BHoM_Adapter - Issues 43/45/63 - Refresh Interface for the adapter : config, params and error log #70

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions BHoM_Adapter/AdapterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@
{
public class AdapterConfig
{
/***************************************************/
/**** Public Properties ****/
/***************************************************/

public bool ProcessInMemory { get; set; } = true;

public bool SeparateProperties { get; set; } = false;

public bool MergeWithComparer { get; set; } = false;

public bool UseAdapterId { get; set; } = true;

public bool CloneBeforePush { get; set; } = true;

/***************************************************/
}
}

80 changes: 44 additions & 36 deletions BHoM_Adapter/BHoMAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BH.oM.Base;
using BH.oM.Base.CRUD;
using BH.oM.DataManipulation.Queries;
using System;
using System.Collections;
Expand All @@ -18,8 +19,6 @@ public abstract partial class BHoMAdapter

public Guid BHoM_Guid { get; set; } = Guid.NewGuid();

public List<string> ErrorLog { get; set; } = new List<string>();

protected AdapterConfig Config { get; set; } = new AdapterConfig();


Expand All @@ -28,17 +27,12 @@ public abstract partial class BHoMAdapter
/**** Public Adapter Methods ****/
/***************************************************/

public virtual List<IObject> Push(IEnumerable<IObject> objects, string tag = "", Dictionary<string, object> config = null)
public virtual List<IObject> Push(IEnumerable<IObject> objects, string tag = "", CrudConfig config = null)
{
bool success = true;

string pushType;

object ptObj;
if (config != null && config.TryGetValue("PushType", out ptObj))
pushType = ptObj.ToString();
else
pushType = "Replace";
if (config == null)
config = new CrudConfig();

List<IObject> objectsToPush = Config.CloneBeforePush ? objects.Select(x => x is BHoMObject ? ((BHoMObject)x).GetShallowClone() : x).ToList() : objects.ToList(); //ToList() necessary for the return collection to function properly for cloned objects

Expand All @@ -52,11 +46,14 @@ public virtual List<IObject> Push(IEnumerable<IObject> objects, string tag = "",

if (iBHoMObjectType.IsAssignableFrom(typeGroup.Key))
{
if (pushType == "Replace")
success &= Replace(list as dynamic, tag);
else if (pushType == "UpdateOnly")
switch (config.PushActionType)
{
success &= UpdateOnly(list as dynamic, tag);
case PushActionType.Replace:
success &= Replace(list as dynamic, tag, config);
break;
case PushActionType.UpdateOnly:
success &= UpdateOnly(list as dynamic, tag, config);
break;
}
}
}
Expand All @@ -66,7 +63,7 @@ public virtual List<IObject> Push(IEnumerable<IObject> objects, string tag = "",

/***************************************************/

public virtual IEnumerable<object> Pull(IQuery query, Dictionary<string, object> config = null)
public virtual IEnumerable<object> Pull(IQuery query, CrudConfig config = null)
{
// Make sure this is a FilterQuery
FilterQuery filter = query as FilterQuery;
Expand All @@ -75,7 +72,7 @@ public virtual IEnumerable<object> Pull(IQuery query, Dictionary<string, object>

// Read the IBHoMObjects
if (typeof(IBHoMObject).IsAssignableFrom(filter.Type))
return Read(filter);
return Read(filter, config);

// Read the IResults
if (typeof(BH.oM.Common.IResult).IsAssignableFrom(filter.Type))
Expand Down Expand Up @@ -104,15 +101,15 @@ public virtual IEnumerable<object> Pull(IQuery query, Dictionary<string, object>
else
divisions = 5;

List<BH.oM.Common.IResult> results = ReadResults(filter.Type, objectIds, cases, divisions).ToList();
List<BH.oM.Common.IResult> results = ReadResults(filter.Type, objectIds, cases, divisions, config).ToList();
results.Sort();
return results;
}

// Read the IResultCollections
if (typeof(BH.oM.Common.IResultCollection).IsAssignableFrom(filter.Type))
{
List<BH.oM.Common.IResultCollection> results = ReadResults(filter).ToList();
List<BH.oM.Common.IResultCollection> results = ReadResults(filter, config).ToList();
return results;
}

Expand All @@ -121,34 +118,34 @@ public virtual IEnumerable<object> Pull(IQuery query, Dictionary<string, object>

/***************************************************/

public virtual bool PullTo(BHoMAdapter to, IQuery query, Dictionary<string, object> config = null)
public virtual bool PullTo(BHoMAdapter to, IQuery query, CrudConfig sourceConfig = null, CrudConfig targetConfig = null)
{
string tag = "";
if (query is FilterQuery)
tag = (query as FilterQuery).Tag;

IEnumerable<object> objects = this.Pull(query, config);
IEnumerable<object> objects = this.Pull(query, sourceConfig);
int count = objects.Count();
return to.Push(objects.Cast<IObject>(), tag).Count() == count;
return to.Push(objects.Cast<IObject>(), tag, targetConfig).Count() == count;
}

/***************************************************/

public virtual int UpdateProperty(FilterQuery filter, string property, object newValue, Dictionary<string, object> config = null)
public virtual int UpdateProperty(FilterQuery filter, string property, object newValue, CrudConfig config = null)
{
return PullUpdatePush(filter, property, newValue);
return PullUpdatePush(filter, property, newValue, config);
}

/***************************************************/

public virtual int Delete(FilterQuery filter, Dictionary<string, object> config = null)
public virtual int Delete(FilterQuery filter, CrudConfig config = null)
{
return Delete(filter.Type, filter.Tag);
return Delete(filter.Type, filter.Tag, config);
}

/***************************************************/

public virtual bool Execute(string command, Dictionary<string, object> parameters = null, Dictionary<string, object> config = null)
public virtual bool Execute(string command, IExecuteParams parameters = null, CrudConfig config = null)
{
return false;
}
Expand All @@ -173,46 +170,57 @@ protected virtual void OnDataUpdated()
/**** Protected Abstract CRUD Methods ****/
/***************************************************/

/***************************************************/
// Level 1 - Always required

protected abstract bool Create<T>(IEnumerable<T> objects, bool replaceAll = false) where T : IObject;
protected abstract bool Create<T>(IEnumerable<T> objects, bool replaceAll = false, CrudConfig config = null) where T : IObject;

protected abstract IEnumerable<IBHoMObject> Read(Type type, IList ids);
/***************************************************/

protected abstract IEnumerable<IBHoMObject> Read(Type type, IList ids, CrudConfig config = null);

/***************************************************/
// Level 2 - Optional

public virtual int UpdateProperty(Type type, IEnumerable<object> ids, string property, object newValue)
public virtual int UpdateProperty(Type type, IEnumerable<object> ids, string property, object newValue, CrudConfig config = null)
{
return 0;
}

protected virtual int Delete(Type type, IEnumerable<object> ids)
/***************************************************/

protected virtual int Delete(Type type, IEnumerable<object> ids, CrudConfig config = null)
{
return 0;
}

protected virtual IEnumerable<BH.oM.Common.IResult> ReadResults(Type type, IList ids = null, IList cases = null, int divisions = 5)
/***************************************************/

protected virtual IEnumerable<BH.oM.Common.IResult> ReadResults(Type type, IList ids = null, IList cases = null, int divisions = 5, CrudConfig config = null)
{
return new List<BH.oM.Common.IResult>();
}

protected virtual IEnumerable<BH.oM.Common.IResultCollection> ReadResults(FilterQuery query)
/***************************************************/

protected virtual IEnumerable<BH.oM.Common.IResultCollection> ReadResults(FilterQuery query, CrudConfig config = null)
{
return new List<BH.oM.Common.IResultCollection>();
}

protected virtual bool UpdateObjects<T>(IEnumerable<T> objects) where T:IObject
/***************************************************/

protected virtual bool UpdateObjects<T>(IEnumerable<T> objects, CrudConfig config = null) where T:IObject
{
Type objectType = typeof(T);
if (Config.UseAdapterId && typeof(IBHoMObject).IsAssignableFrom(objectType))
{
Delete(typeof(T), objects.Select(x => ((IBHoMObject)x).CustomData[AdapterId]));
Delete(typeof(T), objects.Select(x => ((IBHoMObject)x).CustomData[AdapterId]), config);
}
return Create(objects);
return Create(objects, false, config);
}


/***************************************************/
// Optional Id query

protected virtual object NextId(Type objectType, bool refresh = false)
Expand Down
1 change: 1 addition & 0 deletions BHoM_Adapter/BHoM_Adapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="CRUD\UpdateProperty.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
12 changes: 7 additions & 5 deletions BHoM_Adapter/CRUD/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using BH.oM.Base;
using BH.oM.Base.CRUD;

namespace BH.Adapter
{
Expand All @@ -11,28 +12,29 @@ public abstract partial class BHoMAdapter
/**** Protected Methods ****/
/***************************************************/

protected virtual int Delete(Type type, string tag = "", Dictionary<string, object> config = null)
protected virtual int Delete(Type type, string tag = "", CrudConfig config = null)
{
if (tag == "")
{
return Delete(type, null as List<object>);
return Delete(type, null as List<object>, config);
}
else
{
// Get all with tag
IEnumerable<IBHoMObject> withTag = Read(type, tag);
IEnumerable<IBHoMObject> withTag = Read(type, tag, config);

// Get indices of all with that tag only
IEnumerable<object> ids = withTag.Where(x => x.Tags.Count == 1).Select(x => x.CustomData[AdapterId]).OrderBy(x => x);
Delete(type, ids);
Delete(type, ids, config);

// Remove tag if other tags as well
IEnumerable<IBHoMObject> multiTags = withTag.Where(x => x.Tags.Count > 1);
UpdateProperty(type, multiTags.Select(x => x.CustomData[AdapterId]), "Tags", multiTags.Select(x => x.Tags));
UpdateProperty(type, multiTags.Select(x => x.CustomData[AdapterId]), "Tags", multiTags.Select(x => x.Tags), config);

return ids.Count();
}
}

/***************************************************/
}
}
9 changes: 5 additions & 4 deletions BHoM_Adapter/CRUD/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections;
using System.Linq;
using BH.oM.DataManipulation.Queries;
using BH.oM.Base.CRUD;

namespace BH.Adapter
{
Expand All @@ -13,10 +14,10 @@ public abstract partial class BHoMAdapter
/**** BHoM Adapter Methods ****/
/***************************************************/

protected IEnumerable<IBHoMObject> Read(Type type, string tag = "")
protected virtual IEnumerable<IBHoMObject> Read(Type type, string tag = "", CrudConfig config = null)
{
// Get the objects based on the ids
IEnumerable<IBHoMObject> objects = Read(type, null as List<object>);
IEnumerable<IBHoMObject> objects = Read(type, null as List<object>, config);

// Filter by tag if any
if (tag == "")
Expand All @@ -27,15 +28,15 @@ protected IEnumerable<IBHoMObject> Read(Type type, string tag = "")

/***************************************************/

public virtual IEnumerable<IBHoMObject> Read(FilterQuery query)
public virtual IEnumerable<IBHoMObject> Read(FilterQuery query, CrudConfig config = null)
{
IList objectIds = null;
object idObject;
if (query.Equalities.TryGetValue("ObjectIds", out idObject) && idObject is IList)
objectIds = idObject as IList;

// Get the objects based on the ids
IEnumerable<IBHoMObject> objects = Read(query.Type, objectIds);
IEnumerable<IBHoMObject> objects = Read(query.Type, objectIds, config);

// Filter by tag if any
if (query.Tag == "")
Expand Down
Loading