Skip to content
This repository has been archived by the owner on Dec 5, 2018. It is now read-only.

Commit

Permalink
refactor: normalize dto hydration recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi Levin committed Nov 16, 2017
1 parent a8ce0d8 commit 7e28c7b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 138 deletions.
69 changes: 69 additions & 0 deletions Gravity/Gravity/DAL/RSAPI/RsapiDao.Get.Document.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using kCura.Relativity.Client;
using kCura.Relativity.Client.DTOs;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Gravity.Base;
using Gravity.Exceptions;
using Gravity.Extensions;

namespace Gravity.DAL.RSAPI
{
public partial class RsapiDao
{
public ResultSet<Document> QueryDocumentsByDocumentViewID(int documentViewId)
{
ResultSet<Document> returnObject;

Query<Document> query = new Query<Document>()
{
Condition = new ViewCondition(documentViewId),
Fields = FieldValue.SelectedFields
};

using (IRSAPIClient proxy = CreateProxy())
{
try
{
returnObject = invokeWithRetryService.InvokeWithRetry(() => proxy.Repositories.Document.Query(query));
}
catch (Exception ex)
{
throw new ProxyOperationFailedException("Failed in method: " + MethodBase.GetCurrentMethod(), ex);
}
}

return returnObject;
}

public KeyValuePair<byte[], FileMetadata> DownloadDocumentNative(int documentId)
{
Document doc = new Document(documentId);
byte[] documentBytes;

KeyValuePair<DownloadResponse, Stream> documentNativeResponse = new KeyValuePair<DownloadResponse, Stream>();

using (IRSAPIClient proxy = CreateProxy())
{
try
{
documentNativeResponse = invokeWithRetryService.InvokeWithRetry(() => proxy.Repositories.Document.DownloadNative(doc));
}
catch (Exception ex)
{
throw new ProxyOperationFailedException("Failed in method: " + MethodInfo.GetCurrentMethod(), ex);
}
}

using (MemoryStream ms = (MemoryStream)documentNativeResponse.Value)
{
documentBytes = ms.ToArray();
}

return new KeyValuePair<byte[], FileMetadata>(documentBytes, documentNativeResponse.Key.Metadata);
}
}
}
157 changes: 19 additions & 138 deletions Gravity/Gravity/DAL/RSAPI/RsapiDao.Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,115 +120,46 @@ protected RelativityFile GetFile(int fileFieldArtifactId, int ourFileContainerIn
}
#endregion

public List<T> GetAllDTOs<T>()
where T : BaseDto, new()
{
List<RDO> objectsRdos = GetRdos<T>();

return objectsRdos.Select<RDO, T>(rdo => rdo.ToHydratedDto<T>()).ToList();
}

public List<T> GetAllDTOs<T>(Condition queryCondition = null, ObjectFieldsDepthLevel depthLevel = ObjectFieldsDepthLevel.FirstLevelOnly)
where T : BaseDto, new()
{
List<T> returnList = null;

List<RDO> objectsRdos = GetRdos<T>(queryCondition);

switch (depthLevel)
{
case ObjectFieldsDepthLevel.FirstLevelOnly:
returnList = objectsRdos.Select<RDO, T>(rdo => rdo.ToHydratedDto<T>()).ToList();
break;
case ObjectFieldsDepthLevel.FullyRecursive:
var allDtos = new List<T>();

foreach (var rdo in objectsRdos)
{
var dto = rdo.ToHydratedDto<T>();

PopulateChildrenRecursively<T>(dto, rdo, depthLevel);

allDtos.Add(dto);
}

returnList = allDtos;
break;
default:
return objectsRdos.Select<RDO, T>(rdo => rdo.ToHydratedDto<T>()).ToList();

}

return returnList;
return objectsRdos.Select(rdo => GetHydratedDTO<T>(rdo, depthLevel)).ToList();
}

public List<T> GetAllChildDTOs<T>(Guid parentFieldGuid, int parentArtifactID, ObjectFieldsDepthLevel depthLevel)
where T : BaseDto, new()
{
Condition queryCondition = new WholeNumberCondition(parentFieldGuid, NumericConditionEnum.EqualTo, parentArtifactID);
List<RDO> objectsRdos = GetRdos<T>(queryCondition);

switch (depthLevel)
{
case ObjectFieldsDepthLevel.FirstLevelOnly:
return objectsRdos.Select<RDO, T>(rdo => rdo.ToHydratedDto<T>()).ToList();
case ObjectFieldsDepthLevel.FullyRecursive:
var allChildDtos = new List<T>();
foreach (var childRdo in objectsRdos)
{
var childDto = childRdo.ToHydratedDto<T>();

PopulateChildrenRecursively<T>(childDto, childRdo, depthLevel);

allChildDtos.Add(childDto);
}
return allChildDtos;
default:
return objectsRdos.Select<RDO, T>(rdo => rdo.ToHydratedDto<T>()).ToList();
}
return GetAllDTOs<T>(queryCondition, depthLevel);
}

public List<T> GetDTOs<T>(int[] artifactIDs, ObjectFieldsDepthLevel depthLevel)
where T : BaseDto, new()
{
List<RDO> objectsRdos = GetRdos(artifactIDs);
switch (depthLevel)
{
case ObjectFieldsDepthLevel.FirstLevelOnly:
return objectsRdos.Select<RDO, T>(rdo => rdo.ToHydratedDto<T>()).ToList();
case ObjectFieldsDepthLevel.FullyRecursive:
var allDtos = new List<T>();

foreach (var rdo in objectsRdos)
{
var dto = rdo.ToHydratedDto<T>();

PopulateChildrenRecursively<T>(dto, rdo, depthLevel);

allDtos.Add(dto);
}

return allDtos;
default:
return objectsRdos.Select<RDO, T>(rdo => rdo.ToHydratedDto<T>()).ToList();
}
return objectsRdos.Select(rdo => GetHydratedDTO<T>(rdo, depthLevel)).ToList();
}

internal T GetDTO<T>(int artifactID, ObjectFieldsDepthLevel depthLevel)
where T : BaseDto, new()
{
RDO objectRdo = GetRdo(artifactID);

return GetHydratedDTO<T>(objectRdo, depthLevel);
}

private T GetHydratedDTO<T>(RDO objectRdo, ObjectFieldsDepthLevel depthLevel) where T : BaseDto, new()
{
T dto = objectRdo.ToHydratedDto<T>();

switch (depthLevel)
{
case ObjectFieldsDepthLevel.FirstLevelOnly:
return objectRdo.ToHydratedDto<T>();
case ObjectFieldsDepthLevel.FullyRecursive:
T dto = objectRdo.ToHydratedDto<T>();
PopulateChildrenRecursively<T>(dto, objectRdo, depthLevel);
return dto;
default:
return objectRdo.ToHydratedDto<T>();
return dto;
}
}

Expand Down Expand Up @@ -317,67 +248,17 @@ internal void PopulateChildrenRecursively<T>(BaseDto baseDto, RDO objectRdo, Obj
public T GetRelativityObject<T>(int artifactId, ObjectFieldsDepthLevel depthLevel)
where T : BaseDto, new()
{
RDO objectRdo = GetRdo(artifactId);

T theObject = objectRdo.ToHydratedDto<T>();

if (depthLevel != ObjectFieldsDepthLevel.OnlyParentObject)
{
PopulateChildrenRecursively<T>(theObject, objectRdo, depthLevel);
}

return theObject;
}

public ResultSet<Document> QueryDocumentsByDocumentViewID(int documentViewId)
{
ResultSet<Document> returnObject;

Query<Document> query = new Query<Document>();
query.Condition = new ViewCondition(documentViewId);
query.Fields = FieldValue.SelectedFields;

using (IRSAPIClient proxy = CreateProxy())
{
try
{
returnObject = invokeWithRetryService.InvokeWithRetry(() => proxy.Repositories.Document.Query(query));
}
catch (Exception ex)
{
throw new ProxyOperationFailedException("Failed in method: " + System.Reflection.MethodInfo.GetCurrentMethod(), ex);
}
}

return returnObject;
}

public KeyValuePair<byte[], kCura.Relativity.Client.FileMetadata> DownloadDocumentNative(int documentId)
{
kCura.Relativity.Client.DTOs.Document doc = new kCura.Relativity.Client.DTOs.Document(documentId);
byte[] documentBytes;

KeyValuePair<DownloadResponse, Stream> documentNativeResponse = new KeyValuePair<DownloadResponse, Stream>();

using (IRSAPIClient proxy = CreateProxy())
{
try
{
documentNativeResponse = invokeWithRetryService.InvokeWithRetry(() => proxy.Repositories.Document.DownloadNative(doc));
}
catch (Exception ex)
{
throw new ProxyOperationFailedException("Failed in method: " + System.Reflection.MethodInfo.GetCurrentMethod(), ex);
}
}
var objectRdo = GetRdo(artifactId);
var dto = objectRdo.ToHydratedDto<T>();

using (MemoryStream ms = (MemoryStream)documentNativeResponse.Value)
switch (depthLevel)
{
documentBytes = ms.ToArray();
case ObjectFieldsDepthLevel.OnlyParentObject:
return dto;
default:
PopulateChildrenRecursively<T>(dto, objectRdo, depthLevel);
return dto;
}

return new KeyValuePair<byte[], FileMetadata>(documentBytes, documentNativeResponse.Key.Metadata);
}

}
}
1 change: 1 addition & 0 deletions Gravity/Gravity/Gravity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Compile Include="Attributes\RelativityObjectFieldParentArtifactIdAttribute.cs" />
<Compile Include="Base\RelativityFile.cs" />
<Compile Include="Base\AppConfigConnectionHelper.cs" />
<Compile Include="DAL\RSAPI\RsapiDao.Get.Document.cs" />
<Compile Include="Extensions\BaseDtoExtensions.cs" />
<Compile Include="Extensions\CollectionsExtensions.cs" />
<Compile Include="Extensions\EnumHelpers.cs" />
Expand Down

0 comments on commit 7e28c7b

Please sign in to comment.