001    /**
002     * Copyright (c) 2000-2012 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.util.Validator;
025    import com.liferay.portal.kernel.xml.Document;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.kernel.xml.SAXReaderUtil;
028    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
029    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetLocalServiceUtil;
030    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
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                    setDataPortletPreferences(
043                            "recordSetId", "displayDDMTemplateId", "formDDMTemplateId");
044            }
045    
046            @Override
047            protected PortletPreferences doDeleteData(
048                            PortletDataContext portletDataContext, String portletId,
049                            PortletPreferences portletPreferences)
050                    throws Exception {
051    
052                    if (portletPreferences == null) {
053                            return portletPreferences;
054                    }
055    
056                    portletPreferences.setValue("recordSetId", StringPool.BLANK);
057                    portletPreferences.setValue("displayDDMTemplateId", StringPool.BLANK);
058                    portletPreferences.setValue("formDDMTemplateId", StringPool.BLANK);
059                    portletPreferences.setValue("editable", Boolean.TRUE.toString());
060                    portletPreferences.setValue("spreadsheet", Boolean.FALSE.toString());
061    
062                    return portletPreferences;
063            }
064    
065            @Override
066            protected String doExportData(
067                            PortletDataContext portletDataContext, String portletId,
068                            PortletPreferences portletPreferences)
069                    throws Exception {
070    
071                    portletDataContext.addPermissions(
072                            "com.liferay.portlet.dynamicdatalist",
073                            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("No record set found for " + portletId);
081                            }
082    
083                            return StringPool.BLANK;
084                    }
085    
086                    Element rootElement = addExportRootElement();
087    
088                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.getRecordSet(
089                            recordSetId);
090    
091                    StagedModelDataHandlerUtil.exportStagedModel(
092                            portletDataContext, rootElement, recordSet);
093    
094                    return rootElement.formattedString();
095            }
096    
097            @Override
098            protected PortletPreferences doImportData(
099                            PortletDataContext portletDataContext, String portletId,
100                            PortletPreferences portletPreferences, String data)
101                    throws Exception {
102    
103                    portletDataContext.importPermissions(
104                            "com.liferay.portlet.dynamicdatalist",
105                            portletDataContext.getSourceGroupId(),
106                            portletDataContext.getScopeGroupId());
107    
108                    if (Validator.isNull(data)) {
109                            return null;
110                    }
111    
112                    Document document = SAXReaderUtil.read(data);
113    
114                    Element rootElement = document.getRootElement();
115    
116                    Element recordSetElement = rootElement.element("record-set");
117    
118                    if (recordSetElement != null) {
119                            StagedModelDataHandlerUtil.importStagedModel(
120                                    portletDataContext, recordSetElement);
121                    }
122    
123                    long importedRecordSetId = GetterUtil.getLong(
124                            portletPreferences.getValue("recordSetId", null));
125                    long importedDisplayDDMTemplateId = GetterUtil.getLong(
126                            portletPreferences.getValue("displayDDMTemplateId", null));
127                    long importedFormDDMTemplateId = GetterUtil.getLong(
128                            portletPreferences.getValue("formDDMTemplateId", null));
129    
130                    Map<Long, Long> recordSetIds =
131                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
132                                    DDLRecordSet.class);
133    
134                    long recordSetId = MapUtil.getLong(
135                            recordSetIds, importedRecordSetId, importedRecordSetId);
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 formDDMTemplateId = MapUtil.getLong(
146                            templateIds, importedFormDDMTemplateId, importedFormDDMTemplateId);
147    
148                    portletPreferences.setValue("recordSetId", String.valueOf(recordSetId));
149                    portletPreferences.setValue(
150                            "displayDDMTemplateId", String.valueOf(displayDDMTemplateId));
151                    portletPreferences.setValue(
152                            "formDDMTemplateId", String.valueOf(formDDMTemplateId));
153    
154                    return portletPreferences;
155            }
156    
157            private static Log _log = LogFactoryUtil.getLog(
158                    DDLDisplayPortletDataHandler.class);
159    
160    }