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.admininterface.trees;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.ItemType;
38 import info.magnolia.module.admininterface.AdminTreeMVCHandler;
39 import info.magnolia.module.templating.Template;
40 import info.magnolia.module.templating.TemplateManager;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import javax.jcr.RepositoryException;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48
49
50
51
52
53
54 public class WebsiteTreeHandler extends AdminTreeMVCHandler {
55 private static final Logger log = LoggerFactory.getLogger(WebsiteTreeHandler.class);
56
57 private final TemplateManager templateManager;
58
59 public WebsiteTreeHandler(String name, HttpServletRequest request, HttpServletResponse response) {
60 super(name, request, response);
61 templateManager = TemplateManager.getInstance();
62 }
63
64 @Override
65 public String createNode() {
66 String view = super.createNode();
67 try {
68
69 if (this.getCreateItemType().equals(ItemType.CONTENT.getSystemName())) {
70 Content parentNode = this.getHierarchyManager().getContent(this.getPath());
71 Content newNode = parentNode.getContent(this.getNewNodeName());
72 Template newTemplate = getDefaultTemplate(newNode);
73 if (newTemplate != null) {
74 newNode.getMetaData().setTemplate(newTemplate.getName());
75 newNode.save();
76 }
77 }
78 }
79 catch (RepositoryException e) {
80 log.error("can't set template", e);
81 }
82 return view;
83 }
84
85 protected Template getDefaultTemplate(Content node) {
86 return templateManager.getDefaultTemplate(node);
87 }
88
89 @Override
90 public String show() {
91
92 if (!templateManager.getAvailableTemplates().hasNext()) {
93 try {
94 request.getRequestDispatcher("/.magnolia/pages/quickstart.html").forward(request, response);
95 return "";
96 } catch (Exception e) {
97 log.error("Couldn't forward to quickstart page: " + e.getMessage());
98 }
99 }
100 return super.show();
101 }
102 }