View Javadoc
1   /*
2    * Copyright (C) 2003-2010 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.wiki.service.wysiwyg;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.exoplatform.services.log.ExoLogger;
24  import org.exoplatform.services.log.Log;
25  import org.xwiki.gwt.wysiwyg.client.wiki.WikiPageReference;
26  import org.xwiki.model.EntityType;
27  import org.xwiki.model.reference.AttachmentReference;
28  import org.xwiki.model.reference.DocumentReference;
29  import org.xwiki.model.reference.EntityReference;
30  
31  public class EntityReferenceConverter {
32    /**
33     * Maps entity types to client reference component names.
34     */
35    private static final Map<EntityType, String> REFERENCE_COMPONENT_NAME;
36    
37    private static final Log      log               = ExoLogger.getLogger(EntityReferenceConverter.class);
38  
39    static {
40      REFERENCE_COMPONENT_NAME = new HashMap<EntityType, String>();
41      REFERENCE_COMPONENT_NAME.put(org.xwiki.model.EntityType.WIKI, WikiPageReference.WIKI_NAME);
42      REFERENCE_COMPONENT_NAME.put(EntityType.SPACE, WikiPageReference.SPACE_NAME);
43      REFERENCE_COMPONENT_NAME.put(EntityType.DOCUMENT, WikiPageReference.PAGE_NAME);
44      REFERENCE_COMPONENT_NAME.put(EntityType.ATTACHMENT,
45                                   org.xwiki.gwt.wysiwyg.client.wiki.AttachmentReference.FILE_NAME);
46    }
47  
48    /**
49     * Converts an entity reference received from the client to an entity
50     * reference to be used on the server.
51     * 
52     * @param clientEntityReference a client-side entity reference
53     * @return a server-side entity reference
54     */
55    public EntityReference convert(org.xwiki.gwt.wysiwyg.client.wiki.EntityReference clientEntityReference) {
56      if (clientEntityReference == null) {
57        return null;
58      }
59      EntityReference serverEntityReference = null;
60      String wikiName = clientEntityReference.getComponent(REFERENCE_COMPONENT_NAME.get(EntityType.WIKI));
61      if (!StringUtils.isEmpty(wikiName)) {
62        serverEntityReference = new EntityReference(wikiName, EntityType.WIKI);
63      }
64      String spaceName = clientEntityReference.getComponent(REFERENCE_COMPONENT_NAME.get(EntityType.SPACE));
65      if (!StringUtils.isEmpty(spaceName)) {
66        serverEntityReference = new EntityReference(spaceName,
67                                                    EntityType.SPACE,
68                                                    serverEntityReference);
69      }
70      String pageName = clientEntityReference.getComponent(REFERENCE_COMPONENT_NAME.get(EntityType.DOCUMENT));
71      if (!StringUtils.isEmpty(pageName)) {
72        serverEntityReference = new EntityReference(pageName,
73                                                    EntityType.DOCUMENT,
74                                                    serverEntityReference);
75      }
76      String fileName = clientEntityReference.getComponent(REFERENCE_COMPONENT_NAME.get(EntityType.ATTACHMENT));
77      if (!StringUtils.isEmpty(fileName)) {
78        serverEntityReference = new EntityReference(fileName,
79                                                    EntityType.ATTACHMENT,
80                                                    serverEntityReference);
81      }
82      return serverEntityReference;
83    }
84  
85    /**
86     * Converts an entity reference used on the server side to an entity reference
87     * to be sent to the client.
88     * 
89     * @param serverEntityReference a server-side entity reference
90     * @return the corresponding client-side entity reference
91     */
92    public org.xwiki.gwt.wysiwyg.client.wiki.EntityReference convert(EntityReference serverEntityReference) {
93      org.xwiki.gwt.wysiwyg.client.wiki.EntityReference clientEntityReference = new org.xwiki.gwt.wysiwyg.client.wiki.EntityReference();
94      try {
95        clientEntityReference.setType(org.xwiki.gwt.wysiwyg.client.wiki.EntityReference.EntityType.valueOf(serverEntityReference.getType()
96                                                                                                                                .toString()));
97      } catch (Exception e) {
98        log.error("Can't set type for client entity reference", e);
99        return null;
100     }
101     EntityReference child = serverEntityReference;
102     while (child != null) {
103       String componentName = REFERENCE_COMPONENT_NAME.get(child.getType());
104       if (componentName != null) {
105         clientEntityReference.setComponent(componentName, child.getName());
106       }
107       child = child.getParent();
108     }
109     return clientEntityReference;
110   }
111 
112   /**
113    * @param documentReference a document reference
114    * @return the corresponding wiki page reference
115    */
116   public WikiPageReference convert(DocumentReference documentReference) {
117     String wikiName = documentReference.getWikiReference().getName();
118     String spaceName = documentReference.getLastSpaceReference().getName();
119     String pageName = documentReference.getName();
120     return new WikiPageReference(wikiName, spaceName, pageName);
121   }
122 
123   /**
124    * @param reference a wiki page reference
125    * @return the corresponding document reference
126    */
127   public DocumentReference convert(WikiPageReference reference) {
128     return new DocumentReference(reference.getWikiName(),
129                                  reference.getSpaceName(),
130                                  reference.getPageName());
131   }
132 
133   /**
134    * @param attachmentReference an attachment reference
135    * @return the corresponding client attachment reference
136    */
137   public org.xwiki.gwt.wysiwyg.client.wiki.AttachmentReference convert(org.xwiki.model.reference.AttachmentReference attachmentReference) {
138     return new org.xwiki.gwt.wysiwyg.client.wiki.AttachmentReference(attachmentReference.getName(),
139                                                                      convert(attachmentReference.getDocumentReference()));
140   }
141 
142   /**
143    * @param clientAttachmentReference a client attachment reference
144    * @return the corresponding server-side attachment reference
145    */
146   public AttachmentReference convert(org.xwiki.gwt.wysiwyg.client.wiki.AttachmentReference clientAttachmentReference) {
147     return new AttachmentReference(clientAttachmentReference.getFileName(),
148                                    convert(clientAttachmentReference.getWikiPageReference()));
149   }
150 }