This sample shows a report that outputs the results of a job search.
This could be used to report on all Active Jobs of a particular type.
using Contracts;
using Contracts.Data;
using Contracts.Databases;
using Contracts.Responses;
using SynergyServerScripting;
using System;
using System.Collections.Generic;
namespace OutOfTheBoxReports.EntityReporting.Projects
{
public class SearchProjectListReport : IReport, IReportCategoriser
{
private EntityID _user_id;
private const string INPUT_SEARCH_QUERY = "Search query";
private const string INPUT_ATTRIBUTE_LIST = "Attribute list";
/// <summary>
/// Get the name of the report
/// </summary>
/// <returns></returns>
public string GetName() => "Job List Report";
/// <summary>
/// Get the description of the report
/// </summary>
/// <returns></returns>
public string GetDescription() => "A report contains a table with rows for the jobs found.";
/// <summary>
/// Get the fixed GUID
/// </summary>
/// <returns></returns>
public Guid GetGuid() => Guid.Parse("{EC24DBB0-02A5-4D56-AE4A-7E40F74474C6}");
/// <summary>
/// Get the available extensions
/// </summary>
/// <returns></returns>
public List<string> GetExtensions() => new List<string> { ".html", ".csv" };
/// <summary>
/// Get all inputs
/// </summary>
/// <returns></returns>
public IEnumerable<ScriptInput> GetInputs()
{
List<ScriptInput> inputs = new List<ScriptInput>();
inputs.Add(new QueryScriptInput(INPUT_SEARCH_QUERY, "Job search query", false, EntityType.Project));
inputs.Add(new AttributeListScriptInput(INPUT_ATTRIBUTE_LIST, "List of attributes to be displayed in the report", true, AttributeSearchContext.ProjectAttribute));
return inputs;
}
/// <summary>
/// Can the user access this report? In this case, yes - what the report returns is permission checked
/// </summary>
/// <param name="user_id"></param>
/// <param name="database"></param>
/// <returns></returns>
public bool CanUserAccess(EntityID user_id, IDatabase database)
{
_user_id = user_id;
return true;
}
/// <summary>
/// Generate the report
/// </summary>
/// <param name="inputs"></param>
/// <param name="output_file"></param>
/// <param name="db"></param>
/// <returns></returns>
public OperationResponse GenerateReport(Dictionary<string, ScriptInput> inputs, string output_file, IDatabase db)
{
try
{
var stored_query = ((QueryScriptInput) inputs[INPUT_SEARCH_QUERY]).query;
var attribute_id_list = new List<EntityID>();
ScriptInput attribute_list_input;
if (inputs.TryGetValue(INPUT_ATTRIBUTE_LIST, out attribute_list_input))
{
attribute_id_list = ((AttributeListScriptInput) attribute_list_input).attribute_ids;
}
// resolve attribute names
var attribute_id_name_dic = new Dictionary<EntityID, string>();
foreach (EntityID id in attribute_id_list)
{
var info = SynergyServerScripting.Entities.Attribute.GetAttributeInfo(id, db);
if (info == null) continue;
attribute_id_name_dic[id] = info.name;
}
// perform job search
List<ProjectInfo> job_list = SynergyServerScripting.Entities.Project.SearchProjects(stored_query.query, _user_id, db);
// basic template
string header = GlobalReportHelper.GetHTMLHeader("Job List Report", db);
string footer = GlobalReportHelper.GetHTMLFooter();
// generate table
SimpleTable project_table = new SimpleTable();
project_table.AddColumn("Job Name", "JobName");
foreach (KeyValuePair<EntityID, string> pair in attribute_id_name_dic)
{
project_table.AddColumn(pair.Value, pair.Key.ToString());
}
// add rows to the table
foreach (ProjectInfo p_info in job_list)
{
SimpleTable.Row row = project_table.NewRow();
row[project_table.GetColumnIndexByVariable("JobName")] = p_info.name;
// attributes
foreach (KeyValuePair<EntityID, string> pair in attribute_id_name_dic)
{
// find the attibute
string value = "";
var attribute = p_info.attributes.Find(a => a.id == pair.Key);
if (attribute != null && attribute.value != null && attribute.value.Value != null)
value = attribute.value.Value.ToString();
row[project_table.GetColumnIndexByVariable(pair.Key.ToString())] = value;
}
project_table.AddRow(row);
}
// output to a file
using (var w = new System.IO.StreamWriter(output_file))
{
if (string.Compare(Contracts.IO.Path.GetExtension(output_file), ".html", StringComparison.OrdinalIgnoreCase) == 0)
{
w.Write(header);
w.WriteLine(GlobalReportHelper.GetReportGadgetStart("Search Results"));
w.Write(project_table.ToString());
w.WriteLine(GlobalReportHelper.GetReportGadgetEnd());
w.Write(footer);
}
else if (string.Compare(Contracts.IO.Path.GetExtension(output_file), ".csv", StringComparison.OrdinalIgnoreCase) == 0)
{
w.Write(project_table.ToCSV());
}
}
}
catch (Exception ex)
{
return OperationResponse.Error(0, ex.Message);
}
return new OperationResponse();
}
/// <summary>
/// The category of the report - useful for UI purposes
/// </summary>
public string Category => "Jobs";
}
}
This language is not supported or no code example is available.