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 java.util.GregorianCalendar;
20  import java.util.List;
21  
22  import javax.jcr.Node;
23  import javax.jcr.Session;
24  
25  import org.exoplatform.services.jcr.ext.common.SessionProvider;
26  import org.exoplatform.services.wcm.core.BaseWebSchemaHandler;
27  import org.exoplatform.services.wcm.core.WebSchemaConfigService;
28  import org.exoplatform.services.wcm.link.LiveLinkManagerService;
29  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
30  
31  /**
32   * Created by The eXo Platform SAS.
33   *
34   * @author : Hoa.Pham
35   * hoa.pham@exoplatform.com
36   * Jun 23, 2008
37   */
38  
39  public class HTMLFileSchemaHandler extends BaseWebSchemaHandler {
40  
41    /* (non-Javadoc)
42     * @see org.exoplatform.services.wcm.core.BaseWebSchemaHandler#getHandlerNodeType()
43     */
44    protected String getHandlerNodeType() {   return "nt:file"; }
45  
46    /* (non-Javadoc)
47     * @see org.exoplatform.services.wcm.core.BaseWebSchemaHandler#getParentNodeType()
48     */
49    protected String getParentNodeType() { return "exo:webFolder"; }
50  
51    /*
52     * (non-Javadoc)
53     * @see
54     * org.exoplatform.services.wcm.core.BaseWebSchemaHandler#matchHandler(javax
55     * .jcr.Node, org.exoplatform.services.jcr.ext.common.SessionProvider)
56     */
57    public boolean matchHandler(SessionProvider sessionProvider, Node node) throws Exception {
58      if(!matchNodeType(node))
59        return false;
60      if(!matchMimeType(node))
61        return false;
62      if(!matchParentNodeType(node)) {
63        if(!isInWebContent(node))
64          return false;
65      }
66      return true;
67    }
68  
69    /**
70     * Match node type.
71     *
72     * @param node the node
73     *
74     * @return true, if successful
75     *
76     * @throws Exception the exception
77     */
78    private boolean matchNodeType(Node node) throws Exception{
79      return node.getPrimaryNodeType().getName().equals("nt:file");
80    }
81  
82    /**
83     * Match mime type.
84     *
85     * @param node the node
86     *
87     * @return true, if successful
88     *
89     * @throws Exception the exception
90     */
91    private boolean matchMimeType(Node node) throws Exception {
92      String mimeType = getFileMimeType(node);
93      if("text/html".equals(mimeType))
94        return true;
95      if("text/plain".equals(mimeType))
96        return true;
97      return false;
98    }
99  
100   /**
101    * Checks if is in web content.
102    *
103    * @param file the file
104    *
105    * @return true, if is in web content
106    *
107    * @throws Exception the exception
108    */
109   public boolean isInWebContent(Node file) throws Exception{
110     if(file.getParent().isNodeType("exo:webContent")) {
111       return file.isNodeType("exo:htmlFile");
112     }
113     return false;
114   }
115 
116   /**
117    * Match parent node type.
118    *
119    * @param file the file
120    *
121    * @return true, if successful
122    *
123    * @throws Exception the exception
124    */
125   private boolean matchParentNodeType(Node file) throws Exception{
126     return file.getParent().isNodeType("exo:webFolder");
127   }
128 
129   /*
130    * (non-Javadoc)
131    * @see
132    * org.exoplatform.services.wcm.core.BaseWebSchemaHandler#onCreateNode(javax
133    * .jcr.Node, org.exoplatform.services.jcr.ext.common.SessionProvider)
134    */
135   public void onCreateNode(SessionProvider sessionProvider, final Node file) throws Exception {
136     Session session = file.getSession();
137     Node webFolder = file.getParent();
138     String fileName = file.getName();
139     //create temp folder
140     addMixin(file, "exo:htmlFile");
141     file.setProperty("exo:presentationType","exo:htmlFile");
142     String tempFolderName = fileName + this.hashCode();
143     Node tempFolder = webFolder.addNode(tempFolderName, NT_UNSTRUCTURED);
144     String tempPath = tempFolder.getPath() + "/" +file.getName();
145     session.move(file.getPath(),tempPath);
146     webFolder.save();
147     //rename the folder
148     Node webContent = webFolder.addNode(fileName, "exo:webContent");
149     addMixin(webContent,"exo:privilegeable");
150     addMixin(webContent,"exo:owneable");
151     // need check why WebContentSchemaHandler doesn't run for this case
152     WebSchemaConfigService schemaConfigService = WCMCoreUtils.getService(WebSchemaConfigService.class);
153     WebContentSchemaHandler contentSchemaHandler = schemaConfigService.getWebSchemaHandlerByType(WebContentSchemaHandler.class);
154     contentSchemaHandler.createSchema(webContent);
155     webFolder.save();
156     //the htmlFile become default.html file for the web content
157     String htmlFilePath = webContent.getPath() + "/default.html";
158     session.move(tempPath, htmlFilePath);
159     tempFolder.remove();
160     createDefautWebData(webContent);
161     webFolder.save();
162   }
163 
164   /*
165    * (non-Javadoc)
166    * @see
167    * org.exoplatform.services.wcm.core.BaseWebSchemaHandler#onModifyNode(javax
168    * .jcr.Node, org.exoplatform.services.jcr.ext.common.SessionProvider)
169    */
170   public void onModifyNode(final SessionProvider sessionProvider, final Node node) throws Exception {
171     if(node.hasNode("jcr:content")) {
172       Node contentNode = node.getNode("jcr:content");
173       contentNode.setProperty("jcr:lastModified",new GregorianCalendar());
174       contentNode.setProperty("jcr:mimeType", "text/html");
175     }
176 
177     Node parent = node.getParent();
178     if(!parent.isNodeType("exo:webContent"))
179       return;
180     if (!parent.isCheckedOut() || parent.isLocked() || !node.isCheckedOut()) {
181       return;
182     }
183     LiveLinkManagerService liveLinkManagerService = WCMCoreUtils.getService(LiveLinkManagerService.class);
184     List<String> newLinks = liveLinkManagerService.extractLinks(node);
185     liveLinkManagerService.updateLinkDataForNode(parent,newLinks);
186   }
187 
188 }