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.ecm.connector.fckeditor;
18  
19  import java.text.SimpleDateFormat;
20  
21  import javax.jcr.Node;
22  
23  import org.exoplatform.commons.utils.ISO8601;
24  import org.exoplatform.container.ExoContainer;
25  import org.exoplatform.services.cms.templates.TemplateService;
26  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
27  import org.w3c.dom.Document;
28  import org.w3c.dom.Element;
29  
30  /**
31   * Created by The eXo Platform SAS.
32   *
33   * @author : Hoa.Pham hoa.pham@exoplatform.com Jun 23, 2008
34   */
35  public class FCKFileHandler {
36  
37    private TemplateService templateService;
38  
39    private static final String[] IMAGE_MIMETYPE = {"image/gif", "image/jpeg", "image/bmp", "image/png", "image/tiff"};
40  
41    /**
42     * Instantiates a new fCK file handler.
43     *
44     * @param container the container
45     */
46    public FCKFileHandler(ExoContainer container) {
47      templateService = WCMCoreUtils.getService(TemplateService.class);
48    }
49  
50    /**
51     * Gets the file type.
52     *
53     * @param node the node
54     * @param resourceType the resource type
55     * @return the file type
56     * @throws Exception the exception
57     */
58    public String getFileType(final Node node, final String resourceType) throws Exception {
59      if(FCKUtils.DOCUMENT_TYPE.equalsIgnoreCase(resourceType)) {
60        return getDocumentType(node);
61      }else if(FCKUtils.IMAGE_TYPE.equalsIgnoreCase(resourceType)) {
62        return getImageType(node);
63      }else if(FCKUtils.FLASH_TYPE.equalsIgnoreCase(resourceType)) {
64        return getFlashType(node);
65      }else if(FCKUtils.LINK_TYPE.equalsIgnoreCase(resourceType)) {
66        return getLinkType(node);
67      }
68      return null;
69    }
70  
71    /**
72     * Gets the file url.
73     *
74     * @param file the file
75     * @return the file url
76     * @throws Exception the exception
77     */
78    protected String getFileURL(final Node file) throws Exception {
79      return FCKUtils.createWebdavURL(file);
80    }
81  
82    /**
83     * Creates the file element for connector response looks like that {@code <File
84     * name="" fileType="" dateCreated="" dateModified="" creator="" size=""
85     * url="" />}.
86     *
87     * @param document the document
88     * @param child the child
89     * @param fileType the file type
90     * @return the org.w3c.dom.Element element
91     * @throws Exception the exception
92     */
93    public Element createFileElement(Document document, Node child, String fileType) throws Exception {
94      Element file = document.createElement("File");
95      file.setAttribute("name", child.getName());
96      SimpleDateFormat formatter = new SimpleDateFormat(ISO8601.SIMPLE_DATETIME_FORMAT);
97      file.setAttribute("dateCreated", formatter.format(child.getProperty("exo:dateCreated").getDate().getTime()));
98      file.setAttribute("dateModified", formatter.format(child.getProperty("exo:dateModified").getDate().getTime()));
99      file.setAttribute("creator", child.getProperty("exo:owner").getString());
100     file.setAttribute("fileType", fileType);
101     file.setAttribute("url",getFileURL(child));
102     if(child.isNodeType(FCKUtils.NT_FILE)) {
103       long size = child.getNode("jcr:content").getProperty("jcr:data").getLength();
104       file.setAttribute("size", "" + size / 1000);
105     }else {
106       file.setAttribute("size", "");
107     }
108     return file;
109   }
110 
111   /**
112    * Gets the document type.
113    *
114    * @param node the node
115    * @return the document type
116    * @throws Exception the exception
117    */
118   protected String getDocumentType(final Node node) throws Exception {
119     if (node.isNodeType("exo:presentationable"))
120       return node.getProperty("exo:presentationType").getString();
121     String primaryType = node.getPrimaryNodeType().getName();
122     if (templateService.getDocumentTemplates().contains(primaryType))
123       return primaryType;
124     return null;
125   }
126 
127   /**
128    * Gets the image type.
129    *
130    * @param node the node
131    * @return the image type
132    * @throws Exception the exception
133    */
134   protected String getImageType(final Node node) throws Exception {
135     if(node.isNodeType("nt:file")) {
136       String mimeType = node.getNode("jcr:content").getProperty("jcr:mimeType").getString();
137       for(String s:IMAGE_MIMETYPE) {
138         if(s.equals(mimeType)) {
139           return node.getPrimaryNodeType().getName();
140         }
141       }
142     }
143     return null;
144   }
145 
146   /**
147    * Gets the flash type.
148    *
149    * @param node the node
150    * @return the flash type
151    * @throws Exception the exception
152    */
153   protected String getFlashType(final Node node) throws Exception {
154     return null;
155   }
156 
157   /**
158    * Gets the link type.
159    *
160    * @param node the node
161    * @return the link type
162    * @throws Exception the exception
163    */
164   protected String getLinkType(final Node node) throws Exception {
165     return "exo:link";
166   }
167 
168 }