001    /**
002     * Copyright (c) 2000-2012 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            @Override
049            public void processAction(
050                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
051                            ActionRequest actionRequest, ActionResponse actionResponse)
052                    throws Exception {
053    
054                    try {
055                            updateInstance(actionRequest);
056    
057                            sendRedirect(actionRequest, actionResponse);
058                    }
059                    catch (Exception e) {
060                            if (e instanceof NoSuchCompanyException ||
061                                    e instanceof PrincipalException) {
062    
063                                    SessionErrors.add(actionRequest, e.getClass().getName());
064    
065                                    setForward(actionRequest, "portlet.admin.error");
066                            }
067                            else if (e instanceof CompanyMxException ||
068                                             e instanceof CompanyVirtualHostException ||
069                                             e instanceof CompanyWebIdException) {
070    
071                                    SessionErrors.add(actionRequest, e.getClass().getName());
072                            }
073                            else {
074                                    throw e;
075                            }
076                    }
077            }
078    
079            @Override
080            public ActionForward render(
081                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
082                            RenderRequest renderRequest, RenderResponse renderResponse)
083                    throws Exception {
084    
085                    try {
086                            ActionUtil.getInstance(renderRequest);
087                    }
088                    catch (Exception e) {
089                            if (e instanceof NoSuchCompanyException ||
090                                    e instanceof PrincipalException) {
091    
092                                    SessionErrors.add(renderRequest, e.getClass().getName());
093    
094                                    return mapping.findForward("portlet.admin.error");
095                            }
096                            else {
097                                    throw e;
098                            }
099                    }
100    
101                    return mapping.findForward(
102                            getForward(renderRequest, "portlet.admin.edit_instance"));
103            }
104    
105            protected void updateInstance(ActionRequest actionRequest)
106                    throws Exception {
107    
108                    long companyId = ParamUtil.getLong(actionRequest, "companyId");
109    
110                    String webId = ParamUtil.getString(actionRequest, "webId");
111                    String virtualHostname = ParamUtil.getString(
112                            actionRequest, "virtualHostname");
113                    String mx = ParamUtil.getString(actionRequest, "mx");
114                    String shardName = ParamUtil.getString(
115                            actionRequest, "shardName", PropsValues.SHARD_DEFAULT_NAME);
116                    boolean system = false;
117                    int maxUsers = ParamUtil.getInteger(actionRequest, "maxUsers", 0);
118                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
119    
120                    if (companyId <= 0) {
121    
122                            // Add instance
123    
124                            Company company = CompanyServiceUtil.addCompany(
125                                    webId, virtualHostname, mx, shardName, system, maxUsers,
126                                    active);
127    
128                            ServletContext servletContext =
129                                    (ServletContext)actionRequest.getAttribute(WebKeys.CTX);
130    
131                            PortalInstances.initCompany(servletContext, company.getWebId());
132                    }
133                    else {
134    
135                            // Update instance
136    
137                            CompanyServiceUtil.updateCompany(
138                                    companyId, virtualHostname, mx, maxUsers, active);
139                    }
140            }
141    
142    }