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