-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetDataCommandHandler.cs
More file actions
126 lines (111 loc) · 5.23 KB
/
Copy pathGetDataCommandHandler.cs
File metadata and controls
126 lines (111 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using ShapeDiver.SDK.GeometryBackend;
using ShapeDiver.SDK.GeometryBackend.DTO;
using ShapeDiver.SDK.Stargate;
using ShapeDiver.SDK.Stargate.Commands.Interfaces;
using ShapeDiver.SDK.Stargate.Dto.Commands;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace StargateDotNetClientExample
{
/// <summary>
/// This handler is called by the ShapeDiver Platform when the user wants to get data for
/// a given input (parameter) of the ShapeDiver model.
/// </summary>
internal class GetDataCommandHandler : BaseCommandHandler, IGetDataCommandHandler
{
public GetDataCommandHandler(IServices services) : base(services)
{
}
/// <summary>
/// Supported content types for file parameters.
/// TODO extend this list as required.
/// </summary>
List<string> supportedContentTypes = new List<string>()
{
"application/dwg",
"application/json"
};
public async Task<GetDataReplyDto> Handle(GetDataCommandDto data)
{
Services.LogMessage("Handling GetDataCommand");
// get a session context for the model
IGeometryBackendContext sessionContext = await this.Services.GeometryBackendContextCache.GetContextAsync(data.Model.Id, false, ModelTokenScopes);
// parameter sanity check
if (String.IsNullOrEmpty(data?.Parameter?.Id))
{
throw new StargateException($"Missing parameter id.");
}
if (!sessionContext.ModelData.Parameters.ContainsKey(data.Parameter.Id))
{
throw new StargateException($"Could not find parameter with id \"{data.Parameter.Id}\" for model with id \"{data.Model.Id}\".");
}
var parameter = sessionContext.ModelData.Parameters[data.Parameter.Id];
if (parameter.Type != ParameterTypeEnum.File)
{
throw new StargateException($"Parameter with id \"{data.Parameter.Id}\" is not of type 'File'.");
}
// content type check - get the first matching content type
var selectedContentType = parameter.Format.Where(format => supportedContentTypes.Contains(format)).FirstOrDefault();
if (String.IsNullOrEmpty(selectedContentType))
{
throw new StargateException($"Could not find a matching content type for parameter with id \"{data.Parameter.Id}\".");
}
/// TODO: Get file depending on selectedContentType, read result into a memory stream.
/// TODO: upload file, and send success result as shown below. Adapt the feedback messages to the user as required.
/// Dummy example by ShapeDiver:
if (selectedContentType == "application/json")
{
if (File.Exists("test.json"))
{
using (FileStream fileStream = File.OpenRead("test.json"))
{
var uploadResult = await sessionContext.GeometryBackendClient.UploadFile(sessionContext, parameter.Id, fileStream, selectedContentType, "test.json");
return SuccessResult(uploadResult.FileId, null, 1, GetDataResultEnum.Success, $"Uploaded JSON file for parameter '{parameter.Name}'");
}
}
}
else if (selectedContentType == "application/dwg")
{
if (File.Exists("test.dwg"))
{
using (FileStream fileStream = File.OpenRead("test.dwg"))
{
var uploadResult = await sessionContext.GeometryBackendClient.UploadFile(sessionContext, parameter.Id, fileStream, selectedContentType, "test.dwg");
return SuccessResult(uploadResult.FileId, null, 1, GetDataResultEnum.Success, $"Uploaded DWG file for parameter '{parameter.Name}'");
}
}
}
// TODO Optional feedback message in case the user cancelled the input.
// return SuccessResult(null, null, 0, GetDataResultEnum.Cancel, $"Handled GetDataCommand for parameter '{parameter.Name}'");
// TODO Feedback message in case the user did not select any data.
return SuccessResult(null, null, 0, GetDataResultEnum.Nothing, $"Handled GetDataCommand for parameter '{parameter.Name}'");
}
private GetDataReplyDto SuccessResult(string assetId, string chunkId, int count, GetDataResultEnum result, string message)
{
var reply = new GetDataReplyDto()
{
Info = new GetDataInfoReplyDto()
{
Count = count,
Result = result,
Message = message
}
};
if (!String.IsNullOrEmpty(assetId))
{
reply.Asset = new GetDataAssetReplyDto()
{
Id = assetId,
Chunk = chunkId == null ? null : new GetDataAssetChunkReplyDto()
{
Id = chunkId
}
};
}
return reply;
}
}
}