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.commands.impl;
35
36 import info.magnolia.context.Context;
37 import info.magnolia.jcr.util.NodeTypes;
38
39 import java.util.Calendar;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
42
43 import javax.jcr.Node;
44
45 import org.apache.commons.lang.StringUtils;
46
47
48
49
50
51 public class ExtractCommentCommand extends BaseRepositoryCommand {
52
53 private String pattern;
54
55 private String commentTemplate;
56
57 @Override
58 public boolean execute(Context context) throws Exception {
59 Node node = getJCRNode(context);
60 if (node == null) {
61 return false;
62 }
63 Calendar creationDate = NodeTypes.Created.getCreated(node);
64 if (creationDate == null || creationDate.equals(NodeTypes.LastModified.getLastModified(node))) {
65 return false;
66 }
67 if(node.hasProperty("text")){
68 String text = node.getProperty("text").getString();
69
70 Pattern pattern = Pattern.compile(getPattern());
71 Matcher matcher = pattern.matcher(text);
72 if (StringUtils.isBlank(commentTemplate)) {
73 if (matcher.find())
74 {
75 context.put("comment", matcher.group(1));
76 }
77 } else {
78 if (matcher.find()) {
79 for (int idx = 1; idx <= matcher.groupCount(); idx++) {
80 commentTemplate = commentTemplate.replaceAll("\\(" + idx + "\\)", matcher.group(idx));
81 }
82 }
83 context.put("comment", commentTemplate);
84 }
85 }
86 return true;
87 }
88
89 private String getPattern() {
90 return pattern;
91 }
92
93 public void setPattern(String pattern) {
94 this.pattern = pattern;
95 }
96
97 public String getCommentTemplate() {
98 return commentTemplate;
99 }
100
101 public void setCommentTemplate(String commentTemplate) {
102 this.commentTemplate = commentTemplate;
103 }
104 }