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.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.lar.BasePortletDataHandler;
021    import com.liferay.portal.kernel.lar.PortletDataContext;
022    import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
023    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
024    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
027    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
028    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
029    import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
030    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
031    import com.liferay.portlet.bookmarks.service.persistence.BookmarksEntryActionableDynamicQuery;
032    import com.liferay.portlet.bookmarks.service.persistence.BookmarksFolderActionableDynamicQuery;
033    
034    import java.util.List;
035    
036    import javax.portlet.PortletPreferences;
037    
038    /**
039     * @author Jorge Ferrer
040     * @author Bruno Farache
041     * @author Raymond Augé
042     * @author Juan Fernández
043     * @author Mate Thurzo
044     * @author Daniel Kocsis
045     */
046    public class BookmarksPortletDataHandler extends BasePortletDataHandler {
047    
048            public static final String NAMESPACE = "bookmarks";
049    
050            public BookmarksPortletDataHandler() {
051                    setAlwaysExportable(true);
052                    setExportControls(
053                            new PortletDataHandlerBoolean(
054                                    NAMESPACE, "folders-and-entries", true, true));
055                    setExportMetadataControls(
056                            new PortletDataHandlerBoolean(
057                                    NAMESPACE, "bookmarks", true,
058                                    new PortletDataHandlerControl[] {
059                                            new PortletDataHandlerBoolean(NAMESPACE, "categories"),
060                                            new PortletDataHandlerBoolean(NAMESPACE, "ratings"),
061                                            new PortletDataHandlerBoolean(NAMESPACE, "tags")
062                                    }));
063                    setPublishToLiveByDefault(true);
064            }
065    
066            @Override
067            protected PortletPreferences doDeleteData(
068                            PortletDataContext portletDataContext, String portletId,
069                            PortletPreferences portletPreferences)
070                    throws Exception {
071    
072                    if (portletDataContext.addPrimaryKey(
073                                    BookmarksPortletDataHandler.class, "deleteData")) {
074    
075                            return portletPreferences;
076                    }
077    
078                    BookmarksFolderLocalServiceUtil.deleteFolders(
079                            portletDataContext.getScopeGroupId());
080    
081                    BookmarksEntryLocalServiceUtil.deleteEntries(
082                            portletDataContext.getScopeGroupId(),
083                            BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
084    
085                    return portletPreferences;
086            }
087    
088            @Override
089            protected String doExportData(
090                            final PortletDataContext portletDataContext, String portletId,
091                            PortletPreferences portletPreferences)
092                    throws Exception {
093    
094                    portletDataContext.addPermissions(
095                            "com.liferay.portlet.bookmarks",
096                            portletDataContext.getScopeGroupId());
097    
098                    Element rootElement = addExportDataRootElement(portletDataContext);
099    
100                    rootElement.addAttribute(
101                            "group-id", String.valueOf(portletDataContext.getScopeGroupId()));
102    
103                    ActionableDynamicQuery folderActionableDynamicQuery =
104                            new BookmarksFolderActionableDynamicQuery() {
105    
106                            @Override
107                            protected void addCriteria(DynamicQuery dynamicQuery) {
108                                    portletDataContext.addDateRangeCriteria(
109                                            dynamicQuery, "modifiedDate");
110                            }
111    
112                            @Override
113                            protected void performAction(Object object) throws PortalException {
114                                    BookmarksFolder folder = (BookmarksFolder)object;
115    
116                                    StagedModelDataHandlerUtil.exportStagedModel(
117                                            portletDataContext, folder);
118                            }
119    
120                    };
121    
122                    folderActionableDynamicQuery.setGroupId(
123                            portletDataContext.getScopeGroupId());
124    
125                    folderActionableDynamicQuery.performActions();
126    
127                    ActionableDynamicQuery entryActionableDynamicQuery =
128                            new BookmarksEntryActionableDynamicQuery() {
129    
130                            @Override
131                            protected void addCriteria(DynamicQuery dynamicQuery) {
132                                    portletDataContext.addDateRangeCriteria(
133                                            dynamicQuery, "modifiedDate");
134                            }
135    
136                            @Override
137                            protected void performAction(Object object) throws PortalException {
138                                    BookmarksEntry entry = (BookmarksEntry)object;
139    
140                                    StagedModelDataHandlerUtil.exportStagedModel(
141                                            portletDataContext, entry);
142                            }
143    
144                    };
145    
146                    entryActionableDynamicQuery.setGroupId(
147                            portletDataContext.getScopeGroupId());
148    
149                    entryActionableDynamicQuery.performActions();
150    
151                    return getExportDataRootElementString(rootElement);
152            }
153    
154            @Override
155            protected PortletPreferences doImportData(
156                            PortletDataContext portletDataContext, String portletId,
157                            PortletPreferences portletPreferences, String data)
158                    throws Exception {
159    
160                    portletDataContext.importPermissions(
161                            "com.liferay.portlet.bookmarks",
162                            portletDataContext.getSourceGroupId(),
163                            portletDataContext.getScopeGroupId());
164    
165                    Element foldersElement = portletDataContext.getImportDataGroupElement(
166                            BookmarksFolder.class);
167    
168                    List<Element> folderElements = foldersElement.elements();
169    
170                    for (Element folderElement : folderElements) {
171                            StagedModelDataHandlerUtil.importStagedModel(
172                                    portletDataContext, folderElement);
173                    }
174    
175                    Element entriesElement = portletDataContext.getImportDataGroupElement(
176                            BookmarksEntry.class);
177    
178                    List<Element> entryElements = entriesElement.elements();
179    
180                    for (Element entryElement : entryElements) {
181                            StagedModelDataHandlerUtil.importStagedModel(
182                                    portletDataContext, entryElement);
183                    }
184    
185                    return null;
186            }
187    
188    }