1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.wcm.core;
18
19 import java.util.Calendar;
20
21 import javax.jcr.Node;
22 import javax.jcr.RepositoryException;
23 import javax.jcr.lock.LockException;
24 import javax.jcr.nodetype.ConstraintViolationException;
25 import javax.jcr.nodetype.NoSuchNodeTypeException;
26 import javax.jcr.version.VersionException;
27
28 import org.exoplatform.commons.utils.MimeTypeResolver;
29 import org.exoplatform.container.ExoContainer;
30 import org.exoplatform.container.ExoContainerContext;
31 import org.exoplatform.container.component.BaseComponentPlugin;
32 import org.exoplatform.services.jcr.ext.common.SessionProvider;
33 import org.exoplatform.services.wcm.portal.LivePortalManagerService;
34
35
36
37
38
39
40 public abstract class BaseWebSchemaHandler extends BaseComponentPlugin implements WebSchemaHandler {
41
42 protected final String EXO_OWNABLE = "exo:owneable";
43 protected final String NT_FOLDER = "nt:folder";
44 protected final String NT_UNSTRUCTURED = "nt:unstructured";
45 protected final String NT_FILE = "nt:file" ;
46
47
48
49
50 public boolean matchHandler(SessionProvider sessionProvider, Node node) throws Exception {
51 String handlerNodeType = getHandlerNodeType();
52 String parentNodeType = getParentNodeType();
53 if(!node.getPrimaryNodeType().getName().equals(handlerNodeType))
54 return false;
55 if(!node.getParent().isNodeType(parentNodeType))
56 return false;
57 return true;
58 }
59
60
61
62
63 public void onCreateNode(SessionProvider sessionProvider, Node node) throws Exception { }
64
65
66
67
68 public void onModifyNode(SessionProvider sessionProvider, Node node) throws Exception { }
69
70
71
72
73 public void onRemoveNode(SessionProvider sessionProvider, Node node) throws Exception { }
74
75
76
77
78
79
80 protected abstract String getHandlerNodeType() ;
81
82
83
84
85
86
87 protected abstract String getParentNodeType();
88
89
90
91
92
93
94
95
96
97
98
99
100 protected void addMixin(Node node, String mixin) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
101 if (!node.isNodeType(mixin)) node.addMixin(mixin);
102 }
103
104 protected <T> T getService(Class<T> clazz) {
105 ExoContainer container = ExoContainerContext.getCurrentContainer();
106 return clazz.cast(container.getComponentInstanceOfType(clazz));
107 }
108
109 protected Node findPortalNode(SessionProvider sessionProvider, Node child) throws Exception{
110 LivePortalManagerService livePortalManagerService = getService(LivePortalManagerService.class);
111 String portalName = null;
112 for(String portalPath: livePortalManagerService.getLivePortalsPath()) {
113 if(child.getPath().startsWith(portalPath)) {
114 portalName = livePortalManagerService.getPortalNameByPath(portalPath);
115 break;
116 }
117 }
118 if(portalName == null) return null;
119 return livePortalManagerService.getLivePortal(sessionProvider, portalName);
120 }
121
122 protected String getFileMimeType(Node file) throws Exception{
123 String mimeType = null;
124 try {
125 mimeType = file.getNode("jcr:content").getProperty("jcr:mimeType").getString();
126 } catch (Exception e) {
127 MimeTypeResolver resolver = new MimeTypeResolver();
128 resolver.setDefaultMimeType("text/plain");
129 mimeType = resolver.getMimeType(file.getName());
130 }
131 return mimeType;
132 }
133
134 protected void createDefautWebData(Node webContent) throws Exception{
135
136 Node defaultCSS = addNodeAsNTFile(webContent.getNode("css"), "default.css", "text/css", "");
137 addMixin(defaultCSS, "exo:cssFile");
138 addMixin(defaultCSS,"exo:owneable");
139
140 Node defaultJS = addNodeAsNTFile(webContent.getNode("js"), "default.js", "application/x-javascript", "");
141 addMixin(defaultJS, "exo:jsFile");
142 addMixin(defaultJS,"exo:owneable");
143
144 if(!webContent.hasNode("default.html")){
145 Node defaultHTML = addNodeAsNTFile(webContent, "default.html", "text/html", "");
146 addMixin(defaultHTML, "exo:htmlFile");
147 addMixin(defaultHTML,"exo:owneable");
148 }
149
150 Node illustration = addNodeAsNTFile(webContent.getNode("medias/images"), "illustration", "", "");
151 addMixin(illustration, "exo:owneable");
152 }
153
154 private Node addNodeAsNTFile(Node home, String fileName,String mimeType,String data) throws Exception{
155 Node file = home.addNode(fileName,"nt:file");
156 Node jcrContent = file.addNode("jcr:content","nt:resource");
157 jcrContent.addMixin("dc:elementSet");
158 jcrContent.setProperty("jcr:encoding", "UTF-8");
159 jcrContent.setProperty("jcr:lastModified", Calendar.getInstance());
160 jcrContent.setProperty("jcr:mimeType", mimeType);
161 jcrContent.setProperty("jcr:data", data);
162 return file;
163 }
164
165 }