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