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