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