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