| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.plugin.activitystream.impl; |
| 21 |
|
|
| 22 |
|
import org.apache.commons.lang3.StringUtils; |
| 23 |
|
import org.slf4j.Logger; |
| 24 |
|
import org.slf4j.LoggerFactory; |
| 25 |
|
import org.xwiki.rendering.syntax.Syntax; |
| 26 |
|
|
| 27 |
|
import com.xpn.xwiki.XWikiContext; |
| 28 |
|
import com.xpn.xwiki.XWikiException; |
| 29 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 30 |
|
import com.xpn.xwiki.objects.BaseObject; |
| 31 |
|
import com.xpn.xwiki.plugin.activitystream.plugin.ActivityStreamPlugin; |
| 32 |
|
import com.xpn.xwiki.plugin.scheduler.SchedulerPlugin; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
@version |
| 39 |
|
|
| |
|
| 17.7% |
Uncovered Elements: 65 (79) |
Complexity: 17 |
Complexity Density: 0.31 |
|
| 40 |
|
public final class ActivityStreamCleaner |
| 41 |
|
{ |
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(ActivityStreamCleaner.class); |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
private static final String CLEANER_JOB_DOCNAME = "Scheduler.ActivityStreamCleaner"; |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
private static final String CLEANER_JOB_NAME = "ActivityStream cleaner"; |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
private static final String CLEANER_JOB_CRON = "0 0 0 ? * SUN"; |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
private static final String XWIKI_DEFAULT_ADMIN = "XWiki.Admin"; |
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
private static final String XWIKI_RIGHTS_CLASS = "XWiki.XWikiRights"; |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
private static ActivityStreamCleaner instance; |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 80 |
1 |
private ActivityStreamCleaner()... |
| 81 |
|
{ |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
@return |
| 86 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 87 |
1 |
public static ActivityStreamCleaner getInstance()... |
| 88 |
|
{ |
| 89 |
1 |
synchronized (ActivityStreamCleaner.class) { |
| 90 |
1 |
if (instance == null) { |
| 91 |
1 |
instance = new ActivityStreamCleaner(); |
| 92 |
|
} |
| 93 |
|
} |
| 94 |
|
|
| 95 |
1 |
return instance; |
| 96 |
|
} |
| 97 |
|
|
| 98 |
|
|
| 99 |
|
@param |
| 100 |
|
@return |
| 101 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 102 |
1 |
public static int getNumberOfDaysToKeep(XWikiContext context)... |
| 103 |
|
{ |
| 104 |
1 |
ActivityStreamPlugin plugin = |
| 105 |
|
(ActivityStreamPlugin) context.getWiki().getPlugin(ActivityStreamPlugin.PLUGIN_NAME, context); |
| 106 |
1 |
String pref = plugin.getActivityStreamPreference("daystokeepevents", "0", context); |
| 107 |
1 |
return Integer.parseInt(pref); |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
@param |
| 114 |
|
@return |
| 115 |
|
|
| |
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
| 116 |
0 |
private boolean setCleanerCommonDocumentsFields(XWikiDocument doc)... |
| 117 |
|
{ |
| 118 |
0 |
boolean needsUpdate = false; |
| 119 |
|
|
| 120 |
0 |
if (StringUtils.isBlank(doc.getAuthor())) { |
| 121 |
0 |
needsUpdate = true; |
| 122 |
0 |
doc.setAuthor(XWIKI_DEFAULT_ADMIN); |
| 123 |
|
} |
| 124 |
|
|
| 125 |
0 |
if (StringUtils.isBlank(doc.getCreator())) { |
| 126 |
0 |
needsUpdate = true; |
| 127 |
0 |
doc.setCreator(XWIKI_DEFAULT_ADMIN); |
| 128 |
|
} |
| 129 |
|
|
| 130 |
0 |
if (StringUtils.isBlank(doc.getParent())) { |
| 131 |
0 |
needsUpdate = true; |
| 132 |
0 |
doc.setParent("Scheduler.WebHome"); |
| 133 |
|
} |
| 134 |
|
|
| 135 |
0 |
return needsUpdate; |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
@param |
| 142 |
|
@param |
| 143 |
|
@return |
| 144 |
|
@throws |
| 145 |
|
|
| |
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 146 |
0 |
private boolean createWatchListJobRightsObject(XWikiDocument doc, XWikiContext context) throws XWikiException... |
| 147 |
|
{ |
| 148 |
0 |
BaseObject rights = doc.getObject(XWIKI_RIGHTS_CLASS); |
| 149 |
0 |
if (rights == null) { |
| 150 |
0 |
int index = doc.createNewObject(XWIKI_RIGHTS_CLASS, context); |
| 151 |
0 |
rights = doc.getObject(XWIKI_RIGHTS_CLASS, index); |
| 152 |
0 |
rights.setLargeStringValue("groups", "XWiki.XWikiAdminGroup"); |
| 153 |
0 |
rights.setStringValue("levels", "edit,delete"); |
| 154 |
0 |
rights.setIntValue("allow", 1); |
| 155 |
0 |
return true; |
| 156 |
|
} |
| 157 |
|
|
| 158 |
0 |
return false; |
| 159 |
|
} |
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
@param |
| 165 |
|
@throws |
| 166 |
|
|
| |
|
| 0% |
Uncovered Elements: 31 (31) |
Complexity: 5 |
Complexity Density: 0.2 |
|
| 167 |
0 |
private void initCleanerJob(XWikiContext context) throws XWikiException... |
| 168 |
|
{ |
| 169 |
0 |
XWikiDocument doc; |
| 170 |
0 |
boolean needsUpdate = false; |
| 171 |
0 |
BaseObject job = null; |
| 172 |
|
|
| 173 |
0 |
try { |
| 174 |
0 |
doc = context.getWiki().getDocument(CLEANER_JOB_DOCNAME, context); |
| 175 |
0 |
needsUpdate = setCleanerCommonDocumentsFields(doc); |
| 176 |
|
|
| 177 |
0 |
job = doc.getXObject(SchedulerPlugin.XWIKI_JOB_CLASSREFERENCE); |
| 178 |
0 |
if (job == null) { |
| 179 |
0 |
needsUpdate = true; |
| 180 |
0 |
job = doc.newXObject(SchedulerPlugin.XWIKI_JOB_CLASSREFERENCE, context); |
| 181 |
0 |
job.setStringValue("jobName", CLEANER_JOB_NAME); |
| 182 |
0 |
job.setStringValue("jobClass", ActivityStreamCleanerJob.class.getName()); |
| 183 |
0 |
job.setStringValue("cron", CLEANER_JOB_CRON); |
| 184 |
0 |
job.setStringValue("contextUser", XWIKI_DEFAULT_ADMIN); |
| 185 |
0 |
job.setStringValue("contextLang", "en"); |
| 186 |
0 |
job.setStringValue("contextDatabase", "xwiki"); |
| 187 |
|
} |
| 188 |
|
|
| 189 |
0 |
needsUpdate = createWatchListJobRightsObject(doc, context); |
| 190 |
|
|
| 191 |
0 |
if (StringUtils.isBlank(doc.getContent())) { |
| 192 |
0 |
needsUpdate = true; |
| 193 |
0 |
doc.setContent("{{include reference=\"XWiki.SchedulerJobSheet\"/}}"); |
| 194 |
0 |
doc.setSyntax(Syntax.XWIKI_2_0); |
| 195 |
|
} |
| 196 |
|
|
| 197 |
0 |
if (needsUpdate) { |
| 198 |
0 |
context.getWiki().saveDocument(doc, "", true, context); |
| 199 |
0 |
((SchedulerPlugin) context.getWiki().getPlugin("scheduler", context)).scheduleJob(job, context); |
| 200 |
|
} |
| 201 |
|
} catch (Exception e) { |
| 202 |
0 |
LOGGER.error("Cannot initialize ActivityStreamCleanerJob", e); |
| 203 |
|
} |
| 204 |
|
} |
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
@param |
| 210 |
|
@throws |
| 211 |
|
|
| |
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
| 212 |
1 |
public void init(XWikiContext context) throws XWikiException... |
| 213 |
|
{ |
| 214 |
1 |
if (getNumberOfDaysToKeep(context) > 0) { |
| 215 |
0 |
initCleanerJob(context); |
| 216 |
|
} |
| 217 |
|
} |
| 218 |
|
} |