1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.wcm.skin;
18
19 import javax.jcr.Node;
20
21 import org.exoplatform.services.jcr.ext.common.SessionProvider;
22 import org.exoplatform.services.wcm.core.BaseWebSchemaHandler;
23 import org.exoplatform.services.wcm.core.WebSchemaConfigService;
24 import org.exoplatform.services.wcm.portal.PortalFolderSchemaHandler;
25 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
26
27
28
29
30
31
32
33
34 public class CSSFileHandler extends BaseWebSchemaHandler {
35
36
37
38
39 protected String getHandlerNodeType() { return "nt:file"; }
40
41
42
43
44 protected String getParentNodeType() { return "exo:cssFolder" ; }
45
46
47 private boolean isInPortalCSSFolder = false;
48
49
50
51
52
53
54
55 public boolean matchHandler(SessionProvider sessionProvider, Node node) throws Exception {
56 if (!matchNodeType(node))
57 return false;
58 if (!matchMimeType(node))
59 return false;
60 isInPortalCSSFolder = matchPortalCSSFolder(sessionProvider, node);
61 if (isInPortalCSSFolder)
62 return true;
63 if (!matchParentNodeType(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/css".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 private boolean matchParentNodeType(Node file) throws Exception {
110 return file.getParent().isNodeType("exo:cssFolder");
111 }
112
113
114
115
116
117
118
119
120
121
122
123 private boolean matchPortalCSSFolder(SessionProvider sessionProvider, Node file) throws Exception {
124 Node portal = findPortalNode(sessionProvider, file);
125 if (portal == null)
126 return false;
127 WebSchemaConfigService schemaConfigService = WCMCoreUtils.getService(WebSchemaConfigService.class);
128 PortalFolderSchemaHandler schemaHandler = schemaConfigService.getWebSchemaHandlerByType(PortalFolderSchemaHandler.class);
129 Node cssFolder = schemaHandler.getCSSFolder(portal);
130 return cssFolder == null ? false : file.getPath().startsWith(cssFolder.getPath());
131 }
132
133
134
135
136
137
138
139 public void onCreateNode(SessionProvider sessionProvider, Node file) throws Exception {
140 addMixin(file, "exo:cssFile");
141 addMixin(file,"exo:owneable");
142 file.setProperty("exo:presentationType","exo:cssFile");
143 if(isInPortalCSSFolder) {
144 file.setProperty("exo:sharedCSS",true);
145 }
146 }
147
148
149
150
151
152
153
154 public void onModifyNode(SessionProvider sessionProvider, Node file) throws Exception {
155 if(isInPortalCSSFolder) {
156 Node portal = findPortalNode(sessionProvider, file);
157 XSkinService skinService = WCMCoreUtils.getService(XSkinService.class);
158 skinService.updatePortalSkinOnModify(portal, file);
159 }
160 }
161
162
163
164
165
166
167
168 public void onRemoveNode(SessionProvider sessionProvider, Node file) throws Exception {
169 if (isInPortalCSSFolder) {
170 XSkinService skinService = WCMCoreUtils.getService(XSkinService.class);
171 Node portal = findPortalNode(sessionProvider, file);
172 skinService.updatePortalSkinOnRemove(portal, file);
173 }
174 }
175
176 }