Skip to content

Commit 2f8e8d6

Browse files
committed
Added a mustUseAdvisory parameter to the Create() methods for Consumers.
1 parent a62a959 commit 2f8e8d6

7 files changed

Lines changed: 38 additions & 19 deletions

File tree

Code/Sif3Framework/Sif.Framework/Consumers/Consumer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ public string GetChangesSinceMarker(string zoneId = null, string contextId = nul
174174
}
175175

176176
/// <summary>
177-
/// <see cref="IConsumer{TSingle,TMultiple,TPrimaryKey}.Create(TSingle, string, string)">Create</see>
177+
/// <see cref="IConsumer{TSingle,TMultiple,TPrimaryKey}.Create(TSingle, bool?, string, string)">Create</see>
178178
/// </summary>
179-
public virtual TSingle Create(TSingle obj, string zoneId = null, string contextId = null)
179+
public virtual TSingle Create(TSingle obj, bool? mustUseAdvisory = null, string zoneId = null, string contextId = null)
180180
{
181181

182182
if (!RegistrationService.Registered)
@@ -186,17 +186,17 @@ public virtual TSingle Create(TSingle obj, string zoneId = null, string contextI
186186

187187
string url = EnvironmentUtils.ParseServiceUrl(EnvironmentTemplate) + "/" + TypeName + "s" + "/" + TypeName + HttpUtils.MatrixParameters(zoneId, contextId);
188188
string body = SerialiseSingle(obj);
189-
string xml = HttpUtils.PostRequest(url, RegistrationService.AuthorisationToken, body);
189+
string xml = HttpUtils.PostRequest(url, RegistrationService.AuthorisationToken, body, mustUseAdvisory: mustUseAdvisory);
190190
if (log.IsDebugEnabled) log.Debug("XML from POST request ...");
191191
if (log.IsDebugEnabled) log.Debug(xml);
192192

193193
return DeserialiseSingle(xml);
194194
}
195195

196196
/// <summary>
197-
/// <see cref="IConsumer{TSingle,TMultiple,TPrimaryKey}.Create(TMultiple, string, string)">Create</see>
197+
/// <see cref="IConsumer{TSingle,TMultiple,TPrimaryKey}.Create(TMultiple, bool?, string, string)">Create</see>
198198
/// </summary>
199-
public virtual MultipleCreateResponse Create(TMultiple obj, string zoneId = null, string contextId = null)
199+
public virtual MultipleCreateResponse Create(TMultiple obj, bool? mustUseAdvisory = null, string zoneId = null, string contextId = null)
200200
{
201201

202202
if (!RegistrationService.Registered)
@@ -206,7 +206,7 @@ public virtual MultipleCreateResponse Create(TMultiple obj, string zoneId = null
206206

207207
string url = EnvironmentUtils.ParseServiceUrl(EnvironmentTemplate) + "/" + TypeName + "s" + HttpUtils.MatrixParameters(zoneId, contextId);
208208
string body = SerialiseMultiple(obj);
209-
string xml = HttpUtils.PostRequest(url, RegistrationService.AuthorisationToken, body);
209+
string xml = HttpUtils.PostRequest(url, RegistrationService.AuthorisationToken, body, mustUseAdvisory: mustUseAdvisory);
210210
if (log.IsDebugEnabled) log.Debug("XML from POST request ...");
211211
if (log.IsDebugEnabled) log.Debug(xml);
212212
createResponseType createResponseType = SerialiserFactory.GetXmlSerialiser<createResponseType>().Deserialise(xml);

Code/Sif3Framework/Sif.Framework/Consumers/IConsumer.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,22 @@ public interface IConsumer<TSingle, TMultiple, TPrimaryKey> : IPayloadSerialisab
5858
/// <para>POST /StudentPersonals/StudentPersonal</para>
5959
/// </summary>
6060
/// <param name="obj">Object to create.</param>
61+
/// <param name="mustUseAdvisory">Flag to indicate whether the object's identifier should be retained.</param>
6162
/// <param name="zoneId">Zone associated with the request.</param>
6263
/// <param name="contextId">Zone context.</param>
6364
/// <returns>Created object.</returns>
64-
TSingle Create(TSingle obj, string zoneId = null, string contextId = null);
65+
TSingle Create(TSingle obj, bool? mustUseAdvisory = null, string zoneId = null, string contextId = null);
6566

6667
/// <summary>
6768
/// Create multiple objects.
6869
/// <para>POST /StudentPersonals</para>
6970
/// </summary>
7071
/// <param name="obj">Object (multiple object entity) to create.</param>
72+
/// <param name="mustUseAdvisory">Flag to indicate whether the object's identifier should be retained.</param>
7173
/// <param name="zoneId">Zone associated with the request.</param>
7274
/// <param name="contextId">Zone context.</param>
7375
/// <returns>Response containing status of each object created.</returns>
74-
MultipleCreateResponse Create(TMultiple obj, string zoneId = null, string contextId = null);
76+
MultipleCreateResponse Create(TMultiple obj, bool? mustUseAdvisory = null, string zoneId = null, string contextId = null);
7577

7678
/// <summary>
7779
/// Retrieve a single object.

Code/Sif3Framework/Sif.Framework/Utils/HttpUtils.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ internal enum RequestHeader
6565
/// <param name="methodOverride">Overrides the method of the request, e.g. to implement a GET with a payload over a POST request.</param>
6666
/// <param name="contentTypeOverride">Overrides the ContentType header.</param>
6767
/// <param name="acceptOverride">Overrides the Accept header.</param>
68+
/// <param name="mustUseAdvisory">Flag to indicate whether the object's identifier should be retained.</param>
6869
/// <returns>HTTP web request.</returns>
6970
private static HttpWebRequest CreateHttpWebRequest(RequestMethod requestMethod,
7071
string url,
@@ -74,7 +75,8 @@ private static HttpWebRequest CreateHttpWebRequest(RequestMethod requestMethod,
7475
int? navigationPageSize = null,
7576
string methodOverride = null,
7677
string contentTypeOverride = null,
77-
string acceptOverride = null)
78+
string acceptOverride = null,
79+
bool? mustUseAdvisory = null)
7880
{
7981
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
8082
request.ContentType = "application/xml";
@@ -125,6 +127,11 @@ private static HttpWebRequest CreateHttpWebRequest(RequestMethod requestMethod,
125127
request.ContentType = contentTypeOverride.Trim();
126128
}
127129

130+
if (mustUseAdvisory.HasValue)
131+
{
132+
request.Headers.Add(RequestHeader.mustUseAdvisory.ToDescription(), mustUseAdvisory.Value.ToString());
133+
}
134+
128135
return request;
129136
}
130137

@@ -224,6 +231,7 @@ private static string RequestWithoutPayload(RequestMethod requestMethod,
224231
/// <param name="methodOverride">Overrides the method of the request, e.g. to implement a GET with a payload over a POST request.</param>
225232
/// <param name="contentTypeOverride">Overrides the ContentType header.</param>
226233
/// <param name="acceptOverride">Overrides the Accept header.</param>
234+
/// <param name="mustUseAdvisory">Flag to indicate whether the object's identifier should be retained.</param>
227235
/// <returns>Response.</returns>
228236
private static string RequestWithPayload(RequestMethod requestMethod,
229237
string url,
@@ -232,9 +240,10 @@ private static string RequestWithPayload(RequestMethod requestMethod,
232240
ServiceType? serviceType = null,
233241
string methodOverride = null,
234242
string contentTypeOverride = null,
235-
string acceptOverride = null)
243+
string acceptOverride = null,
244+
bool? mustUseAdvisory = null)
236245
{
237-
HttpWebRequest request = CreateHttpWebRequest(requestMethod, url, authorisationToken, serviceType, null, null, methodOverride, contentTypeOverride, acceptOverride);
246+
HttpWebRequest request = CreateHttpWebRequest(requestMethod, url, authorisationToken, serviceType, null, null, methodOverride, contentTypeOverride, acceptOverride, mustUseAdvisory);
238247

239248
using (Stream requestStream = request.GetRequestStream())
240249
{
@@ -377,16 +386,18 @@ public static WebHeaderCollection HeadRequest(string url, AuthorisationToken aut
377386
/// <param name="methodOverride">The method that can be used to override the POST, e.g. to issue a GET with a payload.</param>
378387
/// <param name="contentTypeOverride">Overrides the ContentType header.</param>
379388
/// <param name="acceptOverride">Overrides the Accept header.</param>
389+
/// <param name="mustUseAdvisory">Flag to indicate whether the object's identifier should be retained.</param>
380390
/// <returns>Response.</returns>
381391
public static string PostRequest(string url,
382392
AuthorisationToken authorisationToken,
383393
string body,
384394
ServiceType serviceType = ServiceType.OBJECT,
385395
string methodOverride = null,
386396
string contentTypeOverride = null,
387-
string acceptOverride = null)
397+
string acceptOverride = null,
398+
bool? mustUseAdvisory = null)
388399
{
389-
return RequestWithPayload(RequestMethod.POST, url, authorisationToken, body, serviceType, methodOverride, contentTypeOverride, acceptOverride);
400+
return RequestWithPayload(RequestMethod.POST, url, authorisationToken, body, serviceType, methodOverride, contentTypeOverride, acceptOverride, mustUseAdvisory);
390401
}
391402

392403
/// <summary>

Code/Sif3FrameworkDemo/Sif.Framework.Demo.Au.Consumer/ConsumerApp.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ void RunStudentPersonalConsumer()
9393
SIF_ExtendedElementsTypeSIF_ExtendedElement[] extendedElements = new SIF_ExtendedElementsTypeSIF_ExtendedElement[] { extendedElement };
9494
NameOfRecordType newStudentName = new NameOfRecordType { FamilyName = "Wayne", GivenName = "Bruce", Type = NameOfRecordTypeType.LGL };
9595
PersonInfoType newStudentInfo = new PersonInfoType { Name = newStudentName };
96-
StudentPersonal newStudent = new StudentPersonal { LocalId = "555", PersonInfo = newStudentInfo, SIF_ExtendedElements = extendedElements };
97-
StudentPersonal retrievedNewStudent = studentPersonalConsumer.Create(newStudent);
98-
if (log.IsInfoEnabled) log.Info("Created new student " + newStudent.PersonInfo.Name.GivenName + " " + newStudent.PersonInfo.Name.FamilyName);
96+
string studentID = Guid.NewGuid().ToString();
97+
StudentPersonal newStudent = new StudentPersonal { RefId = studentID, LocalId = "555", PersonInfo = newStudentInfo, SIF_ExtendedElements = extendedElements };
98+
StudentPersonal retrievedNewStudent = studentPersonalConsumer.Create(newStudent, true);
99+
if (log.IsInfoEnabled) log.Info($"Created new student {newStudent.PersonInfo.Name.GivenName} {newStudent.PersonInfo.Name.FamilyName} with ID of {studentID}.");
99100

100101
// Create multiple new students.
101102
if (log.IsInfoEnabled) log.Info("*** Create multiple new students.");

Code/Sif3FrameworkDemo/Sif.Framework.Demo.Au.Provider/Services/StudentPersonalService.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,14 @@ static StudentPersonalService()
115115

116116
public StudentPersonal Create(StudentPersonal obj, bool? mustUseAdvisory = null, string zoneId = null, string contextId = null)
117117
{
118-
string refId = Guid.NewGuid().ToString();
119-
obj.RefId = refId;
120-
studentsCache.Add(refId, obj);
118+
119+
if (!mustUseAdvisory.HasValue || !mustUseAdvisory.Value)
120+
{
121+
string refId = Guid.NewGuid().ToString();
122+
obj.RefId = refId;
123+
}
124+
125+
studentsCache.Add(obj.RefId, obj);
121126

122127
return obj;
123128
}
0 Bytes
Binary file not shown.
-512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)