1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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 
162             ServiceContext serviceContext = new ServiceContext();
163 
164             return UserLocalServiceUtil.addUser(
165                 creatorUserId, companyId, autoPassword, password1, password2,
166                 autoScreenName, screenName, emailAddress, openId, locale,
167                 firstName, middleName, lastName, prefixId, suffixId, male,
168                 birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
169                 organizationIds, roleIds, userGroupIds, sendEmail,
170                 serviceContext);
171         }
172     }
173 
174     private static Log _log = LogFactoryUtil.getLog(TCKAction.class);
175 
176 }