Java tutorial
/** * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.portlet.admin.action; import com.liferay.portal.CompanyMxException; import com.liferay.portal.CompanyVirtualHostException; import com.liferay.portal.CompanyWebIdException; import com.liferay.portal.NoSuchCompanyException; import com.liferay.portal.kernel.servlet.SessionErrors; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.model.Company; import com.liferay.portal.security.auth.PrincipalException; import com.liferay.portal.service.CompanyServiceUtil; import com.liferay.portal.struts.PortletAction; import com.liferay.portal.util.PortalInstances; import com.liferay.portal.util.PropsValues; import com.liferay.portal.util.WebKeys; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletConfig; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import javax.servlet.ServletContext; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * @author Brian Wing Shun Chan */ public class EditInstanceAction extends PortletAction { @Override public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { try { updateInstance(actionRequest); sendRedirect(actionRequest, actionResponse); } catch (Exception e) { if (e instanceof NoSuchCompanyException || e instanceof PrincipalException) { SessionErrors.add(actionRequest, e.getClass().getName()); setForward(actionRequest, "portlet.admin.error"); } else if (e instanceof CompanyMxException || e instanceof CompanyVirtualHostException || e instanceof CompanyWebIdException) { SessionErrors.add(actionRequest, e.getClass().getName()); } else { throw e; } } } @Override public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig portletConfig, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception { try { ActionUtil.getInstance(renderRequest); } catch (Exception e) { if (e instanceof NoSuchCompanyException || e instanceof PrincipalException) { SessionErrors.add(renderRequest, e.getClass().getName()); return mapping.findForward("portlet.admin.error"); } else { throw e; } } return mapping.findForward(getForward(renderRequest, "portlet.admin.edit_instance")); } protected void updateInstance(ActionRequest actionRequest) throws Exception { long companyId = ParamUtil.getLong(actionRequest, "companyId"); String webId = ParamUtil.getString(actionRequest, "webId"); String virtualHostname = ParamUtil.getString(actionRequest, "virtualHostname"); String mx = ParamUtil.getString(actionRequest, "mx"); String shardName = ParamUtil.getString(actionRequest, "shardName", PropsValues.SHARD_DEFAULT_NAME); boolean system = false; int maxUsers = ParamUtil.getInteger(actionRequest, "maxUsers", 0); boolean active = ParamUtil.getBoolean(actionRequest, "active"); if (companyId <= 0) { // Add instance Company company = CompanyServiceUtil.addCompany(webId, virtualHostname, mx, shardName, system, maxUsers, active); ServletContext servletContext = (ServletContext) actionRequest.getAttribute(WebKeys.CTX); PortalInstances.initCompany(servletContext, company.getWebId()); } else { // Update instance CompanyServiceUtil.updateCompany(companyId, virtualHostname, mx, maxUsers, active); } } }