/// <summary>
        ///     Adds the relationship.
        /// </summary>
        /// <param name="isForward">if set to <c>true</c> [is forward].</param>
        private void AddRelationship(bool isForward)
        {
            long id;

            if (IsEntityValid(SelectedText, out id))
            {
                AddRelationshipDialog dialog = new AddRelationshipDialog(PluginSettings, id, isForward);
                var result = dialog.ShowDialog( );

                if (result == true)
                {
                    AddRelationshipDialogViewModel vm = dialog.DataContext as AddRelationshipDialogViewModel;

                    if (vm != null)
                    {
                        RelationshipPicker selectedRelationship = vm.SelectedRelationship;

                        if (selectedRelationship != null && selectedRelationship.SelectedInstance != null)
                        {
                            var instance = selectedRelationship.SelectedInstance;

                            var dbManager = new DatabaseManager(PluginSettings.DatabaseSettings);

                            string commandText = @"--ReadiMon: Insert Relationship
DECLARE @contextInfo VARBINARY(128) = CONVERT( VARBINARY(128), 'Entity Browser->Add Relationship' )
SET CONTEXT_INFO @contextInfo

INSERT INTO Relationship (TenantId, TypeId, FromId, ToId) VALUES (@tenantId, @typeId, @fromId, @toId)";


                            using (var command = dbManager.CreateCommand(commandText))
                            {
                                dbManager.AddParameter(command, "@tenantId", selectedRelationship.TenantId);
                                dbManager.AddParameter(command, "@typeId", selectedRelationship.Id);
                                dbManager.AddParameter(command, "@fromId", selectedRelationship.IsForward ? SelectedEntityId : instance.Id);
                                dbManager.AddParameter(command, "@toId", selectedRelationship.IsForward ? instance.Id : SelectedEntityId);

                                command.ExecuteNonQuery( );

                                LoadEntity(SelectedText, true);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Loads the fields.
        /// </summary>
        private void LoadRelationships( )
        {
            var dbManager = new DatabaseManager(PluginSettings.DatabaseSettings);

            string commandText;

            if (IsForward)
            {
                commandText = @"-- ReadiMon: GetForwardRelationships
SET NOCOUNT ON

DECLARE @tenantId BIGINT
SELECT @tenantId = TenantId FROM Entity WHERE Id = @entityId

DECLARE @isOfType BIGINT = dbo.fnAliasNsId( 'isOfType', 'core', @tenantId )
DECLARE @inherits BIGINT = dbo.fnAliasNsId( 'inherits', 'core', @tenantId )
DECLARE @from BIGINT = dbo.fnAliasNsId( 'fromType', 'core', @tenantId )
DECLARE @to BIGINT = dbo.fnAliasNsId( 'toType', 'core', @tenantId )
DECLARE @name BIGINT = dbo.fnAliasNsId( 'name', 'core', @tenantId )
DECLARE @alias BIGINT = dbo.fnAliasNsId( 'alias', 'core', @tenantId )
DECLARE @description BIGINT = dbo.fnAliasNsId( 'description', 'core', @tenantId )
DECLARE @cardinality BIGINT = dbo.fnAliasNsId( 'cardinality', 'core', @tenantId )

DECLARE @typeId BIGINT
SELECT @typeId = ToId FROM Relationship WHERE TenantId = @tenantId AND FromId = @entityId AND TypeId = @isOfType

SELECT DISTINCT
	[TenantId] = @tenantId,
	[TypeId] = r.FromId,
	[TypeUpgradeId] = e.UpgradeId,
	[Name] = n.Data,
	[Description] = d.Data,
	[FromId] = r.ToId,
	[From] = yna.Data,
	[ToId] = o.ToId,
	[To] = xna.Data,
	[Cardinality] = ca.Data,
	[Alias] = ta.Namespace + ':' + ta.Data
FROM
	dbo.fnAncestorsAndSelf( @inherits, @typeId, @tenantId ) a
JOIN
	Relationship r ON r.TenantId = @tenantId AND r.ToId = a.Id AND r.TypeId = @from
JOIN
	Relationship o ON o.TenantId = @tenantId AND r.FromId = o.FromId AND o.TypeId = @to
LEFT JOIN
	Data_Alias ta ON ta.TenantId = @tenantId AND r.FromId = ta.EntityId AND ta.FieldId = @alias AND ta.AliasMarkerId = 0
LEFT JOIN
	Data_NVarChar xna ON xna.TenantId = @tenantId AND o.ToId = xna.EntityId AND xna.FieldId = @name
LEFT JOIN
	Data_NVarChar yna ON yna.TenantId = @tenantId AND r.ToId = yna.EntityId AND yna.FieldId = @name
LEFT JOIN
	Relationship c ON r.TenantId = c.TenantId AND r.FromId = c.FromId AND c.TypeId = @cardinality
LEFT JOIN
	Data_Alias ca ON r.TenantId = ca.TenantId AND ca.EntityId = c.ToId AND ca.FieldId = @alias
JOIN
	Entity e ON r.FromId = e.Id AND e.TenantId = @tenantId
LEFT JOIN
	Data_NVarChar n ON n.TenantId = @tenantId AND r.FromId = n.EntityId AND n.FieldId = @name
LEFT JOIN
	Data_NVarChar d ON d.TenantId = @tenantId AND r.FromId = d.EntityId AND d.FieldId = @description
ORDER BY
	n.Data

SELECT
	TenantId,
	TypeId,
	FromId,
	ToId
FROM
	Relationship
WHERE
	TenantId = @tenantId AND
	FromId = @entityId"    ;
            }
            else
            {
                commandText = @"-- ReadiMon: GetReverseRelationships
SET NOCOUNT ON

DECLARE @tenantId BIGINT
SELECT @tenantId = TenantId FROM Entity WHERE Id = @entityId

DECLARE @isOfType BIGINT = dbo.fnAliasNsId( 'isOfType', 'core', @tenantId )
DECLARE @inherits BIGINT = dbo.fnAliasNsId( 'inherits', 'core', @tenantId )
DECLARE @from BIGINT = dbo.fnAliasNsId( 'fromType', 'core', @tenantId )
DECLARE @to BIGINT = dbo.fnAliasNsId( 'toType', 'core', @tenantId )
DECLARE @name BIGINT = dbo.fnAliasNsId( 'name', 'core', @tenantId )
DECLARE @alias BIGINT = dbo.fnAliasNsId( 'alias', 'core', @tenantId )
DECLARE @reverseAlias BIGINT = dbo.fnAliasNsId( 'reverseAlias', 'core', @tenantId )
DECLARE @description BIGINT = dbo.fnAliasNsId( 'description', 'core', @tenantId )
DECLARE @cardinality BIGINT = dbo.fnAliasNsId( 'cardinality', 'core', @tenantId )

DECLARE @typeId BIGINT
SELECT @typeId = ToId FROM Relationship WHERE TenantId = @tenantId AND FromId = @entityId AND TypeId = @isOfType

SELECT DISTINCT
	[TenantId] = @tenantId,
	[TypeId] = r.FromId,
	[TypeUpgradeId] = e.UpgradeId,
	[Name] = n.Data,
	[Description] = d.Data,
	[FromId] = o.ToId,
	[From] = xna.Data,
	[ToId] = r.ToId,
	[To] = yna.Data,
	[Cardinality] = ca.Data,
	[Alias] = ta.Namespace + ':' + ta.Data
FROM
	dbo.fnAncestorsAndSelf( @inherits, @typeId, @tenantId ) a
JOIN
	Relationship r ON r.TenantId = @tenantId AND r.ToId = a.Id AND r.TypeId = @to
JOIN
	Relationship o ON o.TenantId = @tenantId AND r.FromId = o.FromId AND o.TypeId = @from
LEFT JOIN
	Data_Alias ta ON ta.TenantId = @tenantId AND r.FromId = ta.EntityId AND ta.FieldId = @reverseAlias AND ta.AliasMarkerId = 1
LEFT JOIN
	Data_NVarChar xna ON xna.TenantId = @tenantId AND o.ToId = xna.EntityId AND xna.FieldId = @name
LEFT JOIN
	Data_NVarChar yna ON yna.TenantId = @tenantId AND r.ToId = yna.EntityId AND yna.FieldId = @name
LEFT JOIN
	Relationship c ON r.TenantId = c.TenantId AND r.FromId = c.FromId AND c.TypeId = @cardinality
LEFT JOIN
	Data_Alias ca ON r.TenantId = ca.TenantId AND ca.EntityId = c.ToId AND ca.FieldId = @alias
JOIN
	Entity e ON r.FromId = e.Id AND e.TenantId = @tenantId
LEFT JOIN
	Data_NVarChar n ON n.TenantId = @tenantId AND r.FromId = n.EntityId AND n.FieldId = @name
LEFT JOIN
	Data_NVarChar d ON d.TenantId = @tenantId AND r.FromId = d.EntityId AND d.FieldId = @description
ORDER BY
	n.Data

SELECT
	TenantId,
	TypeId,
	FromId,
	ToId
FROM
	Relationship
WHERE
	TenantId = @tenantId AND
	ToId = @entityId"    ;
            }

            var command = dbManager.CreateCommand(commandText);

            dbManager.AddParameter(command, "@entityId", SelectedEntityId);

            using (IDataReader reader = command.ExecuteReader( ))
            {
                var relationships = new List <RelationshipPicker>( );

                Dictionary <long, RelationshipPicker> map = new Dictionary <long, RelationshipPicker>( );

                while (reader.Read( ))
                {
                    var tenantId    = reader.GetInt64(0);
                    var id          = reader.GetInt64(1);
                    var upgradeId   = reader.GetGuid(2);
                    var name        = reader.GetString(3, "<Unnamed>");
                    var description = reader.GetString(4, "No description specified");
                    var fromId      = reader.GetInt64(5);
                    var from        = reader.GetString(6, "<Unnamed>");
                    var toId        = reader.GetInt64(7);
                    var to          = reader.GetString(8, "<Unnamed>");
                    var cardinality = reader.GetString(9, null);
                    var alias       = reader.GetString(10, null);

                    RelationshipPicker rel = new RelationshipPicker(id, tenantId, upgradeId, name + (alias == null ? "" : $" - ({alias})"), description, fromId, from, toId, to, cardinality, IsForward, PluginSettings);

                    relationships.Add(rel);
                    map[rel.Id] = rel;
                }

                if (reader.NextResult( ))
                {
                    while (reader.Read( ))
                    {
                        var existingRelationshipId = reader.GetInt64(1);

                        RelationshipPicker relationship;

                        if (map.TryGetValue(existingRelationshipId, out relationship))
                        {
                            if (relationship.Cardinality == "oneToOne" || relationship.Cardinality == "oneToMany")
                            {
                                relationship.Disabled = true;
                            }
                        }
                    }
                }

                Relationships = relationships;
            }
        }