Skip to content

Commit

Permalink
Merge pull request #23 from nsip/develop-release9
Browse files Browse the repository at this point in the history
Separate event consumer demo from CRUD consumer demo.
  • Loading branch information
rafidzal authored Jul 16, 2018
2 parents 4f56ec1 + 9a3c8fe commit de0f675
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 94 deletions.
105 changes: 11 additions & 94 deletions Code/Sif3FrameworkDemo/Sif.Framework.Demo.Au.Consumer/ConsumerApp.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2018 Systemic Pty Ltd
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -27,8 +27,7 @@

namespace Sif.Framework.Demo.Au.Consumer
{

class ConsumerApp
internal class ConsumerApp
{
private static readonly slf4net.ILogger log = slf4net.LoggerFactory.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Expand All @@ -55,25 +54,7 @@ private static List<StudentPersonal> CreateStudents(int count)
return studentPersonalsCache;
}

private static bool RunDemo(string demoName)
{
Console.WriteLine();
Console.Write("Would you like run the " + demoName + " demo (Y/N)? - ");
ConsoleKeyInfo info = new ConsoleKeyInfo();

do
{
info = Console.ReadKey();
}
while (info.Key != ConsoleKey.N && info.Key != ConsoleKey.Y && info.Key != ConsoleKey.Enter);

Console.WriteLine();
Console.WriteLine();

return (info.Key == ConsoleKey.Y);
}

void RunStudentPersonalConsumer()
private void RunStudentPersonalConsumer()
{
StudentPersonalConsumer studentPersonalConsumer = new StudentPersonalConsumer(
SettingsManager.ConsumerSettings.ApplicationKey,
Expand All @@ -85,7 +66,6 @@ void RunStudentPersonalConsumer()

try
{

// Retrieve Bart Simpson using QBE.
if (log.IsInfoEnabled) log.Info("*** Retrieve Bart Simpson using QBE.");
NameOfRecordType name = new NameOfRecordType { FamilyName = "Simpson", GivenName = "Bart" };
Expand Down Expand Up @@ -158,7 +138,6 @@ void RunStudentPersonalConsumer()
{
if (log.IsInfoEnabled) log.Info("Update status code is " + status.StatusCode);
}

}
catch (UnauthorizedAccessException)
{
Expand All @@ -182,13 +161,11 @@ void RunStudentPersonalConsumer()
{
if (log.IsInfoEnabled) log.Info("Delete status code is " + status.StatusCode);
}

}
catch (UnauthorizedAccessException)
{
if (log.IsInfoEnabled) log.Info($"Access to delete multiple students is rejected.");
}

}
catch (UnauthorizedAccessException)
{
Expand All @@ -206,7 +183,6 @@ void RunStudentPersonalConsumer()

if (students.Count() > 1)
{

// Retrieve a single student.
if (log.IsInfoEnabled) log.Info("*** Retrieve a single student.");
string studentId = students.ElementAt(1).RefId;
Expand Down Expand Up @@ -246,13 +222,11 @@ void RunStudentPersonalConsumer()
{
if (log.IsInfoEnabled) log.Info("Student " + secondStudent.PersonInfo.Name.GivenName + " " + secondStudent.PersonInfo.Name.FamilyName + " was NOT deleted.");
}

}
catch (UnauthorizedAccessException)
{
if (log.IsInfoEnabled) log.Info($"Access to delete a student is rejected.");
}

}

// Retrieve students based on Teaching Group using Service Paths.
Expand All @@ -271,21 +245,15 @@ void RunStudentPersonalConsumer()

if (student.SIF_ExtendedElements != null && student.SIF_ExtendedElements.Length > 0)
{

foreach (SIF_ExtendedElementsTypeSIF_ExtendedElement element in student.SIF_ExtendedElements)
{

foreach (string content in element.Text)
{
if (log.IsInfoEnabled) log.Info("Extended element text is ...\n" + content);
}

}

}

}

}
catch (UnauthorizedAccessException)
{
Expand All @@ -305,12 +273,10 @@ void RunStudentPersonalConsumer()
}
else
{

foreach (StudentPersonal student in changedStudents)
{
if (log.IsInfoEnabled) log.Info("Student name is " + student.PersonInfo.Name.GivenName + " " + student.PersonInfo.Name.FamilyName);
}

}

changesSinceMarker = nextChangesSinceMarker;
Expand All @@ -324,12 +290,10 @@ void RunStudentPersonalConsumer()
}
else
{

foreach (StudentPersonal student in changedStudents)
{
if (log.IsInfoEnabled) log.Info("Student name is " + student.PersonInfo.Name.GivenName + " " + student.PersonInfo.Name.FamilyName);
}

}

changesSinceMarker = nextChangesSinceMarker;
Expand All @@ -343,14 +307,11 @@ void RunStudentPersonalConsumer()
}
else
{

foreach (StudentPersonal student in changedStudents)
{
if (log.IsInfoEnabled) log.Info("Student name is " + student.PersonInfo.Name.GivenName + " " + student.PersonInfo.Name.FamilyName);
}

}

}
catch (UnauthorizedAccessException)
{
Expand All @@ -365,67 +326,23 @@ void RunStudentPersonalConsumer()
studentPersonalConsumer.Unregister();
if (log.IsInfoEnabled) log.Info("Unregistered the Consumer.");
}

}

static void Main(string[] args)
private static void Main(string[] args)
{
ConsumerApp app = new ConsumerApp();

if (RunDemo("Student Personal CRUD Consumer"))
try
{

try
{
app.RunStudentPersonalConsumer();
}
catch (Exception e)
{
if (log.IsErrorEnabled) log.Error("Error running the Student Personal CRUD Consumer.\n" + ExceptionUtils.InferErrorResponseMessage(e), e);
}

app.RunStudentPersonalConsumer();
}

Console.WriteLine();
Console.WriteLine("********************************************************************************");
Console.WriteLine();
Console.WriteLine("To run the following Event Consumer demo, the following is required:");
Console.WriteLine();
Console.WriteLine(" 1) The consumer.environment.url app setting in the SifFramework.config file needs to reference the BROKERED environment endpoint.");
Console.WriteLine(" 2) The Sif.Framework.Demo.Broker needs to be run instead of the Sif.Framework.EnvironmentProvider.");
Console.WriteLine();
Console.WriteLine("********************************************************************************");

if (RunDemo("Student Personal Event Consumer"))
catch (Exception e)
{

try
{
StudentPersonalEventConsumer studentPersonalConsumer = new StudentPersonalEventConsumer(
SettingsManager.ConsumerSettings.ApplicationKey,
SettingsManager.ConsumerSettings.InstanceId,
SettingsManager.ConsumerSettings.UserToken,
SettingsManager.ConsumerSettings.SolutionId);
studentPersonalConsumer.Start("Sif3DemoZone1", "DEFAULT");
if (log.IsInfoEnabled) log.Info("Started the Event Consumer.");

Console.WriteLine("Press any key to stop the Event Consumer (may take several seconds to complete) ...");
Console.ReadKey();

studentPersonalConsumer.Stop();
if (log.IsInfoEnabled) log.Info("Stopped the Event Consumer.");
}
catch (Exception e)
{
if (log.IsErrorEnabled) log.Error("Error running the Student Personal Event Consumer.\n" + ExceptionUtils.InferErrorResponseMessage(e), e);
}

if (log.IsErrorEnabled) log.Error("Error running the Student Personal CRUD Consumer.\n" + ExceptionUtils.InferErrorResponseMessage(e), e);
}

Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
}

}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2018 Systemic Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Sif.Framework.Demo.Au.Consumer.Consumers;
using Sif.Framework.Utils;
using System;

namespace Sif.Framework.Demo.Au.Consumer
{
internal class EventConsumerApp
{
private static readonly slf4net.ILogger log = slf4net.LoggerFactory.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

private static bool RunDemo(string demoName)
{
Console.WriteLine();
Console.Write("Would you like run the " + demoName + " demo (Y/N)? - ");
ConsoleKeyInfo info = new ConsoleKeyInfo();

do
{
info = Console.ReadKey();
}
while (info.Key != ConsoleKey.N && info.Key != ConsoleKey.Y && info.Key != ConsoleKey.Enter);

Console.WriteLine();
Console.WriteLine();

return (info.Key == ConsoleKey.Y);
}

private static void Main(string[] args)
{
Console.WriteLine();
Console.WriteLine("********************************************************************************");
Console.WriteLine();
Console.WriteLine("To run the following Event Consumer demo, the following is required:");
Console.WriteLine();
Console.WriteLine(" 1) The consumer.environment.url app setting in the SifFramework.config file needs to reference the BROKERED environment endpoint.");
Console.WriteLine(" 2) The Sif.Framework.Demo.Broker needs to be run instead of the Sif.Framework.EnvironmentProvider.");
Console.WriteLine();
Console.WriteLine("********************************************************************************");

if (RunDemo("Student Personal Event Consumer"))
{
try
{
StudentPersonalEventConsumer studentPersonalConsumer = new StudentPersonalEventConsumer(
SettingsManager.ConsumerSettings.ApplicationKey,
SettingsManager.ConsumerSettings.InstanceId,
SettingsManager.ConsumerSettings.UserToken,
SettingsManager.ConsumerSettings.SolutionId);
studentPersonalConsumer.Start("Sif3DemoZone1", "DEFAULT");
if (log.IsInfoEnabled) log.Info("Started the Event Consumer.");

Console.WriteLine("Press any key to stop the Event Consumer (may take several seconds to complete) ...");
Console.ReadKey();

studentPersonalConsumer.Stop();
if (log.IsInfoEnabled) log.Info("Stopped the Event Consumer.");
}
catch (Exception e)
{
if (log.IsErrorEnabled) log.Error("Error running the Student Personal Event Consumer.\n" + ExceptionUtils.InferErrorResponseMessage(e), e);
}
}

Console.WriteLine("Press any key to continue ...");
Console.ReadKey();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="EventConsumerApp.cs" />
<Compile Include="EnrollmentConsumerApp.cs" />
<Compile Include="ConsumerApp.cs" />
<Compile Include="Consumers\StudentSchoolEnrollmentConsumer.cs" />
Expand Down

0 comments on commit de0f675

Please sign in to comment.