001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.lar;
016    
017    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
020    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.MapUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.xml.Document;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.kernel.xml.SAXReaderUtil;
027    import com.liferay.portlet.documentlibrary.model.DLFolder;
028    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
029    import com.liferay.portlet.documentlibrary.service.persistence.DLFolderUtil;
030    
031    import java.util.List;
032    import java.util.Map;
033    
034    import javax.portlet.PortletPreferences;
035    
036    /**
037     * @author Raymond Augé
038     */
039    public class DLDisplayPortletDataHandlerImpl extends BasePortletDataHandler {
040    
041            public PortletDataHandlerControl[] getExportControls() {
042                    return new PortletDataHandlerControl[] {
043                            _foldersAndDocuments, _shortcuts, _ranks, _comments, _ratings, _tags
044                    };
045            }
046    
047            public PortletDataHandlerControl[] getImportControls() {
048                    return new PortletDataHandlerControl[] {
049                            _foldersAndDocuments, _shortcuts, _ranks, _comments, _ratings, _tags
050                    };
051            }
052    
053            public boolean isPublishToLiveByDefault() {
054                    return _PUBLISH_TO_LIVE_BY_DEFAULT;
055            }
056    
057            protected PortletPreferences doDeleteData(
058                            PortletDataContext context, String portletId,
059                            PortletPreferences preferences)
060                    throws Exception {
061    
062                    preferences.setValue("rootFolderId", StringPool.BLANK);
063                    preferences.setValue("showBreadcrumbs", StringPool.BLANK);
064                    preferences.setValue("showFoldersSearch", StringPool.BLANK);
065                    preferences.setValue("showSubfolders", StringPool.BLANK);
066                    preferences.setValue("foldersPerPage", StringPool.BLANK);
067                    preferences.setValue("folderColumns", StringPool.BLANK);
068                    preferences.setValue("showFileEntriesSearch", StringPool.BLANK);
069                    preferences.setValue("fileEntriesPerPage", StringPool.BLANK);
070                    preferences.setValue("fileEntryColumns", StringPool.BLANK);
071                    preferences.setValue("enable-comment-ratings", StringPool.BLANK);
072    
073                    return preferences;
074            }
075    
076            protected String doExportData(
077                            PortletDataContext context, String portletId,
078                            PortletPreferences preferences)
079                    throws Exception {
080    
081                    context.addPermissions(
082                            "com.liferay.portlet.documentlibrary", context.getScopeGroupId());
083    
084                    long rootFolderId = GetterUtil.getLong(
085                            preferences.getValue("rootFolderId", null));
086    
087                    Document document = SAXReaderUtil.createDocument();
088    
089                    Element rootElement = document.addElement(
090                            "documentlibrary-display-data");
091    
092                    Element foldersElement = rootElement.addElement("folders");
093                    Element fileEntriesElement = rootElement.addElement("file-entries");
094                    Element fileShortcutsElement = rootElement.addElement("file-shortcuts");
095                    Element fileRanksElement = rootElement.addElement("file-ranks");
096    
097                    if (rootFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
098                            List<DLFolder> folders = DLFolderUtil.findByGroupId(
099                                    context.getScopeGroupId());
100    
101                            for (DLFolder folder : folders) {
102                                    DLPortletDataHandlerImpl.exportFolder(
103                                            context, foldersElement, fileEntriesElement,
104                                            fileShortcutsElement, fileRanksElement, folder);
105                            }
106                    }
107                    else {
108                            DLFolder folder = DLFolderUtil.findByPrimaryKey(rootFolderId);
109    
110                            rootElement.addAttribute(
111                                    "root-folder-id", String.valueOf(folder.getFolderId()));
112    
113                            DLPortletDataHandlerImpl.exportFolder(
114                                    context, foldersElement, fileEntriesElement,
115                                    fileShortcutsElement, fileRanksElement, folder);
116                    }
117    
118                    return document.formattedString();
119            }
120    
121            protected PortletPreferences doImportData(
122                            PortletDataContext context, String portletId,
123                            PortletPreferences preferences, String data)
124                    throws Exception {
125    
126                    context.importPermissions(
127                            "com.liferay.portlet.documentlibrary",
128                            context.getSourceGroupId(), context.getScopeGroupId());
129    
130                    Document document = SAXReaderUtil.read(data);
131    
132                    Element rootElement = document.getRootElement();
133    
134                    Element foldersElement = rootElement.element("folders");
135    
136                    List<Element> folderElements = foldersElement.elements("folder");
137    
138                    for (Element folderElement : folderElements) {
139                            DLPortletDataHandlerImpl.importFolder(context, folderElement);
140                    }
141    
142                    Element fileEntriesElement = rootElement.element("file-entries");
143    
144                    List<Element> fileEntryElements = fileEntriesElement.elements(
145                            "file-entry");
146    
147                    for (Element fileEntryElement : fileEntryElements) {
148                            DLPortletDataHandlerImpl.importFileEntry(context, fileEntryElement);
149                    }
150    
151                    if (context.getBooleanParameter(_NAMESPACE, "shortcuts")) {
152                            List<Element> fileShortcutElements = rootElement.element(
153                                    "file-shortcuts").elements("file-shortcut");
154    
155                            for (Element fileShortcutElement : fileShortcutElements) {
156                                    DLPortletDataHandlerImpl.importFileShortcut(
157                                            context, fileShortcutElement);
158                            }
159                    }
160    
161                    if (context.getBooleanParameter(_NAMESPACE, "ranks")) {
162                            Element fileRanksElement = rootElement.element("file-ranks");
163    
164                            List<Element> fileRankElements = fileRanksElement.elements(
165                                    "file-rank");
166    
167                            for (Element fileRankElement : fileRankElements) {
168                                    DLPortletDataHandlerImpl.importFileRank(
169                                            context, fileRankElement);
170                            }
171                    }
172    
173                    long rootFolderId = GetterUtil.getLong(
174                            rootElement.attributeValue("root-folder-id"));
175    
176                    if (rootFolderId > 0) {
177                            Map<Long, Long> folderPKs =
178                                    (Map<Long, Long>)context.getNewPrimaryKeysMap(DLFolder.class);
179    
180                            rootFolderId = MapUtil.getLong(
181                                    folderPKs, rootFolderId, rootFolderId);
182    
183                            preferences.setValue("rootFolderId", String.valueOf(rootFolderId));
184                    }
185    
186                    return preferences;
187            }
188    
189            private static final String _NAMESPACE = "document_library";
190    
191            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = true;
192    
193            private static PortletDataHandlerBoolean _comments =
194                    new PortletDataHandlerBoolean(_NAMESPACE, "comments");
195    
196            private static PortletDataHandlerBoolean _foldersAndDocuments =
197                    new PortletDataHandlerBoolean(
198                            _NAMESPACE, "folders-and-documents", true, true);
199    
200            private static PortletDataHandlerBoolean _ranks =
201                    new PortletDataHandlerBoolean(_NAMESPACE, "ranks");
202    
203            private static PortletDataHandlerBoolean _ratings =
204                    new PortletDataHandlerBoolean(_NAMESPACE, "ratings");
205    
206            private static PortletDataHandlerBoolean _shortcuts=
207                    new PortletDataHandlerBoolean(_NAMESPACE, "shortcuts");
208    
209            private static PortletDataHandlerBoolean _tags =
210                    new PortletDataHandlerBoolean(_NAMESPACE, "tags");
211    
212    }