1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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              context.addPermissions(
80                  "com.liferay.portlet.documentlibrary",
81                  context.getScopeGroupId());
82  
83              long rootFolderId = GetterUtil.getLong(
84                  preferences.getValue("rootFolderId", null));
85  
86              Document doc = SAXReaderUtil.createDocument();
87  
88              Element root = doc.addElement("documentlibrary-display-data");
89  
90              Element foldersEl = root.addElement("folders");
91              Element fileEntriesEl = root.addElement("file-entries");
92              Element fileShortcutsEl = root.addElement("file-shortcuts");
93              Element fileRanksEl = root.addElement("file-ranks");
94  
95              if (rootFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
96                  List<DLFolder> folders = DLFolderUtil.findByGroupId(
97                      context.getScopeGroupId());
98  
99                  for (DLFolder folder : folders) {
100                     DLPortletDataHandlerImpl.exportFolder(
101                         context, foldersEl, fileEntriesEl, fileShortcutsEl,
102                         fileRanksEl, folder);
103                 }
104             }
105             else {
106                 DLFolder folder = DLFolderUtil.findByPrimaryKey(rootFolderId);
107 
108                 root.addAttribute(
109                     "root-folder-id", String.valueOf(folder.getFolderId()));
110 
111                 DLPortletDataHandlerImpl.exportFolder(
112                     context, foldersEl, fileEntriesEl, fileShortcutsEl,
113                     fileRanksEl, folder);
114             }
115 
116             return doc.formattedString();
117         }
118         catch (Exception e) {
119             throw new PortletDataException(e);
120         }
121     }
122 
123     public PortletDataHandlerControl[] getExportControls() {
124         return new PortletDataHandlerControl[] {
125             _foldersAndDocuments, _shortcuts, _ranks, _comments, _ratings, _tags
126         };
127     }
128 
129     public PortletDataHandlerControl[] getImportControls() {
130         return new PortletDataHandlerControl[] {
131             _foldersAndDocuments, _shortcuts, _ranks, _comments, _ratings, _tags
132         };
133     }
134 
135     public PortletPreferences importData(
136             PortletDataContext context, String portletId,
137             PortletPreferences preferences, String data)
138         throws PortletDataException {
139 
140         try {
141             context.importPermissions(
142                 "com.liferay.portlet.documentlibrary",
143                 context.getSourceGroupId(), context.getScopeGroupId());
144 
145             Document doc = SAXReaderUtil.read(data);
146 
147             Element root = doc.getRootElement();
148 
149             List<Element> folderEls = root.element("folders").elements(
150                 "folder");
151 
152             Map<Long, Long> folderPKs =
153                 (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFolder.class);
154 
155             for (Element folderEl : folderEls) {
156                 String path = folderEl.attributeValue("path");
157 
158                 if (!context.isPathNotProcessed(path)) {
159                     continue;
160                 }
161 
162                 DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
163 
164                 DLPortletDataHandlerImpl.importFolder(
165                     context, folderPKs, folder);
166             }
167 
168             List<Element> fileEntryEls = root.element("file-entries").elements(
169                 "file-entry");
170 
171             Map<String, String> fileEntryNames =
172                 (Map<String, String>)context.getNewPrimaryKeysMap(
173                     DLFileEntry.class);
174 
175             for (Element fileEntryEl : fileEntryEls) {
176                 String path = fileEntryEl.attributeValue("path");
177 
178                 if (!context.isPathNotProcessed(path)) {
179                     continue;
180                 }
181 
182                 DLFileEntry fileEntry =
183                     (DLFileEntry)context.getZipEntryAsObject(path);
184 
185                 String binPath = fileEntryEl.attributeValue("bin-path");
186 
187                 DLPortletDataHandlerImpl.importFileEntry(
188                     context, folderPKs, fileEntryNames, fileEntry, binPath);
189             }
190 
191             if (context.getBooleanParameter(_NAMESPACE, "shortcuts")) {
192                 List<Element> fileShortcutEls = root.element(
193                     "file-shortcuts").elements("file-shortcut");
194 
195                 for (Element fileShortcutEl : fileShortcutEls) {
196                     String path = fileShortcutEl.attributeValue("path");
197 
198                     if (!context.isPathNotProcessed(path)) {
199                         continue;
200                     }
201 
202                     DLFileShortcut fileShortcut =
203                         (DLFileShortcut)context.getZipEntryAsObject(path);
204 
205                     DLPortletDataHandlerImpl.importFileShortcut(
206                         context, folderPKs, fileEntryNames, fileShortcut);
207                 }
208             }
209 
210             if (context.getBooleanParameter(_NAMESPACE, "ranks")) {
211                 List<Element> fileRankEls = root.element("file-ranks").elements(
212                     "file-rank");
213 
214                 for (Element fileRankEl : fileRankEls) {
215                     String path = fileRankEl.attributeValue("path");
216 
217                     if (!context.isPathNotProcessed(path)) {
218                         continue;
219                     }
220 
221                     DLFileRank fileRank =
222                         (DLFileRank)context.getZipEntryAsObject(path);
223 
224                     DLPortletDataHandlerImpl.importFileRank(
225                         context, folderPKs, fileEntryNames, fileRank);
226                 }
227             }
228 
229             long rootFolderId = GetterUtil.getLong(
230                 root.attributeValue("root-folder-id"));
231 
232             if (Validator.isNotNull(rootFolderId)) {
233                 rootFolderId = MapUtil.getLong(
234                     folderPKs, rootFolderId, rootFolderId);
235 
236                 preferences.setValue(
237                     "rootFolderId", String.valueOf(rootFolderId));
238             }
239 
240             return preferences;
241         }
242         catch (Exception e) {
243             throw new PortletDataException(e);
244         }
245     }
246 
247     private static final String _NAMESPACE = "document_library";
248 
249     private static final PortletDataHandlerBoolean _foldersAndDocuments =
250         new PortletDataHandlerBoolean(
251             _NAMESPACE, "folders-and-documents", true, true);
252 
253     private static final PortletDataHandlerBoolean _ranks =
254         new PortletDataHandlerBoolean(_NAMESPACE, "ranks");
255 
256     private static final PortletDataHandlerBoolean _shortcuts=
257         new PortletDataHandlerBoolean(_NAMESPACE, "shortcuts");
258 
259     private static final PortletDataHandlerBoolean _comments =
260         new PortletDataHandlerBoolean(_NAMESPACE, "comments");
261 
262     private static final PortletDataHandlerBoolean _ratings =
263         new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
264 
265     private static final PortletDataHandlerBoolean _tags =
266         new PortletDataHandlerBoolean(_NAMESPACE, "tags");
267 
268 }