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