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

Feature kfs slingshot.ccb.update #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions Slingshot.CCB/Slingshot.CCB/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Label Content="Advanced Options:" FontWeight="Bold" Margin="0,0,0,0"/>
<CheckBox Name="cbDumpResponseToXmlFile" Content="Dump response to XML files" Margin="5,10,0,10" Padding="3,0,0,0" />
<CheckBox Name="cbConsolidateSchedules" IsChecked="True" Content="Consolidate schedules to Day of Week and Time" Margin="5,0,0,10" Padding="3,0,0,0" />
<CheckBox Name="cbDirectorsAsGroups" IsChecked="True" Content="Export Directors as Groups" Margin="5,0,0,10" Padding="3,0,0,0" />
<StackPanel Orientation="Horizontal" Margin="5,0,0,0">
<Label Name="lblItemsPerPage" Content="Items Per Page:" HorizontalAlignment="Left" Padding="0,5,5,5" Width="100" />
<TextBox x:Name="txtItemsPerPage"
Expand Down
3 changes: 3 additions & 0 deletions Slingshot.CCB/Slingshot.CCB/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ private void btnDownloadPackage_Click( object sender, RoutedEventArgs e )
// Set ConsolidateScheduleNames to true to consolidate schedules names as 'Sunday at 11:00 AM'
CcbApi.ConsolidateScheduleNames = cbConsolidateSchedules.IsChecked ?? false;

// Set ExportDepartmentDirectorsAsGroups to true to export Directors as groups
CcbApi.ExportDirectorsAsGroups = cbDirectorsAsGroups.IsChecked ?? false;

// Reset API Request Counter.
CcbApi.ApiRequestCount = 0;

Expand Down
25 changes: 21 additions & 4 deletions Slingshot.CCB/Slingshot.CCB/Utilities/CcbApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ public static int ApiThrottleRate
/// </value>
public static bool ConsolidateScheduleNames { get; set; } = false;

/// <summary>
/// Gets or sets a value indicating whether [export directors as groups].
/// Set ExportDirectorsAsGroups to true to export Directors as Groups in the hierarchy.
/// </summary>
/// <value>
/// <c>true</c> if [export directors as groups]; otherwise, <c>false</c>.
/// </value>
public static bool ExportDirectorsAsGroups { get; set; } = false;

/// <summary>
/// Gets or sets the daily limit.
/// </summary>
Expand Down Expand Up @@ -474,7 +483,7 @@ private static bool GetGroupProfiles( List<int> selectedGroupTypes, DateTime? mo

if ( selectedGroupTypes.Contains( groupTypeId ) )
{
var importGroups = CcbGroup.Translate( groupNode );
var importGroups = CcbGroup.Translate( groupNode, CcbApi.ExportDirectorsAsGroups );

if ( importGroups != null )
{
Expand Down Expand Up @@ -562,7 +571,7 @@ private static bool GetGroupProfilesWorkaround( List<int> selectedGroupTypes, Da
// any participants.
IncompleteGroups.Add( groupId );

var importGroups = CcbGroup.Translate( groupNode );
var importGroups = CcbGroup.Translate( groupNode, CcbApi.ExportDirectorsAsGroups );

if ( importGroups != null )
{
Expand Down Expand Up @@ -615,7 +624,7 @@ private static void GetGroupProfileById( int groupId )
return;
}

var importGroups = CcbGroup.Translate( groupNode );
var importGroups = CcbGroup.Translate( groupNode, CcbApi.ExportDirectorsAsGroups );
if ( importGroups == null )
{
return;
Expand Down Expand Up @@ -645,11 +654,19 @@ private static void ExportDepartments()
}

var sourceDepartments = xdocCustomFields.Element( "ccb_api" )?.Element( "response" )?.Elements( "items" );
MD5 md5Hasher = MD5.Create();

foreach ( var sourceDepartment in sourceDepartments.Elements( "item" ) )
{
// Use hash of 9999 + department id to create a unique group id
var hashedDepartmentId = md5Hasher.ComputeHash( Encoding.UTF8.GetBytes( $@"
{9999}
{sourceDepartment.Element( "id" ).Value}
" ) );
var groupId = Math.Abs( BitConverter.ToInt32( hashedDepartmentId, 0 ) ); // used abs to ensure positive number

var group = new Group();
group.Id = ( "9999" + sourceDepartment.Element( "id" ).Value ).AsInteger();
group.Id = groupId;
group.Name = sourceDepartment.Element( "name" )?.Value;
group.Order = sourceDepartment.Element( "order" ).Value.AsInteger();
group.IsActive = true;
Expand Down
45 changes: 36 additions & 9 deletions Slingshot.CCB/Slingshot.CCB/Utilities/Translators/CcbGroup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
Expand All @@ -11,7 +12,7 @@ namespace Slingshot.CCB.Utilities.Translators
{
public static class CcbGroup
{
public static List<Group> Translate( XElement inputGroup )
public static List<Group> Translate( XElement inputGroup, bool exportDirectorsAsGroups = true )
{
List<Group> groups = new List<Group>();

Expand Down Expand Up @@ -40,22 +41,48 @@ public static List<Group> Translate( XElement inputGroup )

groups.Add( group );

// add the department as a group with an id of 9999 + its id to create a unique group id for it
if ( inputGroup.Element( "department" ) != null && inputGroup.Element( "department" ).Attribute( "id" ) != null && inputGroup.Element( "department" ).Attribute( "id" ).Value.IsNotNullOrWhitespace() )
var hasDepartment = inputGroup.Element( "department" ) != null && inputGroup.Element( "department" ).Attribute( "id" ) != null && inputGroup.Element( "department" ).Attribute( "id" ).Value.IsNotNullOrWhitespace();
var hasDirector = inputGroup.Element( "director" ) != null && inputGroup.Element( "director" ).Attribute( "id" ) != null && inputGroup.Element( "director" ).Attribute( "id" ).Value.IsNotNullOrWhitespace();

MD5 md5Hasher = MD5.Create();

// add the department as a group with id set to hash of 9999 + department id to create a unique group id for it
if ( hasDepartment )
{
departmentId = ( "9999" + inputGroup.Element( "department" ).Attribute( "id" ).Value ).AsInteger();
var hashedDepartmentId = md5Hasher.ComputeHash( Encoding.UTF8.GetBytes( $@"
{9999}
{inputGroup.Element( "department" ).Attribute( "id" ).Value}
" ) );
departmentId = Math.Abs( BitConverter.ToInt32( hashedDepartmentId, 0 ) ); // used abs to ensure positive number

var departmentName = inputGroup.Element( "department" ).Value;
if ( departmentName.IsNullOrWhiteSpace() )
{
departmentName = "No Department Name";
}
groups.Add( new Group { Id = departmentId.Value, Name = inputGroup.Element( "department" ).Value, IsActive = true, GroupTypeId = 9999 } );
var departmentGroup = new Group();
departmentGroup.Id = departmentId.Value;
departmentGroup.IsActive = true;
departmentGroup.Name = departmentName;
departmentGroup.GroupTypeId = 9999;

if ( !exportDirectorsAsGroups && hasDirector )
{
departmentGroup.GroupMembers.Add( new GroupMember { PersonId = inputGroup.Element( "director" ).Attribute( "id" ).Value.AsInteger(), Role = "Director", GroupId = departmentGroup.Id } );
}
groups.Add( departmentGroup );
}

// add the director as a group with an id of 9998 + its id to create a unique group id for it
if ( inputGroup.Element( "director" ) != null && inputGroup.Element( "director" ).Attribute( "id" ) != null && inputGroup.Element( "director" ).Attribute( "id" ).Value.IsNotNullOrWhitespace() )
if ( exportDirectorsAsGroups && hasDirector )
{
directorId = ( "9998" + inputGroup.Element( "director" ).Attribute( "id" ).Value ).AsInteger();
// add the director as a group with an id of 9998 + its id + its department id (if any) to create a unique group id for it
guymaness2 marked this conversation as resolved.
Show resolved Hide resolved
var departmentIdString = departmentId.HasValue ? departmentId.Value.ToString() : string.Empty;
var hashedDirectorId = md5Hasher.ComputeHash( Encoding.UTF8.GetBytes( $@"
{9998}
{inputGroup.Element( "director" ).Attribute( "id" ).Value}
{departmentIdString}
" ) );
directorId = Math.Abs( BitConverter.ToInt32( hashedDirectorId, 0 ) ); // used abs to ensure positive number

var directorGroup = new Group();
directorGroup.Id = directorId.Value;
Expand Down Expand Up @@ -145,7 +172,7 @@ public static List<Group> Translate( XElement inputGroup )
}

// determine the parent group
if ( directorId.HasValue )
if ( exportDirectorsAsGroups && directorId.HasValue )
{
group.ParentGroupId = directorId.Value;
}
Expand Down