163 lines
6.1 KiB
Plaintext
163 lines
6.1 KiB
Plaintext
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using DTS.Common.Classes.Tags;
|
|
using DTS.Common.Interface.Tags;
|
|
using DTS.Common.Utilities.Logging;
|
|
|
|
namespace DTS.Common.Classes
|
|
{
|
|
public abstract class TagAwareBase : Base.BasePropertyChanged
|
|
{
|
|
public abstract TagTypes TagType { get; }
|
|
|
|
#region Tags
|
|
public byte[] TagsBlobBytes
|
|
{
|
|
get
|
|
{
|
|
var result = new byte[TagIDs.Length * sizeof(int)];
|
|
Buffer.BlockCopy(TagIDs, 0, result, 0, result.Length);
|
|
return result;
|
|
}
|
|
set
|
|
{
|
|
if (value.Length < sizeof(int)) return;
|
|
var tagsBlob = new int[value.Length / sizeof(int)];
|
|
try
|
|
{
|
|
Buffer.BlockCopy(value, 0, tagsBlob, 0, value.Length);
|
|
TagIDs = tagsBlob;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
APILogger.Log(ex);
|
|
}
|
|
}
|
|
}
|
|
private int[] _tagIDs = new int[0];
|
|
public int[] TagIDs
|
|
{
|
|
get => _tagIDs;
|
|
set => _tagIDs = value ?? new int[0];
|
|
}
|
|
|
|
public void SetTagsFromCommaSeparatedString(string tagText,
|
|
Tags.TagsInstance.GetSqlCommandDelegate getSqlCommand,
|
|
Tags.TagsInstance.TagsGetDelegate tagsGet,
|
|
Tags.TagsInstance.TagsGetIdDelegate tagsGetId,
|
|
Tags.TagsInstance.TagsInsertDelegate tagsInsert)
|
|
{
|
|
//if (string.IsNullOrEmpty(tagText)) { return; } // http://fogbugz/fogbugz/default.asp?7176
|
|
SetTags(tagText.Split(','), getSqlCommand, tagsGet, tagsGetId, tagsInsert);
|
|
}
|
|
public virtual void SetTags(string[] tagsText, Tags.TagsInstance.GetSqlCommandDelegate getSqlCommand,
|
|
Tags.TagsInstance.TagsGetDelegate tagsGet, Tags.TagsInstance.TagsGetIdDelegate tagsGetId,
|
|
Tags.TagsInstance.TagsInsertDelegate tagsInsert)
|
|
{
|
|
Tags.TagsInstance.AddRange(tagsText, getSqlCommand, tagsGet, tagsGetId, tagsInsert);
|
|
TagIDs = Tags.TagsInstance.GetIDsFromTagText(tagsText, tagsGet, tagsGetId);
|
|
OnPropertyChanged("TagIDs");
|
|
}
|
|
public string GetTagsAsCommaSeparatedString(Tags.TagsInstance.TagsGetDelegate tagsGet)
|
|
{
|
|
var sb = new StringBuilder();
|
|
var tagArray = GetTagsArray(tagsGet);
|
|
foreach (var s in tagArray)
|
|
{
|
|
if (sb.Length > 0) { sb.Append(","); }
|
|
sb.Append(s);
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
#region ITagAware
|
|
|
|
public virtual string[] GetTagsArray(Tags.TagsInstance.TagsGetDelegate tagsGet)
|
|
{
|
|
return Tags.TagsInstance.GetTagTextFromIDs(TagIDs, tagsGet);
|
|
}
|
|
public virtual int[] GetTagIDs() { return TagIDs; }
|
|
public virtual void RemoveTags(string[] tagsText)
|
|
{
|
|
// remove tags!!!
|
|
//-;
|
|
}
|
|
|
|
public bool TagCompatible(string tags, Tags.TagsInstance.TagsGetDelegate tagsGet)
|
|
{
|
|
//Make sure there are Tags to check
|
|
if (string.IsNullOrWhiteSpace(tags)) return true;
|
|
var newTagsArray = tags.Split(',');
|
|
|
|
//Make sure all Tags are not empty strings
|
|
if (newTagsArray.All(string.IsNullOrWhiteSpace)) return true;
|
|
|
|
var comparisonArray = GetTagsArray(tagsGet);
|
|
foreach (var tag in newTagsArray)
|
|
{
|
|
//If a Tag is an empty string, ignore it
|
|
if (string.IsNullOrWhiteSpace(tag)) continue;
|
|
if (comparisonArray.Contains(tag.Trim()))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
public virtual bool TagCompatible(int[] tags)
|
|
{
|
|
return !tags.Any() || HasIntersectingTag(tags);
|
|
}
|
|
public virtual bool HasIntersectingTag(int[] tags)
|
|
{
|
|
return tags.Intersect(TagIDs).Any();
|
|
}
|
|
#endregion
|
|
#endregion
|
|
|
|
public void InsertTagsFromCommaSeparatedString(int id, TagTypes tagType, string tags,
|
|
Tags.TagsInstance.GetSqlCommandDelegate getSqlCommand, Tags.TagsInstance.TagsGetDelegate tagsGet,
|
|
Tags.TagsInstance.TagsGetIdDelegate tagsIdGet, Tags.TagsInstance.TagsInsertDelegate tagsInsert,
|
|
Tags.TagsInstance.TagAssignmentsDelete tagAssignmentsDelete, Tags.TagsInstance.TagAssignmentsInsert tagAssignmentsInsert)
|
|
{
|
|
SetTagsFromCommaSeparatedString(tags, getSqlCommand, tagsGet, tagsIdGet, tagsInsert);
|
|
Commit(id, tagType, tagAssignmentsDelete, tagAssignmentsInsert);
|
|
}
|
|
|
|
public void Commit(int id, TagTypes tagType,
|
|
Tags.TagsInstance.TagAssignmentsDelete tagAssignmentsDelete,
|
|
Tags.TagsInstance.TagAssignmentsInsert tagAssignmentInsert)
|
|
{
|
|
_ = tagAssignmentsDelete(tagType, id);
|
|
|
|
if (!TagIDs.Any()) return;
|
|
|
|
foreach (var tagId in TagIDs)
|
|
{
|
|
_ = tagAssignmentInsert(new TagAssignment() { ObjectID = id, ObjectType = tagType, TagID = tagId });
|
|
}
|
|
}
|
|
|
|
public List<int> GetTagIdList(int objectId, TagTypes tagType,
|
|
Tags.TagsInstance.TagAssignmentsGet tagAssignmentsGet)
|
|
{
|
|
var tagIdList = new List<int>();
|
|
var hr = tagAssignmentsGet(tagType, out var records);
|
|
if (0 == hr && null != records && records.Any())
|
|
{
|
|
foreach (var record in records)
|
|
{
|
|
if( record.ObjectID != objectId) { continue; }
|
|
if (!tagIdList.Contains(record.TagID))
|
|
{
|
|
tagIdList.Add(record.TagID);
|
|
}
|
|
}
|
|
}
|
|
|
|
return tagIdList;
|
|
}
|
|
}
|
|
}
|