Skip to content

Commit

Permalink
feat(policy): policy seeding data update (#88)
Browse files Browse the repository at this point in the history
Refs: #25
Reviewed-by: Phil Schneider <info@philschneider.de>
Co-authored-by: Phil Schneider <info@philschneider.de>
  • Loading branch information
AnuragNagpure and Phil91 authored Apr 26, 2024
1 parent 7623856 commit 10bb931
Show file tree
Hide file tree
Showing 24 changed files with 1,262 additions and 235 deletions.
18 changes: 14 additions & 4 deletions src/database/PolicyHub.DbAccess/Repositories/PolicyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public IAsyncEnumerable<string> GetAttributeKeys() =>
public IAsyncEnumerable<PolicyTypeResponse> GetPolicyTypes(PolicyTypeId? type, UseCaseId? useCase) =>
dbContext.Policies
.Where(p =>
(type == null || p.Types.Any(x => x.Id == type)) &&
(useCase == null || p.UseCases.Any(x => x.Id == useCase)))
p.IsActive &&
(type == null || p.Types.Any(x => x.Id == type && x.IsActive)) &&
(useCase == null || p.UseCases.Any(x => x.Id == useCase && x.IsActive)))
.Select(p => new PolicyTypeResponse(
p.TechnicalKey,
p.Types.Where(t => t.IsActive).Select(t => t.Id),
Expand All @@ -50,20 +51,29 @@ public IAsyncEnumerable<PolicyTypeResponse> GetPolicyTypes(PolicyTypeId? type, U
public Task<(bool Exists, string LeftOperand, (AttributeKeyId? Key, IEnumerable<string> Values) Attributes, string? RightOperandValue)> GetPolicyContentAsync(UseCaseId? useCase, PolicyTypeId type, string credential) =>
dbContext.Policies
.Where(p =>
p.IsActive &&
p.Types.Any(t => t.IsActive && t.Id == type) &&
(useCase == null || p.UseCases.Any(x => x.Id == useCase)) &&
(useCase == null || p.UseCases.Any(x => x.Id == useCase && x.IsActive)) &&
p.TechnicalKey == credential)
.Select(p => new ValueTuple<bool, string, ValueTuple<AttributeKeyId?, IEnumerable<string>>, string?>(
true,
p.LeftOperandValue ?? p.TechnicalKey,
new ValueTuple<AttributeKeyId?, IEnumerable<string>>(p.AttributeKeyId, p.AttributeKey!.PolicyAttributes.Where(pa => pa.IsActive && pa.PolicyId == p.Id).Select(a => a.AttributeValue)),
new ValueTuple<AttributeKeyId?, IEnumerable<string>>(
p.AttributeKeyId,
p.AttributeKey!.PolicyAttributes.Where(pa =>
pa.IsActive &&
pa.PolicyId == p.Id &&
pa.IsActive &&
(useCase == null || pa.PolicyAttributeAssignedUseCases.Any(x => x.UseCaseId == useCase && x.IsActive))
).Select(a => a.AttributeValue)),
p.PolicyKind!.Configuration!.RightOperandValue
))
.FirstOrDefaultAsync();

public IAsyncEnumerable<(string TechnicalKey, string LeftOperand, (AttributeKeyId? Key, IEnumerable<string> Values) Attributes, string? RightOperandValue)> GetPolicyForOperandContent(PolicyTypeId type, IEnumerable<string> technicalKeys) =>
dbContext.Policies
.Where(p =>
p.IsActive &&
p.Types.Any(t => t.IsActive && t.Id == type) &&
technicalKeys.Contains(p.TechnicalKey))
.Select(p => new ValueTuple<string, string, ValueTuple<AttributeKeyId?, IEnumerable<string>>, string?>(
Expand Down
6 changes: 6 additions & 0 deletions src/database/PolicyHub.Entities/Entities/IActiveEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities;

public interface IActiveEntity
{
bool IsActive { get; set; }
}
2 changes: 1 addition & 1 deletion src/database/PolicyHub.Entities/Entities/Policy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities;

public class Policy
public class Policy : IActiveEntity
{
private Policy()
{
Expand Down
15 changes: 10 additions & 5 deletions src/database/PolicyHub.Entities/Entities/PolicyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,35 @@

namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities;

public class PolicyAttribute
public class PolicyAttribute : IActiveEntity
{
private PolicyAttribute()
{
AttributeValue = null!;
PolicyAttributeAssignedUseCases = new HashSet<PolicyAttributeAssignedUseCases>();
}

public PolicyAttribute(Guid policyId, AttributeKeyId key, string attributeValue)
public PolicyAttribute(Guid id, Guid policyId, AttributeKeyId key, string attributeValue)
: this()
{
Id = id;
PolicyId = policyId;
Key = key;
AttributeValue = attributeValue;
}

public Guid PolicyId { get; private set; }
public Guid Id { get; set; }
public Guid PolicyId { get; set; }

public AttributeKeyId Key { get; private set; }
public AttributeKeyId Key { get; set; }

public string AttributeValue { get; private set; }
public string AttributeValue { get; set; }

public bool IsActive { get; set; }

public virtual Policy? Policy { get; private set; }

public virtual AttributeKey? AttributeKey { get; private set; }

public virtual ICollection<PolicyAttributeAssignedUseCases> PolicyAttributeAssignedUseCases { get; private set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/********************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Org.Eclipse.TractusX.PolicyHub.Entities.Enums;

namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities;

public class PolicyAttributeAssignedUseCases
{
private PolicyAttributeAssignedUseCases()
{
PolicyAttribute = null!;
UseCase = null!;
}

public PolicyAttributeAssignedUseCases(Guid attributeId, UseCaseId useCaseId)
: this()
{
AttributeId = attributeId;
UseCaseId = useCaseId;
}

public Guid AttributeId { get; set; }

public UseCaseId UseCaseId { get; set; }

public bool IsActive { get; set; }

public virtual PolicyAttribute? PolicyAttribute { get; private set; }
public virtual UseCase? UseCase { get; private set; }
}
2 changes: 1 addition & 1 deletion src/database/PolicyHub.Entities/Entities/PolicyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities;

public class PolicyType
public class PolicyType : IActiveEntity
{
private PolicyType()
{
Expand Down
8 changes: 6 additions & 2 deletions src/database/PolicyHub.Entities/Entities/UseCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@

namespace Org.Eclipse.TractusX.PolicyHub.Entities.Entities;

public class UseCase
public class UseCase : IActiveEntity
{
private UseCase()
{
Label = null!;
Policies = new HashSet<Policy>();
PolicyAttributeAssignedUseCases = new HashSet<PolicyAttributeAssignedUseCases>();
}

public UseCase(UseCaseId useCaseId, bool isActive) : this()
public UseCase(UseCaseId useCaseId, bool isActive)
: this()
{
Id = useCaseId;
Label = useCaseId.ToString();
Expand All @@ -43,4 +45,6 @@ public UseCase(UseCaseId useCaseId, bool isActive) : this()

// Navigation properties
public virtual ICollection<Policy> Policies { get; private set; }

public virtual ICollection<PolicyAttributeAssignedUseCases> PolicyAttributeAssignedUseCases { get; private set; }
}
24 changes: 21 additions & 3 deletions src/database/PolicyHub.Entities/PolicyHubContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<PolicyAttribute>(entity =>
{
entity.Property(x => x.IsActive).HasDefaultValue(true);
entity
.HasKey(x => new { x.PolicyId, x.Key, x.AttributeValue });
.HasKey(x => new { x.Id });
entity.Property(x => x.IsActive).HasDefaultValue(true);
entity
.HasOne(pa => pa.AttributeKey)
Expand All @@ -127,6 +127,24 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasForeignKey(x => x.PolicyId);
});

modelBuilder.Entity<PolicyAttributeAssignedUseCases>(entity =>
{
entity.Property(x => x.IsActive).HasDefaultValue(true);
entity
.HasKey(x => new { x.AttributeId, x.UseCaseId });
entity
.HasOne(pa => pa.PolicyAttribute)
.WithMany(p => p.PolicyAttributeAssignedUseCases)
.HasForeignKey(x => x.AttributeId);
entity
.HasOne(pa => pa.UseCase)
.WithMany(p => p.PolicyAttributeAssignedUseCases)
.HasForeignKey(x => x.UseCaseId);
});

modelBuilder.Entity<PolicyType>(entity =>
{
entity.Property(x => x.IsActive).HasDefaultValue(true);
Expand Down
Loading

0 comments on commit 10bb931

Please sign in to comment.