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