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                            ThemeDisplay themeDisplay, Date startDate, Date endDate)
122                    throws PortletDataException {
123    
124                    validateDateRange(startDate, endDate);
125    
126                    PortletDataContext portletDataContext = createPortletDataContext(
127                            themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId());
128    
129                    portletDataContext.setEndDate(endDate);
130                    portletDataContext.setStartDate(startDate);
131    
132                    return portletDataContext;
133            }
134    
135            protected PortletDataContext createPortletDataContext(
136                    long companyId, long groupId) {
137    
138                    PortletDataContext portletDataContext = new PortletDataContextImpl();
139    
140                    try {
141                            Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
142                                    companyId);
143    
144                            portletDataContext.setCompanyGroupId(companyGroup.getGroupId());
145                    }
146                    catch (Exception e) {
147                            if (!CompanyThreadLocal.isDeleteInProcess()) {
148                                    throw new IllegalStateException(e);
149                            }
150                    }
151    
152                    portletDataContext.setCompanyId(companyId);
153                    portletDataContext.setGroupId(groupId);
154                    portletDataContext.setScopeGroupId(groupId);
155    
156                    try {
157                            Group userPersonalSiteGroup =
158                                    GroupLocalServiceUtil.getUserPersonalSiteGroup(companyId);
159    
160                            portletDataContext.setUserPersonalSiteGroupId(
161                                    userPersonalSiteGroup.getGroupId());
162                    }
163                    catch (Exception e) {
164                            if (!CompanyThreadLocal.isDeleteInProcess()) {
165                                    throw new IllegalStateException(e);
166                            }
167                    }
168    
169                    return portletDataContext;
170            }
171    
172            protected void validateDateRange(Date startDate, Date endDate)
173                    throws PortletDataException {
174    
175                    if ((startDate == null) && (endDate != null)) {
176                            throw new PortletDataException(
177                                    PortletDataException.END_DATE_IS_MISSING_START_DATE);
178                    }
179                    else if ((startDate != null) && (endDate == null)) {
180                            throw new PortletDataException(
181                                    PortletDataException.START_DATE_IS_MISSING_END_DATE);
182                    }
183    
184                    if (startDate != null) {
185                            if (startDate.after(endDate) || startDate.equals(endDate)) {
186                                    throw new PortletDataException(
187                                            PortletDataException.START_DATE_AFTER_END_DATE);
188                            }
189    
190                            Date now = new Date();
191    
192                            if (startDate.after(now)) {
193                                    throw new PortletDataException(
194                                            PortletDataException.FUTURE_START_DATE);
195                            }
196    
197                            if (endDate.after(now)) {
198                                    throw new PortletDataException(
199                                            PortletDataException.FUTURE_END_DATE);
200                            }
201                    }
202            }
203    
204    }