View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.wcm.javascript;
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  
26  /**
27   * Created by The eXo Platform SAS
28   * @author : Hoa.Pham
29   *          hoa.pham@exoplatform.com
30   * May 28, 2008
31   */
32  public class JSFileHandler extends BaseWebSchemaHandler {
33  
34    protected String getHandlerNodeType() { return "nt:file"; }
35    protected String getParentNodeType() { return "exo:jsFolder" ;}
36    private boolean isPortalJSFolder = false;
37  
38    public boolean matchHandler(SessionProvider sessionProvider, Node node) throws Exception {
39      if(!matchNodeType(node))
40        return false;
41      if(!matchMimeType(node))
42        return false;
43      isPortalJSFolder = isInPortalJSFolder(sessionProvider, node);
44      if(isPortalJSFolder) {
45        return true;
46      }
47      if(!matchParenNodeType(node)) {
48        return false;
49      }
50      return true;
51    }
52  
53    private boolean matchNodeType(Node node) throws Exception{
54      return node.getPrimaryNodeType().getName().equals("nt:file");
55    }
56  
57    private boolean matchParenNodeType(Node node ) throws Exception{
58      return node.getParent().isNodeType("exo:jsFolder");
59    }
60  
61    private boolean matchMimeType(Node node) throws Exception{
62      String mimeType = getFileMimeType(node);
63      if("text/javascript".equals(mimeType))
64        return true;
65      if("application/x-javascript".equals(mimeType))
66        return true;
67      if("text/ecmascript".equals(mimeType))
68        return true;
69      if("text/plain".equals(mimeType))
70        return true;
71      return false;
72    }
73  
74    private boolean isInPortalJSFolder(SessionProvider sessionProvider, Node file) throws Exception {
75      Node portal = findPortalNode(sessionProvider, file);
76      if(portal == null)  {
77        return false;
78      }
79      WebSchemaConfigService schemaConfigService = getService(WebSchemaConfigService.class);
80      PortalFolderSchemaHandler schemaHandler = schemaConfigService.getWebSchemaHandlerByType(PortalFolderSchemaHandler.class);
81      Node jsFolder = schemaHandler.getJSFolder(portal);
82      return jsFolder == null ? false : file.getPath().startsWith(jsFolder.getPath());
83    }
84  
85    public void onCreateNode(SessionProvider sessionProvider, Node file) throws Exception {
86      addMixin(file, "exo:jsFile") ;
87      addMixin(file,"exo:owneable");
88      file.setProperty("exo:presentationType","exo:jsFile");
89      //If this jsFile belong to jsFolder of portal, the jsFile will be shared jsFile
90      if(isPortalJSFolder) {
91        file.setProperty("exo:sharedJS",true);
92      }
93    }
94  
95    public void onModifyNode(SessionProvider sessionProvider, Node file) throws Exception {
96      if(isPortalJSFolder) {
97        Node portal = findPortalNode(sessionProvider, file);
98        XJavascriptService javascriptService = getService(XJavascriptService.class);
99        javascriptService.updatePortalJSOnModify(portal, file);
100     }
101   }
102 
103   public void onRemoveNode(SessionProvider sessionProvider, Node file) throws Exception {
104     if(isPortalJSFolder) {
105       Node portal = findPortalNode(sessionProvider, file);
106       XJavascriptService javascriptService = getService(XJavascriptService.class);
107       javascriptService.updatePortalJSOnRemove(portal, file);
108     }
109   }
110 
111 }