This scheduled extension will run in the background and update the work area names with the number of hits/matches for the work areas and saved queries. See the image below. Since it's a scheduled extension and it's not running all the time the result isn't always 100% up-to-date and users are to view this more as an indication for the number or hits/matches for a workarea.
It's well recieved by some of our customers that relies a lot on saved queries for their workflows as they at a glance can see if they have work to perform or not.
Please upvote this feature request if you would like inriver to implement this as a standard functionality.
The code below is without bells and whistles so there are opportunities for improvements and additional functionality (I have a nice backlog and have already implemented but not shared some of it). It's offered as-is, so use at your own discretion.
using inRiver.Remoting.Extension;
using inRiver.Remoting.Extension.Interface;
using inRiver.Remoting.Log;
using inRiver.Remoting.Objects;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace ICOS.Extensions.WorkAreas
{
public class WorkAreaHitCounterScheduledExtension : IScheduledExtension
{
public inRiverContext Context { get; set; }
public Dictionary<string, string> DefaultSettings => new Dictionary<string, string>();
public void Execute(bool force)
{
UpdateSharedWorkAreaFolders();
UpdateWorkAreaFoldersForUsers();
}
public string Test()
{
return $"The '{Context.ExtensionId}' is responding.";
}
private void UpdateSharedWorkAreaFolders()
{
// Get the list of the shared work area folders
List<WorkAreaFolder> sharedWorkAreaFolderList = null;
try
{
sharedWorkAreaFolderList = Context.ExtensionManager.UtilityService.GetAllSharedWorkAreaFolders(true);
}
catch (Exception ex)
{
Context.Log(LogLevel.Warning, "Failed to update shared work area folders!", ex);
}
if (sharedWorkAreaFolderList?.Count == 0)
return;
// Updates all shared work area folders
foreach (var workArea in sharedWorkAreaFolderList)
UpdateWorkAreaFolder(workArea);
}
private void UpdateWorkAreaFoldersForUsers()
{
// Get the list of users
List<User> userList = null;
try
{
userList = Context.ExtensionManager.UserService.GetAllUsers();
}
catch (Exception ex)
{
Context.Log(LogLevel.Warning, "Failed to get list of users!", ex);
}
if (userList?.Count == 0)
return;
// Updates work area folders for all users
foreach (var user in userList)
{
try
{
var userWorkAreaFolderList = Context.ExtensionManager.UtilityService.GetAllPersonalWorkAreaFoldersForUser(user.Username, true);
if (userWorkAreaFolderList?.Count == 0)
continue;
foreach (var workAreaFolder in userWorkAreaFolderList)
UpdateWorkAreaFolder(workAreaFolder, user);
}
catch (Exception ex)
{
// TODO: This will some times fail if user hasn't logged in for the first time? Report as a bug to inriver if it turns out to be the case.
Context.Log(LogLevel.Warning, "Failed to get work areas for user: " + user.Username, ex);
}
}
}
private void UpdateWorkAreaFolder(WorkAreaFolder workArea, User user = null)
{
if (workArea == null || string.IsNullOrEmpty(workArea.Name))
return;
var numberOfEntities = 0;
if (workArea.IsQuery)
{
try
{
// Get the work area again because we don't get the query when we get all work areas (TODO: Report as bug to inriver or is it fixed now?)
if (user != null)
workArea = Context.ExtensionManager.UtilityService.GetPersonalWorkAreaFolder(workArea.Id);
else
workArea = Context.ExtensionManager.UtilityService.GetSharedWorkAreaFolder(workArea.Id);
}
catch (Exception ex)
{
Context.Logger.Log(LogLevel.Warning, $"Failed to get work area folder with id '{workArea?.Id}' for user '{user?.Username}'!", ex);
return;
}
if (workArea?.Query == null)
return;
try
{
var result = Context.ExtensionManager.DataService.Search(workArea.Query, LoadLevel.Shallow);
if (result != null)
numberOfEntities = result.Count;
}
catch (Exception ex)
{
Context.Logger.Log(LogLevel.Warning, $"Failed to execute query for work area folder with id '{workArea?.Id}' for user '{user?.Username}'!", ex);
}
}
else
{
if (workArea.FolderEntities?.Count > 0)
numberOfEntities = workArea.FolderEntities.Count;
}
// Get new name if needed
var newName = workArea.Name;
try
{
Regex regex = new Regex(@"^\(\d+\) .+");
Match match = regex.Match(newName);
if (match.Success)
{
var numberString = newName.Substring(1, newName.IndexOf(')') - 1);
var nameAfterNumber = newName.Substring(newName.IndexOf(')') + 2);
if (int.TryParse(numberString, out int currentNumber))
{
if (currentNumber == numberOfEntities)
return;
newName = $"({numberOfEntities}) {nameAfterNumber}";
}
}
else
{
newName = $"({numberOfEntities}) {newName}";
}
}
catch (Exception ex)
{
Context.Logger.Log(LogLevel.Warning, $"Failed to get new name for work area folder with id '{workArea?.Id}' for user '{user?.Username}'!", ex);
}
if (newName == workArea.Name)
return;
// Update work area folder with new name
try
{
if (user != null)
Context.ExtensionManager.UtilityService.UpdatePersonalWorkAreaFolderName(workArea.Id, newName);
else
Context.ExtensionManager.UtilityService.UpdateSharedWorkAreaFolderName(workArea.Id, newName);
}
catch (Exception ex)
{
Context.Logger.Log(LogLevel.Warning, $"Failed to update name for work area folder with id '{workArea?.Id}' for user '{user?.Username}'!", ex);
}
}
}
}
-
Hi Roy Eriksson!
Great to see more tips & tricks! This is fantastic 🤩Thanks for sharing!
0
Please sign in to leave a comment.
Comments
1 comment