Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.DataAccess.ADOHelper/3.2/ManyToManyRelationHelper.cs @ 3025

Last change on this file since 3025 was 1759, checked in by svonolfe, 16 years ago

Small refactoring of the manyToManyRelationHelper 2 (#527)

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