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