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.BasePortletDataHandler;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
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 DDLDisplayPortletDataHandlerImpl extends BasePortletDataHandler {
040    
041            @Override
042            public String[] getDataPortletPreferences() {
043                    return new String[] {
044                            "recordSetId", "detailDDMTemplateId", "listDDMTemplateId"};
045            }
046    
047            @Override
048            public boolean isAlwaysExportable() {
049                    return _ALWAYS_EXPORTABLE;
050            }
051    
052            @Override
053            public boolean isDataLocalized() {
054                    return _DATA_LOCALIZED;
055            }
056    
057            @Override
058            protected PortletPreferences doDeleteData(
059                            PortletDataContext portletDataContext, String portletId,
060                            PortletPreferences portletPreferences)
061                    throws Exception {
062    
063                    if (portletPreferences == null) {
064                            return portletPreferences;
065                    }
066    
067                    portletPreferences.setValue("recordSetId", StringPool.BLANK);
068                    portletPreferences.setValue("detailDDMTemplateId", StringPool.BLANK);
069                    portletPreferences.setValue("listDDMTemplateId", StringPool.BLANK);
070                    portletPreferences.setValue("editable", Boolean.TRUE.toString());
071                    portletPreferences.setValue("spreadsheet", Boolean.FALSE.toString());
072    
073                    return portletPreferences;
074            }
075    
076            @Override
077            protected String doExportData(
078                            PortletDataContext portletDataContext, String portletId,
079                            PortletPreferences portletPreferences)
080                    throws Exception {
081    
082                    portletDataContext.addPermissions(
083                            "com.liferay.portlet.dynamicdatalist",
084                            portletDataContext.getScopeGroupId());
085    
086                    long recordSetId = GetterUtil.getLong(
087                            portletPreferences.getValue("recordSetId", null), 0);
088    
089                    if (recordSetId == 0) {
090                            if (_log.isDebugEnabled()) {
091                                    _log.debug("No record set found for " + portletId);
092                            }
093    
094                            return StringPool.BLANK;
095                    }
096    
097                    Document document = SAXReaderUtil.createDocument();
098    
099                    Element rootElement = document.addElement("record-set-data");
100    
101                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.getRecordSet(
102                            recordSetId);
103    
104                    DDLPortletDataHandler ddlPortletDataHandler =
105                            DDLPortletDataHandlerUtil.getDDLPortletDataHandler();
106    
107                    ddlPortletDataHandler.exportRecordSet(
108                            portletDataContext, rootElement, recordSet);
109    
110                    return document.formattedString();
111            }
112    
113            @Override
114            protected PortletPreferences doImportData(
115                            PortletDataContext portletDataContext, String portletId,
116                            PortletPreferences portletPreferences, String data)
117                    throws Exception {
118    
119                    portletDataContext.importPermissions(
120                            "com.liferay.portlet.dynamicdatalist",
121                            portletDataContext.getSourceGroupId(),
122                            portletDataContext.getScopeGroupId());
123    
124                    if (Validator.isNull(data)) {
125                            return null;
126                    }
127    
128                    Document document = SAXReaderUtil.read(data);
129    
130                    Element rootElement = document.getRootElement();
131    
132                    Element recordSetElement = rootElement.element("record-set");
133    
134                    if (recordSetElement != null) {
135                            DDLPortletDataHandler ddlPortletDataHandler =
136                                    DDLPortletDataHandlerUtil.getDDLPortletDataHandler();
137    
138                            ddlPortletDataHandler.importRecordSet(
139                                    portletDataContext, recordSetElement);
140                    }
141    
142                    long importedRecordSetId = GetterUtil.getLong(
143                            portletPreferences.getValue("recordSetId", null));
144                    long importedDetailDDMTemplateId = GetterUtil.getLong(
145                            portletPreferences.getValue("detailDDMTemplateId", null));
146                    long importedListDDMTemplateId = GetterUtil.getLong(
147                            portletPreferences.getValue("listDDMTemplateId", null));
148    
149                    Map<Long, Long> recordSetIds =
150                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
151                                    DDLRecordSet.class);
152    
153                    long recordSetId = MapUtil.getLong(
154                            recordSetIds, importedRecordSetId, importedRecordSetId);
155    
156                    Map<Long, Long> templateIds =
157                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
158                                    DDMTemplate.class);
159    
160                    long detailDDMTemplateId = MapUtil.getLong(
161                            templateIds, importedDetailDDMTemplateId,
162                            importedDetailDDMTemplateId);
163    
164                    long listDDMTemplateId = MapUtil.getLong(
165                            templateIds, importedListDDMTemplateId, importedListDDMTemplateId);
166    
167                    portletPreferences.setValue("recordSetId", String.valueOf(recordSetId));
168                    portletPreferences.setValue(
169                            "detailDDMTemplateId", String.valueOf(detailDDMTemplateId));
170                    portletPreferences.setValue(
171                            "listDDMTemplateId", String.valueOf(listDDMTemplateId));
172    
173                    return portletPreferences;
174            }
175    
176            private static final boolean _ALWAYS_EXPORTABLE = true;
177    
178            private static final boolean _DATA_LOCALIZED = true;
179    
180            private static Log _log = LogFactoryUtil.getLog(
181                    DDLDisplayPortletDataHandlerImpl.class);
182    
183    }