1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.documentlibrary.lar;
21  
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.kernel.xml.Document;
26  import com.liferay.portal.kernel.xml.Element;
27  import com.liferay.portal.kernel.xml.SAXReaderUtil;
28  import com.liferay.portal.lar.PortletDataContext;
29  import com.liferay.portal.lar.PortletDataException;
30  import com.liferay.portal.lar.PortletDataHandler;
31  import com.liferay.portal.lar.PortletDataHandlerBoolean;
32  import com.liferay.portal.lar.PortletDataHandlerControl;
33  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
34  import com.liferay.portlet.documentlibrary.model.DLFileRank;
35  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
36  import com.liferay.portlet.documentlibrary.model.DLFolder;
37  import com.liferay.portlet.documentlibrary.service.persistence.DLFolderUtil;
38  import com.liferay.util.MapUtil;
39  
40  import java.util.List;
41  import java.util.Map;
42  
43  import javax.portlet.PortletPreferences;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  /**
49   * <a href="DLDisplayPortletDataHandlerImpl.java.html"><b><i>View Source</i></b>
50   * </a>
51   *
52   * @author Raymond Augé
53   *
54   */
55  public class DLDisplayPortletDataHandlerImpl implements PortletDataHandler {
56  
57      public PortletPreferences deleteData(
58              PortletDataContext context, String portletId,
59              PortletPreferences preferences)
60          throws PortletDataException {
61  
62          try {
63              preferences.setValue("rootFolderId", StringPool.BLANK);
64              preferences.setValue("showBreadcrumbs", StringPool.BLANK);
65              preferences.setValue("showFoldersSearch", StringPool.BLANK);
66              preferences.setValue("showSubfolders", StringPool.BLANK);
67              preferences.setValue("foldersPerPage", StringPool.BLANK);
68              preferences.setValue("folderColumns", StringPool.BLANK);
69              preferences.setValue("showFileEntriesSearch", StringPool.BLANK);
70              preferences.setValue("fileEntriesPerPage", StringPool.BLANK);
71              preferences.setValue("fileEntryColumns", StringPool.BLANK);
72              preferences.setValue("enable-comment-ratings", StringPool.BLANK);
73  
74              return preferences;
75          }
76          catch (Exception e) {
77              throw new PortletDataException(e);
78          }
79      }
80  
81      public String exportData(
82              PortletDataContext context, String portletId,
83              PortletPreferences preferences)
84          throws PortletDataException {
85  
86          try {
87              long rootFolderId = GetterUtil.getLong(
88                  preferences.getValue("rootFolderId", null));
89  
90              Document doc = SAXReaderUtil.createDocument();
91  
92              Element root = doc.addElement("documentlibrary-display-data");
93  
94              Element foldersEl = root.addElement("folders");
95              Element fileEntriesEl = root.addElement("file-entries");
96              Element fileShortcutsEl = root.addElement("file-shortcuts");
97              Element fileRanksEl = root.addElement("file-ranks");
98  
99              DLFolder folder = DLFolderUtil.findByPrimaryKey(rootFolderId);
100 
101             root.addAttribute(
102                 "root-folder-id", String.valueOf(folder.getFolderId()));
103 
104             DLPortletDataHandlerImpl.exportFolder(
105                 context, foldersEl, fileEntriesEl, fileShortcutsEl,
106                 fileRanksEl, folder);
107 
108             return doc.formattedString();
109         }
110         catch (Exception e) {
111             throw new PortletDataException(e);
112         }
113     }
114 
115     public PortletDataHandlerControl[] getExportControls() {
116         return new PortletDataHandlerControl[] {
117             _foldersAndDocuments, _shortcuts, _ranks, _comments, _ratings, _tags
118         };
119     }
120 
121     public PortletDataHandlerControl[] getImportControls() {
122         return new PortletDataHandlerControl[] {
123             _foldersAndDocuments, _shortcuts, _ranks, _comments, _ratings, _tags
124         };
125     }
126 
127     public PortletPreferences importData(
128             PortletDataContext context, String portletId,
129             PortletPreferences preferences, String data)
130         throws PortletDataException {
131 
132         try {
133             Document doc = SAXReaderUtil.read(data);
134 
135             Element root = doc.getRootElement();
136 
137             List<Element> folderEls = root.element("folders").elements(
138                 "folder");
139 
140             Map<Long, Long> folderPKs =
141                 (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFolder.class);
142 
143             for (Element folderEl : folderEls) {
144                 String path = folderEl.attributeValue("path");
145 
146                 if (!context.isPathNotProcessed(path)) {
147                     continue;
148                 }
149 
150                 DLFolder folder = (DLFolder)context.getZipEntryAsObject(path);
151 
152                 DLPortletDataHandlerImpl.importFolder(
153                     context, folderPKs, folder);
154             }
155 
156             List<Element> fileEntryEls = root.element("file-entries").elements(
157                 "file-entry");
158 
159             Map<String, String> fileEntryNames =
160                 (Map<String, String>)context.getNewPrimaryKeysMap(
161                     DLFileEntry.class);
162 
163             for (Element fileEntryEl : fileEntryEls) {
164                 String path = fileEntryEl.attributeValue("path");
165 
166                 if (!context.isPathNotProcessed(path)) {
167                     continue;
168                 }
169 
170                 DLFileEntry fileEntry =
171                     (DLFileEntry)context.getZipEntryAsObject(path);
172 
173                 String binPath = fileEntryEl.attributeValue("bin-path");
174 
175                 DLPortletDataHandlerImpl.importFileEntry(
176                     context, folderPKs, fileEntryNames, fileEntry, binPath);
177             }
178 
179             if (context.getBooleanParameter(_NAMESPACE, "shortcuts")) {
180                 List<Element> fileShortcutEls = root.element(
181                     "file-shortcuts").elements("file-shortcut");
182 
183                 for (Element fileShortcutEl : fileShortcutEls) {
184                     String path = fileShortcutEl.attributeValue("path");
185 
186                     if (!context.isPathNotProcessed(path)) {
187                         continue;
188                     }
189 
190                     DLFileShortcut fileShortcut =
191                         (DLFileShortcut)context.getZipEntryAsObject(path);
192 
193                     DLPortletDataHandlerImpl.importFileShortcut(
194                         context, folderPKs, fileEntryNames, fileShortcut);
195                 }
196             }
197 
198             if (context.getBooleanParameter(_NAMESPACE, "ranks")) {
199                 List<Element> fileRankEls = root.element("file-ranks").elements(
200                     "file-rank");
201 
202                 for (Element fileRankEl : fileRankEls) {
203                     String path = fileRankEl.attributeValue("path");
204 
205                     if (!context.isPathNotProcessed(path)) {
206                         continue;
207                     }
208 
209                     DLFileRank fileRank =
210                         (DLFileRank)context.getZipEntryAsObject(path);
211 
212                     DLPortletDataHandlerImpl.importFileRank(
213                         context, folderPKs, fileEntryNames, fileRank);
214                 }
215             }
216 
217             long rootFolderId = GetterUtil.getLong(
218                 root.attributeValue("root-folder-id"));
219 
220             if (Validator.isNotNull(rootFolderId)) {
221                 rootFolderId = MapUtil.getLong(
222                     folderPKs, rootFolderId, rootFolderId);
223 
224                 preferences.setValue(
225                     "rootFolderId", String.valueOf(rootFolderId));
226             }
227 
228             return preferences;
229         }
230         catch (Exception e) {
231             throw new PortletDataException(e);
232         }
233     }
234 
235     public boolean isPublishToLiveByDefault() {
236         return false;
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     private static Log _log =
261         LogFactory.getLog(DLDisplayPortletDataHandlerImpl.class);
262 
263 }