[1580] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 |
|
---|
| 6 | namespace 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 |
|
---|
[1656] | 24 | public void UpdateRelationships(Guid objectA,
|
---|
[1729] | 25 | IList<Guid> relationships, int childIndex) {
|
---|
| 26 | UpdateRelationships(objectA, relationships, null, childIndex);
|
---|
[1656] | 27 | }
|
---|
| 28 |
|
---|
| 29 | public void UpdateRelationships(Guid objectA,
|
---|
| 30 | IList<Guid> relationships,
|
---|
[1729] | 31 | IList<object> additionalAttributes, int childIndex) {
|
---|
[1580] | 32 | //firstly check for created references
|
---|
| 33 | IList<Guid> existing =
|
---|
[1729] | 34 | this.GetRelationships(objectA, childIndex);
|
---|
[1580] | 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;
|
---|
[1656] | 42 | if(additionalAttributes != null)
|
---|
| 43 | rel.AdditionalAttributes =
|
---|
| 44 | new List<object>(additionalAttributes);
|
---|
[1580] | 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 =
|
---|
[1729] | 65 | FindRow(objectA, relationship, childIndex);
|
---|
[1580] | 66 | if (toDelete != null) {
|
---|
| 67 | toDelete.Delete();
|
---|
| 68 | tableAdapterWrapper.UpdateRow(toDelete);
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[1729] | 73 | public IList<Guid> GetRelationships(Guid objectA, int childIndex) {
|
---|
[1580] | 74 | IList<Guid> result =
|
---|
| 75 | new List<Guid>();
|
---|
| 76 |
|
---|
| 77 | IEnumerable<RowT> rows =
|
---|
| 78 | tableAdapterWrapper.FindById(objectA);
|
---|
| 79 |
|
---|
| 80 | foreach(RowT row in rows) {
|
---|
[1729] | 81 | result.Add((Guid)row[childIndex]);
|
---|
[1580] | 82 | }
|
---|
| 83 |
|
---|
| 84 | return result;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[1729] | 87 | private RowT FindRow(Guid objectA, Guid objectB, int childIndex) {
|
---|
[1580] | 88 | IEnumerable<RowT> rows =
|
---|
| 89 | tableAdapterWrapper.FindById(objectA);
|
---|
| 90 |
|
---|
| 91 | RowT found = null;
|
---|
| 92 |
|
---|
| 93 | foreach (RowT row in rows) {
|
---|
[1729] | 94 | if (row[childIndex].Equals(objectB)) {
|
---|
[1580] | 95 | found = row;
|
---|
| 96 | break;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | return found;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 | }
|
---|