001    /**
002     * Copyright (c) 2000-2010 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            public ActionForward execute(
055                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
056                            HttpServletResponse response)
057                    throws Exception {
058    
059                    try {
060                            if (!PropsValues.TCK_URL) {
061                                    throw new PrincipalException("TCK testing is disabled");
062                            }
063    
064                            User user = _getUser(request);
065    
066                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
067                                    WebKeys.THEME_DISPLAY);
068    
069                            String[] portletIds = request.getParameterValues("portletId");
070    
071                            if (portletIds == null) {
072                                    portletIds = request.getParameterValues("portletName");
073                            }
074    
075                            for (int i = 0; i < portletIds.length; i++) {
076                                    String[] nameAndWar = StringUtil.split(portletIds[i], "/");
077    
078                                    portletIds[i] = PortalUtil.getJsSafePortletId(
079                                            nameAndWar[1] + PortletConstants.WAR_SEPARATOR +
080                                                    nameAndWar[0]);
081                            }
082    
083                            long userId = user.getUserId();
084                            long groupId = user.getGroup().getGroupId();
085    
086                            ServiceContext serviceContext = new ServiceContext();
087    
088                            Layout layout = LayoutLocalServiceUtil.addLayout(
089                                    userId, groupId, false,
090                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "TCKAction",
091                                    StringPool.BLANK, StringPool.BLANK,
092                                    LayoutConstants.TYPE_PORTLET, false, StringPool.BLANK,
093                                    serviceContext);
094    
095                            LayoutTypePortlet layoutType =
096                                    (LayoutTypePortlet)layout.getLayoutType();
097    
098                            for (String portletId : portletIds) {
099                                    layoutType.addPortletId(userId, portletId, false);
100    
101                                    String rootPortletId = PortletConstants.getRootPortletId(
102                                            portletId);
103    
104                                    String portletPrimaryKey = PortletPermissionUtil.getPrimaryKey(
105                                            layout.getPlid(), portletId);
106    
107                                    ResourceLocalServiceUtil.addResources(
108                                            user.getCompanyId(), groupId, 0, rootPortletId,
109                                            portletPrimaryKey, true, true, true);
110                            }
111    
112                            LayoutLocalServiceUtil.updateLayout(
113                                    layout.getGroupId(), layout.isPrivateLayout(),
114                                    layout.getLayoutId(), layout.getTypeSettings());
115    
116                            request.setAttribute(
117                                    WebKeys.FORWARD_URL,
118                                    themeDisplay.getPathMain() + "/portal/layout?p_l_id=" +
119                                            layout.getPlid());
120    
121                            return mapping.findForward(ActionConstants.COMMON_FORWARD_JSP);
122                    }
123                    catch (Exception e) {
124                            if (_log.isWarnEnabled()) {
125                                    _log.warn(e, e);
126                            }
127    
128                            PortalUtil.sendError(e, request, response);
129    
130                            return null;
131                    }
132            }
133    
134            private User _getUser(HttpServletRequest request) throws Exception {
135                    long companyId = PortalUtil.getCompanyId(request);
136    
137                    try {
138                            return UserLocalServiceUtil.getUserByScreenName(companyId, "tck");
139                    }
140                    catch (Exception e) {
141                            long creatorUserId = 0;
142                            boolean autoPassword = false;
143                            String password1 = "password";
144                            String password2 = password1;
145                            boolean autoScreenName = false;
146                            String screenName = "tck";
147                            String emailAddress = "tck@liferay.com";
148                            long facebookId = 0;
149                            String openId = StringPool.BLANK;
150                            Locale locale = Locale.US;
151                            String firstName = "TCK";
152                            String middleName = StringPool.BLANK;
153                            String lastName = "User";
154                            int prefixId = 0;
155                            int suffixId = 0;
156                            boolean male = true;
157                            int birthdayMonth = Calendar.JANUARY;
158                            int birthdayDay = 1;
159                            int birthdayYear = 1970;
160                            String jobTitle = StringPool.BLANK;
161                            long[] groupIds = null;
162                            long[] organizationIds = null;
163                            long[] roleIds = null;
164                            long[] userGroupIds = null;
165                            boolean sendEmail = false;
166    
167                            ServiceContext serviceContext = new ServiceContext();
168    
169                            return UserLocalServiceUtil.addUser(
170                                    creatorUserId, companyId, autoPassword, password1, password2,
171                                    autoScreenName, screenName, emailAddress, facebookId, openId,
172                                    locale, firstName, middleName, lastName, prefixId, suffixId,
173                                    male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
174                                    groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
175                                    serviceContext);
176                    }
177            }
178    
179            private static Log _log = LogFactoryUtil.getLog(TCKAction.class);
180    
181    }