1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
49   * <a href="DLSharepointStorageImpl.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Bruno Farache
52   */
53  public class DLSharepointStorageImpl extends BaseSharepointStorageImpl {
54  
55      public void addDocumentElements(
56              SharepointRequest sharepointRequest, Element element)
57          throws Exception {
58  
59          String parentFolderPath = sharepointRequest.getRootPath();
60  
61          long groupId = SharepointUtil.getGroupId(parentFolderPath);
62          long parentFolderId = getLastFolderId(
63              groupId, parentFolderPath,
64              DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
65  
66          if (parentFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
67              return;
68          }
69  
70          List<DLFileEntry> fileEntries =
71              DLFileEntryLocalServiceUtil.getFileEntries(parentFolderId);
72  
73          for (DLFileEntry fileEntry : fileEntries) {
74              StringBuilder sb = new StringBuilder();
75  
76              sb.append(parentFolderPath);
77              sb.append(StringPool.SLASH);
78              sb.append(fileEntry.getTitleWithExtension());
79  
80              addDocumentElement(
81                  element, sb.toString(), fileEntry.getCreateDate(),
82                  fileEntry.getModifiedDate(), fileEntry.getUserName());
83          }
84      }
85  
86      public void createFolder(SharepointRequest sharepointRequest)
87          throws Exception {
88  
89          String folderPath = sharepointRequest.getRootPath();
90          String parentFolderPath = getParentFolderPath(folderPath);
91  
92          long groupId = SharepointUtil.getGroupId(parentFolderPath);
93          long parentFolderId = getLastFolderId(
94              groupId, parentFolderPath,
95              DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
96          String folderName = getResourceName(folderPath);
97          String description = StringPool.BLANK;
98  
99          ServiceContext serviceContext = new ServiceContext();
100 
101         serviceContext.setAddCommunityPermissions(true);
102         serviceContext.setAddGuestPermissions(true);
103 
104         DLFolderServiceUtil.addFolder(
105             groupId, parentFolderId, folderName, description, serviceContext);
106     }
107 
108     public InputStream getDocumentInputStream(
109             SharepointRequest sharepointRequest)
110         throws Exception {
111 
112         DLFileEntry fileEntry = getFileEntry(sharepointRequest);
113 
114         return DLFileEntryLocalServiceUtil.getFileAsStream(
115             sharepointRequest.getCompanyId(), sharepointRequest.getUserId(),
116             fileEntry.getFolderId(), fileEntry.getName());
117     }
118 
119     public Tree getDocumentTree(SharepointRequest sharepointRequest)
120         throws Exception {
121 
122         String documentPath = sharepointRequest.getRootPath();
123         String parentFolderPath = getParentFolderPath(documentPath);
124 
125         DLFileEntry fileEntry = getFileEntry(sharepointRequest);
126 
127         return getFileEntryTree(fileEntry, parentFolderPath);
128     }
129 
130     public Tree getDocumentsTree(SharepointRequest sharepointRequest)
131         throws Exception {
132 
133         Tree documentsTree = new Tree();
134 
135         String parentFolderPath = sharepointRequest.getRootPath();
136 
137         long groupId = SharepointUtil.getGroupId(parentFolderPath);
138         long parentFolderId = getLastFolderId(
139             groupId, parentFolderPath,
140             DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
141 
142         if (parentFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
143             List<DLFileEntry> fileEntries =
144                 DLFileEntryLocalServiceUtil.getFileEntries(parentFolderId);
145 
146             for (DLFileEntry fileEntry : fileEntries) {
147                 documentsTree.addChild(
148                     getFileEntryTree(fileEntry, parentFolderPath));
149             }
150         }
151 
152         return documentsTree;
153     }
154 
155     public Tree getFolderTree(SharepointRequest sharepointRequest)
156         throws Exception {
157 
158         String folderPath = sharepointRequest.getRootPath();
159         String parentFolderPath = getParentFolderPath(folderPath);
160 
161         long groupId = SharepointUtil.getGroupId(folderPath);
162         long folderId = getLastFolderId(
163             groupId, folderPath, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
164 
165         DLFolder folder = DLFolderServiceUtil.getFolder(folderId);
166 
167         return getFolderTree(folder, parentFolderPath);
168     }
169 
170     public Tree getFoldersTree(SharepointRequest sharepointRequest)
171         throws Exception {
172 
173         Tree foldersTree = new Tree();
174 
175         String parentFolderPath = sharepointRequest.getRootPath();
176 
177         long groupId = SharepointUtil.getGroupId(parentFolderPath);
178         long parentFolderId = getLastFolderId(
179             groupId, parentFolderPath,
180             DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
181 
182         List<DLFolder> folders = DLFolderServiceUtil.getFolders(
183             groupId, parentFolderId);
184 
185         for (DLFolder folder : folders) {
186             foldersTree.addChild(getFolderTree(folder, parentFolderPath));
187         }
188 
189         foldersTree.addChild(getFolderTree(parentFolderPath));
190 
191         return foldersTree;
192     }
193 
194     public void getParentFolderIds(
195             long groupId, String path, List<Long> folderIds)
196         throws Exception {
197 
198         String[] pathArray = SharepointUtil.getPathArray(path);
199 
200         if (pathArray.length == 0) {
201             return;
202         }
203 
204         long parentFolderId = folderIds.get(folderIds.size() - 1);
205         long folderId = DLFolderServiceUtil.getFolderId(
206             groupId, parentFolderId, pathArray[0]);
207 
208         folderIds.add(folderId);
209 
210         if (pathArray.length > 1) {
211             path = removeFoldersFromPath(path, 1);
212 
213             getParentFolderIds(groupId, path, folderIds);
214         }
215     }
216 
217     public Tree[] moveDocument(SharepointRequest sharepointRequest)
218         throws Exception {
219 
220         String parentFolderPath = sharepointRequest.getRootPath();
221 
222         long groupId = SharepointUtil.getGroupId(parentFolderPath);
223 
224         DLFolder folder = null;
225         DLFileEntry fileEntry = null;
226 
227         try {
228             long parentFolderId = getLastFolderId(
229                 groupId, parentFolderPath,
230                 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
231 
232             folder = DLFolderServiceUtil.getFolder(parentFolderId);
233         }
234         catch (Exception e1) {
235             if (e1 instanceof NoSuchFolderException) {
236                 try {
237                     fileEntry = getFileEntry(sharepointRequest);
238                 }
239                 catch (Exception e2) {
240                 }
241             }
242         }
243 
244         Tree movedDocsTree = new Tree();
245         Tree movedDirsTree = new Tree();
246 
247         String newPath = sharepointRequest.getParameterValue("newUrl");
248         String newParentFolderPath = getParentFolderPath(newPath);
249 
250         long newGroupId = SharepointUtil.getGroupId(newParentFolderPath);
251 
252         long newParentFolderId = getLastFolderId(
253             newGroupId, newParentFolderPath,
254             DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
255 
256         String newName = getResourceName(newPath);
257 
258         ServiceContext serviceContext = new ServiceContext();
259 
260         if (fileEntry != null) {
261             long folderId = fileEntry.getFolderId();
262             String name = fileEntry.getName();
263             String description = fileEntry.getDescription();
264             String extraSettings = fileEntry.getExtraSettings();
265 
266             InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
267                 sharepointRequest.getCompanyId(), sharepointRequest.getUserId(),
268                 fileEntry.getFolderId(), fileEntry.getName());
269 
270             byte[] bytes = FileUtil.getBytes(is);
271 
272             String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
273                 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
274 
275             serviceContext.setTagsEntries(tagsEntries);
276 
277             fileEntry = DLFileEntryServiceUtil.updateFileEntry(
278                 folderId, newParentFolderId, name, newName, newName,
279                 description, extraSettings, bytes, serviceContext);
280 
281             Tree documentTree = getFileEntryTree(
282                 fileEntry, newParentFolderPath);
283 
284             movedDocsTree.addChild(documentTree);
285         }
286         else if (folder != null) {
287             long folderId = folder.getFolderId();
288             String description = folder.getDescription();
289 
290             folder = DLFolderServiceUtil.updateFolder(
291                 folderId, newParentFolderId, newName, description,
292                 serviceContext);
293 
294             Tree folderTree = getFolderTree(folder, newParentFolderPath);
295 
296             movedDirsTree.addChild(folderTree);
297         }
298 
299         return new Tree[] {movedDocsTree, movedDirsTree};
300     }
301 
302     public void putDocument(SharepointRequest sharepointRequest)
303         throws Exception {
304 
305         String documentPath = sharepointRequest.getRootPath();
306         String parentFolderPath = getParentFolderPath(documentPath);
307 
308         long groupId = SharepointUtil.getGroupId(parentFolderPath);
309         long parentFolderId = getLastFolderId(
310             groupId, parentFolderPath,
311             DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
312         String name = getResourceName(documentPath);
313         String title = name;
314         String description = StringPool.BLANK;
315         String extraSettings = StringPool.BLANK;
316 
317         ServiceContext serviceContext = new ServiceContext();
318 
319         serviceContext.setAddCommunityPermissions(true);
320         serviceContext.setAddGuestPermissions(true);
321 
322         try {
323             DLFileEntry fileEntry = getFileEntry(sharepointRequest);
324 
325             name = fileEntry.getName();
326             description = fileEntry.getDescription();
327             extraSettings = fileEntry.getExtraSettings();
328 
329             String[] tagsEntries = TagsEntryLocalServiceUtil.getEntryNames(
330                 DLFileEntry.class.getName(), fileEntry.getFileEntryId());
331 
332             serviceContext.setTagsEntries(tagsEntries);
333 
334             DLFileEntryServiceUtil.updateFileEntry(
335                 parentFolderId, parentFolderId, name, title, title,
336                 description, extraSettings, sharepointRequest.getBytes(),
337                 serviceContext);
338         }
339         catch (NoSuchFileEntryException nsfee) {
340             File file = FileUtil.createTempFile(FileUtil.getExtension(name));
341 
342             FileUtil.write(file, sharepointRequest.getBytes());
343 
344             DLFileEntryServiceUtil.addFileEntry(
345                 parentFolderId, name, title, description, extraSettings, file,
346                 serviceContext);
347         }
348     }
349 
350     public Tree[] removeDocument(SharepointRequest sharepointRequest) {
351         String parentFolderPath = sharepointRequest.getRootPath();
352 
353         long groupId = SharepointUtil.getGroupId(parentFolderPath);
354 
355         DLFolder folder = null;
356         DLFileEntry fileEntry = null;
357 
358         try {
359             long parentFolderId = getLastFolderId(
360                 groupId, parentFolderPath,
361                 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
362 
363             folder = DLFolderServiceUtil.getFolder(parentFolderId);
364         }
365         catch (Exception e1) {
366             if (e1 instanceof NoSuchFolderException) {
367                 try {
368                     fileEntry = getFileEntry(sharepointRequest);
369                 }
370                 catch (Exception e2) {
371                 }
372             }
373         }
374 
375         Tree documentTree = new Tree();
376 
377         Tree removedDocsTree = new Tree();
378         Tree failedDocsTree = new Tree();
379 
380         Tree folderTree = new Tree();
381 
382         Tree removedDirsTree = new Tree();
383         Tree failedDirsTree = new Tree();
384 
385         if (fileEntry != null) {
386             try {
387                 documentTree = getFileEntryTree(fileEntry, parentFolderPath);
388 
389                 DLFileEntryServiceUtil.deleteFileEntry(
390                     fileEntry.getFolderId(), fileEntry.getName());
391 
392                 removedDocsTree.addChild(documentTree);
393             }
394             catch (Exception e1) {
395                 try {
396                     failedDocsTree.addChild(documentTree);
397                 }
398                 catch (Exception e2) {
399                 }
400             }
401         }
402         else if (folder != null) {
403             try {
404                 folderTree = getFolderTree(folder, parentFolderPath);
405 
406                 DLFolderServiceUtil.deleteFolder(folder.getFolderId());
407 
408                 removedDirsTree.addChild(folderTree);
409             }
410             catch (Exception e1) {
411                 try {
412                     failedDirsTree.addChild(folderTree);
413                 }
414                 catch (Exception e2) {
415                 }
416             }
417         }
418 
419         return new Tree[] {
420             removedDocsTree, removedDirsTree, failedDocsTree, failedDirsTree};
421     }
422 
423     protected Tree getFolderTree(DLFolder folder, String parentFolderPath) {
424         StringBuilder sb = new StringBuilder();
425 
426         sb.append(parentFolderPath);
427         sb.append(StringPool.SLASH);
428         sb.append(folder.getName());
429 
430         return getFolderTree(
431             sb.toString(), folder.getCreateDate(), folder.getModifiedDate(),
432             folder.getLastPostDate());
433     }
434 
435     protected DLFileEntry getFileEntry(SharepointRequest sharepointRequest)
436         throws Exception {
437 
438         String documentPath = sharepointRequest.getRootPath();
439         String parentFolderPath = getParentFolderPath(documentPath);
440 
441         long groupId = SharepointUtil.getGroupId(parentFolderPath);
442         long parentFolderId = getLastFolderId(
443             groupId, parentFolderPath,
444             DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
445         String title = getResourceName(documentPath);
446 
447         return DLFileEntryServiceUtil.getFileEntryByTitle(
448             parentFolderId, title);
449     }
450 
451     protected Tree getFileEntryTree(
452         DLFileEntry fileEntry, String parentFolderPath) {
453 
454         StringBuilder sb = new StringBuilder();
455 
456         sb.append(parentFolderPath);
457         sb.append(StringPool.SLASH);
458         sb.append(fileEntry.getTitleWithExtension());
459 
460         return getDocumentTree(
461             sb.toString(), fileEntry.getCreateDate(),
462             fileEntry.getModifiedDate(), fileEntry.getSize(),
463             fileEntry.getUserName(), fileEntry.getVersion());
464     }
465 
466 }