2015-12-26 22:44:43 +00:00
|
|
|
|
using Microsoft.Database.Isam;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace DSInternals.DataStore
|
|
|
|
|
{
|
|
|
|
|
public class LinkResolver : IDisposable
|
|
|
|
|
{
|
2016-10-02 14:20:21 +00:00
|
|
|
|
// Column names:
|
2015-12-26 22:44:43 +00:00
|
|
|
|
private const string linkDNCol = "link_DNT";
|
|
|
|
|
private const string backlinkDNCol = "backlink_DNT";
|
|
|
|
|
private const string linkBaseCol = "link_base";
|
2016-01-02 15:30:17 +00:00
|
|
|
|
private const string linkDataCol = "link_data";
|
|
|
|
|
private const string linkMetadataCol = "link_metadata";
|
|
|
|
|
private const string linkDeletedTimeCol = "link_deltime";
|
|
|
|
|
private const string linkDeactivatedTimeCol = "link_deactivetime";
|
2016-01-14 11:06:11 +00:00
|
|
|
|
// Index of valid links post Recycle Bin feature
|
|
|
|
|
private const string linkIndex2008 = "link_present_active_index";
|
|
|
|
|
// Index of valid links before the Recycle Bin feature
|
|
|
|
|
private const string linkIndex2003 = "link_present_index";
|
2015-12-26 22:44:43 +00:00
|
|
|
|
|
|
|
|
|
private Cursor cursor;
|
|
|
|
|
private DirectorySchema schema;
|
|
|
|
|
|
|
|
|
|
public LinkResolver(IsamDatabase database, DirectorySchema schema)
|
|
|
|
|
{
|
|
|
|
|
this.schema = schema;
|
|
|
|
|
this.cursor = database.OpenCursor(ADConstants.LinkTableName);
|
2016-01-14 11:06:11 +00:00
|
|
|
|
if (this.cursor.TableDefinition.Indices.Contains(linkIndex2008))
|
|
|
|
|
{
|
|
|
|
|
this.cursor.SetCurrentIndex(linkIndex2008);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Fallback to the old index if the newer one does not exist
|
|
|
|
|
cursor.SetCurrentIndex(linkIndex2003);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-26 22:44:43 +00:00
|
|
|
|
// TODO: Load column ids instead of names
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int? GetLinkedDNTag(int dnTag, string attributeName)
|
|
|
|
|
{
|
2016-01-02 15:30:17 +00:00
|
|
|
|
try
|
2015-12-26 22:44:43 +00:00
|
|
|
|
{
|
2016-01-14 11:06:11 +00:00
|
|
|
|
// Cast int to int? to force null as default value instead of 0
|
|
|
|
|
return this.GetLinkedDNTags(dnTag, attributeName).Cast<int?>().SingleOrDefault();
|
2016-01-02 15:30:17 +00:00
|
|
|
|
}
|
|
|
|
|
catch(InvalidOperationException)
|
|
|
|
|
{
|
|
|
|
|
// TODO: Make this a special exception type. Move message to resources.
|
|
|
|
|
throw new Exception("More than 1 objects have been found.");
|
2015-12-26 22:44:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-02 15:30:17 +00:00
|
|
|
|
public IEnumerable<int> GetLinkedDNTags(int dnTag, string attributeName)
|
2015-12-26 22:44:43 +00:00
|
|
|
|
{
|
2017-03-12 17:38:21 +00:00
|
|
|
|
// TODO: Check that the attribute type is DN
|
|
|
|
|
this.FindLinkedRecords(dnTag, attributeName);
|
|
|
|
|
|
|
|
|
|
// The cursor is now before the first record in the link_table
|
|
|
|
|
while (cursor.MoveNext())
|
2015-12-26 22:44:43 +00:00
|
|
|
|
{
|
|
|
|
|
// TODO: Not deactivated?
|
|
|
|
|
int foundTag = (int)cursor.IndexRecord[backlinkDNCol];
|
2016-01-02 15:30:17 +00:00
|
|
|
|
yield return foundTag;
|
2015-12-26 22:44:43 +00:00
|
|
|
|
}
|
2016-01-02 15:30:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-12 17:38:21 +00:00
|
|
|
|
public IEnumerable<byte[]> GetLinkedValues(int dnTag, string attributeName)
|
2016-01-02 15:30:17 +00:00
|
|
|
|
{
|
2017-03-12 17:38:21 +00:00
|
|
|
|
// TODO: Check that the attribute is DN-Binary.
|
|
|
|
|
this.FindLinkedRecords(dnTag, attributeName);
|
|
|
|
|
|
|
|
|
|
// The cursor is now before the first record in the link_table
|
|
|
|
|
while (cursor.MoveNext())
|
|
|
|
|
{
|
|
|
|
|
// TODO: Not deactivated?
|
|
|
|
|
byte[] foundValue = cursor.RetrieveColumnAsByteArray(linkDataCol);
|
|
|
|
|
yield return foundValue;
|
|
|
|
|
}
|
2015-12-26 22:44:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing && cursor != null)
|
|
|
|
|
{
|
|
|
|
|
cursor.Dispose();
|
|
|
|
|
cursor = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-12 17:38:21 +00:00
|
|
|
|
|
|
|
|
|
private void FindLinkedRecords(int dnTag, string attributeName)
|
|
|
|
|
{
|
|
|
|
|
SchemaAttribute attr = this.schema.FindAttribute(attributeName);
|
|
|
|
|
if (!attr.LinkId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
//TODO: Throw a proper exception
|
|
|
|
|
throw new Exception("This is not a linked multivalue attribute.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int linkBase = attr.LinkBase.Value;
|
|
|
|
|
// Columns order in index: link_DNT, link_base, backlink_DNT
|
|
|
|
|
Key key = Key.Compose(dnTag, linkBase);
|
|
|
|
|
key.AddWildcard();
|
|
|
|
|
this.cursor.FindRecords(MatchCriteria.EqualTo, key);
|
|
|
|
|
}
|
2015-12-26 22:44:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|