1
14
15 package com.liferay.portlet.documentlibrary.sharepoint;
16
17 import com.liferay.portal.kernel.util.FileUtil;
18 import com.liferay.portal.kernel.util.StringPool;
19 import com.liferay.portal.kernel.xml.Element;
20 import com.liferay.portal.service.ServiceContext;
21 import com.liferay.portal.sharepoint.BaseSharepointStorageImpl;
22 import com.liferay.portal.sharepoint.SharepointRequest;
23 import com.liferay.portal.sharepoint.SharepointUtil;
24 import com.liferay.portal.sharepoint.Tree;
25 import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
26 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
27 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
28 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
29 import com.liferay.portlet.documentlibrary.model.DLFolder;
30 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
31 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
32 import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
33 import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
34
35 import java.io.File;
36 import java.io.InputStream;
37
38 import java.util.List;
39
40
45 public class DLSharepointStorageImpl extends BaseSharepointStorageImpl {
46
47 public void addDocumentElements(
48 SharepointRequest sharepointRequest, Element element)
49 throws Exception {
50
51 String parentFolderPath = sharepointRequest.getRootPath();
52
53 long groupId = SharepointUtil.getGroupId(parentFolderPath);
54 long parentFolderId = getLastFolderId(
55 groupId, parentFolderPath,
56 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
57
58 if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
59 return;
60 }
61
62 List<DLFileEntry> fileEntries =
63 DLFileEntryLocalServiceUtil.getFileEntries(groupId, parentFolderId);
64
65 for (DLFileEntry fileEntry : fileEntries) {
66 String documentPath = parentFolderPath.concat(
67 StringPool.SLASH).concat(fileEntry.getTitle());
68
69 addDocumentElement(
70 element, documentPath, fileEntry.getCreateDate(),
71 fileEntry.getModifiedDate(), fileEntry.getUserName());
72 }
73 }
74
75 public void createFolder(SharepointRequest sharepointRequest)
76 throws Exception {
77
78 String folderPath = sharepointRequest.getRootPath();
79 String parentFolderPath = getParentFolderPath(folderPath);
80
81 long groupId = SharepointUtil.getGroupId(parentFolderPath);
82 long parentFolderId = getLastFolderId(
83 groupId, parentFolderPath,
84 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
85 String folderName = getResourceName(folderPath);
86 String description = StringPool.BLANK;
87
88 ServiceContext serviceContext = new ServiceContext();
89
90 serviceContext.setAddCommunityPermissions(true);
91 serviceContext.setAddGuestPermissions(true);
92
93 DLFolderServiceUtil.addFolder(
94 groupId, parentFolderId, folderName, description, serviceContext);
95 }
96
97 public InputStream getDocumentInputStream(
98 SharepointRequest sharepointRequest)
99 throws Exception {
100
101 DLFileEntry fileEntry = getFileEntry(sharepointRequest);
102
103 return DLFileEntryLocalServiceUtil.getFileAsStream(
104 sharepointRequest.getCompanyId(), sharepointRequest.getUserId(),
105 fileEntry.getGroupId(), fileEntry.getFolderId(),
106 fileEntry.getName());
107 }
108
109 public Tree getDocumentTree(SharepointRequest sharepointRequest)
110 throws Exception {
111
112 String documentPath = sharepointRequest.getRootPath();
113 String parentFolderPath = getParentFolderPath(documentPath);
114
115 DLFileEntry fileEntry = getFileEntry(sharepointRequest);
116
117 return getFileEntryTree(fileEntry, parentFolderPath);
118 }
119
120 public Tree getDocumentsTree(SharepointRequest sharepointRequest)
121 throws Exception {
122
123 Tree documentsTree = new Tree();
124
125 String parentFolderPath = sharepointRequest.getRootPath();
126
127 long groupId = SharepointUtil.getGroupId(parentFolderPath);
128 long parentFolderId = getLastFolderId(
129 groupId, parentFolderPath,
130 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
131
132 if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
133 List<DLFileEntry> fileEntries =
134 DLFileEntryLocalServiceUtil.getFileEntries(
135 groupId, parentFolderId);
136
137 for (DLFileEntry fileEntry : fileEntries) {
138 documentsTree.addChild(
139 getFileEntryTree(fileEntry, parentFolderPath));
140 }
141 }
142
143 return documentsTree;
144 }
145
146 public Tree getFolderTree(SharepointRequest sharepointRequest)
147 throws Exception {
148
149 String folderPath = sharepointRequest.getRootPath();
150 String parentFolderPath = getParentFolderPath(folderPath);
151
152 long groupId = SharepointUtil.getGroupId(folderPath);
153 long folderId = getLastFolderId(
154 groupId, folderPath, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
155
156 DLFolder folder = DLFolderServiceUtil.getFolder(folderId);
157
158 return getFolderTree(folder, parentFolderPath);
159 }
160
161 public Tree getFoldersTree(SharepointRequest sharepointRequest)
162 throws Exception {
163
164 Tree foldersTree = new Tree();
165
166 String parentFolderPath = sharepointRequest.getRootPath();
167
168 long groupId = SharepointUtil.getGroupId(parentFolderPath);
169 long parentFolderId = getLastFolderId(
170 groupId, parentFolderPath,
171 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
172
173 List<DLFolder> folders = DLFolderServiceUtil.getFolders(
174 groupId, parentFolderId);
175
176 for (DLFolder folder : folders) {
177 foldersTree.addChild(getFolderTree(folder, parentFolderPath));
178 }
179
180 foldersTree.addChild(getFolderTree(parentFolderPath));
181
182 return foldersTree;
183 }
184
185 public void getParentFolderIds(
186 long groupId, String path, List<Long> folderIds)
187 throws Exception {
188
189 String[] pathArray = SharepointUtil.getPathArray(path);
190
191 if (pathArray.length == 0) {
192 return;
193 }
194
195 long parentFolderId = folderIds.get(folderIds.size() - 1);
196 long folderId = DLFolderServiceUtil.getFolderId(
197 groupId, parentFolderId, pathArray[0]);
198
199 folderIds.add(folderId);
200
201 if (pathArray.length > 1) {
202 path = removeFoldersFromPath(path, 1);
203
204 getParentFolderIds(groupId, path, folderIds);
205 }
206 }
207
208 public Tree[] moveDocument(SharepointRequest sharepointRequest)
209 throws Exception {
210
211 String parentFolderPath = sharepointRequest.getRootPath();
212
213 long groupId = SharepointUtil.getGroupId(parentFolderPath);
214
215 DLFolder folder = null;
216 DLFileEntry fileEntry = null;
217
218 try {
219 long parentFolderId = getLastFolderId(
220 groupId, parentFolderPath,
221 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
222
223 folder = DLFolderServiceUtil.getFolder(parentFolderId);
224 }
225 catch (Exception e1) {
226 if (e1 instanceof NoSuchFolderException) {
227 try {
228 fileEntry = getFileEntry(sharepointRequest);
229 }
230 catch (Exception e2) {
231 }
232 }
233 }
234
235 Tree movedDocsTree = new Tree();
236 Tree movedDirsTree = new Tree();
237
238 String newPath = sharepointRequest.getParameterValue("newUrl");
239 String newParentFolderPath = getParentFolderPath(newPath);
240
241 long newGroupId = SharepointUtil.getGroupId(newParentFolderPath);
242
243 long newParentFolderId = getLastFolderId(
244 newGroupId, newParentFolderPath,
245 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
246
247 String newName = getResourceName(newPath);
248
249 ServiceContext serviceContext = new ServiceContext();
250
251 if (fileEntry != null) {
252 long folderId = fileEntry.getFolderId();
253 String name = fileEntry.getName();
254 String description = fileEntry.getDescription();
255 String versionDescription = StringPool.BLANK;
256 String extraSettings = fileEntry.getExtraSettings();
257
258 InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
259 sharepointRequest.getCompanyId(), sharepointRequest.getUserId(),
260 fileEntry.getGroupId(), fileEntry.getFolderId(),
261 fileEntry.getName());
262
263 byte[] bytes = FileUtil.getBytes(is);
264
265 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
266 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
267
268 serviceContext.setAssetTagNames(assetTagNames);
269
270 fileEntry = DLFileEntryServiceUtil.updateFileEntry(
271 groupId, folderId, newParentFolderId, name, newName, newName,
272 description, versionDescription, false, extraSettings, bytes,
273 serviceContext);
274
275 Tree documentTree = getFileEntryTree(
276 fileEntry, newParentFolderPath);
277
278 movedDocsTree.addChild(documentTree);
279 }
280 else if (folder != null) {
281 long folderId = folder.getFolderId();
282 String description = folder.getDescription();
283
284 folder = DLFolderServiceUtil.updateFolder(
285 folderId, newParentFolderId, newName, description,
286 serviceContext);
287
288 Tree folderTree = getFolderTree(folder, newParentFolderPath);
289
290 movedDirsTree.addChild(folderTree);
291 }
292
293 return new Tree[] {movedDocsTree, movedDirsTree};
294 }
295
296 public void putDocument(SharepointRequest sharepointRequest)
297 throws Exception {
298
299 String documentPath = sharepointRequest.getRootPath();
300 String parentFolderPath = getParentFolderPath(documentPath);
301
302 long groupId = SharepointUtil.getGroupId(parentFolderPath);
303 long parentFolderId = getLastFolderId(
304 groupId, parentFolderPath,
305 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
306 String name = getResourceName(documentPath);
307 String title = name;
308 String description = StringPool.BLANK;
309 String versionDescription = StringPool.BLANK;
310 String extraSettings = StringPool.BLANK;
311
312 ServiceContext serviceContext = new ServiceContext();
313
314 serviceContext.setAddCommunityPermissions(true);
315 serviceContext.setAddGuestPermissions(true);
316
317 try {
318 DLFileEntry fileEntry = getFileEntry(sharepointRequest);
319
320 name = fileEntry.getName();
321 description = fileEntry.getDescription();
322 extraSettings = fileEntry.getExtraSettings();
323
324 String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
325 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
326
327 serviceContext.setAssetTagNames(assetTagNames);
328
329 DLFileEntryServiceUtil.updateFileEntry(
330 groupId, parentFolderId, parentFolderId, name, title, title,
331 description, versionDescription, false, extraSettings,
332 sharepointRequest.getBytes(), serviceContext);
333 }
334 catch (NoSuchFileEntryException nsfee) {
335 File file = FileUtil.createTempFile(FileUtil.getExtension(name));
336
337 FileUtil.write(file, sharepointRequest.getBytes());
338
339 DLFileEntryServiceUtil.addFileEntry(
340 groupId, parentFolderId, name, title, description,
341 versionDescription, extraSettings, file, serviceContext);
342 }
343 }
344
345 public Tree[] removeDocument(SharepointRequest sharepointRequest) {
346 String parentFolderPath = sharepointRequest.getRootPath();
347
348 long groupId = SharepointUtil.getGroupId(parentFolderPath);
349
350 DLFolder folder = null;
351 DLFileEntry fileEntry = null;
352
353 try {
354 long parentFolderId = getLastFolderId(
355 groupId, parentFolderPath,
356 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
357
358 folder = DLFolderServiceUtil.getFolder(parentFolderId);
359 }
360 catch (Exception e1) {
361 if (e1 instanceof NoSuchFolderException) {
362 try {
363 fileEntry = getFileEntry(sharepointRequest);
364 }
365 catch (Exception e2) {
366 }
367 }
368 }
369
370 Tree documentTree = new Tree();
371
372 Tree removedDocsTree = new Tree();
373 Tree failedDocsTree = new Tree();
374
375 Tree folderTree = new Tree();
376
377 Tree removedDirsTree = new Tree();
378 Tree failedDirsTree = new Tree();
379
380 if (fileEntry != null) {
381 try {
382 documentTree = getFileEntryTree(fileEntry, parentFolderPath);
383
384 DLFileEntryServiceUtil.deleteFileEntry(
385 fileEntry.getGroupId(), fileEntry.getFolderId(),
386 fileEntry.getName());
387
388 removedDocsTree.addChild(documentTree);
389 }
390 catch (Exception e1) {
391 try {
392 failedDocsTree.addChild(documentTree);
393 }
394 catch (Exception e2) {
395 }
396 }
397 }
398 else if (folder != null) {
399 try {
400 folderTree = getFolderTree(folder, parentFolderPath);
401
402 DLFolderServiceUtil.deleteFolder(folder.getFolderId());
403
404 removedDirsTree.addChild(folderTree);
405 }
406 catch (Exception e1) {
407 try {
408 failedDirsTree.addChild(folderTree);
409 }
410 catch (Exception e2) {
411 }
412 }
413 }
414
415 return new Tree[] {
416 removedDocsTree, removedDirsTree, failedDocsTree, failedDirsTree};
417 }
418
419 protected Tree getFolderTree(DLFolder folder, String parentFolderPath) {
420 String folderPath = parentFolderPath.concat(StringPool.SLASH).concat(
421 folder.getName());
422
423 return getFolderTree(
424 folderPath, folder.getCreateDate(), folder.getModifiedDate(),
425 folder.getLastPostDate());
426 }
427
428 protected DLFileEntry getFileEntry(SharepointRequest sharepointRequest)
429 throws Exception {
430
431 String documentPath = sharepointRequest.getRootPath();
432 String parentFolderPath = getParentFolderPath(documentPath);
433
434 long groupId = SharepointUtil.getGroupId(parentFolderPath);
435 long parentFolderId = getLastFolderId(
436 groupId, parentFolderPath,
437 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
438 String title = getResourceName(documentPath);
439
440 return DLFileEntryServiceUtil.getFileEntryByTitle(
441 groupId, parentFolderId, title);
442 }
443
444 protected Tree getFileEntryTree(
445 DLFileEntry fileEntry, String parentFolderPath) {
446
447 String documentPath = parentFolderPath.concat(StringPool.SLASH).concat(
448 fileEntry.getTitle());
449
450 return getDocumentTree(
451 documentPath, fileEntry.getCreateDate(),
452 fileEntry.getModifiedDate(), fileEntry.getSize(),
453 fileEntry.getUserName(), fileEntry.getVersion());
454 }
455
456 }