1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.action;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.model.Layout;
27  import com.liferay.portal.model.LayoutConstants;
28  import com.liferay.portal.model.LayoutTypePortlet;
29  import com.liferay.portal.model.PortletConstants;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.service.LayoutLocalServiceUtil;
33  import com.liferay.portal.service.ServiceContext;
34  import com.liferay.portal.service.UserLocalServiceUtil;
35  import com.liferay.portal.struts.ActionConstants;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.PropsValues;
39  import com.liferay.portal.util.WebKeys;
40  
41  import java.util.Calendar;
42  import java.util.Locale;
43  
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  
47  import org.apache.struts.action.Action;
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="TCKAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class TCKAction extends Action {
59  
60      public ActionForward execute(
61              ActionMapping mapping, ActionForm form, HttpServletRequest request,
62              HttpServletResponse response)
63          throws Exception {
64  
65          try {
66              if (!PropsValues.TCK_URL) {
67                  throw new PrincipalException("TCK testing is disabled");
68              }
69  
70              User user = _getUser(request);
71  
72              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
73                  WebKeys.THEME_DISPLAY);
74  
75              String[] portletIds = request.getParameterValues("portletId");
76  
77              if (portletIds == null) {
78                  portletIds = request.getParameterValues("portletName");
79              }
80  
81              for (int i = 0; i < portletIds.length; i++) {
82                  String[] nameAndWar = StringUtil.split(portletIds[i], "/");
83  
84                  portletIds[i] = PortalUtil.getJsSafePortletId(
85                      nameAndWar[1] + PortletConstants.WAR_SEPARATOR +
86                          nameAndWar[0]);
87              }
88  
89              long userId = user.getUserId();
90              long groupId = user.getGroup().getGroupId();
91  
92              Layout layout = LayoutLocalServiceUtil.addLayout(
93                  userId, groupId, false,
94                  LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "TCKAction",
95                  StringPool.BLANK, StringPool.BLANK,
96                  LayoutConstants.TYPE_PORTLET, false, StringPool.BLANK);
97  
98              LayoutTypePortlet layoutType =
99                  (LayoutTypePortlet)layout.getLayoutType();
100 
101             for (int i = 0; i < portletIds.length; i++) {
102                 layoutType.addPortletId(userId, portletIds[i]);
103             }
104 
105             LayoutLocalServiceUtil.updateLayout(
106                 layout.getGroupId(), layout.isPrivateLayout(),
107                 layout.getLayoutId(), layout.getTypeSettings());
108 
109             request.setAttribute(
110                 WebKeys.FORWARD_URL,
111                 themeDisplay.getPathMain() + "/portal/layout?p_l_id=" +
112                     layout.getPlid());
113 
114             return mapping.findForward(ActionConstants.COMMON_FORWARD_JSP);
115         }
116         catch (Exception e) {
117             if (_log.isWarnEnabled()) {
118                 _log.warn(e, e);
119             }
120 
121             PortalUtil.sendError(e, request, response);
122 
123             return null;
124         }
125     }
126 
127     private User _getUser(HttpServletRequest request) throws Exception {
128         long companyId = PortalUtil.getCompanyId(request);
129 
130         try {
131             return UserLocalServiceUtil.getUserByScreenName(companyId, "tck");
132         }
133         catch (Exception e) {
134             long creatorUserId = 0;
135             boolean autoPassword = false;
136             String password1 = "password";
137             String password2 = password1;
138             boolean autoScreenName = false;
139             String screenName = "tck";
140             String emailAddress = "tck@liferay.com";
141             String openId = StringPool.BLANK;
142             Locale locale = Locale.US;
143             String firstName = "TCK";
144             String middleName = StringPool.BLANK;
145             String lastName = "User";
146             int prefixId = 0;
147             int suffixId = 0;
148             boolean male = true;
149             int birthdayMonth = Calendar.JANUARY;
150             int birthdayDay = 1;
151             int birthdayYear = 1970;
152             String jobTitle = StringPool.BLANK;
153             long[] groupIds = null;
154             long[] organizationIds = null;
155             long[] roleIds = null;
156             long[] userGroupIds = null;
157             boolean sendEmail = false;
158 
159             ServiceContext serviceContext = new ServiceContext();
160 
161             return UserLocalServiceUtil.addUser(
162                 creatorUserId, companyId, autoPassword, password1, password2,
163                 autoScreenName, screenName, emailAddress, openId, locale,
164                 firstName, middleName, lastName, prefixId, suffixId, male,
165                 birthdayMonth, birthdayDay, birthdayYear, jobTitle, groupIds,
166                 organizationIds, roleIds, userGroupIds, sendEmail,
167                 serviceContext);
168         }
169     }
170 
171     private static Log _log = LogFactoryUtil.getLog(TCKAction.class);
172 
173 }