387 lines
17 KiB
Plaintext
387 lines
17 KiB
Plaintext
using DbAPI.Connections;
|
|
using DbAPI.Errors;
|
|
using DbAPI.Logging;
|
|
using DTS.Common.Classes.Tags;
|
|
using DTS.Common.Interface.Database;
|
|
using DTS.Common.Interface.Tags;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Diagnostics;
|
|
|
|
namespace DbAPI.Tags
|
|
{
|
|
/// <summary>
|
|
/// defines functions to create, retrieve, update, delete tags
|
|
/// <inheritdoc cref="ITags"/>
|
|
/// </summary>
|
|
public class Tags : ITags
|
|
{
|
|
/// <summary>
|
|
/// inserts a tag assignment record into the database
|
|
/// </summary>
|
|
/// <param name="user">user making request</param>
|
|
/// <param name="connection">connection request is being made on</param>
|
|
/// <param name="tagAssignment">assignment to be committed</param>
|
|
/// <returns>0 (ERROR_SUCCESS) on success, all other values are error codes</returns>
|
|
public ulong TagAssignmentsInsert(IUserDbRecord user,
|
|
IConnectionDetails connection,
|
|
ITagAssignment tagAssignment)
|
|
{
|
|
if (!DbAPI.Connections.IsUserLoggedIn(user, connection))
|
|
{
|
|
return ErrorCodes.ERROR_ACCESS_DENIED;
|
|
}
|
|
if (null == tagAssignment)
|
|
{
|
|
return ErrorCodes.ERROR_MISSING_PARAMETER;
|
|
}
|
|
var ret = ConnectionManager.GetSqlCommand(connection, out var cmd, "sp_TagAssignmentsInsert");
|
|
if (ret != ErrorCodes.ERROR_SUCCESS) { return ret; }
|
|
try
|
|
{
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
|
|
cmd.Parameters.Add(new SqlParameter("@ObjectID", SqlDbType.Int) { Value = tagAssignment.ObjectID });
|
|
cmd.Parameters.Add(new SqlParameter("@ObjectType", SqlDbType.SmallInt) { Value = (short)tagAssignment.ObjectType });
|
|
cmd.Parameters.Add(new SqlParameter("@TagID", SqlDbType.Int) { Value = tagAssignment.TagID });
|
|
|
|
var errorNumber = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(errorNumber);
|
|
|
|
var errorMessage = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(errorMessage);
|
|
|
|
_ = cmd.ExecuteNonQuery();
|
|
|
|
if (!DBNull.Value.Equals(errorNumber.Value) && 0 != Convert.ToInt32(errorNumber.Value))
|
|
{
|
|
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.Tags, $"TagAssignmentsInsert error - {errorNumber.Value} - {errorMessage.Value}");
|
|
return ErrorCodes.ERROR_UNKNOWN;
|
|
}
|
|
|
|
return ErrorCodes.ERROR_SUCCESS;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.Tags, $"TagAssignmentsInsert error - {ex.Message}");
|
|
return ErrorCodes.ERROR_UNKNOWN;
|
|
}
|
|
finally
|
|
{
|
|
cmd.Connection.Dispose();
|
|
cmd.Dispose();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// deletes all matching tags.
|
|
/// </summary>
|
|
/// <param name="user">user making request</param>
|
|
/// <param name="connection">connection request is being made on</param>
|
|
/// <param name="objectId">object tags assigned to</param>
|
|
/// <param name="tagType">the object type</param>
|
|
/// <returns>0 (ERROR_SUCCESS) on success, all other values are error codes</returns>
|
|
public ulong TagAssignmentsDelete(IUserDbRecord user,
|
|
IConnectionDetails connection,
|
|
int objectId,
|
|
TagTypes tagType)
|
|
{
|
|
if (!DbAPI.Connections.IsUserLoggedIn(user, connection))
|
|
{
|
|
return ErrorCodes.ERROR_ACCESS_DENIED;
|
|
}
|
|
var ret = ConnectionManager.GetSqlCommand(connection, out var cmd, "sp_TagAssignmentsDelete");
|
|
if (ret != ErrorCodes.ERROR_SUCCESS) { return ret; }
|
|
try
|
|
{
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.Parameters.Add(new SqlParameter("@ObjectID", SqlDbType.Int) { Value = objectId });
|
|
cmd.Parameters.Add(new SqlParameter("@ObjectType", SqlDbType.SmallInt) { Value = (short)tagType });
|
|
var errorNumber = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(errorNumber);
|
|
var errorMessage = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(errorMessage);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
if (!DBNull.Value.Equals(errorNumber.Value) && 0 != Convert.ToInt32(errorNumber.Value))
|
|
{
|
|
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.Tags,
|
|
$"TagsAssignmentsDelete error - {errorNumber.Value} - {errorMessage.Value}");
|
|
return ErrorCodes.ERROR_UNKNOWN;
|
|
}
|
|
return ErrorCodes.ERROR_SUCCESS;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.Tags, $"TagAssignmentsDelete error - {ex.Message}");
|
|
return ErrorCodes.ERROR_UNKNOWN;
|
|
}
|
|
finally
|
|
{
|
|
cmd.Connection.Dispose();
|
|
cmd.Dispose();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// retrieves all tags that have been assigned to an object type
|
|
/// </summary>
|
|
/// <param name="user">user making request</param>
|
|
/// <param name="connection">connection request is being made on</param>
|
|
/// <param name="tagType">object type tags are assigned to (use null for all)</param>
|
|
/// <param name="tagAssignments">all matching assignments</param>
|
|
/// <returns>0 (ERROR_SUCCESS) on success, all other values are error codes</returns>
|
|
public ulong TagAssignmentsGet(IUserDbRecord user,
|
|
IConnectionDetails connection,
|
|
TagTypes? tagType,
|
|
out ITagAssignment[] tagAssignments
|
|
)
|
|
{
|
|
tagAssignments = new ITagAssignment[0];
|
|
if (!DbAPI.Connections.IsUserLoggedIn(user, connection))
|
|
{
|
|
return ErrorCodes.ERROR_ACCESS_DENIED;
|
|
}
|
|
var ret = ConnectionManager.GetSqlCommand(connection, out var cmd, "sp_TagAssignmentsGet");
|
|
if (ret != ErrorCodes.ERROR_SUCCESS) { return ret; }
|
|
try
|
|
{
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
if (null == tagType)
|
|
{
|
|
cmd.Parameters.Add(new SqlParameter("@ObjectType", SqlDbType.SmallInt) { Value = null });
|
|
}
|
|
else { cmd.Parameters.Add(new SqlParameter("@ObjectType", SqlDbType.SmallInt) { Value = (short)tagType }); }
|
|
|
|
var reader = cmd.ExecuteReader();
|
|
var list = new List<ITagAssignment>();
|
|
while (reader.Read())
|
|
{
|
|
list.Add(new TagAssignment(reader));
|
|
}
|
|
tagAssignments = list.ToArray();
|
|
return ErrorCodes.ERROR_SUCCESS;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.Tags, $"TagAssignmentsGet error - {ex.Message}");
|
|
return ErrorCodes.ERROR_UNKNOWN;
|
|
}
|
|
finally
|
|
{
|
|
cmd.Connection.Dispose();
|
|
cmd.Dispose();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// inserts a new tag, modifies tag with new id after insert
|
|
/// </summary>
|
|
/// <param name="user">user requesting insert</param>
|
|
/// <param name="connection">connection tag is being inserted on</param>
|
|
/// <param name="tag">tag to insert</param>
|
|
/// <returns>0 (ERROR_SUCCESS) on success, all other values are error codes</returns>
|
|
public ulong TagsInsert(IUserDbRecord user,
|
|
IConnectionDetails connection,
|
|
ref ITag tag)
|
|
{
|
|
if (!DbAPI.Connections.IsUserLoggedIn(user, connection))
|
|
{
|
|
return ErrorCodes.ERROR_ACCESS_DENIED;
|
|
}
|
|
if (null == tag)
|
|
{
|
|
return ErrorCodes.ERROR_MISSING_PARAMETER;
|
|
}
|
|
var ret = ConnectionManager.GetSqlCommand(connection, out var cmd, "sp_TagsInsert");
|
|
if (ret != ErrorCodes.ERROR_SUCCESS) { return ret; }
|
|
try
|
|
{
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.Parameters.Add(new SqlParameter("@TagText", SqlDbType.NVarChar, 255) { Value = tag.Text });
|
|
cmd.Parameters.Add(new SqlParameter("@Obsolete", SqlDbType.Bit) { Value = tag.IsObsolete });
|
|
|
|
var id = new SqlParameter("@new_id", SqlDbType.Int) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(id);
|
|
|
|
var errorNumber = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(errorNumber);
|
|
|
|
var errorMessage = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(errorMessage);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
if (!DBNull.Value.Equals(errorNumber.Value) && 0 != Convert.ToInt32(errorNumber.Value))
|
|
{
|
|
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.Tags, $"TagsInsert error - {errorNumber.Value} - {errorMessage.Value}");
|
|
return ErrorCodes.ERROR_UNKNOWN;
|
|
}
|
|
if (DBNull.Value.Equals(id.Value))
|
|
{
|
|
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.Tags, $"TagsInsert error - no new id returned");
|
|
return ErrorCodes.ERROR_UNKNOWN;
|
|
}
|
|
else { tag.ID = Convert.ToInt32(id.Value); }
|
|
return ErrorCodes.ERROR_SUCCESS;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.Tags, $"TagsInsert error - {ex.Message}");
|
|
return ErrorCodes.ERROR_UNKNOWN;
|
|
}
|
|
finally
|
|
{
|
|
cmd.Connection.Dispose();
|
|
cmd.Dispose();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// returns id for a given tag text
|
|
/// </summary>
|
|
/// <param name="user">user making request</param>
|
|
/// <param name="connection">connection request is being made on</param>
|
|
/// <param name="tagText">text associated with tag</param>
|
|
/// <param name="id">id of tag (or null if not found)</param>
|
|
/// <returns>0 on success, all other values are error codes</returns>
|
|
public ulong TagsGetId(IUserDbRecord user,
|
|
IConnectionDetails connection,
|
|
string tagText,
|
|
out int? id)
|
|
{
|
|
id = null;
|
|
if (!DbAPI.Connections.IsUserLoggedIn(user, connection))
|
|
{
|
|
return ErrorCodes.ERROR_ACCESS_DENIED;
|
|
}
|
|
var ret = ConnectionManager.GetSqlCommand(connection, out var cmd, "sp_TagsGetId");
|
|
if (ret != ErrorCodes.ERROR_SUCCESS) { return ret; }
|
|
try
|
|
{
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
if (tagText != null)
|
|
{
|
|
cmd.Parameters.Add(new SqlParameter("@TagText", SqlDbType.NVarChar, 255) { Value = tagText });
|
|
}
|
|
var tagId = new SqlParameter("@TagId", SqlDbType.Int) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(tagId);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
if (DBNull.Value.Equals(tagId.Value))
|
|
{
|
|
id = null;
|
|
}
|
|
else { id = Convert.ToInt32(tagId.Value); }
|
|
return ErrorCodes.ERROR_SUCCESS;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.Tags, $"TagsGetId error - {ex.Message}");
|
|
return ErrorCodes.ERROR_UNKNOWN;
|
|
}
|
|
finally
|
|
{
|
|
cmd.Connection.Dispose();
|
|
cmd.Dispose();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// deletes all matching tags
|
|
/// </summary>
|
|
/// <param name="tagId">id of tag to be deleted</param>
|
|
/// <param name="connection">connection tag should be deleted on</param>
|
|
/// <param name="user">user requesting delete</param>
|
|
/// <returns>0 (ERROR_SUCCESS) on success, all other values are error codes</returns>
|
|
public ulong TagsDelete(IUserDbRecord user,
|
|
IConnectionDetails connection,
|
|
int tagId)
|
|
{
|
|
if (!DbAPI.Connections.IsUserLoggedIn(user, connection))
|
|
{
|
|
return ErrorCodes.ERROR_ACCESS_DENIED;
|
|
}
|
|
var ret = ConnectionManager.GetSqlCommand(connection, out var cmd, "sp_TagsDelete");
|
|
if (ret != ErrorCodes.ERROR_SUCCESS) { return ret; }
|
|
try
|
|
{
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.Parameters.Add(new SqlParameter("@TagId", SqlDbType.Int) { Value = tagId });
|
|
|
|
var errorNumber = new SqlParameter("@errorNumber", SqlDbType.Int) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(errorNumber);
|
|
|
|
var errorMessage = new SqlParameter("@errorMessage", SqlDbType.NVarChar, 250) { Direction = ParameterDirection.Output };
|
|
cmd.Parameters.Add(errorMessage);
|
|
|
|
cmd.ExecuteNonQuery();
|
|
if (!DBNull.Value.Equals(errorNumber.Value) && 0 != Convert.ToInt32(errorNumber.Value))
|
|
{
|
|
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.Tags, $"TagsDelete error: {errorNumber.Value} - {errorMessage.Value}");
|
|
return ErrorCodes.ERROR_UNKNOWN;
|
|
}
|
|
|
|
return ErrorCodes.ERROR_SUCCESS;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.Tags, $"TagsDelete error : {ex.Message}");
|
|
return ErrorCodes.ERROR_UNKNOWN;
|
|
}
|
|
finally
|
|
{
|
|
cmd.Connection.Dispose();
|
|
cmd.Dispose();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// retrieves all tags matching search criteria
|
|
/// </summary>
|
|
/// <param name="user">user making request</param>
|
|
/// <param name="connection">connection request is made on</param>
|
|
/// <param name="tagId">id of tag to search for (use null to get all tags)</param>
|
|
/// <param name="records">all matching records</param>
|
|
/// <returns>0 (ERROR_SUCCESS) on success, all other values are error codes</returns>
|
|
public ulong TagsGet(IUserDbRecord user,
|
|
IConnectionDetails connection,
|
|
int? tagId,
|
|
out ITag[] records)
|
|
{
|
|
records = new ITag[0];
|
|
if (!DbAPI.Connections.IsUserLoggedIn(user, connection))
|
|
{
|
|
return ErrorCodes.ERROR_ACCESS_DENIED;
|
|
}
|
|
var ret = ConnectionManager.GetSqlCommand(connection, out var cmd, "sp_TagsGet");
|
|
if (ret != ErrorCodes.ERROR_SUCCESS) { return ret; }
|
|
try
|
|
{
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
if (tagId != null)
|
|
{
|
|
cmd.Parameters.Add(new SqlParameter("@TagId", SqlDbType.Int) { Value = (int)tagId });
|
|
}
|
|
|
|
var reader = cmd.ExecuteReader();
|
|
var list = new List<ITag>();
|
|
while (reader.Read())
|
|
{
|
|
list.Add(new Tag(reader));
|
|
}
|
|
records = list.ToArray();
|
|
return ErrorCodes.ERROR_SUCCESS;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogManager.Log(TraceEventType.Error, LogManager.LogEvents.Tags, $"TagsGet error - {ex.Message}");
|
|
return ErrorCodes.ERROR_UNKNOWN;
|
|
}
|
|
finally
|
|
{
|
|
cmd.Connection.Dispose();
|
|
cmd.Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|