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.addPermissions(
073                            DDLPermission.RESOURCE_NAME, portletDataContext.getScopeGroupId());
074    
075                    long recordSetId = GetterUtil.getLong(
076                            portletPreferences.getValue("recordSetId", null), 0);
077    
078                    if (recordSetId == 0) {
079                            if (_log.isDebugEnabled()) {
080                                    _log.debug("Unable to get record set with ID " + portletId);
081                            }
082    
083                            return StringPool.BLANK;
084                    }
085    
086                    Element rootElement = addExportDataRootElement(portletDataContext);
087    
088                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.fetchRecordSet(
089                            recordSetId);
090    
091                    if (recordSet == null) {
092                            return getExportDataRootElementString(rootElement);
093                    }
094    
095                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
096                            portletDataContext, portletId, recordSet);
097    
098                    return getExportDataRootElementString(rootElement);
099            }
100    
101            @Override
102            protected PortletPreferences doImportData(
103                            PortletDataContext portletDataContext, String portletId,
104                            PortletPreferences portletPreferences, String data)
105                    throws Exception {
106    
107                    portletDataContext.importPermissions(
108                            DDLPermission.RESOURCE_NAME, portletDataContext.getSourceGroupId(),
109                            portletDataContext.getScopeGroupId());
110    
111                    Element recordSetsElement =
112                            portletDataContext.getImportDataGroupElement(DDLRecordSet.class);
113    
114                    List<Element> recordSetElements = recordSetsElement.elements();
115    
116                    Element recordSetElement = recordSetElements.get(0);
117    
118                    StagedModelDataHandlerUtil.importStagedModel(
119                            portletDataContext, recordSetElement);
120    
121                    long importedRecordSetId = GetterUtil.getLong(
122                            portletPreferences.getValue("recordSetId", null));
123                    long importedDisplayDDMTemplateId = GetterUtil.getLong(
124                            portletPreferences.getValue("displayDDMTemplateId", null));
125                    long importedFormDDMTemplateId = GetterUtil.getLong(
126                            portletPreferences.getValue("formDDMTemplateId", null));
127    
128                    Map<Long, Long> recordSetIds =
129                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
130                                    DDLRecordSet.class);
131    
132                    long recordSetId = MapUtil.getLong(
133                            recordSetIds, importedRecordSetId, importedRecordSetId);
134    
135                    Map<Long, Long> templateIds =
136                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
137                                    DDMTemplate.class);
138    
139                    long displayDDMTemplateId = MapUtil.getLong(
140                            templateIds, importedDisplayDDMTemplateId,
141                            importedDisplayDDMTemplateId);
142    
143                    long formDDMTemplateId = MapUtil.getLong(
144                            templateIds, importedFormDDMTemplateId, importedFormDDMTemplateId);
145    
146                    portletPreferences.setValue("recordSetId", String.valueOf(recordSetId));
147                    portletPreferences.setValue(
148                            "displayDDMTemplateId", String.valueOf(displayDDMTemplateId));
149                    portletPreferences.setValue(
150                            "formDDMTemplateId", String.valueOf(formDDMTemplateId));
151    
152                    return portletPreferences;
153            }
154    
155            private static Log _log = LogFactoryUtil.getLog(
156                    DDLDisplayPortletDataHandler.class);
157    
158    }