1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.PortalException;
22  import com.liferay.portal.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.util.PortletKeys;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  
32  import java.util.Date;
33  
34  /**
35   * <a href="DLHook.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Jorge Ferrer
38   */
39  public class DLHook extends BaseHook {
40  
41      public void deleteImage(Image image)
42          throws PortalException, SystemException {
43  
44          String fileName = getFileName(image.getImageId(), image.getType());
45  
46          try {
47              DLServiceUtil.deleteFile(
48                  _COMPANY_ID, _PORTLET_ID, _REPOSITORY_ID, fileName);
49          }
50          catch (NoSuchFileException nsfe) {
51              throw new NoSuchImageException(nsfe);
52          }
53      }
54  
55      public byte[] getImageAsBytes(Image image)
56          throws PortalException, SystemException {
57  
58          String fileName = getFileName(image.getImageId(), image.getType());
59  
60          InputStream is = DLLocalServiceUtil.getFileAsStream(
61              _COMPANY_ID, _REPOSITORY_ID, fileName);
62  
63          byte[] bytes = null;
64  
65          try {
66              bytes = FileUtil.getBytes(is);
67          }
68          catch (IOException ioe) {
69              throw new SystemException(ioe);
70          }
71  
72          return bytes;
73      }
74  
75      public InputStream getImageAsStream(Image image)
76          throws PortalException, SystemException {
77  
78          String fileName = getFileName(image.getImageId(), image.getType());
79  
80          return DLLocalServiceUtil.getFileAsStream(
81              _COMPANY_ID, _REPOSITORY_ID, fileName);
82      }
83  
84      public void updateImage(Image image, String type, byte[] bytes)
85          throws PortalException, SystemException {
86  
87          String fileName = getFileName(image.getImageId(), image.getType());
88          Date now = new Date();
89          InputStream is = new UnsyncByteArrayInputStream(bytes);
90  
91          if (DLLocalServiceUtil.hasFile(
92              _COMPANY_ID, _REPOSITORY_ID, fileName, _VERSION_NUMBER)) {
93  
94              DLServiceUtil.deleteFile(
95                  _COMPANY_ID, _PORTLET_ID, _REPOSITORY_ID, fileName);
96          }
97  
98          DLLocalServiceUtil.addFile(
99              _COMPANY_ID, _PORTLET_ID, _GROUP_ID, _REPOSITORY_ID, fileName,
100             _FILE_ENTRY_ID, _PROPERTIES, now, _TAGS_CATEGORIES, _TAGS_ENTRIES,
101             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[] _TAGS_CATEGORIES = new String[0];
115     private static final String[] _TAGS_ENTRIES = new String[0];
116     private static final double _VERSION_NUMBER = 1.0;
117 
118 }