001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
031    
032    import java.util.Map;
033    
034    import javax.portlet.PortletPreferences;
035    
036    /**
037     * @author Michael C. Han
038     */
039    public class DDLDisplayPortletDataHandler extends DDLPortletDataHandler {
040    
041            public DDLDisplayPortletDataHandler() {
042                    setDataLevel(DataLevel.PORTLET_INSTANCE);
043                    setDataPortletPreferences(
044                            "displayDDMTemplateId", "formDDMTemplateId", "recordSetId");
045                    setExportControls(new PortletDataHandlerControl[0]);
046            }
047    
048            @Override
049            protected PortletPreferences doDeleteData(
050                            PortletDataContext portletDataContext, String portletId,
051                            PortletPreferences portletPreferences)
052                    throws Exception {
053    
054                    if (portletPreferences == null) {
055                            return portletPreferences;
056                    }
057    
058                    portletPreferences.setValue("displayDDMTemplateId", StringPool.BLANK);
059                    portletPreferences.setValue("editable", Boolean.TRUE.toString());
060                    portletPreferences.setValue("formDDMTemplateId", StringPool.BLANK);
061                    portletPreferences.setValue("recordSetId", StringPool.BLANK);
062                    portletPreferences.setValue("spreadsheet", Boolean.FALSE.toString());
063    
064                    return portletPreferences;
065            }
066    
067            @Override
068            protected PortletPreferences doProcessExportPortletPreferences(
069                            PortletDataContext portletDataContext, String portletId,
070                            PortletPreferences portletPreferences)
071                    throws Exception {
072    
073                    portletDataContext.addPortletPermissions(DDLPermission.RESOURCE_NAME);
074    
075                    long recordSetId = GetterUtil.getLong(
076                            portletPreferences.getValue("recordSetId", null));
077    
078                    if (recordSetId == 0) {
079                            if (_log.isDebugEnabled()) {
080                                    _log.debug("Unable to get record set with ID " + portletId);
081                            }
082    
083                            return portletPreferences;
084                    }
085    
086                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.fetchRecordSet(
087                            recordSetId);
088    
089                    if (recordSet != null) {
090                            StagedModelDataHandlerUtil.exportReferenceStagedModel(
091                                    portletDataContext, portletId, recordSet);
092                    }
093    
094                    long displayDDMTemplateId = GetterUtil.getLong(
095                            portletPreferences.getValue("displayDDMTemplateId", null));
096    
097                    exportReferenceDDMTemplate(
098                            portletDataContext, portletId, displayDDMTemplateId);
099    
100                    long formDDMTemplateId = GetterUtil.getLong(
101                            portletPreferences.getValue("formDDMTemplateId", null));
102    
103                    exportReferenceDDMTemplate(
104                            portletDataContext, portletId, formDDMTemplateId);
105    
106                    return portletPreferences;
107            }
108    
109            @Override
110            protected PortletPreferences doProcessImportPortletPreferences(
111                            PortletDataContext portletDataContext, String portletId,
112                            PortletPreferences portletPreferences)
113                    throws Exception {
114    
115                    portletDataContext.importPortletPermissions(
116                            DDLPermission.RESOURCE_NAME);
117    
118                    StagedModelDataHandlerUtil.importReferenceStagedModels(
119                            portletDataContext, DDLRecordSet.class);
120    
121                    StagedModelDataHandlerUtil.importReferenceStagedModels(
122                            portletDataContext, DDMTemplate.class);
123    
124                    long importedRecordSetId = GetterUtil.getLong(
125                            portletPreferences.getValue("recordSetId", null));
126    
127                    Map<Long, Long> recordSetIds =
128                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
129                                    DDLRecordSet.class);
130    
131                    long recordSetId = MapUtil.getLong(
132                            recordSetIds, importedRecordSetId, importedRecordSetId);
133    
134                    long importedDisplayDDMTemplateId = GetterUtil.getLong(
135                            portletPreferences.getValue("displayDDMTemplateId", null));
136    
137                    Map<Long, Long> templateIds =
138                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
139                                    DDMTemplate.class);
140    
141                    long displayDDMTemplateId = MapUtil.getLong(
142                            templateIds, importedDisplayDDMTemplateId,
143                            importedDisplayDDMTemplateId);
144    
145                    long importedFormDDMTemplateId = GetterUtil.getLong(
146                            portletPreferences.getValue("formDDMTemplateId", null));
147    
148                    long formDDMTemplateId = MapUtil.getLong(
149                            templateIds, importedFormDDMTemplateId, importedFormDDMTemplateId);
150    
151                    portletPreferences.setValue("recordSetId", String.valueOf(recordSetId));
152                    portletPreferences.setValue(
153                            "displayDDMTemplateId", String.valueOf(displayDDMTemplateId));
154                    portletPreferences.setValue(
155                            "formDDMTemplateId", String.valueOf(formDDMTemplateId));
156    
157                    return portletPreferences;
158            }
159    
160            private void exportReferenceDDMTemplate(
161                            PortletDataContext portletDataContext, String portletId,
162                            long ddmTemplateId)
163                    throws Exception {
164    
165                    if (ddmTemplateId == 0) {
166                            return;
167                    }
168    
169                    DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.fetchDDMTemplate(
170                            ddmTemplateId);
171    
172                    if (ddmTemplate == null) {
173                            if (_log.isWarnEnabled()) {
174                                    _log.warn(
175                                            "Unable to export referenced template with id " +
176                                                    ddmTemplateId);
177                            }
178    
179                            return;
180                    }
181    
182                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
183                            portletDataContext, portletId, ddmTemplate);
184            }
185    
186            private static Log _log = LogFactoryUtil.getLog(
187                    DDLDisplayPortletDataHandler.class);
188    
189    }