PageRenderTime 81ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/portal-impl/src/com/liferay/portlet/documentlibrary/sharepoint/DLSharepointStorageImpl.java

https://github.com/lululiferay/liferay-portal
Java | 496 lines | 355 code | 125 blank | 16 comment | 26 complexity | 5f756699dfa7e13f8b0d217dff6c3eca MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portlet.documentlibrary.sharepoint;
  15. import com.liferay.portal.kernel.repository.model.FileEntry;
  16. import com.liferay.portal.kernel.repository.model.Folder;
  17. import com.liferay.portal.kernel.servlet.HttpHeaders;
  18. import com.liferay.portal.kernel.util.ContentTypes;
  19. import com.liferay.portal.kernel.util.FileUtil;
  20. import com.liferay.portal.kernel.util.GetterUtil;
  21. import com.liferay.portal.kernel.util.HttpUtil;
  22. import com.liferay.portal.kernel.util.MimeTypesUtil;
  23. import com.liferay.portal.kernel.util.StringPool;
  24. import com.liferay.portal.kernel.xml.Element;
  25. import com.liferay.portal.service.ServiceContext;
  26. import com.liferay.portal.sharepoint.BaseSharepointStorageImpl;
  27. import com.liferay.portal.sharepoint.SharepointRequest;
  28. import com.liferay.portal.sharepoint.SharepointUtil;
  29. import com.liferay.portal.sharepoint.Tree;
  30. import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
  31. import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
  32. import com.liferay.portlet.documentlibrary.NoSuchFolderException;
  33. import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
  34. import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
  35. import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
  36. import java.io.File;
  37. import java.io.InputStream;
  38. import java.util.List;
  39. import javax.servlet.http.HttpServletRequest;
  40. /**
  41. * @author Bruno Farache
  42. */
  43. public class DLSharepointStorageImpl extends BaseSharepointStorageImpl {
  44. @Override
  45. public void addDocumentElements(
  46. SharepointRequest sharepointRequest, Element element)
  47. throws Exception {
  48. String parentFolderPath = sharepointRequest.getRootPath();
  49. long groupId = SharepointUtil.getGroupId(parentFolderPath);
  50. long parentFolderId = getLastFolderId(
  51. groupId, parentFolderPath,
  52. DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
  53. if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
  54. return;
  55. }
  56. List<FileEntry> fileEntries = DLAppServiceUtil.getFileEntries(
  57. groupId, parentFolderId);
  58. for (FileEntry fileEntry : fileEntries) {
  59. String documentPath = parentFolderPath.concat(
  60. StringPool.SLASH).concat(fileEntry.getTitle());
  61. addDocumentElement(
  62. element, documentPath, fileEntry.getCreateDate(),
  63. fileEntry.getModifiedDate(), fileEntry.getUserName());
  64. }
  65. }
  66. @Override
  67. public void createFolder(SharepointRequest sharepointRequest)
  68. throws Exception {
  69. String folderPath = sharepointRequest.getRootPath();
  70. String parentFolderPath = getParentFolderPath(folderPath);
  71. long groupId = SharepointUtil.getGroupId(parentFolderPath);
  72. long parentFolderId = getLastFolderId(
  73. groupId, parentFolderPath,
  74. DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
  75. String folderName = getResourceName(folderPath);
  76. String description = StringPool.BLANK;
  77. ServiceContext serviceContext = new ServiceContext();
  78. serviceContext.setAddGroupPermissions(true);
  79. serviceContext.setAddGuestPermissions(true);
  80. DLAppServiceUtil.addFolder(
  81. groupId, parentFolderId, folderName, description, serviceContext);
  82. }
  83. @Override
  84. public InputStream getDocumentInputStream(
  85. SharepointRequest sharepointRequest)
  86. throws Exception {
  87. FileEntry fileEntry = getFileEntry(sharepointRequest);
  88. return fileEntry.getContentStream();
  89. }
  90. @Override
  91. public Tree getDocumentsTree(SharepointRequest sharepointRequest)
  92. throws Exception {
  93. Tree documentsTree = new Tree();
  94. String parentFolderPath = sharepointRequest.getRootPath();
  95. long groupId = SharepointUtil.getGroupId(parentFolderPath);
  96. long parentFolderId = getLastFolderId(
  97. groupId, parentFolderPath,
  98. DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
  99. List<FileEntry> fileEntries = DLAppServiceUtil.getFileEntries(
  100. groupId, parentFolderId);
  101. for (FileEntry fileEntry : fileEntries) {
  102. documentsTree.addChild(
  103. getFileEntryTree(fileEntry, parentFolderPath));
  104. }
  105. return documentsTree;
  106. }
  107. @Override
  108. public Tree getDocumentTree(SharepointRequest sharepointRequest)
  109. throws Exception {
  110. String documentPath = sharepointRequest.getRootPath();
  111. String parentFolderPath = getParentFolderPath(documentPath);
  112. FileEntry fileEntry = getFileEntry(sharepointRequest);
  113. return getFileEntryTree(fileEntry, parentFolderPath);
  114. }
  115. @Override
  116. public Tree getFoldersTree(SharepointRequest sharepointRequest)
  117. throws Exception {
  118. Tree foldersTree = new Tree();
  119. String parentFolderPath = sharepointRequest.getRootPath();
  120. long groupId = SharepointUtil.getGroupId(parentFolderPath);
  121. long parentFolderId = getLastFolderId(
  122. groupId, parentFolderPath,
  123. DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
  124. List<Folder> folders = DLAppServiceUtil.getFolders(
  125. groupId, parentFolderId, false);
  126. for (Folder folder : folders) {
  127. foldersTree.addChild(getFolderTree(folder, parentFolderPath));
  128. }
  129. foldersTree.addChild(getFolderTree(parentFolderPath));
  130. return foldersTree;
  131. }
  132. @Override
  133. public Tree getFolderTree(SharepointRequest sharepointRequest)
  134. throws Exception {
  135. String folderPath = sharepointRequest.getRootPath();
  136. String parentFolderPath = getParentFolderPath(folderPath);
  137. long groupId = SharepointUtil.getGroupId(folderPath);
  138. long folderId = getLastFolderId(
  139. groupId, folderPath, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
  140. Folder folder = DLAppServiceUtil.getFolder(folderId);
  141. return getFolderTree(folder, parentFolderPath);
  142. }
  143. @Override
  144. public void getParentFolderIds(
  145. long groupId, String path, List<Long> folderIds)
  146. throws Exception {
  147. String[] pathArray = SharepointUtil.getPathArray(path);
  148. if (pathArray.length == 0) {
  149. return;
  150. }
  151. long parentFolderId = folderIds.get(folderIds.size() - 1);
  152. Folder folder = DLAppServiceUtil.getFolder(
  153. groupId, parentFolderId, HttpUtil.decodePath(pathArray[0]));
  154. folderIds.add(folder.getFolderId());
  155. if (pathArray.length > 1) {
  156. path = removeFoldersFromPath(path, 1);
  157. getParentFolderIds(groupId, path, folderIds);
  158. }
  159. }
  160. @Override
  161. public Tree[] moveDocument(SharepointRequest sharepointRequest)
  162. throws Exception {
  163. String parentFolderPath = sharepointRequest.getRootPath();
  164. long groupId = SharepointUtil.getGroupId(parentFolderPath);
  165. Folder folder = null;
  166. FileEntry fileEntry = null;
  167. try {
  168. long parentFolderId = getLastFolderId(
  169. groupId, parentFolderPath,
  170. DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
  171. folder = DLAppServiceUtil.getFolder(parentFolderId);
  172. }
  173. catch (Exception e1) {
  174. if (e1 instanceof NoSuchFolderException) {
  175. try {
  176. fileEntry = getFileEntry(sharepointRequest);
  177. }
  178. catch (Exception e2) {
  179. }
  180. }
  181. }
  182. Tree movedDocsTree = new Tree();
  183. Tree movedDirsTree = new Tree();
  184. String newPath = sharepointRequest.getParameterValue("newUrl");
  185. String newParentFolderPath = getParentFolderPath(newPath);
  186. long newGroupId = SharepointUtil.getGroupId(newParentFolderPath);
  187. long newParentFolderId = getLastFolderId(
  188. newGroupId, newParentFolderPath,
  189. DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
  190. String newName = getResourceName(newPath);
  191. ServiceContext serviceContext = new ServiceContext();
  192. if (fileEntry != null) {
  193. File file = null;
  194. try {
  195. long fileEntryId = fileEntry.getFileEntryId();
  196. long folderId = fileEntry.getFolderId();
  197. String mimeType = fileEntry.getMimeType();
  198. String description = fileEntry.getDescription();
  199. String changeLog = StringPool.BLANK;
  200. InputStream is = fileEntry.getContentStream();
  201. file = FileUtil.createTempFile(is);
  202. String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
  203. DLFileEntryConstants.getClassName(),
  204. fileEntry.getFileEntryId());
  205. serviceContext.setAssetTagNames(assetTagNames);
  206. fileEntry = DLAppServiceUtil.updateFileEntry(
  207. fileEntryId, newName, mimeType, newName, description,
  208. changeLog, false, file, serviceContext);
  209. if (folderId != newParentFolderId) {
  210. fileEntry = DLAppServiceUtil.moveFileEntry(
  211. fileEntryId, newParentFolderId, serviceContext);
  212. }
  213. Tree documentTree = getFileEntryTree(
  214. fileEntry, newParentFolderPath);
  215. movedDocsTree.addChild(documentTree);
  216. }
  217. finally {
  218. FileUtil.delete(file);
  219. }
  220. }
  221. else if (folder != null) {
  222. long folderId = folder.getFolderId();
  223. String description = folder.getDescription();
  224. if (newParentFolderId != folder.getParentFolderId()) {
  225. folder = DLAppServiceUtil.moveFolder(
  226. folderId, newParentFolderId, serviceContext);
  227. }
  228. if (!newName.equals(folder.getName())) {
  229. DLAppServiceUtil.updateFolder(
  230. folderId, newName, description, serviceContext);
  231. }
  232. Tree folderTree = getFolderTree(folder, newParentFolderPath);
  233. movedDirsTree.addChild(folderTree);
  234. }
  235. return new Tree[] {movedDocsTree, movedDirsTree};
  236. }
  237. @Override
  238. public void putDocument(SharepointRequest sharepointRequest)
  239. throws Exception {
  240. HttpServletRequest request = sharepointRequest.getHttpServletRequest();
  241. String documentPath = sharepointRequest.getRootPath();
  242. String parentFolderPath = getParentFolderPath(documentPath);
  243. long groupId = SharepointUtil.getGroupId(parentFolderPath);
  244. long parentFolderId = getLastFolderId(
  245. groupId, parentFolderPath,
  246. DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
  247. String title = getResourceName(documentPath);
  248. String description = StringPool.BLANK;
  249. String changeLog = StringPool.BLANK;
  250. ServiceContext serviceContext = new ServiceContext();
  251. serviceContext.setAddGroupPermissions(true);
  252. serviceContext.setAddGuestPermissions(true);
  253. String contentType = GetterUtil.get(
  254. request.getHeader(HttpHeaders.CONTENT_TYPE),
  255. ContentTypes.APPLICATION_OCTET_STREAM);
  256. String extension = FileUtil.getExtension(title);
  257. File file = null;
  258. try {
  259. file = FileUtil.createTempFile(extension);
  260. FileUtil.write(file, sharepointRequest.getBytes());
  261. if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
  262. contentType = MimeTypesUtil.getContentType(file, title);
  263. }
  264. try {
  265. FileEntry fileEntry = getFileEntry(sharepointRequest);
  266. long fileEntryId = fileEntry.getFileEntryId();
  267. description = fileEntry.getDescription();
  268. String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
  269. DLFileEntryConstants.getClassName(),
  270. fileEntry.getFileEntryId());
  271. serviceContext.setAssetTagNames(assetTagNames);
  272. DLAppServiceUtil.updateFileEntry(
  273. fileEntryId, title, contentType, title, description,
  274. changeLog, false, file, serviceContext);
  275. }
  276. catch (NoSuchFileEntryException nsfee) {
  277. DLAppServiceUtil.addFileEntry(
  278. groupId, parentFolderId, title, contentType, title,
  279. description, changeLog, file, serviceContext);
  280. }
  281. }
  282. finally {
  283. FileUtil.delete(file);
  284. }
  285. }
  286. @Override
  287. public Tree[] removeDocument(SharepointRequest sharepointRequest) {
  288. String parentFolderPath = sharepointRequest.getRootPath();
  289. long groupId = SharepointUtil.getGroupId(parentFolderPath);
  290. Folder folder = null;
  291. FileEntry fileEntry = null;
  292. try {
  293. long parentFolderId = getLastFolderId(
  294. groupId, parentFolderPath,
  295. DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
  296. folder = DLAppServiceUtil.getFolder(parentFolderId);
  297. }
  298. catch (Exception e1) {
  299. if (e1 instanceof NoSuchFolderException) {
  300. try {
  301. fileEntry = getFileEntry(sharepointRequest);
  302. }
  303. catch (Exception e2) {
  304. }
  305. }
  306. }
  307. Tree documentTree = new Tree();
  308. Tree removedDocsTree = new Tree();
  309. Tree failedDocsTree = new Tree();
  310. Tree folderTree = new Tree();
  311. Tree removedDirsTree = new Tree();
  312. Tree failedDirsTree = new Tree();
  313. if (fileEntry != null) {
  314. try {
  315. documentTree = getFileEntryTree(fileEntry, parentFolderPath);
  316. DLAppServiceUtil.deleteFileEntry(fileEntry.getFileEntryId());
  317. removedDocsTree.addChild(documentTree);
  318. }
  319. catch (Exception e1) {
  320. try {
  321. failedDocsTree.addChild(documentTree);
  322. }
  323. catch (Exception e2) {
  324. }
  325. }
  326. }
  327. else if (folder != null) {
  328. try {
  329. folderTree = getFolderTree(folder, parentFolderPath);
  330. DLAppServiceUtil.deleteFolder(folder.getFolderId());
  331. removedDirsTree.addChild(folderTree);
  332. }
  333. catch (Exception e1) {
  334. try {
  335. failedDirsTree.addChild(folderTree);
  336. }
  337. catch (Exception e2) {
  338. }
  339. }
  340. }
  341. return new Tree[] {
  342. removedDocsTree, removedDirsTree, failedDocsTree, failedDirsTree};
  343. }
  344. protected FileEntry getFileEntry(SharepointRequest sharepointRequest)
  345. throws Exception {
  346. String documentPath = sharepointRequest.getRootPath();
  347. String parentFolderPath = getParentFolderPath(documentPath);
  348. long groupId = SharepointUtil.getGroupId(parentFolderPath);
  349. long parentFolderId = getLastFolderId(
  350. groupId, parentFolderPath,
  351. DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
  352. String title = getResourceName(documentPath);
  353. return DLAppServiceUtil.getFileEntry(groupId, parentFolderId, title);
  354. }
  355. protected Tree getFileEntryTree(
  356. FileEntry fileEntry, String parentFolderPath) {
  357. String documentPath = parentFolderPath.concat(StringPool.SLASH).concat(
  358. fileEntry.getTitle());
  359. return getDocumentTree(
  360. documentPath, fileEntry.getCreateDate(),
  361. fileEntry.getModifiedDate(), fileEntry.getSize(),
  362. fileEntry.getUserName(), fileEntry.getVersion());
  363. }
  364. protected Tree getFolderTree(Folder folder, String parentFolderPath) {
  365. String folderPath = parentFolderPath.concat(StringPool.SLASH).concat(
  366. folder.getName());
  367. return getFolderTree(
  368. folderPath, folder.getCreateDate(), folder.getModifiedDate(),
  369. folder.getLastPostDate());
  370. }
  371. }