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.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.security.auth.PrincipalException;
027    import com.liferay.portal.setup.SetupWizardUtil;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.portal.util.WebKeys;
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                            else {
094                                    PortalUtil.sendError(e, request, response);
095    
096                                    return null;
097                            }
098                    }
099            }
100    
101            protected void putMessage(
102                    HttpServletRequest request, JSONObject jsonObject, String key,
103                    Object... arguments) {
104    
105                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
106                            WebKeys.THEME_DISPLAY);
107    
108                    String message = themeDisplay.translate(key, arguments);
109    
110                    jsonObject.put("message", message);
111            }
112    
113            protected void testDatabase(
114                            HttpServletRequest request, HttpServletResponse response)
115                    throws Exception {
116    
117                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
118    
119                    try {
120                            SetupWizardUtil.testDatabase(request);
121    
122                            jsonObject.put("success", true);
123    
124                            putMessage(
125                                    request, jsonObject,
126                                    "database-connection-was-established-sucessfully");
127                    }
128                    catch (ClassNotFoundException cnfe) {
129                            putMessage(
130                                    request, jsonObject, "database-driver-x-is-not-present",
131                                    cnfe.getLocalizedMessage());
132                    }
133                    catch (SQLException sqle) {
134                            putMessage(
135                                    request, jsonObject,
136                                    "database-connection-could-not-be-established");
137                    }
138    
139                    response.setContentType(ContentTypes.APPLICATION_JSON);
140                    response.setHeader(
141                            HttpHeaders.CACHE_CONTROL,
142                            HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
143    
144                    ServletResponseUtil.write(response, jsonObject.toString());
145            }
146    
147    }