1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.wcm.webcontent;
18
19 import java.util.GregorianCalendar;
20 import java.util.List;
21
22 import javax.jcr.Node;
23 import javax.jcr.Session;
24
25 import org.exoplatform.services.jcr.ext.common.SessionProvider;
26 import org.exoplatform.services.wcm.core.BaseWebSchemaHandler;
27 import org.exoplatform.services.wcm.core.WebSchemaConfigService;
28 import org.exoplatform.services.wcm.link.LiveLinkManagerService;
29 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
30
31
32
33
34
35
36
37
38
39 public class HTMLFileSchemaHandler extends BaseWebSchemaHandler {
40
41
42
43
44 protected String getHandlerNodeType() { return "nt:file"; }
45
46
47
48
49 protected String getParentNodeType() { return "exo:webFolder"; }
50
51
52
53
54
55
56
57 public boolean matchHandler(SessionProvider sessionProvider, Node node) throws Exception {
58 if(!matchNodeType(node))
59 return false;
60 if(!matchMimeType(node))
61 return false;
62 if(!matchParentNodeType(node)) {
63 if(!isInWebContent(node))
64 return false;
65 }
66 return true;
67 }
68
69
70
71
72
73
74
75
76
77
78 private boolean matchNodeType(Node node) throws Exception{
79 return node.getPrimaryNodeType().getName().equals("nt:file");
80 }
81
82
83
84
85
86
87
88
89
90
91 private boolean matchMimeType(Node node) throws Exception {
92 String mimeType = getFileMimeType(node);
93 if("text/html".equals(mimeType))
94 return true;
95 if("text/plain".equals(mimeType))
96 return true;
97 return false;
98 }
99
100
101
102
103
104
105
106
107
108
109 public boolean isInWebContent(Node file) throws Exception{
110 if(file.getParent().isNodeType("exo:webContent")) {
111 return file.isNodeType("exo:htmlFile");
112 }
113 return false;
114 }
115
116
117
118
119
120
121
122
123
124
125 private boolean matchParentNodeType(Node file) throws Exception{
126 return file.getParent().isNodeType("exo:webFolder");
127 }
128
129
130
131
132
133
134
135 public void onCreateNode(SessionProvider sessionProvider, final Node file) throws Exception {
136 Session session = file.getSession();
137 Node webFolder = file.getParent();
138 String fileName = file.getName();
139
140 addMixin(file, "exo:htmlFile");
141 file.setProperty("exo:presentationType","exo:htmlFile");
142 String tempFolderName = fileName + this.hashCode();
143 Node tempFolder = webFolder.addNode(tempFolderName, NT_UNSTRUCTURED);
144 String tempPath = tempFolder.getPath() + "/" +file.getName();
145 session.move(file.getPath(),tempPath);
146 webFolder.save();
147
148 Node webContent = webFolder.addNode(fileName, "exo:webContent");
149 addMixin(webContent,"exo:privilegeable");
150 addMixin(webContent,"exo:owneable");
151
152 WebSchemaConfigService schemaConfigService = WCMCoreUtils.getService(WebSchemaConfigService.class);
153 WebContentSchemaHandler contentSchemaHandler = schemaConfigService.getWebSchemaHandlerByType(WebContentSchemaHandler.class);
154 contentSchemaHandler.createSchema(webContent);
155 webFolder.save();
156
157 String htmlFilePath = webContent.getPath() + "/default.html";
158 session.move(tempPath, htmlFilePath);
159 tempFolder.remove();
160 createDefautWebData(webContent);
161 webFolder.save();
162 }
163
164
165
166
167
168
169
170 public void onModifyNode(final SessionProvider sessionProvider, final Node node) throws Exception {
171 if(node.hasNode("jcr:content")) {
172 Node contentNode = node.getNode("jcr:content");
173 contentNode.setProperty("jcr:lastModified",new GregorianCalendar());
174 contentNode.setProperty("jcr:mimeType", "text/html");
175 }
176
177 Node parent = node.getParent();
178 if(!parent.isNodeType("exo:webContent"))
179 return;
180 if (!parent.isCheckedOut() || parent.isLocked() || !node.isCheckedOut()) {
181 return;
182 }
183 LiveLinkManagerService liveLinkManagerService = WCMCoreUtils.getService(LiveLinkManagerService.class);
184 List<String> newLinks = liveLinkManagerService.extractLinks(node);
185 liveLinkManagerService.updateLinkDataForNode(parent,newLinks);
186 }
187
188 }