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