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.portal.kernel.lar;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.Time;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.xml.Document;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.kernel.xml.SAXReaderUtil;
025    
026    import javax.portlet.PortletPreferences;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public abstract class BasePortletDataHandler implements PortletDataHandler {
032    
033            public PortletPreferences deleteData(
034                            PortletDataContext portletDataContext, String portletId,
035                            PortletPreferences portletPreferences)
036                    throws PortletDataException {
037    
038                    long startTime = 0;
039    
040                    if (_log.isInfoEnabled()) {
041                            _log.info("Deleting portlet " + portletId);
042    
043                            startTime = System.currentTimeMillis();
044                    }
045    
046                    try {
047                            return doDeleteData(
048                                    portletDataContext, portletId, portletPreferences);
049                    }
050                    catch (Exception e) {
051                            throw new PortletDataException(e);
052                    }
053                    finally {
054                            if (_log.isInfoEnabled()) {
055                                    long duration = System.currentTimeMillis() - startTime;
056    
057                                    _log.info("Deleted portlet in " + Time.getDuration(duration));
058                            }
059                    }
060            }
061    
062            public String exportData(
063                            PortletDataContext portletDataContext, String portletId,
064                            PortletPreferences portletPreferences)
065                    throws PortletDataException {
066    
067                    long startTime = 0;
068    
069                    if (_log.isInfoEnabled()) {
070                            _log.info("Exporting portlet " + portletId);
071    
072                            startTime = System.currentTimeMillis();
073                    }
074    
075                    try {
076                            return doExportData(
077                                    portletDataContext, portletId, portletPreferences);
078                    }
079                    catch (Exception e) {
080                            throw new PortletDataException(e);
081                    }
082                    finally {
083                            if (_log.isInfoEnabled()) {
084                                    long duration = System.currentTimeMillis() - startTime;
085    
086                                    _log.info("Exported portlet in " + Time.getDuration(duration));
087                            }
088                    }
089            }
090    
091            public String[] getDataPortletPreferences() {
092                    return new String[0];
093            }
094    
095            public PortletDataHandlerControl[] getExportControls() {
096                    return new PortletDataHandlerControl[0];
097            }
098    
099            public PortletDataHandlerControl[] getExportMetadataControls() {
100                    return new PortletDataHandlerControl[0];
101            }
102    
103            public PortletDataHandlerControl[] getImportControls() {
104                    return new PortletDataHandlerControl[0];
105            }
106    
107            public PortletDataHandlerControl[] getImportMetadataControls() {
108                    return new PortletDataHandlerControl[0];
109            }
110    
111            public PortletPreferences importData(
112                            PortletDataContext portletDataContext, String portletId,
113                            PortletPreferences portletPreferences, String data)
114                    throws PortletDataException {
115    
116                    long startTime = 0;
117    
118                    if (_log.isInfoEnabled()) {
119                            _log.info("Importing portlet " + portletId);
120    
121                            startTime = System.currentTimeMillis();
122                    }
123    
124                    long sourceGroupId = portletDataContext.getSourceGroupId();
125    
126                    try {
127                            if (Validator.isXml(data)) {
128                                    Document document = SAXReaderUtil.read(data);
129    
130                                    Element rootElement = document.getRootElement();
131    
132                                    long portletSourceGroupId = GetterUtil.getLong(
133                                            rootElement.attributeValue("group-id"));
134    
135                                    if (portletSourceGroupId != 0) {
136                                            portletDataContext.setSourceGroupId(portletSourceGroupId);
137                                    }
138                            }
139    
140                            return doImportData(
141                                    portletDataContext, portletId, portletPreferences, data);
142                    }
143                    catch (Exception e) {
144                            throw new PortletDataException(e);
145                    }
146                    finally {
147                            portletDataContext.setSourceGroupId(sourceGroupId);
148    
149                            if (_log.isInfoEnabled()) {
150                                    long duration = System.currentTimeMillis() - startTime;
151    
152                                    _log.info("Imported portlet in " + Time.getDuration(duration));
153                            }
154                    }
155            }
156    
157            public boolean isAlwaysExportable() {
158                    return _ALWAYS_EXPORTABLE;
159            }
160    
161            public boolean isAlwaysStaged() {
162                    return _ALWAYS_STAGED;
163            }
164    
165            public boolean isDataLocalized() {
166                    return _DATA_LOCALIZED;
167            }
168    
169            public boolean isPublishToLiveByDefault() {
170                    return _PUBLISH_TO_LIVE_BY_DEFAULT;
171            }
172    
173            protected PortletPreferences doDeleteData(
174                            PortletDataContext portletDataContext, String portletId,
175                            PortletPreferences portletPreferences)
176                    throws Exception {
177    
178                    return null;
179            }
180    
181            protected String doExportData(
182                            PortletDataContext portletDataContext, String portletId,
183                            PortletPreferences portletPreferences)
184                    throws Exception {
185    
186                    return null;
187            }
188    
189            protected PortletPreferences doImportData(
190                            PortletDataContext portletDataContext, String portletId,
191                            PortletPreferences portletPreferences, String data)
192                    throws Exception {
193    
194                    return null;
195            }
196    
197            private static final boolean _ALWAYS_EXPORTABLE = false;
198    
199            private static final boolean _ALWAYS_STAGED = false;
200    
201            private static final boolean _DATA_LOCALIZED = false;
202    
203            private static final boolean _PUBLISH_TO_LIVE_BY_DEFAULT = false;
204    
205            private static Log _log = LogFactoryUtil.getLog(
206                    BasePortletDataHandler.class);
207    
208    }