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.StagedModelDataHandlerUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.MapUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.xml.Element;
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.List;
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                            "recordSetId", "displayDDMTemplateId", "formDDMTemplateId");
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("recordSetId", StringPool.BLANK);
058                    portletPreferences.setValue("displayDDMTemplateId", StringPool.BLANK);
059                    portletPreferences.setValue("formDDMTemplateId", StringPool.BLANK);
060                    portletPreferences.setValue("editable", Boolean.TRUE.toString());
061                    portletPreferences.setValue("spreadsheet", Boolean.FALSE.toString());
062    
063                    return portletPreferences;
064            }
065    
066            @Override
067            protected String doExportData(
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 StringPool.BLANK;
083                    }
084    
085                    Element rootElement = addExportDataRootElement(portletDataContext);
086    
087                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.fetchRecordSet(
088                            recordSetId);
089    
090                    if (recordSet == null) {
091                            return getExportDataRootElementString(rootElement);
092                    }
093    
094                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
095                            portletDataContext, portletId, recordSet);
096    
097                    return getExportDataRootElementString(rootElement);
098            }
099    
100            @Override
101            protected PortletPreferences doImportData(
102                            PortletDataContext portletDataContext, String portletId,
103                            PortletPreferences portletPreferences, String data)
104                    throws Exception {
105    
106                    portletDataContext.importPortletPermissions(
107                            DDLPermission.RESOURCE_NAME);
108    
109                    Element recordSetsElement =
110                            portletDataContext.getImportDataGroupElement(DDLRecordSet.class);
111    
112                    List<Element> recordSetElements = recordSetsElement.elements();
113    
114                    Element recordSetElement = recordSetElements.get(0);
115    
116                    StagedModelDataHandlerUtil.importReferenceStagedModel(
117                            portletDataContext, recordSetElement);
118    
119                    long importedRecordSetId = GetterUtil.getLong(
120                            portletPreferences.getValue("recordSetId", null));
121                    long importedDisplayDDMTemplateId = GetterUtil.getLong(
122                            portletPreferences.getValue("displayDDMTemplateId", null));
123                    long importedFormDDMTemplateId = GetterUtil.getLong(
124                            portletPreferences.getValue("formDDMTemplateId", null));
125    
126                    Map<Long, Long> recordSetIds =
127                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
128                                    DDLRecordSet.class);
129    
130                    long recordSetId = MapUtil.getLong(
131                            recordSetIds, importedRecordSetId, importedRecordSetId);
132    
133                    Map<Long, Long> templateIds =
134                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
135                                    DDMTemplate.class);
136    
137                    long displayDDMTemplateId = MapUtil.getLong(
138                            templateIds, importedDisplayDDMTemplateId,
139                            importedDisplayDDMTemplateId);
140    
141                    long formDDMTemplateId = MapUtil.getLong(
142                            templateIds, importedFormDDMTemplateId, importedFormDDMTemplateId);
143    
144                    portletPreferences.setValue("recordSetId", String.valueOf(recordSetId));
145                    portletPreferences.setValue(
146                            "displayDDMTemplateId", String.valueOf(displayDDMTemplateId));
147                    portletPreferences.setValue(
148                            "formDDMTemplateId", String.valueOf(formDDMTemplateId));
149    
150                    return portletPreferences;
151            }
152    
153            private static Log _log = LogFactoryUtil.getLog(
154                    DDLDisplayPortletDataHandler.class);
155    
156    }