1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
50
51
52
53
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
87
88
89
90
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
114
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
125
126
127 public DocumentReference convert(WikiPageReference reference) {
128 return new DocumentReference(reference.getWikiName(),
129 reference.getSpaceName(),
130 reference.getPageName());
131 }
132
133
134
135
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
144
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 }