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.RequiredCompanyException;
022    import com.liferay.portal.kernel.servlet.SessionErrors;
023    import com.liferay.portal.kernel.util.Constants;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.model.Company;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.CompanyServiceUtil;
028    import com.liferay.portal.struts.PortletAction;
029    import com.liferay.portal.util.PortalInstances;
030    import com.liferay.portal.util.PropsValues;
031    import com.liferay.portal.util.WebKeys;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    import javax.servlet.ServletContext;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public class EditInstanceAction extends PortletAction {
049    
050            @Override
051            public void processAction(
052                            ActionMapping actionMapping, ActionForm actionForm,
053                            PortletConfig portletConfig, ActionRequest actionRequest,
054                            ActionResponse actionResponse)
055                    throws Exception {
056    
057                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
058    
059                    try {
060                            if (cmd.equals(Constants.DELETE)) {
061                                    deleteInstance(actionRequest);
062                            }
063                            else {
064                                    updateInstance(actionRequest);
065                            }
066    
067                            sendRedirect(actionRequest, actionResponse);
068                    }
069                    catch (Exception e) {
070                            if (e instanceof NoSuchCompanyException ||
071                                    e instanceof PrincipalException) {
072    
073                                    SessionErrors.add(actionRequest, e.getClass());
074    
075                                    setForward(actionRequest, "portlet.admin.error");
076                            }
077                            else if (e instanceof CompanyMxException ||
078                                             e instanceof CompanyVirtualHostException ||
079                                             e instanceof CompanyWebIdException) {
080    
081                                    SessionErrors.add(actionRequest, e.getClass());
082                            }
083                            else if (e instanceof RequiredCompanyException) {
084                                    SessionErrors.add(actionRequest, e.getClass());
085    
086                                    sendRedirect(actionRequest, actionResponse);
087                            }
088                            else {
089                                    throw e;
090                            }
091                    }
092            }
093    
094            @Override
095            public ActionForward render(
096                            ActionMapping actionMapping, ActionForm actionForm,
097                            PortletConfig portletConfig, RenderRequest renderRequest,
098                            RenderResponse renderResponse)
099                    throws Exception {
100    
101                    try {
102                            ActionUtil.getInstance(renderRequest);
103                    }
104                    catch (Exception e) {
105                            if (e instanceof NoSuchCompanyException ||
106                                    e instanceof PrincipalException) {
107    
108                                    SessionErrors.add(renderRequest, e.getClass());
109    
110                                    return actionMapping.findForward("portlet.admin.error");
111                            }
112                            else {
113                                    throw e;
114                            }
115                    }
116    
117                    return actionMapping.findForward(
118                            getForward(renderRequest, "portlet.admin.edit_instance"));
119            }
120    
121            protected void deleteInstance(ActionRequest actionRequest)
122                    throws Exception {
123    
124                    long companyId = ParamUtil.getLong(actionRequest, "companyId");
125    
126                    CompanyServiceUtil.deleteCompany(companyId);
127            }
128    
129            protected void updateInstance(ActionRequest actionRequest)
130                    throws Exception {
131    
132                    long companyId = ParamUtil.getLong(actionRequest, "companyId");
133    
134                    String webId = ParamUtil.getString(actionRequest, "webId");
135                    String virtualHostname = ParamUtil.getString(
136                            actionRequest, "virtualHostname");
137                    String mx = ParamUtil.getString(actionRequest, "mx");
138                    String shardName = ParamUtil.getString(
139                            actionRequest, "shardName", PropsValues.SHARD_DEFAULT_NAME);
140                    boolean system = false;
141                    int maxUsers = ParamUtil.getInteger(actionRequest, "maxUsers", 0);
142                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
143    
144                    if (companyId <= 0) {
145    
146                            // Add instance
147    
148                            Company company = CompanyServiceUtil.addCompany(
149                                    webId, virtualHostname, mx, shardName, system, maxUsers,
150                                    active);
151    
152                            ServletContext servletContext =
153                                    (ServletContext)actionRequest.getAttribute(WebKeys.CTX);
154    
155                            PortalInstances.initCompany(servletContext, company.getWebId());
156                    }
157                    else {
158    
159                            // Update instance
160    
161                            CompanyServiceUtil.updateCompany(
162                                    companyId, virtualHostname, mx, maxUsers, active);
163                    }
164            }
165    
166    }