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