001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.action;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    import com.liferay.portal.kernel.servlet.HttpHeaders;
021    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
022    import com.liferay.portal.kernel.servlet.SessionErrors;
023    import com.liferay.portal.kernel.util.Constants;
024    import com.liferay.portal.kernel.util.ContentTypes;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.setup.SetupWizardUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.PropsValues;
032    import com.liferay.portal.util.WebKeys;
033    
034    import java.sql.SQLException;
035    
036    import javax.servlet.http.HttpServletRequest;
037    import javax.servlet.http.HttpServletResponse;
038    
039    import org.apache.struts.action.Action;
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Manuel de la Pe??a
046     * @author Brian Wing Shun Chan
047     */
048    public class SetupWizardAction extends Action {
049    
050            @Override
051            public ActionForward execute(
052                            ActionMapping actionMapping, ActionForm actionForm,
053                            HttpServletRequest request, HttpServletResponse response)
054                    throws Exception {
055    
056                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
057                            WebKeys.THEME_DISPLAY);
058    
059                    if (!PropsValues.SETUP_WIZARD_ENABLED) {
060                            response.sendRedirect(themeDisplay.getPathMain());
061                    }
062    
063                    String cmd = ParamUtil.getString(request, Constants.CMD);
064    
065                    try {
066                            if (Validator.isNull(cmd)) {
067                                    return actionMapping.findForward("portal.setup_wizard");
068                            }
069                            else if (cmd.equals(Constants.TRANSLATE)) {
070                                    SetupWizardUtil.updateLanguage(request, response);
071    
072                                    return actionMapping.findForward("portal.setup_wizard");
073                            }
074                            else if (cmd.equals(Constants.TEST)) {
075                                    testDatabase(request, response);
076    
077                                    return null;
078                            }
079                            else if (cmd.equals(Constants.UPDATE)) {
080                                    SetupWizardUtil.updateSetup(request, response);
081                            }
082    
083                            response.sendRedirect(
084                                    themeDisplay.getPathMain() + "/portal/setup_wizard");
085    
086                            return null;
087                    }
088                    catch (Exception e) {
089                            if (e instanceof PrincipalException) {
090                                    SessionErrors.add(request, e.getClass());
091    
092                                    return actionMapping.findForward("portal.setup_wizard");
093                            }
094                            else {
095                                    PortalUtil.sendError(e, request, response);
096    
097                                    return null;
098                            }
099                    }
100            }
101    
102            protected void putMessage(
103                    HttpServletRequest request, JSONObject jsonObject, String key,
104                    Object... arguments) {
105    
106                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
107                            WebKeys.THEME_DISPLAY);
108    
109                    String message = LanguageUtil.format(
110                            themeDisplay.getLocale(), key, arguments);
111    
112                    jsonObject.put("message", message);
113            }
114    
115            protected void testDatabase(
116                            HttpServletRequest request, HttpServletResponse response)
117                    throws Exception {
118    
119                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
120    
121                    try {
122                            SetupWizardUtil.testDatabase(request);
123    
124                            jsonObject.put("success", true);
125    
126                            putMessage(
127                                    request, jsonObject,
128                                    "database-connection-was-established-sucessfully");
129                    }
130                    catch (ClassNotFoundException cnfe) {
131                            putMessage(
132                                    request, jsonObject, "database-driver-x-is-not-present",
133                                    cnfe.getLocalizedMessage());
134                    }
135                    catch (SQLException sqle) {
136                            putMessage(
137                                    request, jsonObject,
138                                    "database-connection-could-not-be-established");
139                    }
140    
141                    response.setContentType(ContentTypes.APPLICATION_JSON);
142                    response.setHeader(
143                            HttpHeaders.CACHE_CONTROL,
144                            HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
145    
146                    ServletResponseUtil.write(response, jsonObject.toString());
147            }
148    
149    }