-
Notifications
You must be signed in to change notification settings - Fork 0
/
TagsScheduledJob.cs
150 lines (120 loc) · 4.5 KB
/
TagsScheduledJob.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
using System;
using System.Collections.Generic;
using System.Linq;
using EPiServer;
using EPiServer.BaseLibrary.Scheduling;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.PlugIn;
using EPiServer.ServiceLocation;
using Geta.Tags.Implementations;
using Geta.Tags.Interfaces;
using Geta.Tags.Models;
using Geta.Tags.Helpers;
namespace Geta.Tags
{
[ScheduledPlugIn(DisplayName = "Geta Tags maintenance", DefaultEnabled = true)]
public class TagsScheduledJob : JobBase
{
private bool _stop;
private readonly ITagService _tagService;
private readonly PageTypeRepository _pageTypeRepository;
public TagsScheduledJob() : this(new TagService())
{
IsStoppable = true;
}
public TagsScheduledJob(ITagService tagService)
{
_tagService = tagService;
_pageTypeRepository = ServiceLocator.Current.GetInstance<PageTypeRepository>();
}
public override string Execute()
{
var tags = _tagService.GetAllTags().ToList();
var pageGuids = GetTaggedPageGuids(tags);
foreach (var pageGuid in pageGuids)
{
if (_stop)
{
return "Geta Tags maintenance was stopped";
}
PageData page = null;
try
{
var contentReference = TagsHelper.GetPageReference(pageGuid);
if (!ContentReference.IsNullOrEmpty(contentReference))
{
page = DataFactory.Instance.GetPage(contentReference);
}
}
catch (PageNotFoundException) {}
if (page == null || page.IsDeleted)
{
RemoveFromAllTags(pageGuid, tags);
continue;
}
CheckPageProperties(page, tags);
}
return "Geta Tags maintenance completed successfully";
}
private void CheckPageProperties(PageData page, IList<Tag> tags)
{
var pageType = _pageTypeRepository.Load(page.PageTypeID);
foreach (var propertyDefinition in pageType.PropertyDefinitions)
{
if (!TagsHelper.IsTagProperty(propertyDefinition))
{
continue;
}
var tagNames = page[propertyDefinition.Name] as string;
IList<Tag> allTags = tags;
if (tagNames == null)
{
RemoveFromAllTags(page.PageGuid, allTags);
continue;
}
var addedTags = ParseTags(tagNames);
// make sure the tags it has added has the pagereference
ValidateTags(allTags, page.PageGuid, addedTags);
// make sure there's no pagereference to this pagereference in the rest of the tags
RemoveFromAllTags(page.PageGuid, allTags);
}
}
private static IEnumerable<Guid> GetTaggedPageGuids(IEnumerable<Tag> tags)
{
return tags.Where(x => x != null && x.PermanentLinks != null)
.SelectMany(x => x.PermanentLinks)
.ToList();
}
private IEnumerable<Tag> ParseTags(string tagNames)
{
return tagNames.Split(',')
.Select(_tagService.GetTagByName)
.Where(tag => tag != null)
.ToList();
}
private void ValidateTags(ICollection<Tag> allTags, Guid pageGuid, IEnumerable<Tag> addedTags)
{
foreach (var addedTag in addedTags)
{
allTags.Remove(addedTag);
if (addedTag.PermanentLinks.Contains(pageGuid)) continue;
addedTag.PermanentLinks.Add(pageGuid);
_tagService.Save(addedTag);
}
}
private void RemoveFromAllTags(Guid guid, IEnumerable<Tag> tags)
{
foreach (var tag in tags)
{
if (tag.PermanentLinks == null || !tag.PermanentLinks.Contains(guid)) continue;
tag.PermanentLinks.Remove(guid);
_tagService.Save(tag);
}
}
public override void Stop()
{
_stop = true;
}
}
}