001
014
015 package com.liferay.portlet.documentlibrary.sharepoint;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.repository.model.FileEntry;
020 import com.liferay.portal.kernel.repository.model.Folder;
021 import com.liferay.portal.kernel.servlet.HttpHeaders;
022 import com.liferay.portal.kernel.util.ContentTypes;
023 import com.liferay.portal.kernel.util.FileUtil;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.HttpUtil;
026 import com.liferay.portal.kernel.util.MimeTypesUtil;
027 import com.liferay.portal.kernel.util.StringPool;
028 import com.liferay.portal.kernel.xml.Element;
029 import com.liferay.portal.service.ServiceContext;
030 import com.liferay.portal.sharepoint.BaseSharepointStorageImpl;
031 import com.liferay.portal.sharepoint.SharepointRequest;
032 import com.liferay.portal.sharepoint.SharepointUtil;
033 import com.liferay.portal.sharepoint.Tree;
034 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
035 import com.liferay.portlet.documentlibrary.exception.NoSuchFileEntryException;
036 import com.liferay.portlet.documentlibrary.exception.NoSuchFolderException;
037 import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
038 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
039 import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
040
041 import java.io.File;
042 import java.io.InputStream;
043
044 import java.util.List;
045
046 import javax.servlet.http.HttpServletRequest;
047
048
051 public class DLSharepointStorageImpl extends BaseSharepointStorageImpl {
052
053 @Override
054 public void addDocumentElements(
055 SharepointRequest sharepointRequest, Element element)
056 throws Exception {
057
058 String parentFolderPath = sharepointRequest.getRootPath();
059
060 long groupId = SharepointUtil.getGroupId(parentFolderPath);
061 long parentFolderId = getLastFolderId(
062 groupId, parentFolderPath,
063 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
064
065 if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
066 return;
067 }
068
069 List<FileEntry> fileEntries = DLAppServiceUtil.getFileEntries(
070 groupId, parentFolderId);
071
072 for (FileEntry fileEntry : fileEntries) {
073 String documentPath = parentFolderPath.concat(
074 StringPool.SLASH).concat(fileEntry.getTitle());
075
076 addDocumentElement(
077 element, documentPath, fileEntry.getCreateDate(),
078 fileEntry.getModifiedDate(), fileEntry.getUserName());
079 }
080 }
081
082 @Override
083 public void createFolder(SharepointRequest sharepointRequest)
084 throws Exception {
085
086 String folderPath = sharepointRequest.getRootPath();
087 String parentFolderPath = getParentFolderPath(folderPath);
088
089 long groupId = SharepointUtil.getGroupId(parentFolderPath);
090 long parentFolderId = getLastFolderId(
091 groupId, parentFolderPath,
092 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
093 String folderName = getResourceName(folderPath);
094 String description = StringPool.BLANK;
095
096 ServiceContext serviceContext = new ServiceContext();
097
098 serviceContext.setAddGroupPermissions(true);
099 serviceContext.setAddGuestPermissions(true);
100
101 DLAppServiceUtil.addFolder(
102 groupId, parentFolderId, folderName, description, serviceContext);
103 }
104
105 @Override
106 public InputStream getDocumentInputStream(
107 SharepointRequest sharepointRequest)
108 throws Exception {
109
110 FileEntry fileEntry = getFileEntry(sharepointRequest);
111
112 return fileEntry.getContentStream();
113 }
114
115 @Override
116 public Tree getDocumentsTree(SharepointRequest sharepointRequest)
117 throws Exception {
118
119 Tree documentsTree = new Tree();
120
121 String parentFolderPath = sharepointRequest.getRootPath();
122
123 long groupId = SharepointUtil.getGroupId(parentFolderPath);
124 long parentFolderId = getLastFolderId(
125 groupId, parentFolderPath,
126 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
127
128 List<FileEntry> fileEntries = DLAppServiceUtil.getFileEntries(
129 groupId, parentFolderId);
130
131 for (FileEntry fileEntry : fileEntries) {
132 documentsTree.addChild(
133 getFileEntryTree(fileEntry, parentFolderPath));
134 }
135
136 return documentsTree;
137 }
138
139 @Override
140 public Tree getDocumentTree(SharepointRequest sharepointRequest)
141 throws Exception {
142
143 String documentPath = sharepointRequest.getRootPath();
144 String parentFolderPath = getParentFolderPath(documentPath);
145
146 FileEntry fileEntry = getFileEntry(sharepointRequest);
147
148 return getFileEntryTree(fileEntry, parentFolderPath);
149 }
150
151 @Override
152 public Tree getFoldersTree(SharepointRequest sharepointRequest)
153 throws Exception {
154
155 Tree foldersTree = new Tree();
156
157 String parentFolderPath = sharepointRequest.getRootPath();
158
159 long groupId = SharepointUtil.getGroupId(parentFolderPath);
160 long parentFolderId = getLastFolderId(
161 groupId, parentFolderPath,
162 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
163
164 List<Folder> folders = DLAppServiceUtil.getFolders(
165 groupId, parentFolderId, false);
166
167 for (Folder folder : folders) {
168 foldersTree.addChild(getFolderTree(folder, parentFolderPath));
169 }
170
171 foldersTree.addChild(getFolderTree(parentFolderPath));
172
173 return foldersTree;
174 }
175
176 @Override
177 public Tree getFolderTree(SharepointRequest sharepointRequest)
178 throws Exception {
179
180 String folderPath = sharepointRequest.getRootPath();
181 String parentFolderPath = getParentFolderPath(folderPath);
182
183 long groupId = SharepointUtil.getGroupId(folderPath);
184 long folderId = getLastFolderId(
185 groupId, folderPath, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
186
187 Folder folder = DLAppServiceUtil.getFolder(folderId);
188
189 return getFolderTree(folder, parentFolderPath);
190 }
191
192 @Override
193 public void getParentFolderIds(
194 long groupId, String path, List<Long> folderIds)
195 throws Exception {
196
197 String[] pathArray = SharepointUtil.getPathArray(path);
198
199 if (pathArray.length == 0) {
200 return;
201 }
202
203 long parentFolderId = folderIds.get(folderIds.size() - 1);
204 Folder folder = DLAppServiceUtil.getFolder(
205 groupId, parentFolderId, HttpUtil.decodePath(pathArray[0]));
206
207 folderIds.add(folder.getFolderId());
208
209 if (pathArray.length > 1) {
210 path = removeFoldersFromPath(path, 1);
211
212 getParentFolderIds(groupId, path, folderIds);
213 }
214 }
215
216 @Override
217 public Tree[] moveDocument(SharepointRequest sharepointRequest)
218 throws Exception {
219
220 String parentFolderPath = sharepointRequest.getRootPath();
221
222 long groupId = SharepointUtil.getGroupId(parentFolderPath);
223
224 Folder folder = null;
225 FileEntry fileEntry = null;
226
227 try {
228 long parentFolderId = getLastFolderId(
229 groupId, parentFolderPath,
230 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
231
232 folder = DLAppServiceUtil.getFolder(parentFolderId);
233 }
234 catch (Exception e1) {
235 if (e1 instanceof NoSuchFolderException) {
236 try {
237 fileEntry = getFileEntry(sharepointRequest);
238 }
239 catch (Exception e2) {
240 }
241 }
242 }
243
244 Tree movedDocsTree = new Tree();
245 Tree movedDirsTree = new Tree();
246
247 String newPath = sharepointRequest.getParameterValue("newUrl");
248 String newParentFolderPath = getParentFolderPath(newPath);
249
250 long newGroupId = SharepointUtil.getGroupId(newParentFolderPath);
251
252 long newParentFolderId = getLastFolderId(
253 newGroupId, newParentFolderPath,
254 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
255
256 String newName = getResourceName(newPath);
257
258 ServiceContext serviceContext = new ServiceContext();
259
260 if (fileEntry != null) {
261 File file = null;
262
263 try {
264 long fileEntryId = fileEntry.getFileEntryId();
265
266 long folderId = fileEntry.getFolderId();
267 String mimeType = fileEntry.getMimeType();
268 String description = fileEntry.getDescription();
269 String changeLog = StringPool.BLANK;
270
271 InputStream is = fileEntry.getContentStream();
272
273 file = FileUtil.createTempFile(is);
274
275 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
276 DLFileEntryConstants.getClassName(),
277 fileEntry.getFileEntryId());
278
279 serviceContext.setAssetTagNames(assetTagNames);
280
281 fileEntry = DLAppServiceUtil.updateFileEntry(
282 fileEntryId, newName, mimeType, newName, description,
283 changeLog, false, file, serviceContext);
284
285 if (folderId != newParentFolderId) {
286 fileEntry = DLAppServiceUtil.moveFileEntry(
287 fileEntryId, newParentFolderId, serviceContext);
288 }
289
290 Tree documentTree = getFileEntryTree(
291 fileEntry, newParentFolderPath);
292
293 movedDocsTree.addChild(documentTree);
294 }
295 finally {
296 FileUtil.delete(file);
297 }
298 }
299 else if (folder != null) {
300 long folderId = folder.getFolderId();
301
302 String description = folder.getDescription();
303
304 if (newParentFolderId != folder.getParentFolderId()) {
305 folder = DLAppServiceUtil.moveFolder(
306 folderId, newParentFolderId, serviceContext);
307 }
308
309 if (!newName.equals(folder.getName())) {
310 DLAppServiceUtil.updateFolder(
311 folderId, newName, description, serviceContext);
312 }
313
314 Tree folderTree = getFolderTree(folder, newParentFolderPath);
315
316 movedDirsTree.addChild(folderTree);
317 }
318
319 return new Tree[] {movedDocsTree, movedDirsTree};
320 }
321
322 @Override
323 public void putDocument(SharepointRequest sharepointRequest)
324 throws Exception {
325
326 HttpServletRequest request = sharepointRequest.getHttpServletRequest();
327
328 String documentPath = sharepointRequest.getRootPath();
329 String parentFolderPath = getParentFolderPath(documentPath);
330
331 long groupId = SharepointUtil.getGroupId(parentFolderPath);
332 long parentFolderId = getLastFolderId(
333 groupId, parentFolderPath,
334 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
335 String title = getResourceName(documentPath);
336 String description = StringPool.BLANK;
337 String changeLog = StringPool.BLANK;
338
339 ServiceContext serviceContext = new ServiceContext();
340
341 serviceContext.setAddGroupPermissions(true);
342 serviceContext.setAddGuestPermissions(true);
343
344 String contentType = GetterUtil.get(
345 request.getHeader(HttpHeaders.CONTENT_TYPE),
346 ContentTypes.APPLICATION_OCTET_STREAM);
347
348 String extension = FileUtil.getExtension(title);
349
350 File file = null;
351
352 try {
353 file = FileUtil.createTempFile(extension);
354
355 FileUtil.write(file, sharepointRequest.getBytes());
356
357 if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
358 contentType = MimeTypesUtil.getContentType(file, title);
359 }
360
361 try {
362 FileEntry fileEntry = getFileEntry(sharepointRequest);
363
364 long fileEntryId = fileEntry.getFileEntryId();
365
366 description = fileEntry.getDescription();
367
368 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
369 DLFileEntryConstants.getClassName(),
370 fileEntry.getFileEntryId());
371
372 serviceContext.setAssetTagNames(assetTagNames);
373
374 DLAppServiceUtil.updateFileEntry(
375 fileEntryId, title, contentType, title, description,
376 changeLog, false, file, serviceContext);
377 }
378 catch (NoSuchFileEntryException nsfee) {
379 if (_log.isDebugEnabled()) {
380 _log.debug(nsfee, nsfee);
381 }
382
383 DLAppServiceUtil.addFileEntry(
384 groupId, parentFolderId, title, contentType, title,
385 description, changeLog, file, serviceContext);
386 }
387 }
388 finally {
389 FileUtil.delete(file);
390 }
391 }
392
393 @Override
394 public Tree[] removeDocument(SharepointRequest sharepointRequest) {
395 String parentFolderPath = sharepointRequest.getRootPath();
396
397 long groupId = SharepointUtil.getGroupId(parentFolderPath);
398
399 Folder folder = null;
400 FileEntry fileEntry = null;
401
402 try {
403 long parentFolderId = getLastFolderId(
404 groupId, parentFolderPath,
405 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
406
407 folder = DLAppServiceUtil.getFolder(parentFolderId);
408 }
409 catch (Exception e1) {
410 if (e1 instanceof NoSuchFolderException) {
411 try {
412 fileEntry = getFileEntry(sharepointRequest);
413 }
414 catch (Exception e2) {
415 }
416 }
417 }
418
419 Tree documentTree = new Tree();
420
421 Tree removedDocsTree = new Tree();
422 Tree failedDocsTree = new Tree();
423
424 Tree folderTree = new Tree();
425
426 Tree removedDirsTree = new Tree();
427 Tree failedDirsTree = new Tree();
428
429 if (fileEntry != null) {
430 try {
431 documentTree = getFileEntryTree(fileEntry, parentFolderPath);
432
433 DLAppServiceUtil.deleteFileEntry(fileEntry.getFileEntryId());
434
435 removedDocsTree.addChild(documentTree);
436 }
437 catch (Exception e1) {
438 try {
439 failedDocsTree.addChild(documentTree);
440 }
441 catch (Exception e2) {
442 }
443 }
444 }
445 else if (folder != null) {
446 try {
447 folderTree = getFolderTree(folder, parentFolderPath);
448
449 DLAppServiceUtil.deleteFolder(folder.getFolderId());
450
451 removedDirsTree.addChild(folderTree);
452 }
453 catch (Exception e1) {
454 try {
455 failedDirsTree.addChild(folderTree);
456 }
457 catch (Exception e2) {
458 }
459 }
460 }
461
462 return new Tree[] {
463 removedDocsTree, removedDirsTree, failedDocsTree, failedDirsTree
464 };
465 }
466
467 protected FileEntry getFileEntry(SharepointRequest sharepointRequest)
468 throws Exception {
469
470 String documentPath = sharepointRequest.getRootPath();
471 String parentFolderPath = getParentFolderPath(documentPath);
472
473 long groupId = SharepointUtil.getGroupId(parentFolderPath);
474 long parentFolderId = getLastFolderId(
475 groupId, parentFolderPath,
476 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
477 String title = getResourceName(documentPath);
478
479 return DLAppServiceUtil.getFileEntry(groupId, parentFolderId, title);
480 }
481
482 protected Tree getFileEntryTree(
483 FileEntry fileEntry, String parentFolderPath) {
484
485 String documentPath = parentFolderPath.concat(StringPool.SLASH).concat(
486 fileEntry.getTitle());
487
488 return getDocumentTree(
489 documentPath, fileEntry.getCreateDate(),
490 fileEntry.getModifiedDate(), fileEntry.getSize(),
491 fileEntry.getUserName(), fileEntry.getVersion());
492 }
493
494 protected Tree getFolderTree(Folder folder, String parentFolderPath) {
495 String folderPath = parentFolderPath.concat(StringPool.SLASH).concat(
496 folder.getName());
497
498 return getFolderTree(
499 folderPath, folder.getCreateDate(), folder.getModifiedDate(),
500 folder.getLastPostDate());
501 }
502
503 private static final Log _log = LogFactoryUtil.getLog(
504 DLSharepointStorageImpl.class);
505
506 }