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.DateFormat;
20  import java.text.SimpleDateFormat;
21  import java.util.Date;
22  
23  import javax.jcr.Node;
24  import javax.jcr.nodetype.NodeType;
25  import javax.ws.rs.core.CacheControl;
26  import javax.ws.rs.core.MediaType;
27  import javax.ws.rs.core.Response;
28  
29  import org.exoplatform.container.ExoContainer;
30  import org.exoplatform.services.cms.templates.TemplateService;
31  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
32  import org.w3c.dom.Document;
33  import org.w3c.dom.Element;
34  
35  /**
36   * Created by The eXo Platform SAS
37   *
38   * @author : Hoa.Pham hoa.pham@exoplatform.com Jun 23, 2008
39   */
40  public class FCKFolderHandler {
41    private TemplateService templateService;
42  
43    private FCKMessage      fckMessage;
44  
45    public FCKFolderHandler(ExoContainer container) {
46      templateService = WCMCoreUtils.getService(TemplateService.class);
47      fckMessage = new FCKMessage();
48    }
49  
50    public String getFolderType(final Node node) throws Exception {
51      // need use a service to get extended folder type for the node
52      NodeType nodeType = node.getPrimaryNodeType();
53      String primaryType = nodeType.getName();
54      if (templateService.getDocumentTemplates().contains(primaryType))
55        return null;
56      if (FCKUtils.NT_UNSTRUCTURED.equals(primaryType) || FCKUtils.NT_FOLDER.equals(primaryType))
57        return primaryType;
58      if (nodeType.isNodeType(FCKUtils.NT_UNSTRUCTURED) || nodeType.isNodeType(FCKUtils.NT_FOLDER)) {
59        // check if the nodetype is exo:videoFolder...
60        return primaryType;
61      }
62      return primaryType;
63    }
64  
65    public String getFolderURL(final Node folder) throws Exception {
66      return FCKUtils.createWebdavURL(folder);
67    }
68  
69    /**
70     * Creates the folder element for connector response look like {@code <folder name=""
71     * url="" folderType="" />}
72     *
73     * @param document the document
74     * @param child the child
75     * @param folderType the folder type
76     * @return the org.w3c.dom.Element element
77     * @throws Exception the exception
78     */
79    public Element createFolderElement(Document document, Node child, String folderType)
80        throws Exception {
81      Element folder = document.createElement("Folder");
82      folder.setAttribute("name", child.getName());
83      folder.setAttribute("url", getFolderURL(child));
84      folder.setAttribute("folderType", folderType);
85      return folder;
86    }
87  
88    public Response createNewFolder(Node currentNode, String newFolderName, String language)
89        throws Exception {
90      CacheControl cacheControl = new CacheControl();
91      cacheControl.setNoCache(true);
92      Document document = null;
93      DateFormat dateFormat = new SimpleDateFormat(FCKUtils.IF_MODIFIED_SINCE_DATE_FORMAT);
94      if (currentNode != null) {
95        if (!FCKUtils.hasAddNodePermission(currentNode)) {
96          Object[] args = { currentNode.getPath() };
97          document = fckMessage.createMessage(FCKMessage.FOLDER_PERMISSION_CREATING, FCKMessage.ERROR,
98              language, args);
99          return Response.ok(document, new MediaType("text", "xml"))
100                        .cacheControl(cacheControl)
101                        .header(FCKUtils.LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
102                        .build();
103       }
104       if (currentNode.hasNode(newFolderName)) {
105         Object[] args = { currentNode.getPath(), newFolderName };
106         document = fckMessage.createMessage(FCKMessage.FOLDER_EXISTED, FCKMessage.ERROR, language,
107             args);
108         return Response.ok(document, new MediaType("text", "xml"))
109                        .cacheControl(cacheControl)
110                        .header(FCKUtils.LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
111                        .build();
112       }
113       currentNode.addNode(newFolderName, FCKUtils.NT_FOLDER);
114       currentNode.save();
115 
116       Element rootElement = FCKUtils.createRootElement("createFolder", currentNode,
117           getFolderType(currentNode));
118       document = rootElement.getOwnerDocument();
119       Element errorElement = document.createElement("Message");
120       errorElement.setAttribute("number", Integer.toString(FCKMessage.FOLDER_CREATED));
121       errorElement.setAttribute("text", fckMessage.getMessage(FCKMessage.FOLDER_CREATED, null,
122           language));
123       errorElement.setAttribute("type", FCKMessage.ERROR);
124       rootElement.appendChild(errorElement);
125       return Response.ok(document, new MediaType("text", "xml"))
126                      .cacheControl(cacheControl)
127                      .header(FCKUtils.LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
128                      .build();
129     }
130 
131     document = fckMessage.createMessage(FCKMessage.FOLDER_NOT_CREATED, FCKMessage.ERROR, language, null);
132     return Response.ok(document, new MediaType("text", "xml"))
133                    .cacheControl(cacheControl)
134                    .header(FCKUtils.LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
135                    .build();
136   }
137 }