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.rendering.module.setup;
35
36 import info.magnolia.jcr.util.NodeTypes;
37 import info.magnolia.jcr.util.NodeUtil;
38 import info.magnolia.module.InstallContext;
39 import info.magnolia.module.delta.AbstractRepositoryTask;
40 import info.magnolia.module.delta.TaskExecutionException;
41
42 import javax.jcr.Node;
43 import javax.jcr.NodeIterator;
44 import javax.jcr.Property;
45 import javax.jcr.RepositoryException;
46 import javax.jcr.Session;
47
48
49
50
51
52 public class InstallRendererContextAttributeTask extends AbstractRepositoryTask {
53
54 public static final String MODULES_ROOT_DIR = "/modules/";
55 public static final String RENDERERS_NODE = "renderers";
56 public static final String CONTEXT_ATTRIBUTES = "contextAttributes";
57 public static final String COMPONENT_CLASS = "componentClass";
58
59 private final String modulePath;
60 private final String name;
61 private final String componentClass;
62 private final String renderer;
63
64 public InstallRendererContextAttributeTask(String moduleName, String renderer, String name, String componentClass) {
65 super("Renderer context attribute install task", String.format("Installing '%s' context attribute in module '%s'.", name, moduleName));
66 this.renderer = renderer;
67 this.modulePath = MODULES_ROOT_DIR + moduleName;
68 this.name = name;
69 this.componentClass = componentClass;
70 }
71
72 @Override
73 protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
74 Session session = installContext.getConfigJCRSession();
75
76 if (session.nodeExists(modulePath)) {
77 Node module = session.getNode(modulePath);
78
79 if (module.hasNode(RENDERERS_NODE + "/" + renderer)) {
80 Node rendererNode = module.getNode(RENDERERS_NODE).getNode(renderer);
81
82 Node context = NodeUtil.createPath(rendererNode, CONTEXT_ATTRIBUTES, NodeTypes.ContentNode.NAME);
83 NodeIterator functions = context.getNodes();
84 boolean installed = false;
85
86 while (functions.hasNext()) {
87 Node function = functions.nextNode();
88
89 if (function.hasProperty(COMPONENT_CLASS)) {
90 Property property = function.getProperty(COMPONENT_CLASS);
91 if (componentClass.equals(property.getString())) {
92 installed = true;
93 break;
94 }
95 }
96 }
97 if (installed) {
98 installContext.info(String.format("Context attribute with class '%s' already installed, skipping.", componentClass));
99 return;
100 }
101 else if (context.hasNode(name)) {
102 installContext.info(String.format("Context attribute with name '%s' already installed, skipping.", name));
103 return;
104 }
105 else {
106 Node contextAttribute = context.addNode(name, NodeTypes.ContentNode.NAME);
107 contextAttribute.setProperty("name", name);
108 contextAttribute.setProperty(COMPONENT_CLASS, componentClass);
109 }
110 }
111 else {
112 installContext.info(String.format("Renderer '%s' is not installed. Context attribute is not installed.", renderer));
113 }
114 }
115 else {
116 installContext.info(String.format("Module '%s' is not installed. Context attribute is not installed.", modulePath));
117 }
118
119 }
120 }