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