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.blogsaggregator.lar;
016    
017    import com.liferay.portal.kernel.lar.DataLevel;
018    import com.liferay.portal.kernel.lar.DefaultConfigurationPortletDataHandler;
019    import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
020    import com.liferay.portal.kernel.lar.PortletDataContext;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.model.Company;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.Organization;
026    import com.liferay.portal.model.Portlet;
027    import com.liferay.portal.service.CompanyLocalServiceUtil;
028    import com.liferay.portal.service.PortletLocalServiceUtil;
029    
030    import javax.portlet.PortletPreferences;
031    
032    /**
033     * @author Julio Camarero
034     */
035    public class BlogsAggregatorPortletDataHandler
036            extends DefaultConfigurationPortletDataHandler {
037    
038            public BlogsAggregatorPortletDataHandler() {
039                    setDataLevel(DataLevel.PORTLET_INSTANCE);
040                    setPublishToLiveByDefault(true);
041            }
042    
043            @Override
044            protected PortletPreferences doProcessExportPortletPreferences(
045                            PortletDataContext portletDataContext, String portletId,
046                            PortletPreferences portletPreferences, Element rootElement)
047                    throws Exception {
048    
049                    return updateExportPortletPreferences(
050                            portletDataContext, portletId, portletPreferences, rootElement);
051            }
052    
053            @Override
054            protected PortletPreferences doProcessImportPortletPreferences(
055                            PortletDataContext portletDataContext, String portletId,
056                            PortletPreferences portletPreferences)
057                    throws Exception {
058    
059                    return updateImportPortletPreferences(
060                            portletDataContext, portletId, portletPreferences);
061            }
062    
063            protected PortletPreferences updateExportPortletPreferences(
064                            PortletDataContext portletDataContext, String portletId,
065                            PortletPreferences portletPreferences, Element rootElement)
066                    throws Exception {
067    
068                    long organizationId = GetterUtil.getLong(
069                            portletPreferences.getValue("organizationId", null));
070    
071                    if (organizationId > 0) {
072                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
073                                    portletDataContext.getCompanyId(), portletId);
074    
075                            ExportImportHelperUtil.updateExportPortletPreferencesClassPKs(
076                                    portletDataContext, portlet, portletPreferences,
077                                    "organizationId", Organization.class.getName(), rootElement);
078                    }
079    
080                    return portletPreferences;
081            }
082    
083            protected PortletPreferences updateImportPortletPreferences(
084                            PortletDataContext portletDataContext, String portletId,
085                            PortletPreferences portletPreferences)
086                    throws Exception {
087    
088                    long organizationId = GetterUtil.getLong(
089                            portletPreferences.getValue("organizationId", null));
090    
091                    if (organizationId > 0) {
092                            Company company = CompanyLocalServiceUtil.getCompanyById(
093                                    portletDataContext.getCompanyId());
094    
095                            Group companyGroup = company.getGroup();
096    
097                            ExportImportHelperUtil.updateImportPortletPreferencesClassPKs(
098                                    portletDataContext, portletPreferences, "organizationId",
099                                    Organization.class, companyGroup.getGroupId());
100                    }
101    
102                    return portletPreferences;
103            }
104    
105    }