1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.ResourceLocalServiceUtil;
29  import com.liferay.portal.service.ServiceContext;
30  import com.liferay.portal.service.UserLocalServiceUtil;
31  import com.liferay.portal.service.permission.PortletPermissionUtil;
32  import com.liferay.portal.struts.ActionConstants;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.PropsValues;
36  import com.liferay.portal.util.WebKeys;
37  
38  import java.util.Calendar;
39  import java.util.Locale;
40  
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  import org.apache.struts.action.Action;
45  import org.apache.struts.action.ActionForm;
46  import org.apache.struts.action.ActionForward;
47  import org.apache.struts.action.ActionMapping;
48  
49  /**
50   * <a href="TCKAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   */
54  public class TCKAction extends Action {
55  
56      public ActionForward execute(
57              ActionMapping mapping, ActionForm form, HttpServletRequest request,
58              HttpServletResponse response)
59          throws Exception {
60  
61          try {
62              if (!PropsValues.TCK_URL) {
63                  throw new PrincipalException("TCK testing is disabled");
64              }
65  
66              User user = _getUser(request);
67  
68              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
69                  WebKeys.THEME_DISPLAY);
70  
71              String[] portletIds = request.getParameterValues("portletId");
72  
73              if (portletIds == null) {
74                  portletIds = request.getParameterValues("portletName");
75              }
76  
77              for (int i = 0; i < portletIds.length; i++) {
78                  String[] nameAndWar = StringUtil.split(portletIds[i], "/");
79  
80                  portletIds[i] = PortalUtil.getJsSafePortletId(
81                      nameAndWar[1] + PortletConstants.WAR_SEPARATOR +
82                          nameAndWar[0]);
83              }
84  
85              long userId = user.getUserId();
86              long groupId = user.getGroup().getGroupId();
87  
88              ServiceContext serviceContext = new ServiceContext();
89  
90              Layout layout = LayoutLocalServiceUtil.addLayout(
91                  userId, groupId, false,
92                  LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "TCKAction",
93                  StringPool.BLANK, StringPool.BLANK,
94                  LayoutConstants.TYPE_PORTLET, false, StringPool.BLANK,
95                  serviceContext);
96  
97              LayoutTypePortlet layoutType =
98                  (LayoutTypePortlet)layout.getLayoutType();
99  
100             for (String portletId : portletIds) {
101                 layoutType.addPortletId(userId, portletId, false);
102 
103                 String rootPortletId = PortletConstants.getRootPortletId(
104                     portletId);
105 
106                 String portletPrimaryKey = PortletPermissionUtil.getPrimaryKey(
107                     layout.getPlid(), portletId);
108 
109                 ResourceLocalServiceUtil.addResources(
110                     user.getCompanyId(), groupId, 0, rootPortletId,
111                     portletPrimaryKey, true, true, true);
112             }
113 
114             LayoutLocalServiceUtil.updateLayout(
115                 layout.getGroupId(), layout.isPrivateLayout(),
116                 layout.getLayoutId(), layout.getTypeSettings());
117 
118             request.setAttribute(
119                 WebKeys.FORWARD_URL,
120                 themeDisplay.getPathMain() + "/portal/layout?p_l_id=" +
121                     layout.getPlid());
122 
123             return mapping.findForward(ActionConstants.COMMON_FORWARD_JSP);
124         }
125         catch (Exception e) {
126             if (_log.isWarnEnabled()) {
127                 _log.warn(e, e);
128             }
129 
130             PortalUtil.sendError(e, request, response);
131 
132             return null;
133         }
134     }
135 
136     private User _getUser(HttpServletRequest request) throws Exception {
137         long companyId = PortalUtil.getCompanyId(request);
138 
139         try {
140             return UserLocalServiceUtil.getUserByScreenName(companyId, "tck");
141         }
142         catch (Exception e) {
143             long creatorUserId = 0;
144             boolean autoPassword = false;
145             String password1 = "password";
146             String password2 = password1;
147             boolean autoScreenName = false;
148             String screenName = "tck";
149             String emailAddress = "tck@liferay.com";
150             String openId = StringPool.BLANK;
151             Locale locale = Locale.US;
152             String firstName = "TCK";
153             String middleName = StringPool.BLANK;
154             String lastName = "User";
155             int prefixId = 0;
156             int suffixId = 0;
157             boolean male = true;
158             int birthdayMonth = Calendar.JANUARY;
159             int birthdayDay = 1;
160             int birthdayYear = 1970;
161             String jobTitle = StringPool.BLANK;
162             long[] groupIds = null;
163             long[] organizationIds = null;
164             long[] roleIds = null;
165             long[] userGroupIds = null;
166             boolean sendEmail = false;
167 
168             ServiceContext serviceContext = new ServiceContext();
169 
170             return UserLocalServiceUtil.addUser(
171                 creatorUserId, companyId, autoPassword, password1, password2,
172                 autoScreenName, screenName, emailAddress, openId, locale,
173                 firstName, middleName, lastName, prefixId, suffixId, male,
174                 birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
175                 organizationIds, roleIds, userGroupIds, sendEmail,
176                 serviceContext);
177         }
178     }
179 
180     private static Log _log = LogFactoryUtil.getLog(TCKAction.class);
181 
182 }