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.webcontent;
18  
19  import javax.jcr.Node;
20  import javax.jcr.Session;
21  
22  import org.exoplatform.services.jcr.ext.common.SessionProvider;
23  import org.exoplatform.services.wcm.core.BaseWebSchemaHandler;
24  
25  /**
26   * Created by The eXo Platform SAS
27   * @author : Hoa.Pham
28   *          hoa.pham@exoplatform.com
29   * May 28, 2008
30   */
31  public class WebContentSchemaHandler extends BaseWebSchemaHandler {
32  
33    protected String getHandlerNodeType() { return "exo:webContent"; }
34    protected String getParentNodeType() { return "nt:unstructured"; }
35  
36    public boolean matchHandler(SessionProvider sessionProvider, Node node) throws Exception {
37      String handlerNodeType = getHandlerNodeType();
38      String parentNodeType = getParentNodeType();
39      if(!node.isNodeType(handlerNodeType))
40        return false;
41      if(!node.getParent().isNodeType(parentNodeType))
42        return false;
43      return true;
44    }
45  
46    public void onCreateNode(SessionProvider sessionProvider, final Node webContent) throws Exception {
47      createSchema(webContent);
48      webContent.getParent().save();
49    }
50  
51    public Node getCSSFolder(final Node webContent) throws Exception {
52      return webContent.getNode("css");
53    }
54  
55    public Node getJSFolder(final Node webContent) throws Exception {
56      return webContent.getNode("js");
57    }
58  
59    public Node getImagesFolders(final Node webContent) throws Exception {
60      return webContent.getNode("medias/images");
61    }
62  
63    public Node getIllustrationImage(final Node webContent) throws Exception {
64      return webContent.getNode("medias/images/illustration");
65    }
66  
67    public Node getVideoFolder(final Node webContent) throws Exception {
68      return webContent.getNode("medias/videos");
69    }
70  
71    public Node getDocumentFolder (final Node webContent) throws Exception {
72      return webContent.getNode("documents");
73    }
74  
75    public void createDefaultSchema(Node webContent) throws Exception{
76      addMixin(webContent,"exo:owneable");
77      createSchema(webContent);
78      createDefautWebData(webContent);
79    }
80  
81    protected void createSchema(final Node webContent) throws Exception {
82      if (!webContent.hasNode("js")) {
83        Node js = webContent.addNode("js","exo:jsFolder");
84        addMixin(js,"exo:owneable");
85      }
86      if (!webContent.hasNode("css")) {
87        Node css = webContent.addNode("css","exo:cssFolder");
88        addMixin(css,"exo:owneable");
89      }
90      if (!webContent.hasNode("medias")) {
91        Node multimedia = webContent.addNode("medias","exo:multimediaFolder");
92        addMixin(multimedia,"exo:owneable");
93        Node images = multimedia.addNode("images",NT_FOLDER);
94        addMixin(images, "exo:pictureFolder");
95        addMixin(images,"exo:owneable");
96        Node video = multimedia.addNode("videos",NT_FOLDER);
97        addMixin(video, "exo:videoFolder");
98        addMixin(video,"exo:owneable");
99        Node audio = multimedia.addNode("audio",NT_FOLDER);
100       addMixin(audio, "exo:musicFolder");
101       addMixin(audio,"exo:owneable");
102     }
103     if (!webContent.hasNode("documents")) {
104       Node document = webContent.addNode("documents",NT_UNSTRUCTURED);
105       addMixin(document, "exo:documentFolder");
106       addMixin(document,"exo:owneable");
107     }
108     //because exo:webcontent is exo:rss-enable so need set exo:title of the webcontent
109     //by default, value of exo:title is webcontent name
110     webContent.setProperty("exo:title", webContent.getName());
111   }
112 
113   public boolean isWebcontentChildNode(Node file) throws Exception{
114     Node parent = file.getParent();
115     //for sub nodes of the webcontent node
116     if(parent.isNodeType("exo:webContent"))
117       return true;
118     //for subnodes in some folders like css, js, documents, medias
119     if(parent.getPath().equals("/"))
120       return false;
121     Node grantParent = parent.getParent();
122     if(grantParent.isNodeType("exo:webContent"))
123       return true;
124     //for subnodes in some folders like images, videos, audio
125     if(grantParent.getPath().equals("/"))
126       return false;
127     Node ansestor = grantParent.getParent();
128     if(ansestor.isNodeType("exo:webContent"))
129       return true;
130     return false;
131   }
132 }