Skip to content

Commit

Permalink
Merge pull request dubeaud#128 from PeterDefendo/master
Browse files Browse the repository at this point in the history
Implemented dubeaud#62
  • Loading branch information
dubeaud committed Dec 16, 2015
2 parents c3fa90d + 7a44f31 commit 72d598c
Show file tree
Hide file tree
Showing 61 changed files with 3,064 additions and 183 deletions.
2 changes: 2 additions & 0 deletions src/BugNET.BLL/BugNET.BLL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
<Compile Include="CategoryTree.cs" />
<Compile Include="Comparers\ObjectComparer.cs" />
<Compile Include="Comparers\ProjectComparer.cs" />
<Compile Include="UserCustomFieldSelectionManager.cs" />
<Compile Include="UserCustomFieldManager.cs" />
<Compile Include="CustomFieldManager.cs" />
<Compile Include="CustomFieldSelectionManager.cs" />
<Compile Include="HostSettingManager.cs" />
Expand Down
238 changes: 238 additions & 0 deletions src/BugNET.BLL/UserCustomFieldManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
using System;
using System.Collections.Generic;
using System.Text;
using BugNET.Common;
using BugNET.DAL;
using BugNET.Entities;
using log4net;

namespace BugNET.BLL
{
public static class UserCustomFieldManager
{
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

/// <summary>
/// Saves this instance.
/// </summary>
/// <param name="entity">The custom field to save.</param>
/// <returns></returns>
public static bool SaveOrUpdate(UserCustomField entity)
{
if (entity == null) throw new ArgumentNullException("entity");
if (string.IsNullOrEmpty(entity.Name)) throw (new ArgumentException("The custom field name cannot be empty or null"));

if (entity.Id > Globals.NEW_ID)
if (DataProviderManager.Provider.UpdateUserCustomField(entity))
{
UpdateCustomFieldView();
return true;
}

var tempId = DataProviderManager.Provider.CreateNewUserCustomField(entity);

if (tempId <= 0)
return false;

entity.Id = tempId;
UpdateCustomFieldView();
return true;
}

/// <summary>
/// Deletes the custom field.
/// </summary>
/// <param name="customFieldId">The custom field id.</param>
/// <returns></returns>
public static bool Delete(int customFieldId)
{
if (customFieldId <= Globals.NEW_ID) throw (new ArgumentOutOfRangeException("customFieldId"));
var entity = GetById(customFieldId);

if (entity == null) return true;

if (DataProviderManager.Provider.DeleteUserCustomField(entity.Id))
{
UpdateCustomFieldView();
return true;
}

return false;
}

/// <summary>
/// Saves the custom field values.
/// </summary>
/// <param name="issueId">The issue id.</param>
/// <param name="fields">The fields.</param>
/// <returns></returns>
public static bool SaveCustomFieldValues(Guid userId, List<UserCustomField> fields)
{
if (fields == null) throw (new ArgumentOutOfRangeException("fields"));

try
{
DataProviderManager.Provider.SaveUserCustomFieldValues(userId, fields);

return true;
}
catch(Exception ex)
{
Log.Error(LoggingManager.GetErrorMessageResource("SaveCustomFieldValuesError"), ex);
return false;
}
}

/// <summary>
/// Gets the custom fields by project id.
/// </summary>
/// <param name="projectId">The project id.</param>
/// <returns></returns>
public static List<UserCustomField> GetAll()
{
return (DataProviderManager.Provider.GetUserCustomFields());
}

/// <summary>
/// Gets the custom field by id.
/// </summary>
/// <param name="customFieldId">The custom field id.</param>
/// <returns></returns>
public static UserCustomField GetById(int customFieldId)
{
if (customFieldId <= Globals.NEW_ID) throw (new ArgumentOutOfRangeException("customFieldId"));

return DataProviderManager.Provider.GetUserCustomFieldById(customFieldId);
}

/// <summary>
/// Gets the custom fields by issue id.
/// </summary>
/// <param name="issueId">The issue id.</param>
/// <returns></returns>
public static List<UserCustomField> GetByUserId(Guid userId)
{
if (userId == Guid.Empty) throw (new ArgumentOutOfRangeException("userId"));

return (DataProviderManager.Provider.GetUserCustomFieldsByUserId(userId));
}

/// <summary>
/// Creates a PIVOT view for the custom fields to make loading them easier for the UI
/// this will only work with SQL 2005 and higher
/// </summary>
/// <param name="projectId">THe project id for the custom fields</param>
public static bool UpdateCustomFieldView()
{
/* not implemented!
var customFields = Get();
var sb = new StringBuilder();
var viewName = Globals.USER_CUSTOM_FIELDS_VIEW_NAME;
sb.AppendFormat("SET ANSI_NULLS ON {0}SET XACT_ABORT ON {0}SET QUOTED_IDENTIFIER ON {0}", Environment.NewLine);
sb.AppendFormat("IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[{0}]') AND OBJECTPROPERTY(id, N'IsView') = 1) {1}", viewName, Environment.NewLine);
sb.AppendFormat("DROP VIEW [{0}] {1}", viewName, Environment.NewLine);
sb.AppendFormat("EXEC dbo.sp_executesql @statement = N'CREATE VIEW [dbo].[{0}]{1} ", viewName, Environment.NewLine);
sb.AppendFormat("AS {0} ", Environment.NewLine);
sb.AppendFormat("SELECT i.ProjectId, i.IssueId, i.[IsClosed], i.[Disabled]{0}", Environment.NewLine);
foreach (var customField in customFields)
{
sb.AppendFormat(",ISNULL(p.[{0}], '''') AS [{2}{0}]{1} ", customField.Name.Replace("'", "''"), Environment.NewLine, Globals.PROJECT_CUSTOM_FIELDS_PREFIX);
}
sb.AppendFormat("FROM{0} ", Environment.NewLine);
sb.AppendFormat("BugNet_IssuesView i{0} ", Environment.NewLine);
if (customFields.Count > 0)
{
sb.AppendFormat("LEFT JOIN{0} ", Environment.NewLine);
sb.AppendFormat("({0}", Environment.NewLine);
sb.AppendFormat("SELECT ucfv.UserId, ucf.CustomFieldName, ucfv.CustomFieldValue{0}", Environment.NewLine);
sb.AppendFormat("FROM BugNet_UserCustomFields ucf {0}", Environment.NewLine);
sb.AppendFormat("INNER JOIN BugNet_UserCustomFieldValues ucfv ON ucf.CustomFieldId = ucfv.CustomFieldId{0} ", Environment.NewLine);
sb.AppendFormat(") AS data{0} ", Environment.NewLine);
sb.AppendFormat("PIVOT{0} ", Environment.NewLine);
sb.AppendFormat("({0} ", Environment.NewLine);
sb.AppendFormat("MAX(data.CustomFieldValue) FOR data.CustomFieldName IN {0} ", Environment.NewLine);
sb.AppendFormat("({0} ", Environment.NewLine);
foreach (var customField in customFields)
{
sb.AppendFormat("[{0}],", customField.Name.Replace("'", "''"));
}
sb.Remove(sb.Length - 1, 1);
sb.AppendFormat("){0} ", Environment.NewLine);
sb.AppendFormat(") AS p ON i.IssueId = p.IssueId AND i.ProjectId = p.ProjectId{0} ", Environment.NewLine);
}
try
{
#if (DEBUG)
System.Diagnostics.Debug.WriteLine(sb.ToString());
#endif
DataProviderManager.Provider.ExecuteScript(new[] { sb.ToString() });
return true;
}
catch (Exception ex)
{
Log.Error(ex);
}
*/

return false;
}

/*
private static List<IssueHistory> GetCustomFieldChanges(int issueId, List<CustomField> originalFields, List<CustomField> newFields)
{
var fieldChanges = new List<IssueHistory>();
foreach(CustomField cf in newFields)
{
var field = originalFields.Find(f => f.Id == cf.Id);
if(field != null && field.Value != cf.Value)
{
var history = new IssueHistory { CreatedUserName = Security.GetUserName(), IssueId = issueId, DateChanged = DateTime.Now };
fieldChanges.Add(GetNewIssueHistory(history, cf.Name, field.Value, cf.Value));
}
else if(field == null)
{
// new field added - do we want to track history for this since a value wasn't selected
var history = new IssueHistory { CreatedUserName = Security.GetUserName(), IssueId = issueId, DateChanged = DateTime.Now };
fieldChanges.Add(GetNewIssueHistory(history, cf.Name, string.Empty, cf.Value));
}
}
return fieldChanges;
}
private static IssueHistory GetNewIssueHistory(IssueHistory history, string fieldChanged, string oldValue, string newValue)
{
return new IssueHistory
{
CreatedUserName = history.CreatedUserName,
CreatorDisplayName = string.Empty,
DateChanged = history.DateChanged,
FieldChanged = fieldChanged,
IssueId = history.IssueId,
NewValue = newValue,
OldValue = oldValue
};
}
private static void UpdateHistory(IEnumerable<IssueHistory> issueChanges)
{
if (issueChanges == null) return;
foreach (var issueHistory in issueChanges)
{
issueHistory.TriggerLastUpdateChange = false; // set this to false since we don't trigger it from here
IssueHistoryManager.SaveOrUpdate(issueHistory);
}
}
*/
}
}
68 changes: 68 additions & 0 deletions src/BugNET.BLL/UserCustomFieldSelectionManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using BugNET.Common;
using BugNET.DAL;
using BugNET.Entities;
using log4net;

namespace BugNET.BLL
{
public static class UserCustomFieldSelectionManager
{
private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

/// <summary>
/// Saves this instance.
/// </summary>
/// <param name="entity">The custom field selection to save.</param>
/// <returns></returns>
public static bool SaveOrUpdate(UserCustomFieldSelection entity)
{
if (entity.Id > Globals.NEW_ID)
return (DataProviderManager.Provider.UpdateUserCustomFieldSelection(entity));

var tempId = DataProviderManager.Provider.CreateNewUserCustomFieldSelection(entity);

if (tempId <= 0)
return false;

entity.Id = tempId;
return true;
}

/// <summary>
/// Deletes the custom field selection.
/// </summary>
/// <param name="customFieldSelectionId">The custom field selection id.</param>
/// <returns></returns>
public static bool Delete(int customFieldSelectionId)
{
if (customFieldSelectionId <= Globals.NEW_ID) throw (new ArgumentOutOfRangeException("customFieldSelectionId"));

return (DataProviderManager.Provider.DeleteUserCustomFieldSelection(customFieldSelectionId));
}


/// <summary>
/// Gets the custom fields selections by custom field id.
/// </summary>
/// <param name="customFieldId">The custom field id.</param>
/// <returns></returns>
public static List<UserCustomFieldSelection> GetByCustomFieldId(int customFieldId)
{
if (customFieldId <= Globals.NEW_ID) throw (new ArgumentOutOfRangeException("customFieldId"));

return (DataProviderManager.Provider.GetUserCustomFieldSelectionsByCustomFieldId(customFieldId));
}

/// <summary>
/// Gets the custom field selection by id.
/// </summary>
/// <param name="id">The id.</param>
/// <returns></returns>
public static UserCustomFieldSelection GetById(int id)
{
return (DataProviderManager.Provider.GetUserCustomFieldSelectionById(id));
}
}
}
3 changes: 3 additions & 0 deletions src/BugNET.Common/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static class Globals
public const string PROJECT_CUSTOM_FIELDS_VIEW_NAME = "BugNet_P{0}_CFV";
public const string PROJECT_CUSTOM_FIELDS_PREFIX = "bgn_cf_";

public const string USER_CUSTOM_FIELDS_VIEW_NAME = "BugNet_USER_CFV";
public const string USER_CUSTOM_FIELDS_PREFIX = "bgn_ucf_";

/// <summary>
/// The default length of short comments (if not specified).
/// </summary>
Expand Down
24 changes: 20 additions & 4 deletions src/BugNET.DAL/DataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ public abstract partial class DataProvider : ProviderBase
public abstract List<IssueNotification> GetIssueNotificationsByIssueId(int issueId);
public abstract bool DeleteIssueNotification(int issueId, string username);

//IssueRevisions
// IssueRevisions
public abstract int CreateNewIssueRevision(IssueRevision newIssueRevision);
public abstract List<IssueRevision> GetIssueRevisionsByIssueId(int issueId);
public abstract bool DeleteIssueRevision(int issueRevisionId);

//IssueVote
// IssueVote
public abstract int CreateNewIssueVote(IssueVote newIssueVote);
public abstract bool HasUserVoted(int issueId, string username);

Expand Down Expand Up @@ -192,17 +192,33 @@ public abstract partial class DataProvider : ProviderBase
public abstract List<Resolution> GetResolutionsByProjectId(int projectId);
public abstract bool CanDeleteResolution(int ResolutionId);

//Project Notifications
// Project Notifications
public abstract int CreateNewProjectNotification(ProjectNotification newProjectNotification);
public abstract List<ProjectNotification> GetProjectNotificationsByProjectId(int projectId);
public abstract bool DeleteProjectNotification(int projectId, string username);
public abstract List<ProjectNotification> GetProjectNotificationsByUsername(string username);

//Users
// Users
public abstract List<ITUser> GetUsersByProjectId(int projectId);
public abstract List<ITUser> GetUsersByProjectId(int projectId, bool excludeReadOnlyUsers);
public abstract string GetUserNameByPasswordResetToken(string token);

// User Custom Fields
public abstract List<UserCustomField> GetUserCustomFields();
public abstract UserCustomField GetUserCustomFieldById(int customFieldId);
public abstract List<UserCustomField> GetUserCustomFieldsByUserId(Guid userId);
public abstract int CreateNewUserCustomField(UserCustomField newCustomField);
public abstract bool UpdateUserCustomField(UserCustomField customFieldToUpdate);
public abstract bool DeleteUserCustomField(int customFieldId);
public abstract bool SaveUserCustomFieldValues(Guid userId, List<UserCustomField> fields);

// User Custom Field Selections
public abstract int CreateNewUserCustomFieldSelection(UserCustomFieldSelection newCustomFieldSelection);
public abstract bool DeleteUserCustomFieldSelection(int customFieldSelectionId);
public abstract List<UserCustomFieldSelection> GetUserCustomFieldSelectionsByCustomFieldId(int customFieldId);
public abstract UserCustomFieldSelection GetUserCustomFieldSelectionById(int customFieldSelectionId);
public abstract bool UpdateUserCustomFieldSelection(UserCustomFieldSelection customFieldSelectionToUpdate);

// Role
public abstract List<Role> GetAllRoles();
public abstract bool UpdateRole(Role roleToUpdate);
Expand Down
Loading

0 comments on commit 72d598c

Please sign in to comment.