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