Free cookie consent management tool by TermsFeed Policy Generator

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

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

Small refactoring of the manyToManyRelationHelper 2 (#527)

File size: 2.8 KB
RevLine 
[1580]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;
[1758]12
13    private int childIndex;
14
[1580]15    public ManyToManyRelationHelper(
[1758]16      ITableAdapterWrapper<AdapterT, ManyToManyRelation, RowT> tableAdapterWrapper,
17      int childIndex) {
[1580]18      this.tableAdapterWrapper = tableAdapterWrapper;
[1758]19      this.childIndex = childIndex;
[1580]20    }
21
22    public Session Session {
23      set {
24        tableAdapterWrapper.Session = value;
25      }
26    }
27
[1656]28    public void UpdateRelationships(Guid objectA,
[1759]29      IList<Guid> relationships) {
[1758]30      UpdateRelationships(objectA, relationships, null);
[1656]31    }
32
33    public void UpdateRelationships(Guid objectA,
34      IList<Guid> relationships,
[1758]35      IList<object> additionalAttributes) {
[1580]36      //firstly check for created references
37      IList<Guid> existing =
[1758]38        this.GetRelationships(objectA);
[1580]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;
[1656]46          if(additionalAttributes != null)
47            rel.AdditionalAttributes =
48              new List<object>(additionalAttributes);
[1580]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 =
[1758]69          FindRow(objectA, relationship);
[1580]70        if (toDelete != null) {
71          toDelete.Delete();
72          tableAdapterWrapper.UpdateRow(toDelete);
73        }
74      }
75    }
76
[1758]77    public IList<Guid> GetRelationships(Guid objectA) {
[1580]78      IList<Guid> result =
79        new List<Guid>();
80
81      IEnumerable<RowT> rows =
82        tableAdapterWrapper.FindById(objectA);
83
84      foreach(RowT row in rows) {
[1729]85        result.Add((Guid)row[childIndex]);
[1580]86      }
87
88      return result;
89    }
90
[1758]91    private RowT FindRow(Guid objectA, Guid objectB) {
[1580]92      IEnumerable<RowT> rows =
93       tableAdapterWrapper.FindById(objectA);
94
95      RowT found = null;
96
97      foreach (RowT row in rows) {
[1729]98        if (row[childIndex].Equals(objectB)) {
[1580]99          found = row;
100          break;
101        }
102      }
103
104      return found;
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.