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