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.util.FileUtil;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.model.Image;
34  import com.liferay.portal.util.PortletKeys;
35  
36  import java.io.ByteArrayInputStream;
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   */
48  public class DLHook extends BaseHook {
49  
50      public void deleteImage(Image image)
51          throws PortalException, SystemException {
52  
53          String fileName = getFileName(image.getImageId(), image.getType());
54  
55          try {
56              DLServiceUtil.deleteFile(
57                  _COMPANY_ID, _PORTLET_ID, _REPOSITORY_ID, fileName);
58          }
59          catch (NoSuchFileException nsfe) {
60              throw new NoSuchImageException(nsfe);
61          }
62      }
63  
64      public byte[] getImageAsBytes(Image image)
65          throws PortalException, SystemException {
66  
67          String fileName = getFileName(image.getImageId(), image.getType());
68  
69          InputStream is = DLLocalServiceUtil.getFileAsStream(
70              _COMPANY_ID, _REPOSITORY_ID, fileName);
71  
72          byte[] bytes = null;
73  
74          try {
75              bytes = FileUtil.getBytes(is);
76          }
77          catch (IOException ioe) {
78              throw new SystemException(ioe);
79          }
80  
81          return bytes;
82      }
83  
84      public InputStream getImageAsStream(Image image)
85          throws PortalException, SystemException {
86  
87          String fileName = getFileName(image.getImageId(), image.getType());
88  
89          return DLLocalServiceUtil.getFileAsStream(
90              _COMPANY_ID, _REPOSITORY_ID, fileName);
91      }
92  
93      public void updateImage(Image image, String type, byte[] bytes)
94          throws PortalException, SystemException {
95  
96          String fileName = getFileName(image.getImageId(), image.getType());
97          Date now = new Date();
98          InputStream is = new ByteArrayInputStream(bytes);
99  
100         if (DLLocalServiceUtil.hasFile(
101             _COMPANY_ID, _REPOSITORY_ID, fileName, _VERSION_NUMBER)) {
102 
103             DLServiceUtil.deleteFile(
104                 _COMPANY_ID, _PORTLET_ID, _REPOSITORY_ID, fileName);
105         }
106 
107         DLLocalServiceUtil.addFile(
108             _COMPANY_ID, _PORTLET_ID, _GROUP_ID, _REPOSITORY_ID, fileName,
109             _FILE_ENTRY_ID, _PROPERTIES, now, _TAGS_CATEGORIES, _TAGS_ENTRIES,
110             is);
111     }
112 
113     protected String getFileName(long imageId, String type) {
114         return imageId + StringPool.PERIOD + type;
115     }
116 
117     private static final long _COMPANY_ID = 0;
118     private static final long _FILE_ENTRY_ID = 0;
119     private static final long _GROUP_ID = 0;
120     private static final String _PORTLET_ID = PortletKeys.PORTAL;
121     private static final String _PROPERTIES = StringPool.BLANK;
122     private static final long _REPOSITORY_ID = 0;
123     private static final String[] _TAGS_CATEGORIES = new String[0];
124     private static final String[] _TAGS_ENTRIES = new String[0];
125     private static final double _VERSION_NUMBER = 1.0;
126 
127 }