001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                    portletPreferences.setValue("recordSetId", StringPool.BLANK);
064                    portletPreferences.setValue("detailDDMTemplateId", StringPool.BLANK);
065                    portletPreferences.setValue("listDDMTemplateId", StringPool.BLANK);
066                    portletPreferences.setValue("editable", Boolean.TRUE.toString());
067                    portletPreferences.setValue("spreadsheet", Boolean.FALSE.toString());
068    
069                    return portletPreferences;
070            }
071    
072            @Override
073            protected String doExportData(
074                            PortletDataContext portletDataContext, String portletId,
075                            PortletPreferences portletPreferences)
076                    throws Exception {
077    
078                    portletDataContext.addPermissions(
079                            "com.liferay.portlet.dynamicdatalist",
080                            portletDataContext.getScopeGroupId());
081    
082                    long recordSetId = GetterUtil.getLong(
083                            portletPreferences.getValue("recordSetId", null), 0);
084    
085                    if (recordSetId == 0) {
086                            if (_log.isDebugEnabled()) {
087                                    _log.debug("No record set found for " + portletId);
088                            }
089    
090                            return StringPool.BLANK;
091                    }
092    
093                    Document document = SAXReaderUtil.createDocument();
094    
095                    Element rootElement = document.addElement("record-set-data");
096    
097                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.getRecordSet(
098                            recordSetId);
099    
100                    DDLPortletDataHandler ddlPortletDataHandler =
101                            DDLPortletDataHandlerUtil.getDDLPortletDataHandler();
102    
103                    ddlPortletDataHandler.exportRecordSet(
104                            portletDataContext, rootElement, recordSet);
105    
106                    return document.formattedString();
107            }
108    
109            @Override
110            protected PortletPreferences doImportData(
111                            PortletDataContext portletDataContext, String portletId,
112                            PortletPreferences portletPreferences, String data)
113                    throws Exception {
114    
115                    portletDataContext.importPermissions(
116                            "com.liferay.portlet.dynamicdatalist",
117                            portletDataContext.getSourceGroupId(),
118                            portletDataContext.getScopeGroupId());
119    
120                    if (Validator.isNull(data)) {
121                            return null;
122                    }
123    
124                    Document document = SAXReaderUtil.read(data);
125    
126                    Element rootElement = document.getRootElement();
127    
128                    Element recordSetElement = rootElement.element("record-set");
129    
130                    if (recordSetElement != null) {
131                            DDLPortletDataHandler ddlPortletDataHandler =
132                                    DDLPortletDataHandlerUtil.getDDLPortletDataHandler();
133    
134                            ddlPortletDataHandler.importRecordSet(
135                                    portletDataContext, recordSetElement);
136                    }
137    
138                    long importedRecordSetId = GetterUtil.getLong(
139                            portletPreferences.getValue("recordSetId", null));
140                    long importedDetailDDMTemplateId = GetterUtil.getLong(
141                            portletPreferences.getValue("detailDDMTemplateId", null));
142                    long importedListDDMTemplateId = GetterUtil.getLong(
143                            portletPreferences.getValue("listDDMTemplateId", null));
144    
145                    Map<Long, Long> recordSetIds =
146                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
147                                    DDLRecordSet.class);
148    
149                    long recordSetId = MapUtil.getLong(
150                            recordSetIds, importedRecordSetId, importedRecordSetId);
151    
152                    Map<Long, Long> templateIds =
153                            (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
154                                    DDMTemplate.class);
155    
156                    long detailDDMTemplateId = MapUtil.getLong(
157                            templateIds, importedDetailDDMTemplateId,
158                            importedDetailDDMTemplateId);
159    
160                    long listDDMTemplateId = MapUtil.getLong(
161                            templateIds, importedListDDMTemplateId, importedListDDMTemplateId);
162    
163                    portletPreferences.setValue("recordSetId", String.valueOf(recordSetId));
164                    portletPreferences.setValue(
165                            "detailDDMTemplateId", String.valueOf(detailDDMTemplateId));
166                    portletPreferences.setValue(
167                            "listDDMTemplateId", String.valueOf(listDDMTemplateId));
168    
169                    return portletPreferences;
170            }
171    
172            private static final boolean _ALWAYS_EXPORTABLE = true;
173    
174            private static final boolean _DATA_LOCALIZED = true;
175    
176            private static Log _log = LogFactoryUtil.getLog(
177                    DDLDisplayPortletDataHandlerImpl.class);
178    
179    }