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.assetcategoriesnavigation.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.model.Company;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.model.Portlet;
024    import com.liferay.portal.service.CompanyLocalServiceUtil;
025    import com.liferay.portal.service.PortletLocalServiceUtil;
026    import com.liferay.portlet.asset.model.AssetVocabulary;
027    
028    import java.util.Enumeration;
029    
030    import javax.portlet.PortletPreferences;
031    
032    /**
033     * @author Julio Camarero
034     */
035    public class AssetCategoriesNavigationPortletDataHandler
036            extends DefaultConfigurationPortletDataHandler {
037    
038            public AssetCategoriesNavigationPortletDataHandler() {
039                    setDataLevel(DataLevel.PORTLET_INSTANCE);
040                    setPublishToLiveByDefault(true);
041            }
042    
043            @Override
044            protected PortletPreferences doProcessExportPortletPreferences(
045                            PortletDataContext portletDataContext, String portletId,
046                            PortletPreferences portletPreferences)
047                    throws Exception {
048    
049                    return updateExportPortletPreferences(
050                            portletDataContext, portletPreferences, portletId);
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,
065                            PortletPreferences portletPreferences, String portletId)
066                    throws Exception {
067    
068                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
069                            portletDataContext.getCompanyId(), portletId);
070    
071                    Enumeration<String> enu = portletPreferences.getNames();
072    
073                    while (enu.hasMoreElements()) {
074                            String name = enu.nextElement();
075    
076                            if (name.equals("assetVocabularyIds")) {
077                                    ExportImportHelperUtil.updateExportPortletPreferencesClassPKs(
078                                            portletDataContext, portlet, portletPreferences, name,
079                                            AssetVocabulary.class.getName(),
080                                            portletDataContext.getExportDataRootElement());
081                            }
082                    }
083    
084                    return portletPreferences;
085            }
086    
087            protected PortletPreferences updateImportPortletPreferences(
088                            PortletDataContext portletDataContext, String portletId,
089                            PortletPreferences portletPreferences)
090                    throws Exception {
091    
092                    Company company = CompanyLocalServiceUtil.getCompanyById(
093                            portletDataContext.getCompanyId());
094    
095                    Group companyGroup = company.getGroup();
096    
097                    Enumeration<String> enu = portletPreferences.getNames();
098    
099                    while (enu.hasMoreElements()) {
100                            String name = enu.nextElement();
101    
102                            if (name.equals("assetVocabularyIds")) {
103                                    ExportImportHelperUtil.updateImportPortletPreferencesClassPKs(
104                                            portletDataContext, portletPreferences, name,
105                                            AssetVocabulary.class, companyGroup.getGroupId());
106                            }
107                    }
108    
109                    return portletPreferences;
110            }
111    
112    }