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.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  public class TCKAction extends Action {
60  
61      public ActionForward execute(
62              ActionMapping mapping, ActionForm form, HttpServletRequest request,
63              HttpServletResponse response)
64          throws Exception {
65  
66          try {
67              if (!PropsValues.TCK_URL) {
68                  throw new PrincipalException("TCK testing is disabled");
69              }
70  
71              User user = _getUser(request);
72  
73              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
74                  WebKeys.THEME_DISPLAY);
75  
76              String[] portletIds = request.getParameterValues("portletId");
77  
78              if (portletIds == null) {
79                  portletIds = request.getParameterValues("portletName");
80              }
81  
82              for (int i = 0; i < portletIds.length; i++) {
83                  String[] nameAndWar = StringUtil.split(portletIds[i], "/");
84  
85                  portletIds[i] = PortalUtil.getJsSafePortletId(
86                      nameAndWar[1] + PortletConstants.WAR_SEPARATOR +
87                          nameAndWar[0]);
88              }
89  
90              long userId = user.getUserId();
91              long groupId = user.getGroup().getGroupId();
92  
93              Layout layout = LayoutLocalServiceUtil.addLayout(
94                  userId, groupId, false,
95                  LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, "TCKAction",
96                  StringPool.BLANK, StringPool.BLANK,
97                  LayoutConstants.TYPE_PORTLET, false, StringPool.BLANK);
98  
99              LayoutTypePortlet layoutType =
100                 (LayoutTypePortlet)layout.getLayoutType();
101 
102             for (int i = 0; i < portletIds.length; i++) {
103                 layoutType.addPortletId(userId, portletIds[i]);
104             }
105 
106             LayoutLocalServiceUtil.updateLayout(
107                 layout.getGroupId(), layout.isPrivateLayout(),
108                 layout.getLayoutId(), layout.getTypeSettings());
109 
110             request.setAttribute(
111                 WebKeys.FORWARD_URL,
112                 themeDisplay.getPathMain() + "/portal/layout?p_l_id=" +
113                     layout.getPlid());
114 
115             return mapping.findForward(ActionConstants.COMMON_FORWARD_JSP);
116         }
117         catch (Exception e) {
118             if (_log.isWarnEnabled()) {
119                 _log.warn(e, e);
120             }
121 
122             PortalUtil.sendError(e, request, response);
123 
124             return null;
125         }
126     }
127 
128     private User _getUser(HttpServletRequest request) throws Exception {
129         long companyId = PortalUtil.getCompanyId(request);
130 
131         try {
132             return UserLocalServiceUtil.getUserByScreenName(companyId, "tck");
133         }
134         catch (Exception e) {
135             long creatorUserId = 0;
136             boolean autoPassword = false;
137             String password1 = "password";
138             String password2 = password1;
139             boolean autoScreenName = false;
140             String screenName = "tck";
141             String emailAddress = "tck@liferay.com";
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[] organizationIds = new long[0];
154             boolean sendEmail = false;
155 
156             return UserLocalServiceUtil.addUser(
157                 creatorUserId, companyId, autoPassword, password1, password2,
158                 autoScreenName, screenName, emailAddress, locale, firstName,
159                 middleName, lastName, prefixId, suffixId, male, birthdayMonth,
160                 birthdayDay, birthdayYear, jobTitle, organizationIds,
161                 sendEmail);
162         }
163     }
164 
165     private static Log _log = LogFactoryUtil.getLog(TCKAction.class);
166 
167 }