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.lar;
016    
017    import com.liferay.portal.kernel.lar.ManifestSummary;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataContextFactory;
020    import com.liferay.portal.kernel.lar.PortletDataException;
021    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
022    import com.liferay.portal.kernel.lar.UserIdStrategy;
023    import com.liferay.portal.kernel.util.MapUtil;
024    import com.liferay.portal.kernel.zip.ZipReader;
025    import com.liferay.portal.kernel.zip.ZipWriter;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.model.Layout;
028    import com.liferay.portal.security.auth.CompanyThreadLocal;
029    import com.liferay.portal.service.GroupLocalServiceUtil;
030    import com.liferay.portal.theme.ThemeDisplay;
031    
032    import java.util.ArrayList;
033    import java.util.Date;
034    import java.util.Map;
035    
036    /**
037     * @author Mate Thurzo
038     */
039    public class PortletDataContextFactoryImpl
040            implements PortletDataContextFactory {
041    
042            @Override
043            public PortletDataContext clonePortletDataContext(
044                    PortletDataContext portletDataContext) {
045    
046                    PortletDataContext clonePortletDataContext =
047                            new PortletDataContextImpl();
048    
049                    clonePortletDataContext.setCompanyId(portletDataContext.getCompanyId());
050                    clonePortletDataContext.setCompanyGroupId(
051                            portletDataContext.getCompanyGroupId());
052                    clonePortletDataContext.setDataStrategy(
053                            portletDataContext.getDataStrategy());
054                    clonePortletDataContext.setEndDate(portletDataContext.getEndDate());
055                    clonePortletDataContext.setGroupId(portletDataContext.getGroupId());
056    
057                    ManifestSummary manifestSummary =
058                            portletDataContext.getManifestSummary();
059    
060                    clonePortletDataContext.setManifestSummary(
061                            (ManifestSummary)manifestSummary.clone());
062    
063                    clonePortletDataContext.setNewLayouts(
064                            portletDataContext.getNewLayouts());
065                    clonePortletDataContext.setParameterMap(
066                            portletDataContext.getParameterMap());
067                    clonePortletDataContext.setScopeGroupId(
068                            portletDataContext.getScopeGroupId());
069                    clonePortletDataContext.setStartDate(portletDataContext.getStartDate());
070                    clonePortletDataContext.setUserIdStrategy(
071                            portletDataContext.getUserIdStrategy());
072                    clonePortletDataContext.setUserPersonalSiteGroupId(
073                            portletDataContext.getUserPersonalSiteGroupId());
074    
075                    return clonePortletDataContext;
076            }
077    
078            @Override
079            public PortletDataContext createExportPortletDataContext(
080                            long companyId, long groupId, Map<String, String[]> parameterMap,
081                            Date startDate, Date endDate, ZipWriter zipWriter)
082                    throws PortletDataException {
083    
084                    validateDateRange(startDate, endDate);
085    
086                    PortletDataContext portletDataContext = createPortletDataContext(
087                            companyId, groupId);
088    
089                    portletDataContext.setEndDate(endDate);
090                    portletDataContext.setParameterMap(parameterMap);
091                    portletDataContext.setStartDate(startDate);
092                    portletDataContext.setZipWriter(zipWriter);
093    
094                    return portletDataContext;
095            }
096    
097            @Override
098            public PortletDataContext createImportPortletDataContext(
099                    long companyId, long groupId, Map<String, String[]> parameterMap,
100                    UserIdStrategy userIdStrategy, ZipReader zipReader) {
101    
102                    PortletDataContext portletDataContext = createPortletDataContext(
103                            companyId, groupId);
104    
105                    String dataStrategy = MapUtil.getString(
106                            parameterMap, PortletDataHandlerKeys.DATA_STRATEGY,
107                            PortletDataHandlerKeys.DATA_STRATEGY_MIRROR);
108    
109                    portletDataContext.setDataStrategy(dataStrategy);
110    
111                    portletDataContext.setNewLayouts(new ArrayList<Layout>());
112                    portletDataContext.setParameterMap(parameterMap);
113                    portletDataContext.setUserIdStrategy(userIdStrategy);
114                    portletDataContext.setZipReader(zipReader);
115    
116                    return portletDataContext;
117            }
118    
119            @Override
120            public PortletDataContext createPreparePortletDataContext(
121                            long companyId, long groupId, Date startDate, Date endDate)
122                    throws PortletDataException {
123    
124                    validateDateRange(startDate, endDate);
125    
126                    PortletDataContext portletDataContext = createPortletDataContext(
127                            companyId, groupId);
128    
129                    portletDataContext.setEndDate(endDate);
130                    portletDataContext.setStartDate(startDate);
131    
132                    return portletDataContext;
133            }
134    
135            @Override
136            public PortletDataContext createPreparePortletDataContext(
137                            ThemeDisplay themeDisplay, Date startDate, Date endDate)
138                    throws PortletDataException {
139    
140                    return createPreparePortletDataContext(
141                            themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId(),
142                            startDate, endDate);
143            }
144    
145            protected PortletDataContext createPortletDataContext(
146                    long companyId, long groupId) {
147    
148                    PortletDataContext portletDataContext = new PortletDataContextImpl();
149    
150                    try {
151                            Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
152                                    companyId);
153    
154                            portletDataContext.setCompanyGroupId(companyGroup.getGroupId());
155                    }
156                    catch (Exception e) {
157                            if (!CompanyThreadLocal.isDeleteInProcess()) {
158                                    throw new IllegalStateException(e);
159                            }
160                    }
161    
162                    portletDataContext.setCompanyId(companyId);
163                    portletDataContext.setGroupId(groupId);
164                    portletDataContext.setScopeGroupId(groupId);
165    
166                    try {
167                            Group userPersonalSiteGroup =
168                                    GroupLocalServiceUtil.getUserPersonalSiteGroup(companyId);
169    
170                            portletDataContext.setUserPersonalSiteGroupId(
171                                    userPersonalSiteGroup.getGroupId());
172                    }
173                    catch (Exception e) {
174                            if (!CompanyThreadLocal.isDeleteInProcess()) {
175                                    throw new IllegalStateException(e);
176                            }
177                    }
178    
179                    return portletDataContext;
180            }
181    
182            protected void validateDateRange(Date startDate, Date endDate)
183                    throws PortletDataException {
184    
185                    if ((startDate == null) && (endDate != null)) {
186                            throw new PortletDataException(
187                                    PortletDataException.END_DATE_IS_MISSING_START_DATE);
188                    }
189                    else if ((startDate != null) && (endDate == null)) {
190                            throw new PortletDataException(
191                                    PortletDataException.START_DATE_IS_MISSING_END_DATE);
192                    }
193    
194                    if (startDate != null) {
195                            if (startDate.after(endDate) || startDate.equals(endDate)) {
196                                    throw new PortletDataException(
197                                            PortletDataException.START_DATE_AFTER_END_DATE);
198                            }
199    
200                            Date now = new Date();
201    
202                            if (startDate.after(now)) {
203                                    throw new PortletDataException(
204                                            PortletDataException.FUTURE_START_DATE);
205                            }
206    
207                            if (endDate.after(now)) {
208                                    throw new PortletDataException(
209                                            PortletDataException.FUTURE_END_DATE);
210                            }
211                    }
212            }
213    
214    }