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.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        // Build REST url to view image
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   //TODO: replace by @Checkin when Chromattic support
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   //TODO: replace by @Checkout when Chromattic support
218   public void checkout() throws RepositoryException {
219     Node pageNode = getJCRNode();
220     pageNode.checkout();
221   }
222   
223   //TODO: replace by @Restore when Chromattic support
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 }