1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.wiki.mow.core.api.wiki;
18
19 import org.chromattic.api.ChromatticSession;
20 import org.chromattic.api.RelationshipType;
21 import org.chromattic.api.annotations.*;
22 import org.chromattic.ext.ntdef.NTFile;
23 import org.chromattic.ext.ntdef.Resource;
24 import org.exoplatform.wiki.WikiException;
25 import org.exoplatform.wiki.chromattic.ext.ntdef.NTVersion;
26 import org.exoplatform.wiki.chromattic.ext.ntdef.VersionableMixin;
27 import org.exoplatform.wiki.mow.api.PermissionType;
28 import org.exoplatform.wiki.mow.core.api.MOWService;
29 import org.exoplatform.wiki.utils.JCRUtils;
30 import org.exoplatform.wiki.utils.Utils;
31
32 import javax.jcr.Node;
33 import javax.jcr.RepositoryException;
34 import javax.jcr.version.Version;
35 import java.io.UnsupportedEncodingException;
36 import java.net.URLEncoder;
37 import java.util.Calendar;
38 import java.util.GregorianCalendar;
39 import java.util.HashMap;
40
41
42 @PrimaryType(name = WikiNodeType.WIKI_ATTACHMENT)
43 public abstract class AttachmentImpl extends NTFile implements Comparable<AttachmentImpl> {
44
45 private PermissionImpl permission = new PermissionImpl();
46
47 @Name
48 public abstract String getName();
49 public abstract void setName(String name);
50
51 @Path
52 public abstract String getPath();
53
54 public String getJCRContentPath() {
55 return getPath() + "/jcr:content";
56 }
57
58 @WorkspaceName
59 public abstract String getWorkspace();
60
61 @Property(name = WikiNodeType.Definition.TITLE)
62 public abstract String getTitle();
63 public abstract void setTitle(String title);
64
65 @Property(name = WikiNodeType.Definition.FILE_TYPE)
66 public abstract String getFileType();
67 public abstract void setFileType(String fileType);
68
69 @Property(name = WikiNodeType.Definition.CREATOR)
70 public abstract String getCreator();
71 public abstract void setCreator(String creator);
72
73 public Calendar getCreatedDate() {
74 Calendar calendar = GregorianCalendar.getInstance();
75 calendar.setTime(getCreated());
76 return calendar;
77 }
78
79 public long getWeightInBytes() {
80 return getContentResource().getData().length;
81 }
82
83 public Calendar getUpdatedDate() {
84 Calendar calendar = GregorianCalendar.getInstance();
85 calendar.setTime(getLastModified());
86 return calendar;
87 }
88
89 public String getDownloadURL() {
90 StringBuilder sb = new StringBuilder();
91 String mimeType = getContentResource().getMimeType();
92 PageImpl page = this.getParentPage();
93 WikiImpl wiki = page.getWiki();
94 if (mimeType != null && mimeType.startsWith("image/") && wiki != null) {
95
96 sb.append(Utils.getDefaultRestBaseURI())
97 .append("/wiki/images/")
98 .append(wiki.getType())
99 .append("/")
100 .append(Utils.SPACE)
101 .append("/")
102 .append(Utils.validateWikiOwner(wiki.getType(), wiki.getOwner()))
103 .append("/")
104 .append(Utils.PAGE)
105 .append("/")
106 .append(page.getName());
107 try{
108 sb.append("/").append(URLEncoder.encode(this.getName(), "UTF-8"));
109 }catch (UnsupportedEncodingException e) {
110 sb.append("/").append(this.getName());
111 }
112 } else {
113 sb.append(JCRUtils.getCurrentRepositoryWebDavUri());
114 sb.append(getWorkspace());
115 String path = getPath();
116 try {
117 String parentPath = path.substring(0, path.lastIndexOf("/"));
118 sb.append(parentPath + "/" + URLEncoder.encode(getName(), "UTF-8"));
119 } catch (UnsupportedEncodingException e) {
120 sb.append(path);
121 }
122 }
123 return sb.toString();
124 }
125
126 public String getFullTitle() {
127 String title = getTitle();
128 if(title == null) {
129 return null;
130 }
131 String fullTitle = (getFileType() == null) ? title : title.concat(getFileType());
132 return (fullTitle != null) ? fullTitle : getName();
133 }
134
135 @ManyToOne
136 public abstract PageImpl getParentPage();
137
138 @Destroy
139 public abstract void remove();
140
141 public String getText() {
142 Resource textContent = getContentResource();
143 if (textContent == null) {
144 setText("");
145 textContent = getContentResource();
146 }
147
148 try {
149 return new String(textContent.getData(), "UTF-8");
150 } catch (UnsupportedEncodingException e) {
151 return new String(textContent.getData());
152 }
153 }
154
155 public void setText(String text) {
156 text = text != null ? text : "";
157 Resource textContent = Resource.createPlainText(text);
158 setContentResource(textContent);
159 }
160
161 public boolean hasPermission(PermissionType permissionType) {
162 if (permission
163 .getMOWService() == null) {
164 permission.setMOWService(getParentPage().getMOWService());
165 }
166 return permission.hasPermission(permissionType, getPath());
167 }
168
169 public HashMap<String, String[]> getPermission() throws WikiException {
170 if (permission.getMOWService() == null) {
171 permission.setMOWService(getParentPage().getMOWService());
172 }
173 return permission.getPermission(getPath());
174 }
175
176 public void setPermission(HashMap<String, String[]> permissions) throws WikiException {
177 if (permission.getMOWService() == null) {
178 permission.setMOWService(getParentPage().getMOWService());
179 }
180 permission.setPermission(permissions, getPath());
181 }
182
183 @Override
184 public int compareTo(AttachmentImpl o) {
185 return getName().toLowerCase().compareTo(o.getName().toLowerCase());
186 }
187
188 @OneToOne(type = RelationshipType.EMBEDDED)
189 @Owner
190 public abstract VersionableMixin getVersionableMixinByChromattic();
191 protected abstract void setVersionableMixinByChromattic(VersionableMixin mix);
192 @Create
193 protected abstract VersionableMixin createVersionableMixin();
194
195 public VersionableMixin getVersionableMixin() {
196 VersionableMixin versionableMixin = getVersionableMixinByChromattic();
197 if (versionableMixin == null) {
198 versionableMixin = createVersionableMixin();
199 setVersionableMixinByChromattic(versionableMixin);
200 }
201 return versionableMixin;
202 }
203
204 public void makeVersionable() {
205 getVersionableMixin();
206 }
207
208
209 public NTVersion checkin() throws RepositoryException {
210 getChromatticSession().save();
211 Node pageNode = getJCRNode();
212 Version newVersion = pageNode.checkin();
213 NTVersion ntVersion = getChromatticSession().findByNode(NTVersion.class, newVersion);
214 return ntVersion;
215 }
216
217
218 public void checkout() throws RepositoryException {
219 Node pageNode = getJCRNode();
220 pageNode.checkout();
221 }
222
223
224 public void restore(String versionName, boolean removeExisting) throws WikiException {
225 try {
226 Node attNode = getJCRNode();
227 attNode.restore(versionName, removeExisting);
228 attNode.checkout();
229 } catch (RepositoryException e) {
230 throw new WikiException("Cannot restore version " + versionName + " of page " + this.getName(), e);
231 }
232 }
233
234 public ChromatticSession getChromatticSession() {
235 return org.exoplatform.wiki.rendering.util.Utils.getService(MOWService.class).getSession();
236 }
237
238 public Node getJCRNode() throws RepositoryException {
239 return (Node) getChromatticSession().getJCRSession().getItem(getPath());
240 }
241
242 @OneToOne(type = RelationshipType.EMBEDDED)
243 @Owner
244 public abstract PageDescriptionMixin getPageDescriptionMixinByChromattic();
245 protected abstract void setPageDescriptionMixinByChromattic(PageDescriptionMixin mix);
246 @Create
247 protected abstract PageDescriptionMixin createPageDescriptionMixin();
248
249 public PageDescriptionMixin getPageDescriptionMixin() {
250 PageDescriptionMixin pageDescriptionMixin = getPageDescriptionMixinByChromattic();
251 if (pageDescriptionMixin == null) {
252 pageDescriptionMixin = createPageDescriptionMixin();
253 setPageDescriptionMixinByChromattic(pageDescriptionMixin);
254 }
255 return pageDescriptionMixin;
256 }
257
258 }