001    /**
002     * Copyright (c) 2000-present 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.deploy.hot.HotDeployUtil;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.security.auth.PrincipalException;
021    import com.liferay.portal.kernel.servlet.HttpHeaders;
022    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
023    import com.liferay.portal.kernel.servlet.SessionErrors;
024    import com.liferay.portal.kernel.theme.ThemeDisplay;
025    import com.liferay.portal.kernel.util.Constants;
026    import com.liferay.portal.kernel.util.ContentTypes;
027    import com.liferay.portal.kernel.util.ParamUtil;
028    import com.liferay.portal.kernel.util.PortalLifecycleUtil;
029    import com.liferay.portal.kernel.util.PortalUtil;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.kernel.util.WebKeys;
032    import com.liferay.portal.setup.SetupWizardUtil;
033    import com.liferay.portal.util.PropsValues;
034    
035    import java.sql.SQLException;
036    
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    
040    import org.apache.struts.action.Action;
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Manuel de la Pe??a
047     * @author Brian Wing Shun Chan
048     */
049    public class SetupWizardAction extends Action {
050    
051            @Override
052            public ActionForward execute(
053                            ActionMapping actionMapping, ActionForm actionForm,
054                            HttpServletRequest request, HttpServletResponse response)
055                    throws Exception {
056    
057                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
058                            WebKeys.THEME_DISPLAY);
059    
060                    if (!PropsValues.SETUP_WIZARD_ENABLED) {
061                            response.sendRedirect(themeDisplay.getPathMain());
062                    }
063    
064                    String cmd = ParamUtil.getString(request, Constants.CMD);
065    
066                    try {
067                            if (Validator.isNull(cmd)) {
068                                    return actionMapping.findForward("portal.setup_wizard");
069                            }
070                            else if (cmd.equals(Constants.TRANSLATE)) {
071                                    SetupWizardUtil.updateLanguage(request, response);
072    
073                                    return actionMapping.findForward("portal.setup_wizard");
074                            }
075                            else if (cmd.equals(Constants.TEST)) {
076                                    testDatabase(request, response);
077    
078                                    return null;
079                            }
080                            else if (cmd.equals(Constants.UPDATE)) {
081                                    SetupWizardUtil.updateSetup(request, response);
082    
083                                    if (ParamUtil.getBoolean(request, "defaultDatabase")) {
084                                            PropsValues.SETUP_WIZARD_ENABLED = false;
085    
086                                            HotDeployUtil.setCapturePrematureEvents(false);
087    
088                                            PortalLifecycleUtil.flushInits();
089                                    }
090                            }
091    
092                            response.sendRedirect(
093                                    themeDisplay.getPathMain() + "/portal/setup_wizard");
094    
095                            return null;
096                    }
097                    catch (Exception e) {
098                            if (e instanceof PrincipalException) {
099                                    SessionErrors.add(request, e.getClass());
100    
101                                    return actionMapping.findForward("portal.setup_wizard");
102                            }
103    
104                            PortalUtil.sendError(e, request, response);
105    
106                            return null;
107                    }
108            }
109    
110            protected void putMessage(
111                    HttpServletRequest request, JSONObject jsonObject, String key,
112                    Object... arguments) {
113    
114                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
115                            WebKeys.THEME_DISPLAY);
116    
117                    String message = themeDisplay.translate(key, arguments);
118    
119                    jsonObject.put("message", message);
120            }
121    
122            protected void testDatabase(
123                            HttpServletRequest request, HttpServletResponse response)
124                    throws Exception {
125    
126                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
127    
128                    try {
129                            SetupWizardUtil.testDatabase(request);
130    
131                            jsonObject.put("success", true);
132    
133                            putMessage(
134                                    request, jsonObject,
135                                    "database-connection-was-established-successfully");
136                    }
137                    catch (ClassNotFoundException cnfe) {
138                            putMessage(
139                                    request, jsonObject, "database-driver-x-is-not-present",
140                                    cnfe.getLocalizedMessage());
141                    }
142                    catch (SQLException sqle) {
143                            putMessage(
144                                    request, jsonObject,
145                                    "database-connection-could-not-be-established");
146                    }
147    
148                    response.setContentType(ContentTypes.APPLICATION_JSON);
149                    response.setHeader(
150                            HttpHeaders.CACHE_CONTROL,
151                            HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
152    
153                    ServletResponseUtil.write(response, jsonObject.toString());
154            }
155    
156    }