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.dynamicdatalists.lar;
016    
017    import com.liferay.portal.kernel.lar.DataLevel;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
020    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.MapUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
027    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetLocalServiceUtil;
028    import com.liferay.portlet.dynamicdatalists.service.permission.DDLPermission;
029    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
030    
031    import java.util.Map;
032    
033    import javax.portlet.PortletPreferences;
034    
035    /**
036     * @author Michael C. Han
037     */
038    public class DDLDisplayPortletDataHandler extends DDLPortletDataHandler {
039    
040            public DDLDisplayPortletDataHandler() {
041                    setDataLevel(DataLevel.PORTLET_INSTANCE);
042                    setDataPortletPreferences(
043                            "displayDDMTemplateId", "formDDMTemplateId", "recordSetId");
044                    setExportControls(new PortletDataHandlerControl[0]);
045            }
046    
047            @Override
048            protected PortletPreferences doDeleteData(
049                            PortletDataContext portletDataContext, String portletId,
050                            PortletPreferences portletPreferences)
051                    throws Exception {
052    
053                    if (portletPreferences == null) {
054                            return portletPreferences;
055                    }
056    
057                    portletPreferences.setValue("displayDDMTemplateId", StringPool.BLANK);
058                    portletPreferences.setValue("editable", Boolean.TRUE.toString());
059                    portletPreferences.setValue("formDDMTemplateId", StringPool.BLANK);
060                    portletPreferences.setValue("recordSetId", StringPool.BLANK);
061                    portletPreferences.setValue("spreadsheet", Boolean.FALSE.toString());
062    
063                    return portletPreferences;
064            }
065    
066            @Override
067            protected PortletPreferences doProcessExportPortletPreferences(
068                            PortletDataContext portletDataContext, String portletId,
069                            PortletPreferences portletPreferences)
070                    throws Exception {
071    
072                    portletDataContext.addPortletPermissions(DDLPermission.RESOURCE_NAME);
073    
074                    long recordSetId = GetterUtil.getLong(
075                            portletPreferences.getValue("recordSetId", null), 0);
076    
077                    if (recordSetId == 0) {
078                            if (_log.isDebugEnabled()) {
079                                    _log.debug("Unable to get record set with ID " + portletId);
080                            }
081    
082                            return portletPreferences;
083                    }
084    
085                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.fetchRecordSet(
086                            recordSetId);
087    
088                    if (recordSet == null) {
089                            return portletPreferences;
090                    }
091    
092                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
093                            portletDataContext, portletId, recordSet);
094    
095                    return portletPreferences;
096            }
097    
098            @Override
099            protected PortletPreferences doProcessImportPortletPreferences(
100                            PortletDataContext portletDataContext, String portletId,
101                            PortletPreferences portletPreferences)
102                    throws Exception {
103    
104                    portletDataContext.importPortletPermissions(
105                            DDLPermission.RESOURCE_NAME);
106    
107                    StagedModelDataHandlerUtil.importReferenceStagedModels(
108                            portletDataContext, DDLRecordSet.class);
109    
110                    long importedRecordSetId = GetterUtil.getLong(
111                            portletPreferences.getValue("recordSetId", null));
112                    long importedDisplayDDMTemplateId = GetterUtil.getLong(
113                            portletPreferences.getValue("displayDDMTemplateId", null));
114                    long importedFormDDMTemplateId = GetterUtil.getLong(
115                            portletPreferences.getValue("formDDMTemplateId", null));
116    
117                    Map<Long, Long> recordSetIds =
118                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
119                                    DDLRecordSet.class);
120    
121                    long recordSetId = MapUtil.getLong(
122                            recordSetIds, importedRecordSetId, importedRecordSetId);
123    
124                    Map<Long, Long> templateIds =
125                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
126                                    DDMTemplate.class);
127    
128                    long displayDDMTemplateId = MapUtil.getLong(
129                            templateIds, importedDisplayDDMTemplateId,
130                            importedDisplayDDMTemplateId);
131    
132                    long formDDMTemplateId = MapUtil.getLong(
133                            templateIds, importedFormDDMTemplateId, importedFormDDMTemplateId);
134    
135                    portletPreferences.setValue("recordSetId", String.valueOf(recordSetId));
136                    portletPreferences.setValue(
137                            "displayDDMTemplateId", String.valueOf(displayDDMTemplateId));
138                    portletPreferences.setValue(
139                            "formDDMTemplateId", String.valueOf(formDDMTemplateId));
140    
141                    return portletPreferences;
142            }
143    
144            private static Log _log = LogFactoryUtil.getLog(
145                    DDLDisplayPortletDataHandler.class);
146    
147    }