001    /**
002     * Copyright (c) 2000-2013 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.repository.model.FileEntry;
022    import com.liferay.portal.kernel.repository.model.Folder;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.MapUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.xml.Document;
027    import com.liferay.portal.kernel.xml.Element;
028    import com.liferay.portal.kernel.xml.SAXReaderUtil;
029    import com.liferay.portal.model.RepositoryEntry;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.portlet.documentlibrary.model.DLFolder;
032    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
033    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
034    
035    import java.util.List;
036    import java.util.Map;
037    
038    import javax.portlet.PortletPreferences;
039    
040    /**
041     * @author Raymond Aug??
042     */
043    public class DLDisplayPortletDataHandlerImpl extends BasePortletDataHandler {
044    
045            @Override
046            public String[] getDataPortletPreferences() {
047                    return new String[] {"rootFolderId"};
048            }
049    
050            @Override
051            public PortletDataHandlerControl[] getExportControls() {
052                    return new PortletDataHandlerControl[] {
053                            _foldersAndDocuments, _shortcuts, _previewsAndThumbnails, _ranks
054                    };
055            }
056    
057            @Override
058            public PortletDataHandlerControl[] getExportMetadataControls() {
059                    return new PortletDataHandlerControl[] {
060                            new PortletDataHandlerBoolean(
061                                    _NAMESPACE, "folders-and-documents", true,
062                                    DLPortletDataHandlerImpl.getMetadataControls())
063                    };
064            }
065    
066            @Override
067            public PortletDataHandlerControl[] getImportControls() {
068                    return new PortletDataHandlerControl[] {
069                            _foldersAndDocuments, _shortcuts, _previewsAndThumbnails, _ranks
070                    };
071            }
072    
073            @Override
074            public PortletDataHandlerControl[] getImportMetadataControls() {
075                    return new PortletDataHandlerControl[] {
076                            new PortletDataHandlerBoolean(
077                                    _NAMESPACE, "folders-and-documents", true,
078                                    DLPortletDataHandlerImpl.getMetadataControls())
079                    };
080            }
081    
082            @Override
083            public boolean isDataLocalized() {
084                    return _DATA_LOCALIZED;
085            }
086    
087            @Override
088            public boolean isPublishToLiveByDefault() {
089                    return PropsValues.DL_PUBLISH_TO_LIVE_BY_DEFAULT;
090            }
091    
092            @Override
093            protected PortletPreferences doDeleteData(
094                            PortletDataContext portletDataContext, String portletId,
095                            PortletPreferences portletPreferences)
096                    throws Exception {
097    
098                    if (portletPreferences == null) {
099                            return portletPreferences;
100                    }
101    
102                    portletPreferences.setValue("rootFolderId", StringPool.BLANK);
103                    portletPreferences.setValue("showFoldersSearch", StringPool.BLANK);
104                    portletPreferences.setValue("showSubfolders", StringPool.BLANK);
105                    portletPreferences.setValue("foldersPerPage", StringPool.BLANK);
106                    portletPreferences.setValue("folderColumns", StringPool.BLANK);
107                    portletPreferences.setValue("fileEntriesPerPage", StringPool.BLANK);
108                    portletPreferences.setValue("fileEntryColumns", StringPool.BLANK);
109                    portletPreferences.setValue("enable-comment-ratings", StringPool.BLANK);
110    
111                    return portletPreferences;
112            }
113    
114            @Override
115            protected String doExportData(
116                            PortletDataContext portletDataContext, String portletId,
117                            PortletPreferences portletPreferences)
118                    throws Exception {
119    
120                    portletDataContext.addPermissions(
121                            "com.liferay.portlet.documentlibrary",
122                            portletDataContext.getScopeGroupId());
123    
124                    Document document = SAXReaderUtil.createDocument();
125    
126                    Element rootElement = document.addElement(
127                            "documentlibrary-display-data");
128    
129                    rootElement.addAttribute(
130                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
131    
132                    Element fileEntryTypesElement = rootElement.addElement(
133                            "file-entry-types");
134                    Element foldersElement = rootElement.addElement("folders");
135                    Element fileEntriesElement = rootElement.addElement("file-entries");
136                    Element fileShortcutsElement = rootElement.addElement("file-shortcuts");
137                    Element fileRanksElement = rootElement.addElement("file-ranks");
138                    Element repositoriesElement = rootElement.addElement("repositories");
139                    Element repositoryEntriesElement = rootElement.addElement(
140                            "repository-entries");
141    
142                    long rootFolderId = GetterUtil.getLong(
143                            portletPreferences.getValue("rootFolderId", null));
144    
145                    if (rootFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
146                            List<Folder> folders = FolderUtil.findByRepositoryId(
147                                    portletDataContext.getScopeGroupId());
148    
149                            for (Folder folder : folders) {
150                                    DLPortletDataHandlerImpl.exportFolder(
151                                            portletDataContext, fileEntryTypesElement, foldersElement,
152                                            fileEntriesElement, fileShortcutsElement, fileRanksElement,
153                                            repositoriesElement, repositoryEntriesElement, folder,
154                                            false);
155                            }
156    
157                            List<FileEntry> fileEntries = FileEntryUtil.findByR_F(
158                                    portletDataContext.getScopeGroupId(),
159                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
160    
161                            for (FileEntry fileEntry : fileEntries) {
162                                    DLPortletDataHandlerImpl.exportFileEntry(
163                                            portletDataContext, fileEntryTypesElement, foldersElement,
164                                            fileEntriesElement, fileRanksElement, repositoriesElement,
165                                            repositoryEntriesElement, fileEntry, true);
166                            }
167                    }
168                    else {
169                            Folder folder = DLAppLocalServiceUtil.getFolder(rootFolderId);
170    
171                            rootElement.addAttribute(
172                                    "root-folder-id", String.valueOf(folder.getFolderId()));
173                            rootElement.addAttribute(
174                                    "default-repository",
175                                    String.valueOf(folder.isDefaultRepository()));
176    
177                            DLPortletDataHandlerImpl.exportFolder(
178                                    portletDataContext, fileEntryTypesElement, foldersElement,
179                                    fileEntriesElement, fileShortcutsElement, fileRanksElement,
180                                    repositoriesElement, repositoryEntriesElement, folder, true);
181                    }
182    
183                    return document.formattedString();
184            }
185    
186            @Override
187            protected PortletPreferences doImportData(
188                            PortletDataContext portletDataContext, String portletId,
189                            PortletPreferences portletPreferences, String data)
190                    throws Exception {
191    
192                    portletDataContext.importPermissions(
193                            "com.liferay.portlet.documentlibrary",
194                            portletDataContext.getSourceGroupId(),
195                            portletDataContext.getScopeGroupId());
196    
197                    Document document = SAXReaderUtil.read(data);
198    
199                    Element rootElement = document.getRootElement();
200    
201                    Element fileEntryTypesElement = rootElement.element("file-entry-types");
202    
203                    List<Element> fileEntryTypeElements = fileEntryTypesElement.elements(
204                            "file-entry-type");
205    
206                    for (Element fileEntryTypeElement : fileEntryTypeElements) {
207                            DLPortletDataHandlerImpl.importFileEntryType(
208                                    portletDataContext, fileEntryTypeElement);
209                    }
210    
211                    Element foldersElement = rootElement.element("folders");
212    
213                    List<Element> folderElements = foldersElement.elements("folder");
214    
215                    for (Element folderElement : folderElements) {
216                            DLPortletDataHandlerImpl.importFolder(
217                                    portletDataContext, folderElement);
218                    }
219    
220                    Element fileEntriesElement = rootElement.element("file-entries");
221    
222                    List<Element> fileEntryElements = fileEntriesElement.elements(
223                            "file-entry");
224    
225                    for (Element fileEntryElement : fileEntryElements) {
226                            DLPortletDataHandlerImpl.importFileEntry(
227                                    portletDataContext, fileEntryElement);
228                    }
229    
230                    if (portletDataContext.getBooleanParameter(_NAMESPACE, "shortcuts")) {
231                            List<Element> fileShortcutElements = rootElement.element(
232                                    "file-shortcuts").elements("file-shortcut");
233    
234                            for (Element fileShortcutElement : fileShortcutElements) {
235                                    DLPortletDataHandlerImpl.importFileShortcut(
236                                            portletDataContext, fileShortcutElement);
237                            }
238                    }
239    
240                    if (portletDataContext.getBooleanParameter(_NAMESPACE, "ranks")) {
241                            Element fileRanksElement = rootElement.element("file-ranks");
242    
243                            List<Element> fileRankElements = fileRanksElement.elements(
244                                    "file-rank");
245    
246                            for (Element fileRankElement : fileRankElements) {
247                                    DLPortletDataHandlerImpl.importFileRank(
248                                            portletDataContext, fileRankElement);
249                            }
250                    }
251    
252                    long rootFolderId = GetterUtil.getLong(
253                            rootElement.attributeValue("root-folder-id"));
254                    boolean defaultRepository = GetterUtil.getBoolean(
255                            rootElement.attributeValue("default-repository"), true);
256    
257                    if (rootFolderId > 0) {
258                            Map<Long, Long> folderIds = null;
259    
260                            if (defaultRepository) {
261                                    folderIds =
262                                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
263                                                    DLFolder.class);
264                            }
265                            else {
266                                    folderIds =
267                                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
268                                                    RepositoryEntry.class);
269                            }
270    
271                            rootFolderId = MapUtil.getLong(
272                                    folderIds, rootFolderId, rootFolderId);
273    
274                            portletPreferences.setValue(
275                                    "rootFolderId", String.valueOf(rootFolderId));
276                    }
277    
278                    return portletPreferences;
279            }
280    
281            private static final boolean _DATA_LOCALIZED = true;
282    
283            private static final String _NAMESPACE = "document_library";
284    
285            private static PortletDataHandlerBoolean _foldersAndDocuments =
286                    new PortletDataHandlerBoolean(
287                            _NAMESPACE, "folders-and-documents", true, true);
288            private static PortletDataHandlerBoolean _previewsAndThumbnails =
289                    new PortletDataHandlerBoolean(_NAMESPACE, "previews-and-thumbnails");
290            private static PortletDataHandlerBoolean _ranks =
291                    new PortletDataHandlerBoolean(_NAMESPACE, "ranks");
292            private static PortletDataHandlerBoolean _shortcuts=
293                    new PortletDataHandlerBoolean(_NAMESPACE, "shortcuts");
294    
295    }