-
Notifications
You must be signed in to change notification settings - Fork 11
/
Tag.cs
328 lines (284 loc) · 12.1 KB
/
Tag.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
// <copyright file="Tag.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.Data;
using System.Globalization;
using System.Text;
using System.Web;
using System.Xml.Serialization;
using Engage.Dnn.Publish.Data;
using Engage.Dnn.Publish.Util;
[XmlRoot(ElementName = "Tag", IsNullable = false)]
public class Tag
{
// attributes hide private members from debugger, so both properties and members aren't shown - BD
/// <summary>
/// Initializes a new instance of the <see cref="Tag"/> class.
/// </summary>
public Tag()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Tag"/> class.
/// </summary>
/// <param name="tagName">The tag name.</param>
/// <param name="tagDescription">The tag description.</param>
/// <param name="tagTotalItems">The total items.</param>
public Tag(string tagName, string tagDescription, int tagTotalItems)
{
this.Name = tagName;
this.Description = tagDescription;
this.TotalItems = tagTotalItems;
this.MostRecentDate = DateTime.Now;
}
/// <summary>
/// Initializes a new instance of the <see cref="Tag"/> class.
/// </summary>
/// <param name="dt">Data table of the tag instance info.</param>
public Tag(DataTable dt)
{
// Make sure that we have at least 1 row returned. If for some reason we have more than one we only use the first one.
if (dt.Rows.Count < 1)
{
return;
}
var row = dt.Rows[0];
this.TagId = Convert.ToInt32(row["tagId"], CultureInfo.InvariantCulture);
this.Name = row["name"].ToString();
this.Description = row["description"].ToString();
this.TotalItems = Convert.ToInt32(row["totalItems"], CultureInfo.InvariantCulture);
this.MostRecentDate = Convert.ToDateTime(row["mostRecentDate"], CultureInfo.InvariantCulture);
this.LanguageId = Convert.ToInt32(row["languageid"], CultureInfo.InvariantCulture);
this.CreatedDate = Convert.ToDateTime(row["datecreated"], CultureInfo.InvariantCulture);
}
/// <summary>
/// Gets the date this tag was created.
/// </summary>
/// <value>The creation date of this rating.</value>
public DateTime CreatedDate { get; private set; }
/// <summary>
/// Gets or sets the tag description for this <see cref="Tag"/> instance.
/// </summary>
/// <value>The description of the Tag.</value>
public string Description { get; set; }
/// <summary>
/// Gets or sets the language id for this <see cref="Tag"/> instance.
/// </summary>
/// <value>The Language Id</value>
public int LanguageId { get; set; }
/// <summary>
/// Gets the date an item was last tagged.
/// </summary>
/// <value>The creation date of this rating.</value>
public DateTime MostRecentDate { get; private set; }
/// <summary>
/// Gets or sets the tag name for this <see cref="Tag"/> instance.
/// </summary>
/// <value>The Name of the Tag.</value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the PortalId this <see cref="Tag"/> instance.
/// </summary>
/// <value>The Language Id</value>
public int PortalId { get; set; }
/// <summary>
/// Gets the _tagId of the tag
/// </summary>
/// <value>The tag id of the tag.</value>
public int TagId { get; private set; }
/// <summary>
/// Gets or sets the total number of items tagged with this <see cref="Tag"/> instance.
/// </summary>
/// <value>The Total Items Tag.</value>
public int TotalItems { get; set; }
public static DataTable GetItemsFromTags(int portalId, ArrayList tagList)
{
var tagIds = new StringBuilder(50);
if (tagList != null)
{
foreach (int tag in tagList)
{
tagIds.Append(tag.ToString());
tagIds.Append("_");
}
}
var cacheKey = string.Format(
CultureInfo.InvariantCulture,
"{0}{1}_{2}",
Utility.CacheKeyPublishItemsFromTags,
tagIds,
portalId);
return Utility.GetValueFromCache(portalId, cacheKey, () => DataProvider.Instance().GetItemsFromTags(portalId, tagList));
}
public static DataTable GetItemsFromTagsPaging(int portalId, ArrayList tagList, int maxItems, int pageId, string sortOrder)
{
var tagIds = new StringBuilder(50);
if (tagList != null)
{
foreach (int tag in tagList)
{
tagIds.Append(tag.ToString());
tagIds.Append("_");
}
}
var cacheKey = string.Format(
CultureInfo.InvariantCulture,
"{0}{1}_{2}_{3}",
Utility.CacheKeyPublishItemsFromTagsPage,
tagIds,
pageId,
portalId);
return Utility.GetValueFromCache(
portalId,
cacheKey,
() => DataProvider.Instance().GetItemsFromTagsPaging(portalId, tagList, maxItems, pageId, sortOrder));
}
public static DataTable GetPopularTags(int portalId, ArrayList tagList, bool selectTop)
{
// TODO: change tagList to a <List> of strings
var tagIds = new StringBuilder(50);
if (tagList != null)
{
foreach (int tag in tagList)
{
tagIds.Append(tag.ToString());
tagIds.Append("_");
}
}
var cacheKey = string.Format(
CultureInfo.InvariantCulture,
"{0}{1}_{2}_{3}",
Utility.CacheKeyPublishPopularTags,
tagIds,
selectTop,
portalId);
return Utility.GetValueFromCache(portalId, cacheKey, () => DataProvider.Instance().GetPopularTags(portalId, tagList, selectTop));
}
public static int GetPopularTagsCount(int portalId, ArrayList tagList, bool selectTop)
{
// TODO: change tagList to a <List> of strings
var sb = new StringBuilder(50);
if (tagList != null)
{
foreach (int tag in tagList)
{
sb.Append(tag.ToString());
sb.Append("_");
}
}
var cacheKey = string.Format(
CultureInfo.InvariantCulture,
"{0}{1}_{2}_{3}",
Utility.CacheKeyPublishPopularTagsCount,
sb,
selectTop,
portalId);
return Utility.GetValueFromCache(portalId, cacheKey, () => DataProvider.Instance().GetPopularTagsCount(portalId, tagList, selectTop));
}
/// <summary>
/// Gets a specific tag for a specific PortalId
/// </summary>
/// <param name="tag">The tag string.</param>
/// <param name="portalId">The Portal Id.</param>
public static Tag GetTag(string tag, int portalId)
{
var cacheKey = string.Format(CultureInfo.InvariantCulture, "{0}{1}_{2}", Utility.CacheKeyPublishTag, tag, portalId);
return Utility.GetValueFromCache(portalId, cacheKey, () => new Tag(DataProvider.Instance().GetTag(tag, portalId)));
}
/// <summary>
/// Gets a specific tag for a specific PortalId
/// </summary>
[Obsolete("Use GetTag(int, int) so that caching can be used")]
public static Tag GetTag(int tagId)
{
return new Tag(DataProvider.Instance().GetTag(tagId));
}
public static Tag GetTag(int tagId, int portalId)
{
var cacheKey = string.Format(CultureInfo.InvariantCulture, "{0}{1}_{2}", Utility.CacheKeyPublishTagById, tagId, portalId);
return Utility.GetValueFromCache(portalId, cacheKey, () => new Tag(DataProvider.Instance().GetTag(tagId)));
}
/// <summary>
/// Gets all Tags for a specific PortalId
/// </summary>
/// <param name="portalId">The Portal Id.</param>
public static DataTable GetTags(int portalId)
{
var cacheKey = Utility.CacheKeyPublishGetTagsByPortal + portalId.ToString(CultureInfo.InvariantCulture);
return Utility.GetValueFromCache(portalId, cacheKey, () => DataProvider.Instance().GetTags(portalId));
}
/// <summary>
/// Gets all tags that match a portion of a particular string for a portal
/// </summary>
/// <param name="partialTag">The string to start matching from. </param>
/// <param name="portalId">The Portal Id.</param>
public static DataTable GetTagsByString(string partialTag, int portalId)
{
var cacheKey = string.Format(CultureInfo.InvariantCulture, "{0}{1}_{2}", Utility.CacheKeyPublishGetTagsByString, partialTag, portalId);
return Utility.GetValueFromCache(portalId, cacheKey, () => DataProvider.Instance().GetTagsByString(partialTag, portalId));
}
public static ArrayList ParseTags(string tags, int portalId)
{
return ParseTags(tags, portalId, Utility.GetTagSeparators(), true);
}
public static ArrayList ParseTags(string tags, int portalId, char[] separators, bool add)
{
var tagList = new ArrayList();
string[] splitList = tags.Trim().Split(separators);
foreach (string sTag in splitList)
{
if (sTag.Trim().Length > 0)
{
Tag t = GetTag(sTag.Trim(), portalId);
if (t.TagId == 0 && add)
{
t.Name = HttpUtility.UrlDecode(sTag).Trim();
t.PortalId = portalId;
// TODO: localize this
t.Description = "Added by article edit";
t.TotalItems = 0;
t.Save();
}
if (t.TagId > 0)
{
tagList.Add(t);
}
}
}
return tagList;
}
public void Save()
{
if (this.TagId == 0)
{
this.TagId = DataProvider.Instance().AddTag(this);
}
else
{
DataProvider.Instance().UpdateTag(this);
}
}
public void Save(IDbTransaction trans)
{
if (this.TagId == 0)
{
this.TagId = DataProvider.Instance().AddTag(trans, this);
}
else
{
DataProvider.Instance().UpdateTag(trans, this);
}
}
}
}