-
Notifications
You must be signed in to change notification settings - Fork 11
/
ItemRelationship.cs
626 lines (545 loc) · 25.7 KB
/
ItemRelationship.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
// <copyright file="ItemRelationship.cs" company="Engage Software">
// Engage: Publish
// Copyright (c) 2004-2013
// by Engage Software ( http://www.engagesoftware.com )
// </copyright>
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
namespace Engage.Dnn.Publish
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Exceptions;
using Engage.Dnn.Publish.Data;
using Engage.Dnn.Publish.Portability;
using Engage.Dnn.Publish.Util;
using TreeNode = System.Windows.Forms.TreeNode;
using TreeView = System.Windows.Forms.TreeView;
/// <summary>
/// Summary description for ItemRelationship.
/// </summary>
/// <remarks>This class should remain public, it is used by the Publish TreeView module</remarks>
[XmlRoot(ElementName = "relationship", IsNullable = false)]
public class ItemRelationship : TransportableElement
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int _childItemId = -1;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private Guid _childItemIdentifier;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int _childItemVersionId = -1;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private Guid _childItemVersionIdentifier;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string _endDate;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int _itemRelationshipId = -1;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int _itemTypeId = -1;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int _parentItemId = -1;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private Guid _parentItemIdentifier;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int _relationshipTypeId = -1;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string _relationshipTypeName;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private int? _sortOrder;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string _startDate;
public ItemRelationship()
{
this._startDate = DateTime.Now.ToString(CultureInfo.InvariantCulture);
}
[XmlElement(Order = 2)]
public int ChildItemId
{
[DebuggerStepThrough]
get { return this._childItemId; }
[DebuggerStepThrough]
set { this._childItemId = value; }
}
[XmlElement(Order = 3)]
public Guid ChildItemIdentifier
{
[DebuggerStepThrough]
get { return this._childItemIdentifier; }
[DebuggerStepThrough]
set { this._childItemIdentifier = value; }
}
[XmlElement(Order = 4)]
public int ChildItemVersionId
{
[DebuggerStepThrough]
get { return this._childItemVersionId; }
[DebuggerStepThrough]
set { this._childItemVersionId = value; }
}
[XmlElement(Order = 5)]
public Guid ChildItemVersionIdentifier
{
[DebuggerStepThrough]
get { return this._childItemVersionIdentifier; }
[DebuggerStepThrough]
set { this._childItemVersionIdentifier = value; }
}
[XmlElement(Order = 11)]
public string EndDate
{
[DebuggerStepThrough]
get { return this._endDate; }
[DebuggerStepThrough]
set { this._endDate = Engage.Utility.HasValue(value) ? value : null; }
}
[XmlIgnore]
public bool HasSortOrderBeenSet
{
get { return this._sortOrder.HasValue; }
}
[XmlElement(Order = 1)]
public int ItemRelationshipId
{
[DebuggerStepThrough]
get { return this._itemRelationshipId; }
[DebuggerStepThrough]
set { this._itemRelationshipId = value; }
}
[XmlIgnore]
public int ItemTypeId
{
[DebuggerStepThrough]
get { return this._itemTypeId; }
[DebuggerStepThrough]
set { this._itemTypeId = value; }
}
[XmlElement(Order = 6)]
public int ParentItemId
{
[DebuggerStepThrough]
get { return this._parentItemId; }
[DebuggerStepThrough]
set { this._parentItemId = value; }
}
[XmlElement(Order = 7)]
public Guid ParentItemIdentifier
{
[DebuggerStepThrough]
get { return this._parentItemIdentifier; }
[DebuggerStepThrough]
set { this._parentItemIdentifier = value; }
}
[XmlElement(Order = 8)]
public int RelationshipTypeId
{
[DebuggerStepThrough]
get { return this._relationshipTypeId; }
[DebuggerStepThrough]
set { this._relationshipTypeId = value; }
}
[XmlElement(Order = 9)]
public string RelationshipTypeName
{
[DebuggerStepThrough]
get { return this._relationshipTypeName; }
[DebuggerStepThrough]
set { this._relationshipTypeName = value; }
}
[XmlElement(Order = 12)]
public int SortOrder
{
[DebuggerStepThrough]
get { return this._sortOrder ?? 0; }
[DebuggerStepThrough]
set { this._sortOrder = value; }
}
[XmlElement(Order = 10)]
public string StartDate
{
[DebuggerStepThrough]
get { return this._startDate; }
[DebuggerStepThrough]
set { this._startDate = Engage.Utility.HasValue(value) ? value : null; }
}
public static void AddItemRelationship(
int childItemId, int childItemVersionId, int parentItemId, int relationshipTypeId, string startDate, string endDate, int sortOrder)
{
DataProvider.Instance().AddItemRelationship(
childItemId, childItemVersionId, parentItemId, relationshipTypeId, startDate, endDate, sortOrder);
}
public static void AddItemRelationship(
IDbTransaction trans,
int childItemId,
int childItemVersionId,
int parentItemId,
int relationshipTypeId,
string startDate,
string endDate,
int sortOrder)
{
DataProvider.Instance().AddItemRelationship(
trans, childItemId, childItemVersionId, parentItemId, relationshipTypeId, startDate, endDate, sortOrder);
}
public static void AddItemRelationshipWithOriginalSortOrder(
IDbTransaction trans,
int childItemId,
int childItemVersionId,
int parentItemId,
int relationshipTypeId,
string startDate,
string endDate,
int originalItemVersionId)
{
DataProvider.Instance().AddItemRelationshipWithOriginalSortOrder(
trans, childItemId, childItemVersionId, parentItemId, relationshipTypeId, startDate, endDate, originalItemVersionId);
}
[SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "TreeNode.#ctor(System.String)",
Justification = "Literal 'Root' is only used internally")]
public static TreeNode BuildHierarchy(DataTable dt)
{
if (dt != null)
{
var nodes = new Dictionary<object, TreeNode>(dt.Rows.Count + 1);
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow r = dt.Rows[i];
object parentId = r["ParentItemId"];
object childId = r["ItemId"];
string text = r["Name"].ToString();
TreeNode parent;
if (!nodes.TryGetValue(parentId, out parent))
{
parent = new TreeNode
{
Tag = parentId
};
nodes.Add(parentId, parent);
}
TreeNode child;
if (!nodes.TryGetValue(childId, out child))
{
child = new TreeNode
{
Tag = childId
};
nodes.Add(childId, child);
}
child.Text = text;
parent.Nodes.Add(child);
}
var root = new TreeNode("Root");
foreach (var node in nodes.Values)
{
if (node.Parent == null || (string.IsNullOrEmpty(node.Parent.Text) && node.Parent.Tag == null))
{
root.Nodes.Add(node);
}
}
// Full path can only be retrieved when a TreeNode has been added to a TreeView
using (var treeView = new TreeView())
treeView.Nodes.Add(root);
return root;
}
return null;
}
[Obsolete(
"There is a public constructor that initializes data and CBO.FillCollection uses reflection to create an instance of this class you must provide a public constructor. Just use new ItemRelationship()."
, false)]
public static ItemRelationship Create()
{
return new ItemRelationship();
}
/// <summary>
/// Inserts a collection of <see cref="ListItem"/> into a <see cref="System.Web.UI.WebControls.ListControl"/>.
/// Each item has a value of the name of the category plus ticks to indicate hierarchy between categories.
/// Each item has a value of the category's itemId.
/// If you have problems showing all Categories, you can try using <see cref="DisplayChildren(ListControl, int, int?)"/> on an instantiated ItemRelationship (see <see cref="Controls.ItemRelationships.UpdateAvailableItems"/> for an example).
/// </summary>
/// <param name="lc">The <see cref="System.Web.UI.WebControls.ListControl"/> into which the category options will be added.</param>
/// <param name="categoryId">The id of the parent category from which the hierarchy will start, or -1 if all categories.</param>
/// <param name="portalId">The id of the portal in which these categories reside.</param>
/// <param name="includeParentCategory">if set to <c>true</c> includes the parent category in the list, otherwise only shows the parent's children. This is ignored if the TopLevelCategory is selected.</param>
public static void DisplayCategoryHierarchy(ListControl lc, int categoryId, int portalId, bool includeParentCategory)
{
DisplayCategoryHierarchy(lc, categoryId, portalId, includeParentCategory, -1);
}
/// <summary>
/// Inserts a collection of <see cref="ListItem"/> into a <see cref="System.Web.UI.WebControls.ListControl"/>.
/// Each item has a value of the name of the category plus ticks to indicate hierarchy between categories.
/// Each item has a value of the category's itemId.
/// If you have problems showing all Categories, you can try using <see cref="DisplayChildren(ListControl, int, int?)"/> on an instantiated ItemRelationship (see <see cref="Controls.ItemRelationships.UpdateAvailableItems"/> for an example).
/// </summary>
/// <param name="lc">The <see cref="System.Web.UI.WebControls.ListControl"/> into which the category options will be added.</param>
/// <param name="categoryId">The id of the parent category from which the hierarchy will start, or -1 if all categories.</param>
/// <param name="portalId">The id of the portal in which these categories reside.</param>
/// <param name="includeParentCategory">if set to <c>true</c> includes the parent category in the list, otherwise only shows the parent's children. This is ignored if the TopLevelCategory is selected.</param>
/// <param name="itemToExclude">An item which you want to exclude from the list (including its children). This is typically used to keep circular relationships from being possibe options.</param>
public static void DisplayCategoryHierarchy(ListControl lc, int categoryId, int portalId, bool includeParentCategory, int itemToExclude)
{
DataTable dt;
if (categoryId < 1)
{
dt = Category.GetCategoriesHierarchy(portalId);
// we ignore includeParentCategory if it is the TopLevelCategory
}
else
{
dt = Item.GetAllChildren(ItemType.Category.GetId(), categoryId, RelationshipType.ItemToParentCategory.GetId(), portalId).Tables[0];
if (includeParentCategory)
{
Category parentCategory = Category.GetCategory(categoryId, portalId);
if (parentCategory != null)
{
DataRow parentRow = dt.NewRow();
parentRow["ParentItemId"] = "-1";
parentRow["ItemId"] = categoryId;
parentRow["Name"] = parentCategory.Name;
dt.Rows.InsertAt(parentRow, 0);
}
}
}
TreeNode root = BuildHierarchy(dt);
FillListControl(root, lc, itemToExclude);
}
public static DataSet GetAllChildren(int parentId, int relationshipTypeId, int portalId)
{
return DataProvider.Instance().GetAllChildren(parentId, relationshipTypeId, portalId);
}
public static TreeNode GetAllChildrenNLevels(int parentCategoryId, int nLevels, int mItems, int portalId)
{
DataTable dt = DataProvider.Instance().GetAllChildrenNLevels(parentCategoryId, nLevels, mItems, portalId);
return BuildHierarchy(dt);
}
public static DataTable GetAllChildrenNLevelsInDataTable(int parentCategoryId, int nLevels, int mItems, int portalId)
{
return DataProvider.Instance().GetAllChildrenNLevels(parentCategoryId, nLevels, mItems, portalId);
}
public static List<ItemRelationship> GetAllRelationships(int moduleId)
{
return CBO.FillCollection<ItemRelationship>(DataProvider.Instance().GetAllRelationships(moduleId));
}
public static List<ItemRelationship> GetAllRelationshipsByPortalId(int portalId)
{
return CBO.FillCollection<ItemRelationship>(DataProvider.Instance().GetAllRelationshipsByPortalId(portalId));
}
public static ArrayList GetItemChildRelationships(int parentItemId, int relationshipTypeId)
{
return CBO.FillCollection(DataProvider.Instance().GetItemChildRelationships(parentItemId, relationshipTypeId), typeof(ItemRelationship));
}
public static ArrayList GetItemChildRelationships(int parentItemId, int relationshipTypeId, int portalId)
{
string cacheKey = Utility.CacheKeyPublishChildItemRelationships + parentItemId.ToString(CultureInfo.InvariantCulture) + "_" +
relationshipTypeId.ToString(CultureInfo.InvariantCulture);
ArrayList al;
if (ModuleBase.UseCachePortal(portalId))
{
object o = DataCache.GetCache(cacheKey);
if (o != null)
{
al = (ArrayList)o;
}
else
{
al = CBO.FillCollection(
DataProvider.Instance().GetItemChildRelationships(parentItemId, relationshipTypeId), typeof(ItemRelationship));
DataCache.SetCache(cacheKey, al, DateTime.Now.AddMinutes(ModuleBase.CacheTimePortal(portalId)));
Utility.AddCacheKey(cacheKey, portalId);
}
}
else
{
al = CBO.FillCollection(DataProvider.Instance().GetItemChildRelationships(parentItemId, relationshipTypeId), typeof(ItemRelationship));
}
return al;
}
public static DataSet GetItemRelationshipByItemRelationshipId(int itemRelationshipId)
{
return DataProvider.Instance().GetItemRelationshipByItemRelationshipId(itemRelationshipId);
}
public static List<ItemRelationship> GetItemRelationships(int childItemId, int childItemVersionId, bool isActive)
{
return CBO.FillCollection<ItemRelationship>(DataProvider.Instance().GetItemRelationships(childItemId, childItemVersionId, isActive));
}
public static ArrayList GetItemRelationships(int childItemId, int childItemVersionId, int relationshipTypeId, bool isActive)
{
return CBO.FillCollection(
DataProvider.Instance().GetItemRelationships(childItemId, childItemVersionId, relationshipTypeId, isActive), typeof(ItemRelationship));
}
public static ArrayList GetItemRelationships(int childItemId, int childItemVersionId, int relationshipTypeId, bool isActive, int portalId)
{
string cacheKey = Utility.CacheKeyPublishItemRelationships + childItemId.ToString(CultureInfo.InvariantCulture) + "_" +
childItemVersionId.ToString(CultureInfo.InvariantCulture) + "_" +
relationshipTypeId.ToString(CultureInfo.InvariantCulture);
ArrayList al;
if (ModuleBase.UseCachePortal(portalId))
{
object o = DataCache.GetCache(cacheKey);
if (o != null)
{
al = (ArrayList)o;
}
else
{
al =
CBO.FillCollection(
DataProvider.Instance().GetItemRelationships(childItemId, childItemVersionId, relationshipTypeId, isActive),
typeof(ItemRelationship));
DataCache.SetCache(cacheKey, al, DateTime.Now.AddMinutes(ModuleBase.CacheTimePortal(portalId)));
Utility.AddCacheKey(cacheKey, portalId);
}
}
else
{
al = CBO.FillCollection(
DataProvider.Instance().GetItemRelationships(childItemId, childItemVersionId, relationshipTypeId, isActive),
typeof(ItemRelationship));
}
return al;
}
public static void UpdateItemRelationship(int itemRelationshipId, int sortOrder)
{
DataProvider.Instance().UpdateItemRelationship(itemRelationshipId, sortOrder);
}
public void CorrectDates()
{
if (!string.IsNullOrEmpty(this.StartDate))
{
this.StartDate = Convert.ToDateTime(this.StartDate, CultureInfo.CurrentCulture).ToString(CultureInfo.InvariantCulture);
}
if (!string.IsNullOrEmpty(this.EndDate))
{
this.EndDate = Convert.ToDateTime(this.EndDate, CultureInfo.CurrentCulture).ToString(CultureInfo.InvariantCulture);
}
}
public void DisplayChildren(ListControl lc, int portalId)
{
this.DisplayChildren(lc, portalId, null);
}
public void DisplayChildren(ListControl lc, int portalId, int? itemToExclude)
{
DataTable dt;
DataSet ds;
if (this._parentItemId < 1)
{
dt = Category.GetCategoriesHierarchy(portalId);
}
else
{
ds = Item.GetAllChildren(this._parentItemId, this._relationshipTypeId, portalId);
dt = ds.Tables[0];
}
TreeNode root = BuildHierarchy(dt);
FillListControl(root, lc, itemToExclude);
}
// public DataTable GetChildren(int childTypeId, int maxItems, int portalId)
// {
// StringBuilder sql = new StringBuilder();
// sql.Append("engageams_spItemListing ");
// sql.Append(childTypeId);
// sql.Append(", ");
// sql.Append(portalId);
// DataTable dt = DataProvider.Instance().GetDataTable(sql.ToString(), portalId);
// SecurityFilter sf = SecurityFilter.Instance;
// sf.FilterCategories(dt);
// //remove rows over the limit
// if (maxItems != -1)
// {
// ArrayList al = new ArrayList();
// for (int i = maxItems; i < dt.Rows.Count; i++)
// {
// al.Add(dt.Rows[i]);
// }
// foreach (DataRow r in al)
// {
// data.Rows.Remove(r);
// }
// }
// return dt;
// }
/// <summary>
/// This method is invoked by the Import mechanism and has to take this instance of a ItemRelationship and resolve
/// all the id's using the names supplied in the export. hk
/// </summary>
public override void Import(int currentModuleId, int portalId)
{
try
{
RelationshipType type = RelationshipType.GetFromName(this.RelationshipTypeName, typeof(RelationshipType));
this._relationshipTypeId = type.GetId();
// Does this exist in my db?
using (
IDataReader dr = DataProvider.Instance().GetItemRelationshipByIdentifiers(
this.ParentItemIdentifier, this.ChildItemVersionIdentifier, portalId))
{
if (dr.Read())
{
// this version does not exist.
if (dr["ParentItemId"] is DBNull || dr["ChildItemId"] is DBNull)
{
// no matching parent or child doesn't exist. Could throw error and stop
// but for now we will handle gracefully.
}
else
{
this._childItemId = (int)dr["ChildItemId"];
this._childItemVersionId = (int)dr["ChildItemVersionId"];
this._parentItemId = (int)dr["ParentItemId"];
AddItemRelationship(
this.ChildItemId,
this.ChildItemVersionId,
this.ParentItemId,
this.RelationshipTypeId,
this.StartDate,
this.EndDate,
this.SortOrder);
}
}
else
{
Exceptions.LogException(new Exception("No matching Parent or Child could be found to create relationship."));
}
}
}
catch (Exception e)
{
Exceptions.LogException(e);
throw;
}
}
private static void FillListControl(TreeNode root, ListControl lc, int? itemToExclude)
{
for (int i = 0; i < root.Nodes.Count; i++)
{
TreeNode child = root.Nodes[i];
if (child.Text.Length > 0)
{
// int level = child.FullPath.Split('\\').Length;
int level = child.FullPath.Split('\\').Length;
// ListItem li = new ListItem(pad + pad + child.Text, child.Tag.ToString());
int itemId = Convert.ToInt32(child.Tag, CultureInfo.InvariantCulture);
if (itemToExclude.HasValue && itemId == itemToExclude.Value)
{
// skip this item and its children
continue;
}
int paddingWidth = (level - 3) * 2;
var padding = new string('-', paddingWidth);
var li = new ListItem(padding + child.Text, itemId.ToString(CultureInfo.InvariantCulture));
lc.Items.Add(li);
}
FillListControl(child, lc, itemToExclude);
}
}
}
}