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