001    /**
002     * Copyright (c) 2000-2010 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.portlet.admin.action;
016    
017    import com.liferay.portal.CompanyMxException;
018    import com.liferay.portal.CompanyVirtualHostException;
019    import com.liferay.portal.CompanyWebIdException;
020    import com.liferay.portal.NoSuchCompanyException;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.model.Company;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.service.CompanyServiceUtil;
026    import com.liferay.portal.struts.PortletAction;
027    import com.liferay.portal.util.PortalInstances;
028    import com.liferay.portal.util.PropsValues;
029    import com.liferay.portal.util.WebKeys;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import javax.servlet.ServletContext;
038    
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 Brian Wing Shun Chan
045     */
046    public class EditInstanceAction extends PortletAction {
047    
048            public void processAction(
049                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
050                            ActionRequest actionRequest, ActionResponse actionResponse)
051                    throws Exception {
052    
053                    try {
054                            updateInstance(actionRequest);
055    
056                            sendRedirect(actionRequest, actionResponse);
057                    }
058                    catch (Exception e) {
059                            if (e instanceof NoSuchCompanyException ||
060                                    e instanceof PrincipalException) {
061    
062                                    SessionErrors.add(actionRequest, e.getClass().getName());
063    
064                                    setForward(actionRequest, "portlet.admin.error");
065                            }
066                            else if (e instanceof CompanyMxException ||
067                                             e instanceof CompanyVirtualHostException ||
068                                             e instanceof CompanyWebIdException) {
069    
070                                    SessionErrors.add(actionRequest, e.getClass().getName());
071                            }
072                            else {
073                                    throw e;
074                            }
075                    }
076            }
077    
078            public ActionForward render(
079                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
080                            RenderRequest renderRequest, RenderResponse renderResponse)
081                    throws Exception {
082    
083                    try {
084                            ActionUtil.getInstance(renderRequest);
085                    }
086                    catch (Exception e) {
087                            if (e instanceof NoSuchCompanyException ||
088                                    e instanceof PrincipalException) {
089    
090                                    SessionErrors.add(renderRequest, e.getClass().getName());
091    
092                                    return mapping.findForward("portlet.admin.error");
093                            }
094                            else {
095                                    throw e;
096                            }
097                    }
098    
099                    return mapping.findForward(
100                            getForward(renderRequest, "portlet.admin.edit_instance"));
101            }
102    
103            protected void updateInstance(ActionRequest actionRequest)
104                    throws Exception {
105    
106                    long companyId = ParamUtil.getLong(actionRequest, "companyId");
107    
108                    String webId = ParamUtil.getString(actionRequest, "webId");
109                    String virtualHost = ParamUtil.getString(actionRequest, "virtualHost");
110                    String mx = ParamUtil.getString(actionRequest, "mx");
111                    String shardName = ParamUtil.getString(
112                            actionRequest, "shardName", PropsValues.SHARD_DEFAULT_NAME);
113                    boolean system = false;
114                    int maxUsers = ParamUtil.getInteger(actionRequest, "maxUsers", 0);
115    
116                    if (companyId <= 0) {
117    
118                            // Add instance
119    
120                            Company company = CompanyServiceUtil.addCompany(
121                                    webId, virtualHost, mx, shardName, system, maxUsers);
122    
123                            ServletContext servletContext =
124                                    (ServletContext)actionRequest.getAttribute(WebKeys.CTX);
125    
126                            PortalInstances.initCompany(servletContext, company.getWebId());
127                    }
128                    else {
129    
130                            // Update instance
131    
132                            CompanyServiceUtil.updateCompany(
133                                    companyId, virtualHost, mx, maxUsers);
134                    }
135            }
136    
137    }