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