/root/modules/php-sdk/source/java/org/alfresco/module/phpIntegration/lib/ContentData.java

https://github.com/alfresco-mirror/alfresco-mirror · Java · 325 lines · 174 code · 38 blank · 113 comment · 25 complexity · 0630f764ef3e505b30e8665561804f70 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2010 Alfresco Software Limited.
  3. *
  4. * This file is part of Alfresco
  5. *
  6. * Alfresco is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Alfresco is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.alfresco.module.phpIntegration.lib;
  20. import org.alfresco.model.ContentModel;
  21. import org.alfresco.module.phpIntegration.PHPProcessorException;
  22. import org.alfresco.service.cmr.repository.ContentReader;
  23. import org.alfresco.service.cmr.repository.ContentService;
  24. import org.alfresco.service.cmr.repository.ContentWriter;
  25. import org.alfresco.service.namespace.QName;
  26. import org.alfresco.web.app.servlet.DownloadContentServlet;
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. /**
  30. * Content data implementation
  31. *
  32. * @author Roy Wetherall
  33. */
  34. public class ContentData implements ScriptObject
  35. {
  36. /** Logger **/
  37. private static Log logger = LogFactory.getLog(ContentData.class);
  38. /** The name of the script extension */
  39. private static final String SCRIPT_OBJECT_NAME = "ContentData";
  40. /** Node that this content resides on */
  41. private Node node;
  42. /** The content property */
  43. private String property;
  44. /** The mimetype */
  45. private String mimetype;
  46. /** The encoding */
  47. private String encoding;
  48. /** The content size */
  49. private long size;
  50. /** Content service */
  51. private ContentService contentService;
  52. /** Indicates that the content data is dirty and there are unsaved changes */
  53. private boolean isDirty;
  54. /** Updated content string and location awaiting update */
  55. private String updatedContentString;
  56. private String updatedContentLocation;
  57. /**
  58. * Constructor
  59. *
  60. * @param node the node
  61. * @param property the content property
  62. * @param mimetype the mimetype
  63. * @param encoding the encoding
  64. */
  65. public ContentData(Node node, String property, String mimetype, String encoding)
  66. {
  67. this.node = node;
  68. this.property = property;
  69. this.mimetype = mimetype;
  70. this.encoding = encoding;
  71. this.isDirty = false;
  72. this.contentService = this.node.getSession().getServiceRegistry().getContentService();
  73. }
  74. /**
  75. * Constructor
  76. *
  77. * @param node the node
  78. * @param property the content property
  79. * @param mimetype the mimetype
  80. * @param encoding the encoding
  81. * @param size the size
  82. */
  83. public ContentData(Node node, String property, String mimetype, String encoding, long size)
  84. {
  85. this.node = node;
  86. this.property = property;
  87. this.mimetype = mimetype;
  88. this.encoding = encoding;
  89. this.size = size;
  90. this.isDirty = false;
  91. this.contentService = this.node.getSession().getServiceRegistry().getContentService();
  92. }
  93. /**
  94. * @see org.alfresco.module.phpIntegration.lib.ScriptObject#getScriptObjectName()
  95. */
  96. public String getScriptObjectName()
  97. {
  98. return SCRIPT_OBJECT_NAME;
  99. }
  100. /**
  101. * Get the mimetype
  102. *
  103. * @return the mimetype
  104. */
  105. public String getMimetype()
  106. {
  107. return this.mimetype;
  108. }
  109. /**
  110. * Set the mimetype
  111. *
  112. * @param mimetype the mimetype
  113. */
  114. public void setMimetype(String mimetype)
  115. {
  116. this.mimetype = mimetype;
  117. this.isDirty = true;
  118. this.node.contentUpdated();
  119. }
  120. /**
  121. * Get the encoding
  122. *
  123. * @return the encoding
  124. */
  125. public String getEncoding()
  126. {
  127. return encoding;
  128. }
  129. /**
  130. * Set the encoding
  131. *
  132. * @param encoding the encoding
  133. */
  134. public void setEncoding(String encoding)
  135. {
  136. this.encoding = encoding;
  137. this.isDirty = true;
  138. this.node.contentUpdated();
  139. }
  140. /**
  141. * Get the size of the content
  142. *
  143. * @return the size of the content
  144. */
  145. public long getSize()
  146. {
  147. return this.size;
  148. }
  149. /**
  150. * Gets the download url for the content. This URL has the ticket already appended for the containing session.
  151. *
  152. * @return String the download URL for the content
  153. */
  154. public String getUrl()
  155. {
  156. return this.node.session.doSessionWork(new SessionWork<String>()
  157. {
  158. public String doWork()
  159. {
  160. // TODO this will do for now ...
  161. String url = DownloadContentServlet.generateBrowserURL(
  162. ContentData.this.node.getNodeRef(),
  163. (String)ContentData.this.node.getSession().getServiceRegistry().getNodeService().getProperty(ContentData.this.node.getNodeRef(), ContentModel.PROP_NAME));
  164. return "/alfresco" + url+"?ticket=" + ContentData.this.node.getSession().getTicket();
  165. }
  166. });
  167. }
  168. /**
  169. * Gets the guest download URL for the content. This URL can only be used to access the repo as a guest.
  170. *
  171. * @return String the guest download URL for the content.
  172. */
  173. public String getGuestUrl()
  174. {
  175. return this.node.session.doSessionWork(new SessionWork<String>()
  176. {
  177. public String doWork()
  178. {
  179. // TODO this will do for now ...
  180. String url = DownloadContentServlet.generateBrowserURL(
  181. ContentData.this.node.getNodeRef(),
  182. (String)ContentData.this.node.getSession().getServiceRegistry().getNodeService().getProperty(ContentData.this.node.getNodeRef(), ContentModel.PROP_NAME));
  183. return "/alfresco" + url + "?guest=true";
  184. }
  185. });
  186. }
  187. /**
  188. * Get the content string
  189. *
  190. * @return the content string
  191. */
  192. public String getContent()
  193. {
  194. String content = null;
  195. if (this.updatedContentString != null)
  196. {
  197. content = this.updatedContentString;
  198. }
  199. else if (this.updatedContentLocation != null)
  200. {
  201. // TODO .. get the content out of the file
  202. }
  203. else if (this.node.isNewNode() == false)
  204. {
  205. content = this.node.session.doSessionWork(new SessionWork<String>()
  206. {
  207. public String doWork()
  208. {
  209. String content = null;
  210. ContentReader contentReader = ContentData.this.contentService.getReader(ContentData.this.node.getNodeRef(), QName.createQName(ContentData.this.property));
  211. if (contentReader != null)
  212. {
  213. content = contentReader.getContentString();
  214. }
  215. return content;
  216. }
  217. });
  218. }
  219. return content;
  220. }
  221. /**
  222. * Set the content string
  223. *
  224. * @param content the content string
  225. */
  226. public void setContent(String content)
  227. {
  228. // Set the content string
  229. this.updatedContentString = content;
  230. this.size = content.length();
  231. this.isDirty = true;
  232. this.node.contentUpdated();
  233. }
  234. public void writeContentFromFile(String file)
  235. {
  236. // TODO
  237. }
  238. public void readContentToFile(String file)
  239. {
  240. // TODO
  241. }
  242. /**
  243. * Saves any changes made to the content data
  244. *
  245. */
  246. /*package*/ void onSave()
  247. {
  248. if (this.isDirty == true)
  249. {
  250. if (logger.isDebugEnabled() == true)
  251. {
  252. logger.debug("ContentData.onSave() - Getting content writer (node=" + this.node.getId() + "; property=" + this.property);
  253. }
  254. // Get the content writer
  255. ContentWriter contentWriter = this.contentService.getWriter(this.node.getNodeRef(), QName.createQName(this.property), true);
  256. if (contentWriter == null)
  257. {
  258. throw new PHPProcessorException("Unable to get content writer for property " + this.property + " on node " + this.node.toString());
  259. }
  260. if (logger.isDebugEnabled() == true)
  261. {
  262. logger.debug("ContentData.onSave() - Setting encoding and mimetype (node=" + this.node.getId() + "; property=" + this.property);
  263. }
  264. // Set the encoding and mimetype
  265. contentWriter.setEncoding(this.encoding);
  266. contentWriter.setMimetype(this.mimetype);
  267. // Put the updated content, id it had been updated
  268. if (this.updatedContentString != null)
  269. {
  270. if (logger.isDebugEnabled() == true)
  271. {
  272. logger.debug("ContentData.onSave() - Putting text content (node=" + this.node.getId() + "; property=" + this.property);
  273. logger.debug("ContentData.onSave() - updatedContentString=" + this.updatedContentString);
  274. }
  275. contentWriter.putContent(this.updatedContentString);
  276. }
  277. else if (this.updatedContentLocation != null)
  278. {
  279. // TODO .. handle loading content from the file location ....
  280. }
  281. // Clear the content date since the content has now been updated
  282. this.updatedContentLocation = null;
  283. this.updatedContentString = null;
  284. this.isDirty = false;
  285. }
  286. }
  287. }