1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
41   * <a href="DLSharepointStorageImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Bruno Farache
44   */
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 = DLFileEntryServiceUtil.getFileEntries(
63              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                 DLFileEntryServiceUtil.getFileEntries(groupId, parentFolderId);
135 
136             for (DLFileEntry fileEntry : fileEntries) {
137                 documentsTree.addChild(
138                     getFileEntryTree(fileEntry, parentFolderPath));
139             }
140         }
141 
142         return documentsTree;
143     }
144 
145     public Tree getFolderTree(SharepointRequest sharepointRequest)
146         throws Exception {
147 
148         String folderPath = sharepointRequest.getRootPath();
149         String parentFolderPath = getParentFolderPath(folderPath);
150 
151         long groupId = SharepointUtil.getGroupId(folderPath);
152         long folderId = getLastFolderId(
153             groupId, folderPath, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
154 
155         DLFolder folder = DLFolderServiceUtil.getFolder(folderId);
156 
157         return getFolderTree(folder, parentFolderPath);
158     }
159 
160     public Tree getFoldersTree(SharepointRequest sharepointRequest)
161         throws Exception {
162 
163         Tree foldersTree = new Tree();
164 
165         String parentFolderPath = sharepointRequest.getRootPath();
166 
167         long groupId = SharepointUtil.getGroupId(parentFolderPath);
168         long parentFolderId = getLastFolderId(
169             groupId, parentFolderPath,
170             DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
171 
172         List<DLFolder> folders = DLFolderServiceUtil.getFolders(
173             groupId, parentFolderId);
174 
175         for (DLFolder folder : folders) {
176             foldersTree.addChild(getFolderTree(folder, parentFolderPath));
177         }
178 
179         foldersTree.addChild(getFolderTree(parentFolderPath));
180 
181         return foldersTree;
182     }
183 
184     public void getParentFolderIds(
185             long groupId, String path, List<Long> folderIds)
186         throws Exception {
187 
188         String[] pathArray = SharepointUtil.getPathArray(path);
189 
190         if (pathArray.length == 0) {
191             return;
192         }
193 
194         long parentFolderId = folderIds.get(folderIds.size() - 1);
195         long folderId = DLFolderServiceUtil.getFolderId(
196             groupId, parentFolderId, pathArray[0]);
197 
198         folderIds.add(folderId);
199 
200         if (pathArray.length > 1) {
201             path = removeFoldersFromPath(path, 1);
202 
203             getParentFolderIds(groupId, path, folderIds);
204         }
205     }
206 
207     public Tree[] moveDocument(SharepointRequest sharepointRequest)
208         throws Exception {
209 
210         String parentFolderPath = sharepointRequest.getRootPath();
211 
212         long groupId = SharepointUtil.getGroupId(parentFolderPath);
213 
214         DLFolder folder = null;
215         DLFileEntry fileEntry = null;
216 
217         try {
218             long parentFolderId = getLastFolderId(
219                 groupId, parentFolderPath,
220                 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
221 
222             folder = DLFolderServiceUtil.getFolder(parentFolderId);
223         }
224         catch (Exception e1) {
225             if (e1 instanceof NoSuchFolderException) {
226                 try {
227                     fileEntry = getFileEntry(sharepointRequest);
228                 }
229                 catch (Exception e2) {
230                 }
231             }
232         }
233 
234         Tree movedDocsTree = new Tree();
235         Tree movedDirsTree = new Tree();
236 
237         String newPath = sharepointRequest.getParameterValue("newUrl");
238         String newParentFolderPath = getParentFolderPath(newPath);
239 
240         long newGroupId = SharepointUtil.getGroupId(newParentFolderPath);
241 
242         long newParentFolderId = getLastFolderId(
243             newGroupId, newParentFolderPath,
244             DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
245 
246         String newName = getResourceName(newPath);
247 
248         ServiceContext serviceContext = new ServiceContext();
249 
250         if (fileEntry != null) {
251             long folderId = fileEntry.getFolderId();
252             String name = fileEntry.getName();
253             String description = fileEntry.getDescription();
254             String versionDescription = StringPool.BLANK;
255             String extraSettings = fileEntry.getExtraSettings();
256 
257             InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
258                 sharepointRequest.getCompanyId(), sharepointRequest.getUserId(),
259                 fileEntry.getGroupId(), fileEntry.getFolderId(),
260                 fileEntry.getName());
261 
262             byte[] bytes = FileUtil.getBytes(is);
263 
264             String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
265                 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
266 
267             serviceContext.setAssetTagNames(assetTagNames);
268 
269             fileEntry = DLFileEntryServiceUtil.updateFileEntry(
270                 groupId, folderId, newParentFolderId, name, newName, newName,
271                 description, versionDescription, false, extraSettings, bytes,
272                 serviceContext);
273 
274             Tree documentTree = getFileEntryTree(
275                 fileEntry, newParentFolderPath);
276 
277             movedDocsTree.addChild(documentTree);
278         }
279         else if (folder != null) {
280             long folderId = folder.getFolderId();
281             String description = folder.getDescription();
282 
283             folder = DLFolderServiceUtil.updateFolder(
284                 folderId, newParentFolderId, newName, description,
285                 serviceContext);
286 
287             Tree folderTree = getFolderTree(folder, newParentFolderPath);
288 
289             movedDirsTree.addChild(folderTree);
290         }
291 
292         return new Tree[] {movedDocsTree, movedDirsTree};
293     }
294 
295     public void putDocument(SharepointRequest sharepointRequest)
296         throws Exception {
297 
298         String documentPath = sharepointRequest.getRootPath();
299         String parentFolderPath = getParentFolderPath(documentPath);
300 
301         long groupId = SharepointUtil.getGroupId(parentFolderPath);
302         long parentFolderId = getLastFolderId(
303             groupId, parentFolderPath,
304             DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
305         String name = getResourceName(documentPath);
306         String title = name;
307         String description = StringPool.BLANK;
308         String versionDescription = StringPool.BLANK;
309         String extraSettings = StringPool.BLANK;
310 
311         ServiceContext serviceContext = new ServiceContext();
312 
313         serviceContext.setAddCommunityPermissions(true);
314         serviceContext.setAddGuestPermissions(true);
315 
316         try {
317             DLFileEntry fileEntry = getFileEntry(sharepointRequest);
318 
319             name = fileEntry.getName();
320             description = fileEntry.getDescription();
321             extraSettings = fileEntry.getExtraSettings();
322 
323             String[] assetTagNames = AssetTagLocalServiceUtil.getTagNames(
324                 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
325 
326             serviceContext.setAssetTagNames(assetTagNames);
327 
328             DLFileEntryServiceUtil.updateFileEntry(
329                 groupId, parentFolderId, parentFolderId, name, title, title,
330                 description, versionDescription, false, extraSettings,
331                 sharepointRequest.getBytes(), serviceContext);
332         }
333         catch (NoSuchFileEntryException nsfee) {
334             File file = FileUtil.createTempFile(FileUtil.getExtension(name));
335 
336             FileUtil.write(file, sharepointRequest.getBytes());
337 
338             DLFileEntryServiceUtil.addFileEntry(
339                 groupId, parentFolderId, name, title, description,
340                 versionDescription, extraSettings, file, serviceContext);
341         }
342     }
343 
344     public Tree[] removeDocument(SharepointRequest sharepointRequest) {
345         String parentFolderPath = sharepointRequest.getRootPath();
346 
347         long groupId = SharepointUtil.getGroupId(parentFolderPath);
348 
349         DLFolder folder = null;
350         DLFileEntry fileEntry = null;
351 
352         try {
353             long parentFolderId = getLastFolderId(
354                 groupId, parentFolderPath,
355                 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
356 
357             folder = DLFolderServiceUtil.getFolder(parentFolderId);
358         }
359         catch (Exception e1) {
360             if (e1 instanceof NoSuchFolderException) {
361                 try {
362                     fileEntry = getFileEntry(sharepointRequest);
363                 }
364                 catch (Exception e2) {
365                 }
366             }
367         }
368 
369         Tree documentTree = new Tree();
370 
371         Tree removedDocsTree = new Tree();
372         Tree failedDocsTree = new Tree();
373 
374         Tree folderTree = new Tree();
375 
376         Tree removedDirsTree = new Tree();
377         Tree failedDirsTree = new Tree();
378 
379         if (fileEntry != null) {
380             try {
381                 documentTree = getFileEntryTree(fileEntry, parentFolderPath);
382 
383                 DLFileEntryServiceUtil.deleteFileEntry(
384                     fileEntry.getGroupId(), fileEntry.getFolderId(),
385                     fileEntry.getName());
386 
387                 removedDocsTree.addChild(documentTree);
388             }
389             catch (Exception e1) {
390                 try {
391                     failedDocsTree.addChild(documentTree);
392                 }
393                 catch (Exception e2) {
394                 }
395             }
396         }
397         else if (folder != null) {
398             try {
399                 folderTree = getFolderTree(folder, parentFolderPath);
400 
401                 DLFolderServiceUtil.deleteFolder(folder.getFolderId());
402 
403                 removedDirsTree.addChild(folderTree);
404             }
405             catch (Exception e1) {
406                 try {
407                     failedDirsTree.addChild(folderTree);
408                 }
409                 catch (Exception e2) {
410                 }
411             }
412         }
413 
414         return new Tree[] {
415             removedDocsTree, removedDirsTree, failedDocsTree, failedDirsTree};
416     }
417 
418     protected Tree getFolderTree(DLFolder folder, String parentFolderPath) {
419         String folderPath = parentFolderPath.concat(StringPool.SLASH).concat(
420             folder.getName());
421 
422         return getFolderTree(
423             folderPath, 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             groupId, parentFolderId, title);
441     }
442 
443     protected Tree getFileEntryTree(
444         DLFileEntry fileEntry, String parentFolderPath) {
445 
446         String documentPath = parentFolderPath.concat(StringPool.SLASH).concat(
447             fileEntry.getTitle());
448 
449         return getDocumentTree(
450             documentPath, fileEntry.getCreateDate(),
451             fileEntry.getModifiedDate(), fileEntry.getSize(),
452             fileEntry.getUserName(), fileEntry.getVersion());
453     }
454 
455 }