1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.portal.image;
24  
25  import com.liferay.documentlibrary.NoSuchFileException;
26  import com.liferay.documentlibrary.service.DLLocalServiceUtil;
27  import com.liferay.documentlibrary.service.DLServiceUtil;
28  import com.liferay.portal.NoSuchImageException;
29  import com.liferay.portal.PortalException;
30  import com.liferay.portal.SystemException;
31  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
32  import com.liferay.portal.kernel.util.FileUtil;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.model.Image;
35  import com.liferay.portal.util.PortletKeys;
36  
37  import java.io.IOException;
38  import java.io.InputStream;
39  
40  import java.util.Date;
41  
42  /**
43   * <a href="DLHook.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Jorge Ferrer
46   */
47  public class DLHook extends BaseHook {
48  
49      public void deleteImage(Image image)
50          throws PortalException, SystemException {
51  
52          String fileName = getFileName(image.getImageId(), image.getType());
53  
54          try {
55              DLServiceUtil.deleteFile(
56                  _COMPANY_ID, _PORTLET_ID, _REPOSITORY_ID, fileName);
57          }
58          catch (NoSuchFileException nsfe) {
59              throw new NoSuchImageException(nsfe);
60          }
61      }
62  
63      public byte[] getImageAsBytes(Image image)
64          throws PortalException, SystemException {
65  
66          String fileName = getFileName(image.getImageId(), image.getType());
67  
68          InputStream is = DLLocalServiceUtil.getFileAsStream(
69              _COMPANY_ID, _REPOSITORY_ID, fileName);
70  
71          byte[] bytes = null;
72  
73          try {
74              bytes = FileUtil.getBytes(is);
75          }
76          catch (IOException ioe) {
77              throw new SystemException(ioe);
78          }
79  
80          return bytes;
81      }
82  
83      public InputStream getImageAsStream(Image image)
84          throws PortalException, SystemException {
85  
86          String fileName = getFileName(image.getImageId(), image.getType());
87  
88          return DLLocalServiceUtil.getFileAsStream(
89              _COMPANY_ID, _REPOSITORY_ID, fileName);
90      }
91  
92      public void updateImage(Image image, String type, byte[] bytes)
93          throws PortalException, SystemException {
94  
95          String fileName = getFileName(image.getImageId(), image.getType());
96          Date now = new Date();
97          InputStream is = new UnsyncByteArrayInputStream(bytes);
98  
99          if (DLLocalServiceUtil.hasFile(
100             _COMPANY_ID, _REPOSITORY_ID, fileName, _VERSION_NUMBER)) {
101 
102             DLServiceUtil.deleteFile(
103                 _COMPANY_ID, _PORTLET_ID, _REPOSITORY_ID, fileName);
104         }
105 
106         DLLocalServiceUtil.addFile(
107             _COMPANY_ID, _PORTLET_ID, _GROUP_ID, _REPOSITORY_ID, fileName,
108             _FILE_ENTRY_ID, _PROPERTIES, now, _TAGS_CATEGORIES, _TAGS_ENTRIES,
109             is);
110     }
111 
112     protected String getFileName(long imageId, String type) {
113         return imageId + StringPool.PERIOD + type;
114     }
115 
116     private static final long _COMPANY_ID = 0;
117     private static final long _FILE_ENTRY_ID = 0;
118     private static final long _GROUP_ID = 0;
119     private static final String _PORTLET_ID = PortletKeys.PORTAL;
120     private static final String _PROPERTIES = StringPool.BLANK;
121     private static final long _REPOSITORY_ID = 0;
122     private static final String[] _TAGS_CATEGORIES = new String[0];
123     private static final String[] _TAGS_ENTRIES = new String[0];
124     private static final double _VERSION_NUMBER = 1.0;
125 
126 }