Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.DataAccess.ADOHelper/3.2/ManyToManyRelationHelper.cs @ 1656

Last change on this file since 1656 was 1656, checked in by svonolfe, 15 years ago

Implemented large parts of the security DAL (#597)

File size: 2.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace HeuristicLab.DataAccess.ADOHelper {
7  public class ManyToManyRelationHelper<AdapterT, RowT>
8    where AdapterT : new()
9    where RowT : System.Data.DataRow
10    {
11    ITableAdapterWrapper<AdapterT, ManyToManyRelation, RowT> tableAdapterWrapper;
12   
13    public ManyToManyRelationHelper(
14      ITableAdapterWrapper<AdapterT, ManyToManyRelation, RowT> tableAdapterWrapper) {
15      this.tableAdapterWrapper = tableAdapterWrapper;
16    }
17
18    public Session Session {
19      set {
20        tableAdapterWrapper.Session = value;
21      }
22    }
23
24    public void UpdateRelationships(Guid objectA,
25      IList<Guid> relationships) {
26      UpdateRelationships(objectA, relationships, null);
27    }
28
29    public void UpdateRelationships(Guid objectA,
30      IList<Guid> relationships,
31      IList<object> additionalAttributes) {
32      //firstly check for created references
33      IList<Guid> existing =
34        this.GetRelationships(objectA);
35
36      foreach (Guid relationship in relationships) {
37        if (!existing.Contains(relationship)) {
38          ManyToManyRelation rel =
39            new ManyToManyRelation();
40          rel.Id = objectA;
41          rel.Id2 = relationship;
42          if(additionalAttributes != null)
43            rel.AdditionalAttributes =
44              new List<object>(additionalAttributes);
45         
46          RowT inserted =
47            tableAdapterWrapper.InsertNewRow(rel);
48
49          tableAdapterWrapper.UpdateRow(inserted);
50        }
51      }
52
53      //secondly check for deleted references
54      ICollection<Guid> deleted =
55        new List<Guid>();
56
57      foreach (Guid relationship in existing) {
58        if(!relationships.Contains(relationship)) {
59          deleted.Add(relationship);
60        }
61      }
62
63      foreach (Guid relationship in deleted) {
64        RowT toDelete =
65          FindRow(objectA, relationship);
66        if (toDelete != null) {
67          toDelete.Delete();
68          tableAdapterWrapper.UpdateRow(toDelete);
69        }
70      }
71    }
72
73    public IList<Guid> GetRelationships(Guid objectA) {
74      IList<Guid> result =
75        new List<Guid>();
76
77      IEnumerable<RowT> rows =
78        tableAdapterWrapper.FindById(objectA);
79
80      foreach(RowT row in rows) {
81        result.Add((Guid)row[1]);
82      }
83
84      return result;
85    }
86
87    private RowT FindRow(Guid objectA, Guid objectB) {
88      IEnumerable<RowT> rows =
89       tableAdapterWrapper.FindById(objectA);
90
91      RowT found = null;
92
93      foreach (RowT row in rows) {
94        if (row[1].Equals(objectB)) {
95          found = row;
96          break;
97        }
98      }
99
100      return found;
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.