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 getDocumentTree(SharepointRequest sharepointRequest)
113 throws Exception {
114
115 String documentPath = sharepointRequest.getRootPath();
116 String parentFolderPath = getParentFolderPath(documentPath);
117
118 FileEntry fileEntry = getFileEntry(sharepointRequest);
119
120 return getFileEntryTree(fileEntry, parentFolderPath);
121 }
122
123 @Override
124 public Tree getDocumentsTree(SharepointRequest sharepointRequest)
125 throws Exception {
126
127 Tree documentsTree = new Tree();
128
129 String parentFolderPath = sharepointRequest.getRootPath();
130
131 long groupId = SharepointUtil.getGroupId(parentFolderPath);
132 long parentFolderId = getLastFolderId(
133 groupId, parentFolderPath,
134 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
135
136 if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
137 List<FileEntry> fileEntries = DLAppServiceUtil.getFileEntries(
138 groupId, parentFolderId);
139
140 for (FileEntry fileEntry : fileEntries) {
141 documentsTree.addChild(
142 getFileEntryTree(fileEntry, parentFolderPath));
143 }
144 }
145
146 return documentsTree;
147 }
148
149 @Override
150 public Tree getFolderTree(SharepointRequest sharepointRequest)
151 throws Exception {
152
153 String folderPath = sharepointRequest.getRootPath();
154 String parentFolderPath = getParentFolderPath(folderPath);
155
156 long groupId = SharepointUtil.getGroupId(folderPath);
157 long folderId = getLastFolderId(
158 groupId, folderPath, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
159
160 Folder folder = DLAppServiceUtil.getFolder(folderId);
161
162 return getFolderTree(folder, parentFolderPath);
163 }
164
165 @Override
166 public Tree getFoldersTree(SharepointRequest sharepointRequest)
167 throws Exception {
168
169 Tree foldersTree = new Tree();
170
171 String parentFolderPath = sharepointRequest.getRootPath();
172
173 long groupId = SharepointUtil.getGroupId(parentFolderPath);
174 long parentFolderId = getLastFolderId(
175 groupId, parentFolderPath,
176 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
177
178 List<Folder> folders = DLAppServiceUtil.getFolders(
179 groupId, parentFolderId);
180
181 for (Folder folder : folders) {
182 foldersTree.addChild(getFolderTree(folder, parentFolderPath));
183 }
184
185 foldersTree.addChild(getFolderTree(parentFolderPath));
186
187 return foldersTree;
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, request.getInputStream());
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 Tree getFolderTree(Folder folder, String parentFolderPath) {
450 String folderPath = parentFolderPath.concat(StringPool.SLASH).concat(
451 folder.getName());
452
453 return getFolderTree(
454 folderPath, folder.getCreateDate(), folder.getModifiedDate(),
455 folder.getLastPostDate());
456 }
457
458 protected FileEntry getFileEntry(SharepointRequest sharepointRequest)
459 throws Exception {
460
461 String documentPath = sharepointRequest.getRootPath();
462 String parentFolderPath = getParentFolderPath(documentPath);
463
464 long groupId = SharepointUtil.getGroupId(parentFolderPath);
465 long parentFolderId = getLastFolderId(
466 groupId, parentFolderPath,
467 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
468 String title = getResourceName(documentPath);
469
470 return DLAppServiceUtil.getFileEntry(groupId, parentFolderId, title);
471 }
472
473 protected Tree getFileEntryTree(
474 FileEntry fileEntry, String parentFolderPath) {
475
476 String documentPath = parentFolderPath.concat(StringPool.SLASH).concat(
477 fileEntry.getTitle());
478
479 return getDocumentTree(
480 documentPath, fileEntry.getCreateDate(),
481 fileEntry.getModifiedDate(), fileEntry.getSize(),
482 fileEntry.getUserName(), fileEntry.getVersion());
483 }
484
485 }