1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.enterpriseadmin.action;
24  
25  import com.liferay.portal.NoSuchOrganizationException;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.upload.UploadPortletRequest;
28  import com.liferay.portal.kernel.util.FileUtil;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.service.LayoutSetServiceUtil;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.util.servlet.UploadException;
35  
36  import java.io.File;
37  
38  import javax.portlet.ActionRequest;
39  import javax.portlet.ActionResponse;
40  import javax.portlet.PortletConfig;
41  import javax.portlet.RenderRequest;
42  import javax.portlet.RenderResponse;
43  
44  import org.apache.struts.action.ActionForm;
45  import org.apache.struts.action.ActionForward;
46  import org.apache.struts.action.ActionMapping;
47  
48  /**
49   * <a href="EditOrganizationLogoAction.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   * @author Julio Camarero
53   */
54  public class EditOrganizationLogoAction extends PortletAction {
55  
56      public void processAction(
57              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
58              ActionRequest actionRequest, ActionResponse actionResponse)
59          throws Exception {
60  
61          try {
62              updateLogo(actionRequest);
63  
64              sendRedirect(actionRequest, actionResponse);
65          }
66          catch (Exception e) {
67              if (e instanceof NoSuchOrganizationException ||
68                  e instanceof PrincipalException) {
69  
70                  SessionErrors.add(actionRequest, e.getClass().getName());
71  
72                  setForward(actionRequest, "portlet.enterprise_admin.error");
73              }
74              else if (e instanceof UploadException) {
75  
76                  SessionErrors.add(actionRequest, e.getClass().getName());
77              }
78              else {
79                  throw e;
80              }
81          }
82      }
83  
84      public ActionForward render(
85              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
86              RenderRequest renderRequest, RenderResponse renderResponse)
87          throws Exception {
88  
89          return mapping.findForward(getForward(
90              renderRequest, "portlet.enterprise_admin.edit_organization_logo"));
91      }
92  
93      protected void updateLogo(ActionRequest actionRequest) throws Exception {
94          UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
95              actionRequest);
96  
97          long groupId = ParamUtil.getLong(uploadRequest, "groupId");
98  
99          File file = uploadRequest.getFile("fileName");
100         byte[] bytes = FileUtil.getBytes(file);
101 
102         if ((bytes == null) || (bytes.length == 0)) {
103             throw new UploadException();
104         }
105 
106         LayoutSetServiceUtil.updateLogo(groupId, true, true, file);
107         LayoutSetServiceUtil.updateLogo(groupId, false, true, file);
108     }
109 
110 }