1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.ecm.webui.component.explorer.search;
18
19 import java.io.InputStream;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.jcr.Node;
27 import javax.jcr.NodeIterator;
28 import javax.jcr.PathNotFoundException;
29 import javax.jcr.RepositoryException;
30 import javax.jcr.Session;
31 import javax.jcr.Value;
32 import javax.jcr.ValueFormatException;
33 import javax.portlet.PortletRequest;
34
35 import org.exoplatform.container.ExoContainer;
36 import org.exoplatform.container.ExoContainerContext;
37 import org.exoplatform.container.xml.PortalContainerInfo;
38 import org.exoplatform.download.DownloadService;
39 import org.exoplatform.download.InputStreamDownloadResource;
40 import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
41 import org.exoplatform.ecm.webui.presentation.AbstractActionComponent;
42 import org.exoplatform.ecm.webui.presentation.UIBaseNodePresentation;
43 import org.exoplatform.ecm.webui.presentation.removeattach.RemoveAttachmentComponent;
44 import org.exoplatform.ecm.webui.presentation.removecomment.RemoveCommentComponent;
45 import org.exoplatform.ecm.webui.utils.Utils;
46 import org.exoplatform.portal.webui.util.Util;
47 import org.exoplatform.resolver.ResourceResolver;
48 import org.exoplatform.services.cms.comments.CommentsService;
49 import org.exoplatform.services.cms.i18n.MultiLanguageService;
50 import org.exoplatform.services.cms.templates.TemplateService;
51 import org.exoplatform.services.cms.views.ManageViewService;
52 import org.exoplatform.services.jcr.core.ManageableRepository;
53 import org.exoplatform.services.jcr.ext.common.SessionProvider;
54 import org.exoplatform.services.log.ExoLogger;
55 import org.exoplatform.services.log.Log;
56 import org.exoplatform.services.wcm.core.NodeLocation;
57 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
58 import org.exoplatform.web.application.Parameter;
59 import org.exoplatform.web.application.RequireJS;
60 import org.exoplatform.webui.application.WebuiRequestContext;
61 import org.exoplatform.webui.application.portlet.PortletRequestContext;
62 import org.exoplatform.webui.config.annotation.ComponentConfig;
63 import org.exoplatform.webui.config.annotation.EventConfig;
64 import org.exoplatform.webui.core.UIComponent;
65 import org.exoplatform.webui.core.UIPopupWindow;
66 import org.exoplatform.webui.event.Event;
67 import org.exoplatform.webui.event.EventListener;
68 import org.exoplatform.webui.ext.UIExtensionManager;
69
70
71
72
73
74
75
76 @ComponentConfig(
77 events = {
78 @EventConfig(listeners = UIViewSearchResult.ChangeLanguageActionListener.class),
79 @EventConfig(listeners = UIViewSearchResult.DownloadActionListener.class),
80 @EventConfig(listeners = UIViewSearchResult.ChangeNodeActionListener.class)
81 }
82 )
83 public class UIViewSearchResult extends UIBaseNodePresentation {
84
85 private NodeLocation node_ ;
86 private String language_ ;
87 final private static String COMMENT_COMPONENT = "Comment";
88
89
90
91 private static final Log LOG = ExoLogger.getLogger(UIViewSearchResult.class.getName());
92
93 public UIViewSearchResult() throws Exception {
94 }
95
96
97 public String getTemplate() {
98 TemplateService templateService = getApplicationComponent(TemplateService.class) ;
99 String userName = Util.getPortalRequestContext().getRemoteUser() ;
100 try {
101 String nodeType = getOriginalNode().getPrimaryNodeType().getName() ;
102 return templateService.getTemplatePathByUser(false, nodeType, userName) ;
103 } catch(Exception e) {
104 if (LOG.isErrorEnabled()) {
105 LOG.error(e);
106 }
107 }
108 return null;
109 }
110
111 public List<Node> getAttachments() throws Exception {
112 List<Node> attachments = new ArrayList<Node>() ;
113 Node originalNode = getOriginalNode();
114 NodeIterator childrenIterator = originalNode.getNodes();;
115 TemplateService templateService = getApplicationComponent(TemplateService.class) ;
116 int attachData = 0 ;
117 while(childrenIterator.hasNext()) {
118 Node childNode = childrenIterator.nextNode();
119 String nodeType = childNode.getPrimaryNodeType().getName();
120 List<String> listCanCreateNodeType =
121 Utils.getListAllowedFileType(originalNode, templateService) ;
122 if(listCanCreateNodeType.contains(nodeType)) {
123
124
125 if (childNode.hasProperty(Utils.JCR_DATA)) {
126 attachData = childNode.getProperty(Utils.JCR_DATA).getStream().available();
127
128
129 if (attachData > 0)
130 attachments.add(childNode);
131 } else {
132 attachments.add(childNode);
133 }
134 }
135 }
136 return attachments;
137 }
138
139 public String getViewableLink(Node attNode, Parameter[] params)
140 throws Exception {
141 return this.event("ChangeNode", Utils.formatNodeName(attNode.getPath()), params);
142 }
143
144 public UIComponent getRemoveAttach() throws Exception {
145 removeChild(RemoveAttachmentComponent.class);
146 UIComponent uicomponent = addChild(RemoveAttachmentComponent.class, null, "UIViewSearchResultRemoveAttach");
147 ((AbstractActionComponent)uicomponent).setLstComponentupdate(Arrays.asList(new Class[] {UIPopupWindow.class}));
148 return uicomponent;
149 }
150
151 public UIComponent getRemoveComment() throws Exception {
152 removeChild(RemoveCommentComponent.class);
153 UIComponent uicomponent = addChild(RemoveCommentComponent.class, null, "UIViewSearchResultRemoveComment");
154 ((AbstractActionComponent)uicomponent).setLstComponentupdate(Arrays.asList(new Class[] {UIPopupWindow.class}));
155 return uicomponent;
156 }
157
158 public Node getNode() throws ValueFormatException, PathNotFoundException, RepositoryException {
159 Node originalNode = getOriginalNode();
160 if(originalNode.hasProperty(Utils.EXO_LANGUAGE)) {
161 String defaultLang = originalNode.getProperty(Utils.EXO_LANGUAGE).getString() ;
162 if(language_ == null) language_ = defaultLang ;
163 if(originalNode.hasNode(Utils.LANGUAGES)) {
164 if(!language_.equals(defaultLang)) {
165 Node curNode = originalNode.getNode(Utils.LANGUAGES + Utils.SLASH + language_) ;
166 return curNode ;
167 }
168 }
169 return originalNode ;
170 }
171 return originalNode ;
172 }
173 public Node getOriginalNode(){
174 return NodeLocation.getNodeByLocation(node_);
175 }
176
177 public String getIcons(Node node, String size) throws Exception {
178 return Utils.getNodeTypeIcon(node, size) ;
179 }
180
181 public String getNodeType() throws Exception { return null; }
182
183 public List<Node> getRelations() throws Exception {
184 List<Node> relations = new ArrayList<Node>() ;
185 Node originalNode = getOriginalNode();
186 if (originalNode.hasProperty(Utils.EXO_RELATION)) {
187 Value[] vals = originalNode.getProperty(Utils.EXO_RELATION).getValues();
188 for (int i = 0; i < vals.length; i++) {
189 String uuid = vals[i].getString();
190 Node node = getNodeByUUID(uuid);
191 relations.add(node);
192 }
193 }
194 return relations;
195 }
196
197 public boolean isRssLink() { return false ; }
198 public String getRssLink() { return null ; }
199
200 public List<String> getSupportedLocalise() throws Exception {
201 MultiLanguageService multiLanguageService = getApplicationComponent(MultiLanguageService.class) ;
202 return multiLanguageService.getSupportedLanguages(getOriginalNode()) ;
203 }
204
205 public String getTemplatePath() throws Exception { return null; }
206
207 public boolean isNodeTypeSupported() { return false; }
208
209 public boolean isNodeTypeSupported(String nodeTypeName) {
210 try {
211 TemplateService templateService = getApplicationComponent(TemplateService.class);
212 return templateService.isManagedNodeType(nodeTypeName);
213 } catch (Exception e) {
214 return false;
215 }
216 }
217
218 public UIComponent getCommentComponent() {
219 UIComponent uicomponent = null;
220 try {
221 UIExtensionManager manager = getApplicationComponent(UIExtensionManager.class);
222 Map<String, Object> context = new HashMap<String, Object>();
223 UIJCRExplorer uiExplorer = getAncestorOfType(UIJCRExplorer.class);
224 context.put(UIJCRExplorer.class.getName(), uiExplorer);
225 context.put(Node.class.getName(), node_);
226 uicomponent = manager.addUIExtension(ManageViewService.EXTENSION_TYPE, COMMENT_COMPONENT, context, this);
227 } catch (Exception e) {
228 if (LOG.isErrorEnabled()) {
229 LOG.error("An error occurs while checking the action", e);
230 }
231 }
232 return (uicomponent != null ? uicomponent : this);
233 }
234
235 public boolean hasPropertyContent(Node node, String property) {
236 try {
237 String value = node.getProperty(property).getString() ;
238 if(value.length() > 0) return true ;
239 } catch (Exception e) {
240 if (LOG.isErrorEnabled()) {
241 LOG.error(e);
242 }
243 }
244 return false ;
245 }
246
247 public void setNode(Node node) {
248 node_ = NodeLocation.getNodeLocationByNode(node);
249 }
250
251 public Node getNodeByUUID(String uuid) {
252 ManageableRepository manageRepo = WCMCoreUtils.getRepository();
253 String[] workspaces = manageRepo.getWorkspaceNames() ;
254 SessionProvider sessionProvider = WCMCoreUtils.getUserSessionProvider();
255 for(String ws : workspaces) {
256 try{
257 return sessionProvider.getSession(ws,manageRepo).getNodeByUUID(uuid);
258 }catch(Exception e) {
259
260 }
261 }
262 return null;
263 }
264
265 public ResourceResolver getTemplateResourceResolver(WebuiRequestContext context, String template) {
266 return getAncestorOfType(UIJCRExplorer.class).getJCRTemplateResourceResolver() ;
267 }
268
269 public List<Node> getComments() throws Exception {
270 return getApplicationComponent(CommentsService.class).getComments(getOriginalNode(), language_) ;
271 }
272
273 public String getViewTemplate(String nodeTypeName, String templateName) throws Exception {
274 TemplateService tempServ = getApplicationComponent(TemplateService.class) ;
275 return tempServ.getTemplatePath(false, nodeTypeName, templateName) ;
276 }
277
278 public String getLanguage() { return language_; }
279
280 public void setLanguage(String language) { language_ = language ; }
281
282 @SuppressWarnings("unchecked")
283 public Object getComponentInstanceOfType(String className) {
284 Object service = null;
285 try {
286 ClassLoader loader = Thread.currentThread().getContextClassLoader();
287 Class clazz = loader.loadClass(className);
288 service = getApplicationComponent(clazz);
289 } catch (ClassNotFoundException ex) {
290 if (LOG.isErrorEnabled()) {
291 LOG.error(ex);
292 }
293 }
294 return service;
295 }
296
297
298 public String getImage(Node node) throws Exception {
299 DownloadService downloadService = getApplicationComponent(DownloadService.class) ;
300 InputStreamDownloadResource inputResource ;
301 Node imageNode = node.getNode(Utils.EXO_IMAGE) ;
302 InputStream input = imageNode.getProperty(Utils.JCR_DATA).getStream() ;
303 inputResource = new InputStreamDownloadResource(input, "image") ;
304 inputResource.setDownloadName(node.getName()) ;
305 return downloadService.getDownloadLink(downloadService.addDownloadResource(inputResource)) ;
306 }
307
308 public String getImage(Node node, String nodeTypeName) throws Exception {
309 DownloadService dservice = getApplicationComponent(DownloadService.class) ;
310 InputStreamDownloadResource dresource ;
311 Node imageNode = node.getNode(nodeTypeName) ;
312 InputStream input = imageNode.getProperty(Utils.JCR_DATA).getStream() ;
313 dresource = new InputStreamDownloadResource(input, "image") ;
314 dresource.setDownloadName(node.getName()) ;
315 return dservice.getDownloadLink(dservice.addDownloadResource(dresource)) ;
316 }
317
318 public String getPortalName() {
319 ExoContainer container = ExoContainerContext.getCurrentContainer();
320 PortalContainerInfo containerInfo = (PortalContainerInfo)container.getComponentInstanceOfType(PortalContainerInfo.class);
321 return containerInfo.getContainerName();
322 }
323
324 public String getRepository() throws Exception {
325 return ((ManageableRepository)getOriginalNode().getSession().getRepository()).getConfiguration().getName() ;
326 }
327
328 public String getWebDAVServerPrefix() throws Exception {
329 PortletRequestContext pRequestContext = PortletRequestContext.getCurrentInstance() ;
330 PortletRequest pRequest = pRequestContext.getRequest() ;
331 String prefixWebDAV = pRequest.getScheme() + "://" + pRequest.getServerName() + ":"
332 + String.format("%s",pRequest.getServerPort()) ;
333 return prefixWebDAV ;
334 }
335
336 public String getWorkspaceName() throws Exception {
337 return getOriginalNode().getSession().getWorkspace().getName() ;
338 }
339 static public class ChangeLanguageActionListener extends EventListener<UIViewSearchResult> {
340 public void execute(Event<UIViewSearchResult> event) throws Exception {
341 UIViewSearchResult uiViewSearchResult = event.getSource() ;
342 String selectedLanguage = event.getRequestContext().getRequestParameter(OBJECTID) ;
343 uiViewSearchResult.setLanguage(selectedLanguage) ;
344 event.getRequestContext().addUIComponentToUpdateByAjax(uiViewSearchResult.getParent()) ;
345 }
346 }
347 public String getDownloadLink(Node node) throws Exception {
348 return org.exoplatform.wcm.webui.Utils.getDownloadLink(node);
349 }
350
351 public String encodeHTML(String text) throws Exception {
352 return Utils.encodeHTML(text) ;
353 }
354
355 public String getTemplateSkin(String nodeTypeName, String skinName) throws Exception {
356 TemplateService tempServ = getApplicationComponent(TemplateService.class) ;
357 return tempServ.getSkinPath(nodeTypeName, skinName, getLanguage()) ;
358 }
359
360
361 public UIComponent getUIComponent(String mimeType) throws Exception {
362 return Utils.getUIComponent(mimeType, this);
363 }
364
365 static public class DownloadActionListener extends EventListener<UIViewSearchResult> {
366 public void execute(Event<UIViewSearchResult> event) throws Exception {
367 UIViewSearchResult uiComp = event.getSource() ;
368 String downloadLink = uiComp.getDownloadLink(org.exoplatform.wcm.webui.Utils.getFileLangNode(uiComp.getNode()));
369 RequireJS requireJS = event.getRequestContext().getJavascriptManager().getRequireJS();
370 requireJS.require("SHARED/ecm-utils", "ecmutil").addScripts("ecmutil.ECMUtils.ajaxRedirect('" + downloadLink + "');");
371 event.getRequestContext().addUIComponentToUpdateByAjax(uiComp.getParent()) ;
372 }
373 }
374
375 static public class ChangeNodeActionListener extends EventListener<UIViewSearchResult> {
376 public void execute(Event<UIViewSearchResult> event) throws Exception {
377 UIViewSearchResult uicomp = event.getSource() ;
378 UIJCRExplorer uiExplorer = uicomp.getAncestorOfType(UIJCRExplorer.class) ;
379 String uri = event.getRequestContext().getRequestParameter(OBJECTID) ;
380 String workspaceName = event.getRequestContext().getRequestParameter("workspaceName") ;
381 Session session = uiExplorer.getSessionByWorkspace(workspaceName) ;
382 Node selectedNode = (Node) session.getItem(uri) ;
383 uicomp.setNode(selectedNode) ;
384 event.getRequestContext().addUIComponentToUpdateByAjax(uicomp.getParent()) ;
385 }
386 }
387
388 public boolean isEnableComment() {
389 return false;
390 }
391
392 public boolean isEnableVote() {
393 return false;
394 }
395
396 public void setEnableComment(boolean value) {
397 }
398
399 public void setEnableVote(boolean value) {
400 }
401
402 public String getInlineEditingField(Node orgNode, String propertyName,
403 String defaultValue, String inputType, String idGenerator, String cssClass,
404 boolean isGenericProperty, String... arguments) throws Exception {
405 if (orgNode.hasProperty(propertyName)) {
406 return orgNode.getProperty(propertyName).getString();
407 }
408 return defaultValue;
409 }
410
411 public String getInlineEditingField(Node orgNode, String propertyName)
412 throws Exception {
413 if (orgNode.hasProperty(propertyName)) {
414 return orgNode.getProperty(propertyName).getString();
415 }
416 return "";
417 }
418 }