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 package info.magnolia.module.rssaggregator.command;
35
36 import info.magnolia.cms.util.AlertUtil;
37 import info.magnolia.cms.util.QueryUtil;
38 import info.magnolia.commands.MgnlCommand;
39 import info.magnolia.context.Context;
40 import info.magnolia.jcr.util.PropertyUtil;
41 import info.magnolia.module.rssaggregator.RSSAggregatorConstants;
42
43 import javax.jcr.Node;
44 import javax.jcr.NodeIterator;
45 import javax.jcr.Session;
46
47
48
49
50
51
52 public class ReplacePlanetUrlCommand extends MgnlCommand {
53
54 public static final String FEED_NAME = "planetFeed";
55 public static final String OLD_URL = "oldurl";
56 public static final String NEW_URL = "newurl";
57
58 @Override
59 public boolean execute(Context context) throws Exception {
60 final String planetFeed = (String) context.get(FEED_NAME);
61 final String oldUrl = (String) context.get(OLD_URL);
62 final String newUrl = (String) context.get(NEW_URL);
63
64 Session session = context.getJCRSession("data");
65
66 String sql = "select * from [nt:base] as t where ISDESCENDANTNODE([/rssaggregator/" + planetFeed + "]) " +
67 "and t.rssLink='" + oldUrl + "'";
68
69 int replCnt = 0;
70
71 NodeIterator entries = QueryUtil.search(RSSAggregatorConstants.WORKSPACE, sql);
72
73 while (entries.hasNext()) {
74 Node entry = entries.nextNode();
75 PropertyUtil.setProperty(entry, "rssLink", newUrl);
76 session.save();
77 replCnt++;
78 }
79
80 AlertUtil.setMessage("Replaced " + replCnt + " URLs within the Planet feed " + planetFeed + ".");
81
82 return true;
83 }
84
85 }