1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.action;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.model.LayoutConstants;
23  import com.liferay.portal.model.LayoutTypePortlet;
24  import com.liferay.portal.model.PortletConstants;
25  import com.liferay.portal.model.User;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.service.LayoutLocalServiceUtil;
28  import com.liferay.portal.service.UserLocalServiceUtil;
29  import com.liferay.portal.struts.ActionConstants;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.portal.util.WebKeys;
34  
35  import java.util.Calendar;
36  import java.util.Locale;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  
41  import org.apache.struts.action.Action;
42  import org.apache.struts.action.ActionForm;
43  import org.apache.struts.action.ActionForward;
44  import org.apache.struts.action.ActionMapping;
45  
46  /**
47   * <a href="TCKAction.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   */
51  public class TCKAction extends Action {
52  
53      public ActionForward execute(
54              ActionMapping mapping, ActionForm form, HttpServletRequest request,
55              HttpServletResponse response)
56          throws Exception {
57  
58          try {
59              if (!PropsValues.TCK_URL) {
60                  throw new PrincipalException("TCK testing is disabled");
61              }
62  
63              User user = _getUser(request);
64  
65              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
66                  WebKeys.THEME_DISPLAY);
67  
68              String[] portletIds = request.getParameterValues("portletId");
69  
70              if (portletIds == null) {
71                  portletIds = request.getParameterValues("portletName");
72              }
73  
74              for (int i = 0; i < portletIds.length; i++) {
75                  String[] nameAndWar = StringUtil.split(portletIds[i], "/");
76  
77                  portletIds[i] = PortalUtil.getJsSafePortletId(
78                      nameAndWar[1] + PortletConstants.WAR_SEPARATOR +
79                          nameAndWar[0]);
80              }
81  
82              long userId = user.getUserId();
83              long groupId = user.getGroup().getGroupId();
84  
85              Layout layout = LayoutLocalServiceUtil.addLayout(
86                  userId, groupId, false,
87                  LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "TCKAction",
88                  StringPool.BLANK, StringPool.BLANK,
89                  LayoutConstants.TYPE_PORTLET, false, StringPool.BLANK);
90  
91              LayoutTypePortlet layoutType =
92                  (LayoutTypePortlet)layout.getLayoutType();
93  
94              for (int i = 0; i < portletIds.length; i++) {
95                  layoutType.addPortletId(userId, portletIds[i]);
96              }
97  
98              LayoutLocalServiceUtil.updateLayout(
99                  layout.getGroupId(), layout.isPrivateLayout(),
100                 layout.getLayoutId(), layout.getTypeSettings());
101 
102             request.setAttribute(
103                 WebKeys.FORWARD_URL,
104                 themeDisplay.getPathMain() + "/portal/layout?p_l_id=" +
105                     layout.getPlid());
106 
107             return mapping.findForward(ActionConstants.COMMON_FORWARD_JSP);
108         }
109         catch (Exception e) {
110             if (_log.isWarnEnabled()) {
111                 _log.warn(e, e);
112             }
113 
114             PortalUtil.sendError(e, request, response);
115 
116             return null;
117         }
118     }
119 
120     private User _getUser(HttpServletRequest request) throws Exception {
121         long companyId = PortalUtil.getCompanyId(request);
122 
123         try {
124             return UserLocalServiceUtil.getUserByScreenName(companyId, "tck");
125         }
126         catch (Exception e) {
127             long creatorUserId = 0;
128             boolean autoPassword = false;
129             String password1 = "password";
130             String password2 = password1;
131             boolean autoScreenName = false;
132             String screenName = "tck";
133             String emailAddress = "tck@liferay.com";
134             Locale locale = Locale.US;
135             String firstName = "TCK";
136             String middleName = StringPool.BLANK;
137             String lastName = "User";
138             int prefixId = 0;
139             int suffixId = 0;
140             boolean male = true;
141             int birthdayMonth = Calendar.JANUARY;
142             int birthdayDay = 1;
143             int birthdayYear = 1970;
144             String jobTitle = StringPool.BLANK;
145             long[] organizationIds = new long[0];
146             boolean sendEmail = false;
147 
148             return UserLocalServiceUtil.addUser(
149                 creatorUserId, companyId, autoPassword, password1, password2,
150                 autoScreenName, screenName, emailAddress, locale, firstName,
151                 middleName, lastName, prefixId, suffixId, male, birthdayMonth,
152                 birthdayDay, birthdayYear, jobTitle, organizationIds,
153                 sendEmail);
154         }
155     }
156 
157     private static Log _log = LogFactoryUtil.getLog(TCKAction.class);
158 
159 }