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.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   * Created by The eXo Platform SAS.
37   *
38   * @author : Hoa.Pham hoa.pham@exoplatform.com May 28, 2008
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    /* (non-Javadoc)
48     * @see org.exoplatform.services.wcm.core.WebSchemaHandler#matchHandler(javax.jcr.Node)
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    /* (non-Javadoc)
61     * @see org.exoplatform.services.wcm.core.WebSchemaHandler#onCreateNode(javax.jcr.Node)
62     */
63    public void onCreateNode(SessionProvider sessionProvider, Node node) throws Exception { }
64  
65    /* (non-Javadoc)
66     * @see org.exoplatform.services.wcm.core.WebSchemaHandler#onModifyNode(javax.jcr.Node)
67     */
68    public void onModifyNode(SessionProvider sessionProvider, Node node) throws Exception { }
69  
70    /* (non-Javadoc)
71     * @see org.exoplatform.services.wcm.core.WebSchemaHandler#onRemoveNode(javax.jcr.Node)
72     */
73    public void onRemoveNode(SessionProvider sessionProvider, Node node) throws Exception { }
74  
75    /**
76     * Gets the handler node type.
77     *
78     * @return the handler node type
79     */
80    protected abstract String getHandlerNodeType() ;
81  
82    /**
83     * Gets the parent node type.
84     *
85     * @return the parent node type
86     */
87    protected abstract String getParentNodeType();
88  
89    /**
90     * Adds the mixin.
91     *
92     * @param node the node
93     * @param mixin the mixin
94     * @throws RepositoryException 
95     * @throws LockException 
96     * @throws ConstraintViolationException 
97     * @throws VersionException 
98     * @throws NoSuchNodeTypeException 
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     //create empty css file:
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 }