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.templatingkit.templates;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.ItemType;
38 import info.magnolia.cms.security.AccessDeniedException;
39 import info.magnolia.cms.util.ContentUtil;
40 import info.magnolia.module.templatingkit.functions.STKTemplatingFunctions;
41 import info.magnolia.module.templatingkit.templates.pages.STKPage;
42 import info.magnolia.module.templatingkit.util.STKUtil;
43 import info.magnolia.rendering.model.RenderingModel;
44 import info.magnolia.templating.functions.TemplatingFunctions;
45
46 import java.util.List;
47 import java.util.Map;
48
49 import javax.inject.Inject;
50 import javax.jcr.Node;
51 import javax.jcr.PathNotFoundException;
52 import javax.jcr.RepositoryException;
53
54
55
56
57
58
59
60 public class SingletonParagraphTemplateModel extends STKTemplateModel<STKPage> {
61
62 private static final String SINGLETON_NODE_NAME = "singleton";
63
64 @Inject
65 public SingletonParagraphTemplateModel(Node content, STKPage definition, RenderingModel<?> parent, STKTemplatingFunctions stkFunctions, TemplatingFunctions templatingFunctions) {
66 super(content, definition, parent, stkFunctions, templatingFunctions);
67 }
68
69 @Override
70 public String execute() {
71 final STKPage templateDef = this.getDefinition();
72 try {
73 STKUtil.doPrivileged(ContentUtil.asContent(content), new STKUtil.PrivilegedOperation(){
74 @Override
75 public void exec(Content contentInSystemContext) throws RepositoryException {
76 createMainArea(contentInSystemContext, templateDef);
77 createExtrasArea(contentInSystemContext, templateDef);
78 }
79 });
80 }
81 catch (RepositoryException e) {
82 throw new RuntimeException("can't auto generate the main or extra area", e);
83 }
84 return super.execute();
85 }
86
87 private void createExtrasArea(Content contentInSystemContext, STKPage templateDef) {
88
89 if( templateDef.getArea("extras") != null && templateDef.getArea("extras") instanceof AutoGeneratedExtrasArea) {
90
91 AutoGeneratedExtrasArea singletonDef = (AutoGeneratedExtrasArea)templateDef.getArea("extras");
92 try {
93 Content extrasNode = ContentUtil.getOrCreateContent(ContentUtil.asContent(content), "extras", ItemType.CONTENTNODE, true);
94
95 Content extrasColumn = ContentUtil.getOrCreateContent(extrasNode, "extras1" , ItemType.CONTENTNODE, true);
96 createExtrasColumnItems(singletonDef, extrasColumn);
97 extrasColumn.save();
98
99 } catch (Exception e) {
100 throw new IllegalStateException("Can't create extras paragraph", e);
101 }
102 }
103
104 }
105
106 private void createExtrasColumnItems(AutoGeneratedExtrasArea singletonDef, Content extrasColumn)
107 throws RepositoryException, PathNotFoundException, AccessDeniedException {
108 List paragraphs = singletonDef.getAutoGeneratedParagraphs();
109 for(int i = 0; i < paragraphs.size(); i++ ){
110 if (!extrasColumn.hasContent("extrasItem"+ i)) {
111 Content singletonNode = extrasColumn.createContent("extrasItem" + i, ItemType.CONTENTNODE);
112 singletonNode.getMetaData().setTemplate(((AutoGeneratedParagraph)paragraphs.get(i)).getName());
113
114 setDefaultValues(singletonNode, ((AutoGeneratedParagraph)paragraphs.get(i)).getDefaultValues());
115 }
116 }
117 }
118
119 private void createMainArea(Content contentInSystemContext, STKPage templateDef) {
120 SingletonParagraphMainArea singletonDef = (SingletonParagraphMainArea)templateDef.getArea("main");
121 try {
122 Content mainCollection = ContentUtil.getOrCreateContent(ContentUtil.asContent(content), "main", ItemType.CONTENTNODE, true);
123 if (!mainCollection.hasContent(SINGLETON_NODE_NAME)) {
124 Content singletonNode = mainCollection.createContent(SINGLETON_NODE_NAME, ItemType.CONTENTNODE);
125 singletonNode.getMetaData().setTemplate(singletonDef.getAutoGeneratedParagraph().getName());
126
127 setDefaultValues(singletonNode, singletonDef.getAutoGeneratedParagraph().getDefaultValues());
128
129 mainCollection.save();
130 }
131 } catch (Exception e) {
132 throw new IllegalStateException("Can't create singleton paragraph", e);
133 }
134 }
135
136 protected void setDefaultValues(Content singletonNode, Map defaultValues)
137 throws RepositoryException {
138
139 for (Object key : defaultValues.keySet()) {
140 String name = (String) key;
141 Object value = defaultValues.get(name);
142 singletonNode.createNodeData(name, value);
143 }
144 }
145
146 }