001    /**
002     * Copyright (c) 2000-present 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.exportimport.kernel.lar;
016    
017    import static com.liferay.exportimport.kernel.configuration.ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT;
018    import static com.liferay.exportimport.kernel.configuration.ExportImportConfigurationConstants.TYPE_EXPORT_PORTLET;
019    import static com.liferay.exportimport.kernel.configuration.ExportImportConfigurationConstants.TYPE_IMPORT_LAYOUT;
020    import static com.liferay.exportimport.kernel.configuration.ExportImportConfigurationConstants.TYPE_IMPORT_PORTLET;
021    import static com.liferay.exportimport.kernel.configuration.ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_LOCAL;
022    import static com.liferay.exportimport.kernel.configuration.ExportImportConfigurationConstants.TYPE_PUBLISH_LAYOUT_REMOTE;
023    import static com.liferay.exportimport.kernel.configuration.ExportImportConfigurationConstants.TYPE_PUBLISH_PORTLET;
024    import static com.liferay.exportimport.kernel.configuration.ExportImportConfigurationConstants.TYPE_SCHEDULED_PUBLISH_LAYOUT_LOCAL;
025    import static com.liferay.exportimport.kernel.configuration.ExportImportConfigurationConstants.TYPE_SCHEDULED_PUBLISH_LAYOUT_REMOTE;
026    
027    import aQute.bnd.annotation.ProviderType;
028    
029    import com.liferay.exportimport.kernel.model.ExportImportConfiguration;
030    import com.liferay.exportimport.kernel.service.ExportImportConfigurationLocalServiceUtil;
031    import com.liferay.portal.kernel.exception.PortalException;
032    import com.liferay.portal.kernel.log.Log;
033    import com.liferay.portal.kernel.log.LogFactoryUtil;
034    import com.liferay.portal.kernel.model.Group;
035    import com.liferay.portal.kernel.model.Layout;
036    import com.liferay.portal.kernel.model.LayoutSet;
037    import com.liferay.portal.kernel.model.StagedGroupedModel;
038    import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
039    import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
040    import com.liferay.portal.kernel.service.LayoutLocalServiceUtil;
041    import com.liferay.portal.kernel.service.LayoutSetLocalServiceUtil;
042    import com.liferay.portal.kernel.theme.ThemeDisplay;
043    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
044    import com.liferay.portal.kernel.util.DateRange;
045    import com.liferay.portal.kernel.util.GetterUtil;
046    import com.liferay.portal.kernel.util.LocaleUtil;
047    import com.liferay.portal.kernel.util.MapUtil;
048    import com.liferay.portal.kernel.util.ParamUtil;
049    import com.liferay.portal.kernel.util.StringPool;
050    import com.liferay.portal.kernel.util.Time;
051    import com.liferay.portal.kernel.util.TimeZoneUtil;
052    import com.liferay.portal.kernel.util.UnicodeProperties;
053    import com.liferay.portal.kernel.util.Validator;
054    import com.liferay.portal.kernel.util.WebKeys;
055    
056    import java.io.Serializable;
057    
058    import java.util.Calendar;
059    import java.util.Date;
060    import java.util.Locale;
061    import java.util.Map;
062    import java.util.TimeZone;
063    
064    import javax.portlet.PortletPreferences;
065    import javax.portlet.PortletRequest;
066    
067    /**
068     * @author Mate Thurzo
069     */
070    @ProviderType
071    public class ExportImportDateUtil {
072    
073            public static final String RANGE = "range";
074    
075            public static final String RANGE_ALL = "all";
076    
077            public static final String RANGE_DATE_RANGE = "dateRange";
078    
079            public static final String RANGE_FROM_LAST_PUBLISH_DATE =
080                    "fromLastPublishDate";
081    
082            public static final String RANGE_LAST = "last";
083    
084            public static void clearLastPublishDate(long groupId, boolean privateLayout)
085                    throws PortalException {
086    
087                    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
088                            groupId, privateLayout);
089    
090                    UnicodeProperties settingsProperties =
091                            layoutSet.getSettingsProperties();
092    
093                    settingsProperties.remove(_LAST_PUBLISH_DATE);
094    
095                    LayoutSetLocalServiceUtil.updateSettings(
096                            groupId, privateLayout, settingsProperties.toString());
097            }
098    
099            public static Calendar getCalendar(
100                    PortletRequest portletRequest, String paramPrefix,
101                    boolean timeZoneSensitive) {
102    
103                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
104                            WebKeys.THEME_DISPLAY);
105    
106                    int dateMonth = ParamUtil.getInteger(
107                            portletRequest, paramPrefix + "Month");
108                    int dateDay = ParamUtil.getInteger(portletRequest, paramPrefix + "Day");
109                    int dateYear = ParamUtil.getInteger(
110                            portletRequest, paramPrefix + "Year");
111                    int dateHour = ParamUtil.getInteger(
112                            portletRequest, paramPrefix + "Hour");
113                    int dateMinute = ParamUtil.getInteger(
114                            portletRequest, paramPrefix + "Minute");
115                    int dateAmPm = ParamUtil.getInteger(
116                            portletRequest, paramPrefix + "AmPm");
117    
118                    return getCalendar(
119                            dateAmPm, dateYear, dateMonth, dateDay, dateHour, dateMinute,
120                            themeDisplay.getLocale(), themeDisplay.getTimeZone(),
121                            timeZoneSensitive);
122            }
123    
124            public static DateRange getDateRange(
125                            ExportImportConfiguration exportImportConfiguration)
126                    throws PortalException {
127    
128                    Map<String, Serializable> settingsMap =
129                            exportImportConfiguration.getSettingsMap();
130    
131                    String portletId = (String)settingsMap.get("portletId");
132    
133                    return getDateRange(exportImportConfiguration, portletId);
134            }
135    
136            public static DateRange getDateRange(
137                            ExportImportConfiguration exportImportConfiguration,
138                            String portletId)
139                    throws PortalException {
140    
141                    Map<String, Serializable> settingsMap =
142                            exportImportConfiguration.getSettingsMap();
143    
144                    Date startDate = (Date)settingsMap.get("startDate");
145                    Date endDate = (Date)settingsMap.get("endDate");
146    
147                    if ((startDate != null) && (endDate != null)) {
148                            return new DateRange(startDate, endDate);
149                    }
150    
151                    Map<String, String[]> parameterMap =
152                            (Map<String, String[]>)settingsMap.get("parameterMap");
153    
154                    String range = MapUtil.getString(
155                            parameterMap, RANGE,
156                            getDefaultDateRange(exportImportConfiguration));
157                    int rangeLast = MapUtil.getInteger(parameterMap, "last");
158                    int startDateAmPm = MapUtil.getInteger(parameterMap, "startDateAmPm");
159                    int startDateYear = MapUtil.getInteger(parameterMap, "startDateYear");
160                    int startDateMonth = MapUtil.getInteger(parameterMap, "startDateMonth");
161                    int startDateDay = MapUtil.getInteger(parameterMap, "startDateDay");
162                    int startDateHour = MapUtil.getInteger(parameterMap, "startDateHour");
163                    int startDateMinute = MapUtil.getInteger(
164                            parameterMap, "startDateMinute");
165                    int endDateAmPm = MapUtil.getInteger(parameterMap, "endDateAmPm");
166                    int endDateYear = MapUtil.getInteger(parameterMap, "endDateYear");
167                    int endDateMonth = MapUtil.getInteger(parameterMap, "endDateMonth");
168                    int endDateDay = MapUtil.getInteger(parameterMap, "endDateDay");
169                    int endDateHour = MapUtil.getInteger(parameterMap, "endDateHour");
170                    int endDateMinute = MapUtil.getInteger(parameterMap, "endDateMinute");
171    
172                    long groupId = MapUtil.getLong(settingsMap, "sourceGroupId");
173                    long plid = MapUtil.getLong(settingsMap, "sourcePlid");
174                    boolean privateLayout = MapUtil.getBoolean(
175                            settingsMap, "privateLayout");
176                    Locale locale = (Locale)settingsMap.get("locale");
177                    TimeZone timeZone = (TimeZone)settingsMap.get("timezone");
178    
179                    return getDateRange(
180                            range, rangeLast, startDateAmPm, startDateYear, startDateMonth,
181                            startDateDay, startDateHour, startDateMinute, endDateAmPm,
182                            endDateYear, endDateMonth, endDateDay, endDateHour, endDateMinute,
183                            portletId, groupId, plid, privateLayout, locale, timeZone);
184            }
185    
186            public static DateRange getDateRange(long exportImportConfigurationId)
187                    throws PortalException {
188    
189                    ExportImportConfiguration exportImportConfiguration =
190                            ExportImportConfigurationLocalServiceUtil.
191                                    getExportImportConfiguration(exportImportConfigurationId);
192    
193                    return getDateRange(exportImportConfiguration);
194            }
195    
196            public static DateRange getDateRange(
197                            PortletRequest portletRequest, long groupId, boolean privateLayout,
198                            long plid, String portletId, String defaultRange)
199                    throws PortalException {
200    
201                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
202                            WebKeys.THEME_DISPLAY);
203    
204                    String range = ParamUtil.getString(portletRequest, RANGE, defaultRange);
205                    int rangeLast = ParamUtil.getInteger(portletRequest, "last");
206                    int startDateAmPm = ParamUtil.getInteger(
207                            portletRequest, "startDateAmPm");
208                    int startDateYear = ParamUtil.getInteger(
209                            portletRequest, "startDateYear");
210                    int startDateMonth = ParamUtil.getInteger(
211                            portletRequest, "startDateMonth");
212                    int startDateDay = ParamUtil.getInteger(portletRequest, "startDateDay");
213                    int startDateHour = ParamUtil.getInteger(
214                            portletRequest, "startDateHour");
215                    int startDateMinute = ParamUtil.getInteger(
216                            portletRequest, "startDateMinute");
217                    int endDateAmPm = ParamUtil.getInteger(portletRequest, "endDateAmPm");
218                    int endDateYear = ParamUtil.getInteger(portletRequest, "endDateYear");
219                    int endDateMonth = ParamUtil.getInteger(portletRequest, "endDateMonth");
220                    int endDateDay = ParamUtil.getInteger(portletRequest, "endDateDay");
221                    int endDateHour = ParamUtil.getInteger(portletRequest, "endDateHour");
222                    int endDateMinute = ParamUtil.getInteger(
223                            portletRequest, "endDateMinute");
224    
225                    return getDateRange(
226                            range, rangeLast, startDateAmPm, startDateYear, startDateMonth,
227                            startDateDay, startDateHour, startDateMinute, endDateAmPm,
228                            endDateYear, endDateMonth, endDateDay, endDateHour, endDateMinute,
229                            portletId, groupId, plid, privateLayout, themeDisplay.getLocale(),
230                            themeDisplay.getTimeZone());
231            }
232    
233            public static Date getLastPublishDate(LayoutSet layoutSet) {
234                    long lastPublishDate = GetterUtil.getLong(
235                            layoutSet.getSettingsProperty(_LAST_PUBLISH_DATE));
236    
237                    if (lastPublishDate == 0) {
238                            return null;
239                    }
240    
241                    return new Date(lastPublishDate);
242            }
243    
244            public static Date getLastPublishDate(
245                            PortletDataContext portletDataContext,
246                            PortletPreferences jxPortletPreferences)
247                    throws PortalException {
248    
249                    Group group = GroupLocalServiceUtil.getGroup(
250                            portletDataContext.getGroupId());
251    
252                    String range = MapUtil.getString(
253                            portletDataContext.getParameterMap(), RANGE);
254    
255                    if (!group.isStagedRemotely() &&
256                            range.equals(RANGE_FROM_LAST_PUBLISH_DATE)) {
257    
258                            Date portletLastPublishDate = getLastPublishDate(
259                                    jxPortletPreferences);
260    
261                            if (portletLastPublishDate == null) {
262                                    return null;
263                            }
264    
265                            // This is a valid scenario in case of group level portlets
266    
267                            if (portletDataContext.getStartDate() == null) {
268                                    return portletLastPublishDate;
269                            }
270    
271                            if (portletLastPublishDate.before(
272                                            portletDataContext.getStartDate())) {
273    
274                                    return portletLastPublishDate;
275                            }
276                    }
277    
278                    return portletDataContext.getStartDate();
279            }
280    
281            public static Date getLastPublishDate(
282                    PortletPreferences jxPortletPreferences) {
283    
284                    long lastPublishDate = GetterUtil.getLong(
285                            jxPortletPreferences.getValue(
286                                    _LAST_PUBLISH_DATE, StringPool.BLANK));
287    
288                    if (lastPublishDate == 0) {
289                            return null;
290                    }
291    
292                    return new Date(lastPublishDate);
293            }
294    
295            public static void updateLastPublishDate(
296                            long groupId, boolean privateLayout, DateRange dateRange,
297                            Date lastPublishDate)
298                    throws PortalException {
299    
300                    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
301                            groupId, privateLayout);
302    
303                    Date originalLastPublishDate = getLastPublishDate(layoutSet);
304    
305                    if (!isValidDateRange(dateRange, originalLastPublishDate)) {
306                            return;
307                    }
308    
309                    if (lastPublishDate == null) {
310                            lastPublishDate = new Date();
311                    }
312    
313                    UnicodeProperties settingsProperties =
314                            layoutSet.getSettingsProperties();
315    
316                    settingsProperties.setProperty(
317                            _LAST_PUBLISH_DATE, String.valueOf(lastPublishDate.getTime()));
318    
319                    LayoutSetLocalServiceUtil.updateSettings(
320                            layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
321                            settingsProperties.toString());
322            }
323    
324            public static void updateLastPublishDate(
325                    StagedGroupedModel stagedGroupedModel, DateRange dateRange,
326                    Date lastPublishDate) {
327    
328                    Date originalLastPublishDate = stagedGroupedModel.getLastPublishDate();
329    
330                    if (!isValidDateRange(dateRange, originalLastPublishDate)) {
331                            return;
332                    }
333    
334                    if (lastPublishDate == null) {
335                            lastPublishDate = new Date();
336                    }
337    
338                    stagedGroupedModel.setLastPublishDate(lastPublishDate);
339            }
340    
341            public static void updateLastPublishDate(
342                    String portletId, PortletPreferences portletPreferences,
343                    DateRange dateRange, Date lastPublishDate) {
344    
345                    Date originalLastPublishDate = getLastPublishDate(portletPreferences);
346    
347                    if (!isValidDateRange(dateRange, originalLastPublishDate)) {
348                            return;
349                    }
350    
351                    if (lastPublishDate == null) {
352                            lastPublishDate = new Date();
353                    }
354    
355                    try {
356                            portletPreferences.setValue(
357                                    _LAST_PUBLISH_DATE, String.valueOf(lastPublishDate.getTime()));
358    
359                            portletPreferences.store();
360                    }
361                    catch (UnsupportedOperationException uoe) {
362                            if (_log.isDebugEnabled()) {
363                                    _log.debug(
364                                            "Not updating the portlet setup for " + portletId +
365                                                    " because no setup was returned for the current " +
366                                                            "page");
367                            }
368                    }
369                    catch (Exception e) {
370                            _log.error(e, e);
371                    }
372            }
373    
374            protected static Calendar getCalendar(
375                    int dateAmPm, int dateYear, int dateMonth, int dateDay, int dateHour,
376                    int dateMinute, Locale locale, TimeZone timeZone,
377                    boolean timeZoneSensitive) {
378    
379                    if (dateAmPm == Calendar.PM) {
380                            dateHour += 12;
381                    }
382    
383                    if (!timeZoneSensitive) {
384                            locale = LocaleUtil.getDefault();
385                            timeZone = TimeZoneUtil.getTimeZone(StringPool.UTC);
386                    }
387    
388                    Calendar calendar = CalendarFactoryUtil.getCalendar(timeZone, locale);
389    
390                    calendar.set(Calendar.MONTH, dateMonth);
391                    calendar.set(Calendar.DATE, dateDay);
392                    calendar.set(Calendar.YEAR, dateYear);
393                    calendar.set(Calendar.HOUR_OF_DAY, dateHour);
394                    calendar.set(Calendar.MINUTE, dateMinute);
395                    calendar.set(Calendar.SECOND, 0);
396                    calendar.set(Calendar.MILLISECOND, 0);
397    
398                    return calendar;
399            }
400    
401            protected static DateRange getDateRange(
402                            String range, int rangeLast, int startDateAmPm, int startDateYear,
403                            int startDateMonth, int startDateDay, int startDateHour,
404                            int startDateMinute, int endDateAmPm, int endDateYear,
405                            int endDateMonth, int endDateDay, int endDateHour,
406                            int endDateMinute, String portletId, long groupId, long plid,
407                            boolean privateLayout, Locale locale, TimeZone timeZone)
408                    throws PortalException {
409    
410                    Date startDate = null;
411                    Date endDate = null;
412    
413                    if (range.equals(RANGE_DATE_RANGE)) {
414                            Calendar startCalendar = getCalendar(
415                                    startDateAmPm, startDateYear, startDateMonth, startDateDay,
416                                    startDateHour, startDateMinute, locale, timeZone, true);
417    
418                            startDate = startCalendar.getTime();
419    
420                            Calendar endCalendar = getCalendar(
421                                    endDateAmPm, endDateYear, endDateMonth, endDateDay, endDateHour,
422                                    endDateMinute, locale, timeZone, true);
423    
424                            endDate = endCalendar.getTime();
425                    }
426                    else if (range.equals(RANGE_FROM_LAST_PUBLISH_DATE)) {
427                            Date lastPublishDate = null;
428    
429                            if (Validator.isNotNull(portletId)) {
430                                    Layout layout = LayoutLocalServiceUtil.fetchLayout(plid);
431    
432                                    PortletPreferences portletPreferences = null;
433    
434                                    if (layout == null) {
435                                            Group group = GroupLocalServiceUtil.getGroup(groupId);
436    
437                                            portletPreferences =
438                                                    PortletPreferencesFactoryUtil.getStrictPortletSetup(
439                                                            group.getCompanyId(), groupId, portletId);
440                                    }
441                                    else {
442                                            portletPreferences =
443                                                    PortletPreferencesFactoryUtil.getStrictPortletSetup(
444                                                            layout, portletId);
445                                    }
446    
447                                    lastPublishDate = getLastPublishDate(portletPreferences);
448                            }
449                            else {
450                                    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
451                                            groupId, privateLayout);
452    
453                                    lastPublishDate = getLastPublishDate(layoutSet);
454                            }
455    
456                            if (lastPublishDate != null) {
457                                    endDate = new Date();
458    
459                                    startDate = lastPublishDate;
460                            }
461                    }
462                    else if (range.equals(RANGE_LAST)) {
463                            Date now = new Date();
464    
465                            startDate = new Date(now.getTime() - (rangeLast * Time.HOUR));
466    
467                            endDate = now;
468                    }
469    
470                    return new DateRange(startDate, endDate);
471            }
472    
473            protected static String getDefaultDateRange(
474                    ExportImportConfiguration exportImportConfiguration) {
475    
476                    if (exportImportConfiguration.getType() == TYPE_EXPORT_LAYOUT) {
477                            return RANGE_ALL;
478                    }
479                    else if (exportImportConfiguration.getType() == TYPE_EXPORT_PORTLET) {
480                            return RANGE_ALL;
481                    }
482                    else if (exportImportConfiguration.getType() == TYPE_IMPORT_LAYOUT) {
483                            return RANGE_ALL;
484                    }
485                    else if (exportImportConfiguration.getType() == TYPE_IMPORT_PORTLET) {
486                            return RANGE_ALL;
487                    }
488                    else if (exportImportConfiguration.getType() ==
489                                            TYPE_PUBLISH_LAYOUT_LOCAL) {
490    
491                            return RANGE_FROM_LAST_PUBLISH_DATE;
492                    }
493                    else if (exportImportConfiguration.getType() ==
494                                            TYPE_PUBLISH_LAYOUT_REMOTE) {
495    
496                            return RANGE_FROM_LAST_PUBLISH_DATE;
497                    }
498                    else if (exportImportConfiguration.getType() == TYPE_PUBLISH_PORTLET) {
499                            return RANGE_FROM_LAST_PUBLISH_DATE;
500                    }
501                    else if (exportImportConfiguration.getType() ==
502                                            TYPE_SCHEDULED_PUBLISH_LAYOUT_LOCAL) {
503    
504                            return RANGE_FROM_LAST_PUBLISH_DATE;
505                    }
506                    else if (exportImportConfiguration.getType() ==
507                                            TYPE_SCHEDULED_PUBLISH_LAYOUT_REMOTE) {
508    
509                            return RANGE_FROM_LAST_PUBLISH_DATE;
510                    }
511    
512                    return RANGE_ALL;
513            }
514    
515            protected static boolean isValidDateRange(
516                    DateRange dateRange, Date originalLastPublishDate) {
517    
518                    if (dateRange == null) {
519    
520                            // This is a valid scenario when publishing all
521    
522                            return true;
523                    }
524    
525                    Date startDate = dateRange.getStartDate();
526                    Date endDate = dateRange.getEndDate();
527    
528                    if (originalLastPublishDate != null) {
529                            if ((startDate != null) &&
530                                    startDate.after(originalLastPublishDate)) {
531    
532                                    return false;
533                            }
534    
535                            if ((endDate != null) && endDate.before(originalLastPublishDate)) {
536                                    return false;
537                            }
538                    }
539                    else if ((startDate != null) || (endDate != null)) {
540                            return false;
541                    }
542    
543                    return true;
544            }
545    
546            private static final String _LAST_PUBLISH_DATE = "last-publish-date";
547    
548            private static final Log _log = LogFactoryUtil.getLog(
549                    ExportImportDateUtil.class);
550    
551    }