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.bookmarks.lar;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
019    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
020    import com.liferay.portal.kernel.lar.PortletDataContext;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
022    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
023    import com.liferay.portal.kernel.lar.StagedModelType;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.MapUtil;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.model.Portlet;
028    import com.liferay.portal.service.PortletLocalServiceUtil;
029    import com.liferay.portal.util.PropsValues;
030    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
031    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
032    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
033    import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
034    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
035    import com.liferay.portlet.bookmarks.service.permission.BookmarksPermission;
036    import com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryExportActionableDynamicQuery;
037    import com.liferay.portlet.bookmarks.service.persistence.BookmarksFolderExportActionableDynamicQuery;
038    
039    import java.util.List;
040    import java.util.Map;
041    
042    import javax.portlet.PortletPreferences;
043    
044    /**
045     * @author Jorge Ferrer
046     * @author Bruno Farache
047     * @author Raymond Aug??
048     * @author Juan Fern??ndez
049     * @author Mate Thurzo
050     * @author Daniel Kocsis
051     */
052    public class BookmarksPortletDataHandler extends BasePortletDataHandler {
053    
054            public static final String NAMESPACE = "bookmarks";
055    
056            public BookmarksPortletDataHandler() {
057                    setDataPortletPreferences("rootFolderId");
058                    setDeletionSystemEventStagedModelTypes(
059                            new StagedModelType(BookmarksEntry.class),
060                            new StagedModelType(BookmarksFolder.class));
061                    setExportControls(
062                            new PortletDataHandlerBoolean(
063                                    NAMESPACE, "entries", true, false, null,
064                                    BookmarksEntry.class.getName()));
065                    setImportControls(getExportControls());
066                    setPublishToLiveByDefault(
067                            PropsValues.BOOKMARKS_PUBLISH_TO_LIVE_BY_DEFAULT);
068            }
069    
070            @Override
071            protected PortletPreferences doDeleteData(
072                            PortletDataContext portletDataContext, String portletId,
073                            PortletPreferences portletPreferences)
074                    throws Exception {
075    
076                    if (portletDataContext.addPrimaryKey(
077                                    BookmarksPortletDataHandler.class, "deleteData")) {
078    
079                            return portletPreferences;
080                    }
081    
082                    BookmarksFolderLocalServiceUtil.deleteFolders(
083                            portletDataContext.getScopeGroupId());
084    
085                    BookmarksEntryLocalServiceUtil.deleteEntries(
086                            portletDataContext.getScopeGroupId(),
087                            BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
088    
089                    return portletPreferences;
090            }
091    
092            @Override
093            protected String doExportData(
094                            final PortletDataContext portletDataContext, String portletId,
095                            PortletPreferences portletPreferences)
096                    throws Exception {
097    
098                    Element rootElement = addExportDataRootElement(portletDataContext);
099    
100                    if (!portletDataContext.getBooleanParameter(NAMESPACE, "entries")) {
101                            return getExportDataRootElementString(rootElement);
102                    }
103    
104                    portletDataContext.addPermissions(
105                            BookmarksPermission.RESOURCE_NAME,
106                            portletDataContext.getScopeGroupId());
107    
108                    rootElement.addAttribute(
109                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
110    
111                    ActionableDynamicQuery folderActionableDynamicQuery =
112                            new BookmarksFolderExportActionableDynamicQuery(portletDataContext);
113    
114                    folderActionableDynamicQuery.performActions();
115    
116                    ActionableDynamicQuery entryActionableDynamicQuery =
117                            new BookmarksEntryExportActionableDynamicQuery(portletDataContext);
118    
119                    entryActionableDynamicQuery.performActions();
120    
121                    return getExportDataRootElementString(rootElement);
122            }
123    
124            @Override
125            protected PortletPreferences doImportData(
126                            PortletDataContext portletDataContext, String portletId,
127                            PortletPreferences portletPreferences, String data)
128                    throws Exception {
129    
130                    if (!portletDataContext.getBooleanParameter(NAMESPACE, "entries")) {
131                            return null;
132                    }
133    
134                    portletDataContext.importPermissions(
135                            BookmarksPermission.RESOURCE_NAME,
136                            portletDataContext.getSourceGroupId(),
137                            portletDataContext.getScopeGroupId());
138    
139                    Element foldersElement = portletDataContext.getImportDataGroupElement(
140                            BookmarksFolder.class);
141    
142                    List<Element> folderElements = foldersElement.elements();
143    
144                    for (Element folderElement : folderElements) {
145                            StagedModelDataHandlerUtil.importStagedModel(
146                                    portletDataContext, folderElement);
147                    }
148    
149                    Element entriesElement = portletDataContext.getImportDataGroupElement(
150                            BookmarksEntry.class);
151    
152                    List<Element> entryElements = entriesElement.elements();
153    
154                    for (Element entryElement : entryElements) {
155                            StagedModelDataHandlerUtil.importStagedModel(
156                                    portletDataContext, entryElement);
157                    }
158    
159                    return null;
160            }
161    
162            @Override
163            protected void doPrepareManifestSummary(
164                            PortletDataContext portletDataContext,
165                            PortletPreferences portletPreferences)
166                    throws Exception {
167    
168                    ActionableDynamicQuery entryExportActionableDynamicQuery =
169                            new BookmarksEntryExportActionableDynamicQuery(portletDataContext);
170    
171                    entryExportActionableDynamicQuery.performCount();
172    
173                    ActionableDynamicQuery folderExportActionableDynamicQuery =
174                            new BookmarksFolderExportActionableDynamicQuery(portletDataContext);
175    
176                    folderExportActionableDynamicQuery.performCount();
177            }
178    
179            @Override
180            protected PortletPreferences doProcessExportPortletPreferences(
181                            PortletDataContext portletDataContext, String portletId,
182                            PortletPreferences portletPreferences, Element rootElement)
183                    throws Exception {
184    
185                    long rootFolderId = GetterUtil.getLong(
186                            portletPreferences.getValue("rootFolderId", null));
187    
188                    if (rootFolderId != BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
189                            BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(
190                                    rootFolderId);
191    
192                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
193                                    portletDataContext.getCompanyId(), portletId);
194    
195                            portletDataContext.addReferenceElement(
196                                    portlet, rootElement, folder, BookmarksFolder.class,
197                                    PortletDataContext.REFERENCE_TYPE_DEPENDENCY,
198                                    !portletDataContext.getBooleanParameter(NAMESPACE, "entries"));
199                    }
200    
201                    return portletPreferences;
202            }
203    
204            @Override
205            protected PortletPreferences doProcessImportPortletPreferences(
206                            PortletDataContext portletDataContext, String portletId,
207                            PortletPreferences portletPreferences)
208                    throws Exception {
209    
210                    long rootFolderId = GetterUtil.getLong(
211                            portletPreferences.getValue("rootFolderId", null));
212    
213                    if (rootFolderId > 0) {
214                            String rootFolderPath = ExportImportPathUtil.getModelPath(
215                                    portletDataContext, BookmarksFolder.class.getName(),
216                                    rootFolderId);
217    
218                            BookmarksFolder folder =
219                                    (BookmarksFolder)portletDataContext.getZipEntryAsObject(
220                                            rootFolderPath);
221    
222                            StagedModelDataHandlerUtil.importStagedModel(
223                                    portletDataContext, folder);
224    
225                            Map<Long, Long> folderIds =
226                                    (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
227                                            BookmarksFolder.class);
228    
229                            rootFolderId = MapUtil.getLong(
230                                    folderIds, rootFolderId, rootFolderId);
231    
232                            portletPreferences.setValue(
233                                    "rootFolderId", String.valueOf(rootFolderId));
234                    }
235    
236                    return portletPreferences;
237            }
238    
239    }