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.portal.kernel.xml.Element;
027    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
028    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetLocalServiceUtil;
029    import com.liferay.portlet.dynamicdatalists.service.permission.DDLPermission;
030    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
031    
032    import java.util.List;
033    import java.util.Map;
034    
035    import javax.portlet.PortletPreferences;
036    
037    /**
038     * @author Michael C. Han
039     */
040    public class DDLDisplayPortletDataHandler extends DDLPortletDataHandler {
041    
042            public DDLDisplayPortletDataHandler() {
043                    setDataLevel(DataLevel.PORTLET_INSTANCE);
044                    setDataPortletPreferences(
045                            "displayDDMTemplateId", "formDDMTemplateId", "recordSetId");
046                    setExportControls(new PortletDataHandlerControl[0]);
047            }
048    
049            @Override
050            protected PortletPreferences doDeleteData(
051                            PortletDataContext portletDataContext, String portletId,
052                            PortletPreferences portletPreferences)
053                    throws Exception {
054    
055                    if (portletPreferences == null) {
056                            return portletPreferences;
057                    }
058    
059                    portletPreferences.setValue("displayDDMTemplateId", StringPool.BLANK);
060                    portletPreferences.setValue("editable", Boolean.TRUE.toString());
061                    portletPreferences.setValue("formDDMTemplateId", StringPool.BLANK);
062                    portletPreferences.setValue("recordSetId", StringPool.BLANK);
063                    portletPreferences.setValue("spreadsheet", Boolean.FALSE.toString());
064    
065                    return portletPreferences;
066            }
067    
068            @Override
069            protected PortletPreferences doProcessExportPortletPreferences(
070                            PortletDataContext portletDataContext, String portletId,
071                            PortletPreferences portletPreferences)
072                    throws Exception {
073    
074                    portletDataContext.addPortletPermissions(DDLPermission.RESOURCE_NAME);
075    
076                    long recordSetId = GetterUtil.getLong(
077                            portletPreferences.getValue("recordSetId", null), 0);
078    
079                    if (recordSetId == 0) {
080                            if (_log.isDebugEnabled()) {
081                                    _log.debug("Unable to get record set with ID " + portletId);
082                            }
083    
084                            return portletPreferences;
085                    }
086    
087                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.fetchRecordSet(
088                            recordSetId);
089    
090                    if (recordSet == null) {
091                            return portletPreferences;
092                    }
093    
094                    StagedModelDataHandlerUtil.exportReferenceStagedModel(
095                            portletDataContext, portletId, recordSet);
096    
097                    return portletPreferences;
098            }
099    
100            @Override
101            protected PortletPreferences doProcessImportPortletPreferences(
102                            PortletDataContext portletDataContext, String portletId,
103                            PortletPreferences portletPreferences)
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    }