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.action;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.model.LayoutConstants;
023    import com.liferay.portal.model.LayoutTypePortlet;
024    import com.liferay.portal.model.PortletConstants;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.LayoutLocalServiceUtil;
028    import com.liferay.portal.service.ResourceLocalServiceUtil;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.service.UserLocalServiceUtil;
031    import com.liferay.portal.service.permission.PortletPermissionUtil;
032    import com.liferay.portal.struts.ActionConstants;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.PropsValues;
036    import com.liferay.portal.util.WebKeys;
037    
038    import java.util.Calendar;
039    import java.util.Locale;
040    
041    import javax.servlet.http.HttpServletRequest;
042    import javax.servlet.http.HttpServletResponse;
043    
044    import org.apache.struts.action.Action;
045    import org.apache.struts.action.ActionForm;
046    import org.apache.struts.action.ActionForward;
047    import org.apache.struts.action.ActionMapping;
048    
049    /**
050     * @author Brian Wing Shun Chan
051     */
052    public class TCKAction extends Action {
053    
054            @Override
055            public ActionForward execute(
056                            ActionMapping actionMapping, ActionForm actionForm,
057                            HttpServletRequest request, HttpServletResponse response)
058                    throws Exception {
059    
060                    try {
061                            if (!PropsValues.TCK_URL) {
062                                    throw new PrincipalException("TCK testing is disabled");
063                            }
064    
065                            User user = _getUser(request);
066    
067                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
068                                    WebKeys.THEME_DISPLAY);
069    
070                            String[] portletIds = request.getParameterValues("portletId");
071    
072                            if (portletIds == null) {
073                                    portletIds = request.getParameterValues("portletName");
074                            }
075    
076                            for (int i = 0; i < portletIds.length; i++) {
077                                    String[] nameAndWar = StringUtil.split(portletIds[i], '/');
078    
079                                    portletIds[i] = PortalUtil.getJsSafePortletId(
080                                            nameAndWar[1] + PortletConstants.WAR_SEPARATOR +
081                                                    nameAndWar[0]);
082                            }
083    
084                            long userId = user.getUserId();
085                            long groupId = user.getGroup().getGroupId();
086    
087                            ServiceContext serviceContext = new ServiceContext();
088    
089                            Layout layout = LayoutLocalServiceUtil.addLayout(
090                                    userId, groupId, false,
091                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "TCKAction",
092                                    StringPool.BLANK, StringPool.BLANK,
093                                    LayoutConstants.TYPE_PORTLET, false, StringPool.BLANK,
094                                    serviceContext);
095    
096                            LayoutTypePortlet layoutType =
097                                    (LayoutTypePortlet)layout.getLayoutType();
098    
099                            for (String portletId : portletIds) {
100                                    layoutType.addPortletId(userId, portletId, false);
101    
102                                    String rootPortletId = PortletConstants.getRootPortletId(
103                                            portletId);
104    
105                                    String portletPrimaryKey = PortletPermissionUtil.getPrimaryKey(
106                                            layout.getPlid(), portletId);
107    
108                                    ResourceLocalServiceUtil.addResources(
109                                            user.getCompanyId(), groupId, 0, rootPortletId,
110                                            portletPrimaryKey, true, true, true);
111                            }
112    
113                            LayoutLocalServiceUtil.updateLayout(
114                                    layout.getGroupId(), layout.isPrivateLayout(),
115                                    layout.getLayoutId(), layout.getTypeSettings());
116    
117                            request.setAttribute(
118                                    WebKeys.FORWARD_URL,
119                                    themeDisplay.getPathMain() + "/portal/layout?p_l_id=" +
120                                            layout.getPlid());
121    
122                            return actionMapping.findForward(
123                                    ActionConstants.COMMON_FORWARD_JSP);
124                    }
125                    catch (Exception e) {
126                            if (_log.isWarnEnabled()) {
127                                    _log.warn(e, e);
128                            }
129    
130                            PortalUtil.sendError(e, request, response);
131    
132                            return null;
133                    }
134            }
135    
136            private User _getUser(HttpServletRequest request) throws Exception {
137                    long companyId = PortalUtil.getCompanyId(request);
138    
139                    try {
140                            return UserLocalServiceUtil.getUserByScreenName(companyId, "tck");
141                    }
142                    catch (Exception e) {
143                            long creatorUserId = 0;
144                            boolean autoPassword = false;
145                            String password1 = "password";
146                            String password2 = password1;
147                            boolean autoScreenName = false;
148                            String screenName = "tck";
149                            String emailAddress = "tck@liferay.com";
150                            long facebookId = 0;
151                            String openId = StringPool.BLANK;
152                            Locale locale = Locale.US;
153                            String firstName = "TCK";
154                            String middleName = StringPool.BLANK;
155                            String lastName = "User";
156                            int prefixId = 0;
157                            int suffixId = 0;
158                            boolean male = true;
159                            int birthdayMonth = Calendar.JANUARY;
160                            int birthdayDay = 1;
161                            int birthdayYear = 1970;
162                            String jobTitle = StringPool.BLANK;
163                            long[] groupIds = null;
164                            long[] organizationIds = null;
165                            long[] roleIds = null;
166                            long[] userGroupIds = null;
167                            boolean sendEmail = false;
168    
169                            ServiceContext serviceContext = new ServiceContext();
170    
171                            return UserLocalServiceUtil.addUser(
172                                    creatorUserId, companyId, autoPassword, password1, password2,
173                                    autoScreenName, screenName, emailAddress, facebookId, openId,
174                                    locale, firstName, middleName, lastName, prefixId, suffixId,
175                                    male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
176                                    groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
177                                    serviceContext);
178                    }
179            }
180    
181            private static Log _log = LogFactoryUtil.getLog(TCKAction.class);
182    
183    }