1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.image;
16  
17  import com.liferay.documentlibrary.NoSuchFileException;
18  import com.liferay.documentlibrary.service.DLLocalServiceUtil;
19  import com.liferay.documentlibrary.service.DLServiceUtil;
20  import com.liferay.portal.NoSuchImageException;
21  import com.liferay.portal.kernel.exception.PortalException;
22  import com.liferay.portal.kernel.exception.SystemException;
23  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
24  import com.liferay.portal.kernel.util.FileUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.model.Image;
27  import com.liferay.portal.service.ServiceContext;
28  import com.liferay.portal.util.PortletKeys;
29  
30  import java.io.IOException;
31  import java.io.InputStream;
32  
33  import java.util.Date;
34  
35  /**
36   * <a href="DLHook.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Jorge Ferrer
39   */
40  public class DLHook extends BaseHook {
41  
42      public void deleteImage(Image image)
43          throws PortalException, SystemException {
44  
45          String fileName = getFileName(image.getImageId(), image.getType());
46  
47          try {
48              DLServiceUtil.deleteFile(
49                  _COMPANY_ID, _PORTLET_ID, _REPOSITORY_ID, fileName);
50          }
51          catch (NoSuchFileException nsfe) {
52              throw new NoSuchImageException(nsfe);
53          }
54      }
55  
56      public byte[] getImageAsBytes(Image image)
57          throws PortalException, SystemException {
58  
59          String fileName = getFileName(image.getImageId(), image.getType());
60  
61          InputStream is = DLLocalServiceUtil.getFileAsStream(
62              _COMPANY_ID, _REPOSITORY_ID, fileName);
63  
64          byte[] bytes = null;
65  
66          try {
67              bytes = FileUtil.getBytes(is);
68          }
69          catch (IOException ioe) {
70              throw new SystemException(ioe);
71          }
72  
73          return bytes;
74      }
75  
76      public InputStream getImageAsStream(Image image)
77          throws PortalException, SystemException {
78  
79          String fileName = getFileName(image.getImageId(), image.getType());
80  
81          return DLLocalServiceUtil.getFileAsStream(
82              _COMPANY_ID, _REPOSITORY_ID, fileName);
83      }
84  
85      public void updateImage(Image image, String type, byte[] bytes)
86          throws PortalException, SystemException {
87  
88          String fileName = getFileName(image.getImageId(), image.getType());
89          Date now = new Date();
90          InputStream is = new UnsyncByteArrayInputStream(bytes);
91  
92          if (DLLocalServiceUtil.hasFile(
93              _COMPANY_ID, _REPOSITORY_ID, fileName, _VERSION_NUMBER)) {
94  
95              DLServiceUtil.deleteFile(
96                  _COMPANY_ID, _PORTLET_ID, _REPOSITORY_ID, fileName);
97          }
98  
99          DLLocalServiceUtil.addFile(
100             _COMPANY_ID, _PORTLET_ID, _GROUP_ID, _REPOSITORY_ID, fileName, true,
101             _FILE_ENTRY_ID, _PROPERTIES, now, new ServiceContext(), is);
102     }
103 
104     protected String getFileName(long imageId, String type) {
105         return imageId + StringPool.PERIOD + type;
106     }
107 
108     private static final long _COMPANY_ID = 0;
109     private static final long _FILE_ENTRY_ID = 0;
110     private static final long _GROUP_ID = 0;
111     private static final String _PORTLET_ID = PortletKeys.PORTAL;
112     private static final String _PROPERTIES = StringPool.BLANK;
113     private static final long _REPOSITORY_ID = 0;
114     private static final String _VERSION_NUMBER = "1.0";
115 
116 }