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