001    /**
002     * Copyright (c) 2000-2012 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 mapping, ActionForm form, HttpServletRequest request,
057                            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 mapping.findForward(ActionConstants.COMMON_FORWARD_JSP);
123                    }
124                    catch (Exception e) {
125                            if (_log.isWarnEnabled()) {
126                                    _log.warn(e, e);
127                            }
128    
129                            PortalUtil.sendError(e, request, response);
130    
131                            return null;
132                    }
133            }
134    
135            private User _getUser(HttpServletRequest request) throws Exception {
136                    long companyId = PortalUtil.getCompanyId(request);
137    
138                    try {
139                            return UserLocalServiceUtil.getUserByScreenName(companyId, "tck");
140                    }
141                    catch (Exception e) {
142                            long creatorUserId = 0;
143                            boolean autoPassword = false;
144                            String password1 = "password";
145                            String password2 = password1;
146                            boolean autoScreenName = false;
147                            String screenName = "tck";
148                            String emailAddress = "tck@liferay.com";
149                            long facebookId = 0;
150                            String openId = StringPool.BLANK;
151                            Locale locale = Locale.US;
152                            String firstName = "TCK";
153                            String middleName = StringPool.BLANK;
154                            String lastName = "User";
155                            int prefixId = 0;
156                            int suffixId = 0;
157                            boolean male = true;
158                            int birthdayMonth = Calendar.JANUARY;
159                            int birthdayDay = 1;
160                            int birthdayYear = 1970;
161                            String jobTitle = StringPool.BLANK;
162                            long[] groupIds = null;
163                            long[] organizationIds = null;
164                            long[] roleIds = null;
165                            long[] userGroupIds = null;
166                            boolean sendEmail = false;
167    
168                            ServiceContext serviceContext = new ServiceContext();
169    
170                            return UserLocalServiceUtil.addUser(
171                                    creatorUserId, companyId, autoPassword, password1, password2,
172                                    autoScreenName, screenName, emailAddress, facebookId, openId,
173                                    locale, firstName, middleName, lastName, prefixId, suffixId,
174                                    male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
175                                    groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
176                                    serviceContext);
177                    }
178            }
179    
180            private static Log _log = LogFactoryUtil.getLog(TCKAction.class);
181    
182    }