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.ServiceContext;
29  import com.liferay.portal.service.UserLocalServiceUtil;
30  import com.liferay.portal.struts.ActionConstants;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.PropsValues;
34  import com.liferay.portal.util.WebKeys;
35  
36  import java.util.Calendar;
37  import java.util.Locale;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  
42  import org.apache.struts.action.Action;
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="TCKAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   */
52  public class TCKAction extends Action {
53  
54      public ActionForward execute(
55              ActionMapping mapping, ActionForm form, HttpServletRequest request,
56              HttpServletResponse response)
57          throws Exception {
58  
59          try {
60              if (!PropsValues.TCK_URL) {
61                  throw new PrincipalException("TCK testing is disabled");
62              }
63  
64              User user = _getUser(request);
65  
66              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
67                  WebKeys.THEME_DISPLAY);
68  
69              String[] portletIds = request.getParameterValues("portletId");
70  
71              if (portletIds == null) {
72                  portletIds = request.getParameterValues("portletName");
73              }
74  
75              for (int i = 0; i < portletIds.length; i++) {
76                  String[] nameAndWar = StringUtil.split(portletIds[i], "/");
77  
78                  portletIds[i] = PortalUtil.getJsSafePortletId(
79                      nameAndWar[1] + PortletConstants.WAR_SEPARATOR +
80                          nameAndWar[0]);
81              }
82  
83              long userId = user.getUserId();
84              long groupId = user.getGroup().getGroupId();
85  
86              Layout layout = LayoutLocalServiceUtil.addLayout(
87                  userId, groupId, false,
88                  LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "TCKAction",
89                  StringPool.BLANK, StringPool.BLANK,
90                  LayoutConstants.TYPE_PORTLET, false, StringPool.BLANK);
91  
92              LayoutTypePortlet layoutType =
93                  (LayoutTypePortlet)layout.getLayoutType();
94  
95              for (int i = 0; i < portletIds.length; i++) {
96                  layoutType.addPortletId(userId, portletIds[i]);
97              }
98  
99              LayoutLocalServiceUtil.updateLayout(
100                 layout.getGroupId(), layout.isPrivateLayout(),
101                 layout.getLayoutId(), layout.getTypeSettings());
102 
103             request.setAttribute(
104                 WebKeys.FORWARD_URL,
105                 themeDisplay.getPathMain() + "/portal/layout?p_l_id=" +
106                     layout.getPlid());
107 
108             return mapping.findForward(ActionConstants.COMMON_FORWARD_JSP);
109         }
110         catch (Exception e) {
111             if (_log.isWarnEnabled()) {
112                 _log.warn(e, e);
113             }
114 
115             PortalUtil.sendError(e, request, response);
116 
117             return null;
118         }
119     }
120 
121     private User _getUser(HttpServletRequest request) throws Exception {
122         long companyId = PortalUtil.getCompanyId(request);
123 
124         try {
125             return UserLocalServiceUtil.getUserByScreenName(companyId, "tck");
126         }
127         catch (Exception e) {
128             long creatorUserId = 0;
129             boolean autoPassword = false;
130             String password1 = "password";
131             String password2 = password1;
132             boolean autoScreenName = false;
133             String screenName = "tck";
134             String emailAddress = "tck@liferay.com";
135             String openId = StringPool.BLANK;
136             Locale locale = Locale.US;
137             String firstName = "TCK";
138             String middleName = StringPool.BLANK;
139             String lastName = "User";
140             int prefixId = 0;
141             int suffixId = 0;
142             boolean male = true;
143             int birthdayMonth = Calendar.JANUARY;
144             int birthdayDay = 1;
145             int birthdayYear = 1970;
146             String jobTitle = StringPool.BLANK;
147             long[] groupIds = null;
148             long[] organizationIds = null;
149             long[] roleIds = null;
150             long[] userGroupIds = null;
151             boolean sendEmail = false;
152 
153             ServiceContext serviceContext = new ServiceContext();
154 
155             return UserLocalServiceUtil.addUser(
156                 creatorUserId, companyId, autoPassword, password1, password2,
157                 autoScreenName, screenName, emailAddress, openId, locale,
158                 firstName, middleName, lastName, prefixId, suffixId, male,
159                 birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
160                 organizationIds, roleIds, userGroupIds, sendEmail,
161                 serviceContext);
162         }
163     }
164 
165     private static Log _log = LogFactoryUtil.getLog(TCKAction.class);
166 
167 }