001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.HttpUtil;
024    import com.liferay.portal.kernel.util.MimeTypesUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.sharepoint.BaseSharepointStorageImpl;
029    import com.liferay.portal.sharepoint.SharepointRequest;
030    import com.liferay.portal.sharepoint.SharepointUtil;
031    import com.liferay.portal.sharepoint.Tree;
032    import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
033    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
034    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
035    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
036    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
037    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
038    
039    import java.io.File;
040    import java.io.InputStream;
041    
042    import java.util.List;
043    
044    import javax.servlet.http.HttpServletRequest;
045    
046    /**
047     * @author Bruno Farache
048     */
049    public class DLSharepointStorageImpl extends BaseSharepointStorageImpl {
050    
051            @Override
052            public void addDocumentElements(
053                            SharepointRequest sharepointRequest, Element element)
054                    throws Exception {
055    
056                    String parentFolderPath = sharepointRequest.getRootPath();
057    
058                    long groupId = SharepointUtil.getGroupId(parentFolderPath);
059                    long parentFolderId = getLastFolderId(
060                            groupId, parentFolderPath,
061                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
062    
063                    if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
064                            return;
065                    }
066    
067                    List<FileEntry> fileEntries = DLAppServiceUtil.getFileEntries(
068                            groupId, parentFolderId);
069    
070                    for (FileEntry fileEntry : fileEntries) {
071                            String documentPath = parentFolderPath.concat(
072                                    StringPool.SLASH).concat(fileEntry.getTitle());
073    
074                            addDocumentElement(
075                                    element, documentPath, fileEntry.getCreateDate(),
076                                    fileEntry.getModifiedDate(), fileEntry.getUserName());
077                    }
078            }
079    
080            @Override
081            public void createFolder(SharepointRequest sharepointRequest)
082                    throws Exception {
083    
084                    String folderPath = sharepointRequest.getRootPath();
085                    String parentFolderPath = getParentFolderPath(folderPath);
086    
087                    long groupId = SharepointUtil.getGroupId(parentFolderPath);
088                    long parentFolderId = getLastFolderId(
089                            groupId, parentFolderPath,
090                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
091                    String folderName = getResourceName(folderPath);
092                    String description = StringPool.BLANK;
093    
094                    ServiceContext serviceContext = new ServiceContext();
095    
096                    serviceContext.setAddGroupPermissions(true);
097                    serviceContext.setAddGuestPermissions(true);
098    
099                    DLAppServiceUtil.addFolder(
100                            groupId, parentFolderId, folderName, description, serviceContext);
101            }
102    
103            @Override
104            public InputStream getDocumentInputStream(
105                            SharepointRequest sharepointRequest)
106                    throws Exception {
107    
108                    FileEntry fileEntry = getFileEntry(sharepointRequest);
109    
110                    return fileEntry.getContentStream();
111            }
112    
113            @Override
114            public Tree getDocumentsTree(SharepointRequest sharepointRequest)
115                    throws Exception {
116    
117                    Tree documentsTree = new Tree();
118    
119                    String parentFolderPath = sharepointRequest.getRootPath();
120    
121                    long groupId = SharepointUtil.getGroupId(parentFolderPath);
122                    long parentFolderId = getLastFolderId(
123                            groupId, parentFolderPath,
124                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
125    
126                    List<FileEntry> fileEntries = DLAppServiceUtil.getFileEntries(
127                            groupId, parentFolderId);
128    
129                    for (FileEntry fileEntry : fileEntries) {
130                            documentsTree.addChild(
131                                    getFileEntryTree(fileEntry, parentFolderPath));
132                    }
133    
134                    return documentsTree;
135            }
136    
137            @Override
138            public Tree getDocumentTree(SharepointRequest sharepointRequest)
139                    throws Exception {
140    
141                    String documentPath = sharepointRequest.getRootPath();
142                    String parentFolderPath = getParentFolderPath(documentPath);
143    
144                    FileEntry fileEntry = getFileEntry(sharepointRequest);
145    
146                    return getFileEntryTree(fileEntry, parentFolderPath);
147            }
148    
149            @Override
150            public Tree getFoldersTree(SharepointRequest sharepointRequest)
151                    throws Exception {
152    
153                    Tree foldersTree = new Tree();
154    
155                    String parentFolderPath = sharepointRequest.getRootPath();
156    
157                    long groupId = SharepointUtil.getGroupId(parentFolderPath);
158                    long parentFolderId = getLastFolderId(
159                            groupId, parentFolderPath,
160                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
161    
162                    List<Folder> folders = DLAppServiceUtil.getFolders(
163                            groupId, parentFolderId, false);
164    
165                    for (Folder folder : folders) {
166                            foldersTree.addChild(getFolderTree(folder, parentFolderPath));
167                    }
168    
169                    foldersTree.addChild(getFolderTree(parentFolderPath));
170    
171                    return foldersTree;
172            }
173    
174            @Override
175            public Tree getFolderTree(SharepointRequest sharepointRequest)
176                    throws Exception {
177    
178                    String folderPath = sharepointRequest.getRootPath();
179                    String parentFolderPath = getParentFolderPath(folderPath);
180    
181                    long groupId = SharepointUtil.getGroupId(folderPath);
182                    long folderId = getLastFolderId(
183                            groupId, folderPath, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
184    
185                    Folder folder = DLAppServiceUtil.getFolder(folderId);
186    
187                    return getFolderTree(folder, parentFolderPath);
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, HttpUtil.decodePath(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                                            DLFileEntryConstants.getClassName(),
275                                            fileEntry.getFileEntryId());
276    
277                                    serviceContext.setAssetTagNames(assetTagNames);
278    
279                                    fileEntry = DLAppServiceUtil.updateFileEntry(
280                                            fileEntryId, newName, mimeType, newName, description,
281                                            changeLog, false, file, serviceContext);
282    
283                                    if (folderId != newParentFolderId) {
284                                            fileEntry = DLAppServiceUtil.moveFileEntry(
285                                                    fileEntryId, newParentFolderId, serviceContext);
286                                    }
287    
288                                    Tree documentTree = getFileEntryTree(
289                                            fileEntry, newParentFolderPath);
290    
291                                    movedDocsTree.addChild(documentTree);
292                            }
293                            finally {
294                                    FileUtil.delete(file);
295                            }
296                    }
297                    else if (folder != null) {
298                            long folderId = folder.getFolderId();
299    
300                            String description = folder.getDescription();
301    
302                            if (newParentFolderId != folder.getParentFolderId()) {
303                                    folder = DLAppServiceUtil.moveFolder(
304                                            folderId, newParentFolderId, serviceContext);
305                            }
306    
307                            if (!newName.equals(folder.getName())) {
308                                    DLAppServiceUtil.updateFolder(
309                                            folderId, newName, description, serviceContext);
310                            }
311    
312                            Tree folderTree = getFolderTree(folder, newParentFolderPath);
313    
314                            movedDirsTree.addChild(folderTree);
315                    }
316    
317                    return new Tree[] {movedDocsTree, movedDirsTree};
318            }
319    
320            @Override
321            public void putDocument(SharepointRequest sharepointRequest)
322                    throws Exception {
323    
324                    HttpServletRequest request = sharepointRequest.getHttpServletRequest();
325    
326                    String documentPath = sharepointRequest.getRootPath();
327                    String parentFolderPath = getParentFolderPath(documentPath);
328    
329                    long groupId = SharepointUtil.getGroupId(parentFolderPath);
330                    long parentFolderId = getLastFolderId(
331                            groupId, parentFolderPath,
332                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
333                    String title = getResourceName(documentPath);
334                    String description = StringPool.BLANK;
335                    String changeLog = StringPool.BLANK;
336    
337                    ServiceContext serviceContext = new ServiceContext();
338    
339                    serviceContext.setAddGroupPermissions(true);
340                    serviceContext.setAddGuestPermissions(true);
341    
342                    String contentType = GetterUtil.get(
343                            request.getHeader(HttpHeaders.CONTENT_TYPE),
344                            ContentTypes.APPLICATION_OCTET_STREAM);
345    
346                    String extension = FileUtil.getExtension(title);
347    
348                    File file = null;
349    
350                    try {
351                            file = FileUtil.createTempFile(extension);
352    
353                            FileUtil.write(file, sharepointRequest.getBytes());
354    
355                            if (contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
356                                    contentType = MimeTypesUtil.getContentType(file, title);
357                            }
358    
359                            try {
360                                    FileEntry fileEntry = getFileEntry(sharepointRequest);
361    
362                                    long fileEntryId = fileEntry.getFileEntryId();
363    
364                                    description = fileEntry.getDescription();
365    
366                                    String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
367                                            DLFileEntryConstants.getClassName(),
368                                            fileEntry.getFileEntryId());
369    
370                                    serviceContext.setAssetTagNames(assetTagNames);
371    
372                                    DLAppServiceUtil.updateFileEntry(
373                                            fileEntryId, title, contentType, title, description,
374                                            changeLog, false, file, serviceContext);
375                            }
376                            catch (NoSuchFileEntryException nsfee) {
377                                    DLAppServiceUtil.addFileEntry(
378                                            groupId, parentFolderId, title, contentType, title,
379                                            description, changeLog, file, serviceContext);
380                            }
381                    }
382                    finally {
383                            FileUtil.delete(file);
384                    }
385            }
386    
387            @Override
388            public Tree[] removeDocument(SharepointRequest sharepointRequest) {
389                    String parentFolderPath = sharepointRequest.getRootPath();
390    
391                    long groupId = SharepointUtil.getGroupId(parentFolderPath);
392    
393                    Folder folder = null;
394                    FileEntry fileEntry = null;
395    
396                    try {
397                            long parentFolderId = getLastFolderId(
398                                    groupId, parentFolderPath,
399                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
400    
401                            folder = DLAppServiceUtil.getFolder(parentFolderId);
402                    }
403                    catch (Exception e1) {
404                            if (e1 instanceof NoSuchFolderException) {
405                                    try {
406                                            fileEntry = getFileEntry(sharepointRequest);
407                                    }
408                                    catch (Exception e2) {
409                                    }
410                            }
411                    }
412    
413                    Tree documentTree = new Tree();
414    
415                    Tree removedDocsTree = new Tree();
416                    Tree failedDocsTree = new Tree();
417    
418                    Tree folderTree = new Tree();
419    
420                    Tree removedDirsTree = new Tree();
421                    Tree failedDirsTree = new Tree();
422    
423                    if (fileEntry != null) {
424                            try {
425                                    documentTree = getFileEntryTree(fileEntry, parentFolderPath);
426    
427                                    DLAppServiceUtil.deleteFileEntry(fileEntry.getFileEntryId());
428    
429                                    removedDocsTree.addChild(documentTree);
430                            }
431                            catch (Exception e1) {
432                                    try {
433                                            failedDocsTree.addChild(documentTree);
434                                    }
435                                    catch (Exception e2) {
436                                    }
437                            }
438                    }
439                    else if (folder != null) {
440                            try {
441                                    folderTree = getFolderTree(folder, parentFolderPath);
442    
443                                    DLAppServiceUtil.deleteFolder(folder.getFolderId());
444    
445                                    removedDirsTree.addChild(folderTree);
446                            }
447                            catch (Exception e1) {
448                                    try {
449                                            failedDirsTree.addChild(folderTree);
450                                    }
451                                    catch (Exception e2) {
452                                    }
453                            }
454                    }
455    
456                    return new Tree[] {
457                            removedDocsTree, removedDirsTree, failedDocsTree, failedDirsTree};
458            }
459    
460            protected FileEntry getFileEntry(SharepointRequest sharepointRequest)
461                    throws Exception {
462    
463                    String documentPath = sharepointRequest.getRootPath();
464                    String parentFolderPath = getParentFolderPath(documentPath);
465    
466                    long groupId = SharepointUtil.getGroupId(parentFolderPath);
467                    long parentFolderId = getLastFolderId(
468                            groupId, parentFolderPath,
469                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
470                    String title = getResourceName(documentPath);
471    
472                    return DLAppServiceUtil.getFileEntry(groupId, parentFolderId, title);
473            }
474    
475            protected Tree getFileEntryTree(
476                    FileEntry fileEntry, String parentFolderPath) {
477    
478                    String documentPath = parentFolderPath.concat(StringPool.SLASH).concat(
479                            fileEntry.getTitle());
480    
481                    return getDocumentTree(
482                            documentPath, fileEntry.getCreateDate(),
483                            fileEntry.getModifiedDate(), fileEntry.getSize(),
484                            fileEntry.getUserName(), fileEntry.getVersion());
485            }
486    
487            protected Tree getFolderTree(Folder folder, String parentFolderPath) {
488                    String folderPath = parentFolderPath.concat(StringPool.SLASH).concat(
489                            folder.getName());
490    
491                    return getFolderTree(
492                            folderPath, folder.getCreateDate(), folder.getModifiedDate(),
493                            folder.getLastPostDate());
494            }
495    
496    }