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.lar;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  import com.liferay.portal.kernel.util.MapUtil;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.kernel.xml.Document;
22  import com.liferay.portal.kernel.xml.Element;
23  import com.liferay.portal.kernel.xml.SAXReaderUtil;
24  import com.liferay.portal.lar.BasePortletDataHandler;
25  import com.liferay.portal.lar.PortletDataContext;
26  import com.liferay.portal.lar.PortletDataException;
27  import com.liferay.portal.lar.PortletDataHandlerBoolean;
28  import com.liferay.portal.lar.PortletDataHandlerControl;
29  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
30  import com.liferay.portlet.documentlibrary.model.DLFileRank;
31  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
32  import com.liferay.portlet.documentlibrary.model.DLFolder;
33  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
34  import com.liferay.portlet.documentlibrary.service.persistence.DLFolderUtil;
35  
36  import java.util.List;
37  import java.util.Map;
38  
39  import javax.portlet.PortletPreferences;
40  
41  /**
42   * <a href="DLDisplayPortletDataHandlerImpl.java.html"><b><i>View Source</i></b>
43   * </a>
44   *
45   * @author Raymond Augé
46   */
47  public class DLDisplayPortletDataHandlerImpl extends BasePortletDataHandler {
48  
49      public PortletPreferences deleteData(
50              PortletDataContext context, String portletId,
51              PortletPreferences preferences)
52          throws PortletDataException {
53  
54          try {
55              preferences.setValue("rootFolderId", StringPool.BLANK);
56              preferences.setValue("showBreadcrumbs", StringPool.BLANK);
57              preferences.setValue("showFoldersSearch", StringPool.BLANK);
58              preferences.setValue("showSubfolders", StringPool.BLANK);
59              preferences.setValue("foldersPerPage", StringPool.BLANK);
60              preferences.setValue("folderColumns", StringPool.BLANK);
61              preferences.setValue("showFileEntriesSearch", StringPool.BLANK);
62              preferences.setValue("fileEntriesPerPage", StringPool.BLANK);
63              preferences.setValue("fileEntryColumns", StringPool.BLANK);
64              preferences.setValue("enable-comment-ratings", StringPool.BLANK);
65  
66              return preferences;
67          }
68          catch (Exception e) {
69              throw new PortletDataException(e);
70          }
71      }
72  
73      public String exportData(
74              PortletDataContext context, String portletId,
75              PortletPreferences preferences)
76          throws PortletDataException {
77  
78          try {
79              long rootFolderId = GetterUtil.getLong(
80                  preferences.getValue("rootFolderId", null));
81  
82              Document doc = SAXReaderUtil.createDocument();
83  
84              Element root = doc.addElement("documentlibrary-display-data");
85  
86              Element foldersEl = root.addElement("folders");
87              Element fileEntriesEl = root.addElement("file-entries");
88              Element fileShortcutsEl = root.addElement("file-shortcuts");
89              Element fileRanksEl = root.addElement("file-ranks");
90  
91              if (rootFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
92                  List<DLFolder> folders = DLFolderUtil.findByGroupId(
93                      context.getGroupId());
94  
95                  for (DLFolder folder : folders) {
96                      DLPortletDataHandlerImpl.exportFolder(
97                          context, foldersEl, fileEntriesEl, fileShortcutsEl,
98                          fileRanksEl, folder);
99                  }
100             }
101             else {
102                 DLFolder folder = DLFolderUtil.findByPrimaryKey(rootFolderId);
103 
104                 root.addAttribute(
105                     "root-folder-id", String.valueOf(folder.getFolderId()));
106 
107                 DLPortletDataHandlerImpl.exportFolder(
108                     context, foldersEl, fileEntriesEl, fileShortcutsEl,
109                     fileRanksEl, folder);
110             }
111 
112             return doc.formattedString();
113         }
114         catch (Exception e) {
115             throw new PortletDataException(e);
116         }
117     }
118 
119     public PortletDataHandlerControl[] getExportControls() {
120         return new PortletDataHandlerControl[] {
121             _foldersAndDocuments, _shortcuts, _ranks, _comments, _ratings, _tags
122         };
123     }
124 
125     public PortletDataHandlerControl[] getImportControls() {
126         return new PortletDataHandlerControl[] {
127             _foldersAndDocuments, _shortcuts, _ranks, _comments, _ratings, _tags
128         };
129     }
130 
131     public PortletPreferences importData(
132             PortletDataContext context, String portletId,
133             PortletPreferences preferences, String data)
134         throws PortletDataException {
135 
136         try {
137             Document doc = SAXReaderUtil.read(data);
138 
139             Element root = doc.getRootElement();
140 
141             List<Element> folderEls = root.element("folders").elements(
142                 "folder");
143 
144             Map<Long, Long> folderPKs =
145                 (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFolder.class);
146 
147             for (Element folderEl : folderEls) {
148                 String path = folderEl.attributeValue("path");
149 
150                 if (!context.isPathNotProcessed(path)) {
151                     continue;
152                 }
153 
154                 DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
155 
156                 DLPortletDataHandlerImpl.importFolder(
157                     context, folderPKs, folder);
158             }
159 
160             List<Element> fileEntryEls = root.element("file-entries").elements(
161                 "file-entry");
162 
163             Map<String, String> fileEntryNames =
164                 (Map<String, String>)context.getNewPrimaryKeysMap(
165                     DLFileEntry.class);
166 
167             for (Element fileEntryEl : fileEntryEls) {
168                 String path = fileEntryEl.attributeValue("path");
169 
170                 if (!context.isPathNotProcessed(path)) {
171                     continue;
172                 }
173 
174                 DLFileEntry fileEntry =
175                     (DLFileEntry)context.getZipEntryAsObject(path);
176 
177                 String binPath = fileEntryEl.attributeValue("bin-path");
178 
179                 DLPortletDataHandlerImpl.importFileEntry(
180                     context, folderPKs, fileEntryNames, fileEntry, binPath);
181             }
182 
183             if (context.getBooleanParameter(_NAMESPACE, "shortcuts")) {
184                 List<Element> fileShortcutEls = root.element(
185                     "file-shortcuts").elements("file-shortcut");
186 
187                 for (Element fileShortcutEl : fileShortcutEls) {
188                     String path = fileShortcutEl.attributeValue("path");
189 
190                     if (!context.isPathNotProcessed(path)) {
191                         continue;
192                     }
193 
194                     DLFileShortcut fileShortcut =
195                         (DLFileShortcut)context.getZipEntryAsObject(path);
196 
197                     DLPortletDataHandlerImpl.importFileShortcut(
198                         context, folderPKs, fileEntryNames, fileShortcut);
199                 }
200             }
201 
202             if (context.getBooleanParameter(_NAMESPACE, "ranks")) {
203                 List<Element> fileRankEls = root.element("file-ranks").elements(
204                     "file-rank");
205 
206                 for (Element fileRankEl : fileRankEls) {
207                     String path = fileRankEl.attributeValue("path");
208 
209                     if (!context.isPathNotProcessed(path)) {
210                         continue;
211                     }
212 
213                     DLFileRank fileRank =
214                         (DLFileRank)context.getZipEntryAsObject(path);
215 
216                     DLPortletDataHandlerImpl.importFileRank(
217                         context, folderPKs, fileEntryNames, fileRank);
218                 }
219             }
220 
221             long rootFolderId = GetterUtil.getLong(
222                 root.attributeValue("root-folder-id"));
223 
224             if (Validator.isNotNull(rootFolderId)) {
225                 rootFolderId = MapUtil.getLong(
226                     folderPKs, rootFolderId, rootFolderId);
227 
228                 preferences.setValue(
229                     "rootFolderId", String.valueOf(rootFolderId));
230             }
231 
232             return preferences;
233         }
234         catch (Exception e) {
235             throw new PortletDataException(e);
236         }
237     }
238 
239     private static final String _NAMESPACE = "document_library";
240 
241     private static final PortletDataHandlerBoolean _foldersAndDocuments =
242         new PortletDataHandlerBoolean(
243             _NAMESPACE, "folders-and-documents", true, true);
244 
245     private static final PortletDataHandlerBoolean _ranks =
246         new PortletDataHandlerBoolean(_NAMESPACE, "ranks");
247 
248     private static final PortletDataHandlerBoolean _shortcuts=
249         new PortletDataHandlerBoolean(_NAMESPACE, "shortcuts");
250 
251     private static final PortletDataHandlerBoolean _comments =
252         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
253 
254     private static final PortletDataHandlerBoolean _ratings =
255         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
256 
257     private static final PortletDataHandlerBoolean _tags =
258         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
259 
260 }