1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.action;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.model.LayoutConstants;
31  import com.liferay.portal.model.LayoutTypePortlet;
32  import com.liferay.portal.model.PortletConstants;
33  import com.liferay.portal.model.User;
34  import com.liferay.portal.security.auth.PrincipalException;
35  import com.liferay.portal.service.LayoutLocalServiceUtil;
36  import com.liferay.portal.service.ServiceContext;
37  import com.liferay.portal.service.UserLocalServiceUtil;
38  import com.liferay.portal.struts.ActionConstants;
39  import com.liferay.portal.theme.ThemeDisplay;
40  import com.liferay.portal.util.PortalUtil;
41  import com.liferay.portal.util.PropsValues;
42  import com.liferay.portal.util.WebKeys;
43  
44  import java.util.Calendar;
45  import java.util.Locale;
46  
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletResponse;
49  
50  import org.apache.struts.action.Action;
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="TCKAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class TCKAction extends Action {
62  
63      public ActionForward execute(
64              ActionMapping mapping, ActionForm form, HttpServletRequest request,
65              HttpServletResponse response)
66          throws Exception {
67  
68          try {
69              if (!PropsValues.TCK_URL) {
70                  throw new PrincipalException("TCK testing is disabled");
71              }
72  
73              User user = _getUser(request);
74  
75              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
76                  WebKeys.THEME_DISPLAY);
77  
78              String[] portletIds = request.getParameterValues("portletId");
79  
80              if (portletIds == null) {
81                  portletIds = request.getParameterValues("portletName");
82              }
83  
84              for (int i = 0; i < portletIds.length; i++) {
85                  String[] nameAndWar = StringUtil.split(portletIds[i], "/");
86  
87                  portletIds[i] = PortalUtil.getJsSafePortletId(
88                      nameAndWar[1] + PortletConstants.WAR_SEPARATOR +
89                          nameAndWar[0]);
90              }
91  
92              long userId = user.getUserId();
93              long groupId = user.getGroup().getGroupId();
94  
95              Layout layout = LayoutLocalServiceUtil.addLayout(
96                  userId, groupId, false,
97                  LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "TCKAction",
98                  StringPool.BLANK, StringPool.BLANK,
99                  LayoutConstants.TYPE_PORTLET, false, StringPool.BLANK);
100 
101             LayoutTypePortlet layoutType =
102                 (LayoutTypePortlet)layout.getLayoutType();
103 
104             for (int i = 0; i < portletIds.length; i++) {
105                 layoutType.addPortletId(userId, portletIds[i]);
106             }
107 
108             LayoutLocalServiceUtil.updateLayout(
109                 layout.getGroupId(), layout.isPrivateLayout(),
110                 layout.getLayoutId(), layout.getTypeSettings());
111 
112             request.setAttribute(
113                 WebKeys.FORWARD_URL,
114                 themeDisplay.getPathMain() + "/portal/layout?p_l_id=" +
115                     layout.getPlid());
116 
117             return mapping.findForward(ActionConstants.COMMON_FORWARD_JSP);
118         }
119         catch (Exception e) {
120             if (_log.isWarnEnabled()) {
121                 _log.warn(e, e);
122             }
123 
124             PortalUtil.sendError(e, request, response);
125 
126             return null;
127         }
128     }
129 
130     private User _getUser(HttpServletRequest request) throws Exception {
131         long companyId = PortalUtil.getCompanyId(request);
132 
133         try {
134             return UserLocalServiceUtil.getUserByScreenName(companyId, "tck");
135         }
136         catch (Exception e) {
137             long creatorUserId = 0;
138             boolean autoPassword = false;
139             String password1 = "password";
140             String password2 = password1;
141             boolean autoScreenName = false;
142             String screenName = "tck";
143             String emailAddress = "tck@liferay.com";
144             String openId = StringPool.BLANK;
145             Locale locale = Locale.US;
146             String firstName = "TCK";
147             String middleName = StringPool.BLANK;
148             String lastName = "User";
149             int prefixId = 0;
150             int suffixId = 0;
151             boolean male = true;
152             int birthdayMonth = Calendar.JANUARY;
153             int birthdayDay = 1;
154             int birthdayYear = 1970;
155             String jobTitle = StringPool.BLANK;
156             long[] groupIds = null;
157             long[] organizationIds = null;
158             long[] roleIds = null;
159             long[] userGroupIds = null;
160             boolean sendEmail = false;
161             ServiceContext serviceContext = new ServiceContext();
162 
163             return UserLocalServiceUtil.addUser(
164                 creatorUserId, companyId, autoPassword, password1, password2,
165                 autoScreenName, screenName, emailAddress, openId, locale,
166                 firstName, middleName, lastName, prefixId, suffixId, male,
167                 birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
168                 organizationIds, roleIds, userGroupIds, sendEmail,
169                 serviceContext);
170         }
171     }
172 
173     private static Log _log = LogFactoryUtil.getLog(TCKAction.class);
174 
175 }