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