1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.impl;
21  
22  import com.liferay.portal.NoSuchImageException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.image.ImageBag;
25  import com.liferay.portal.kernel.image.ImageProcessorUtil;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
29  import com.liferay.portal.kernel.util.FileUtil;
30  import com.liferay.portal.model.Image;
31  import com.liferay.portal.model.impl.ImageImpl;
32  import com.liferay.portal.service.base.ImageLocalServiceBaseImpl;
33  import com.liferay.portal.util.PropsKeys;
34  import com.liferay.portal.util.PropsUtil;
35  
36  import java.awt.image.RenderedImage;
37  
38  import java.io.File;
39  import java.io.FileInputStream;
40  import java.io.IOException;
41  import java.io.InputStream;
42  
43  import java.util.Arrays;
44  import java.util.Date;
45  import java.util.List;
46  
47  /**
48   * <a href="ImageLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class ImageLocalServiceImpl extends ImageLocalServiceBaseImpl {
54  
55      public void afterPropertiesSet() {
56          ClassLoader classLoader = getClass().getClassLoader();
57  
58          try {
59              InputStream is = classLoader.getResourceAsStream(
60                  PropsUtil.get(PropsKeys.IMAGE_DEFAULT_SPACER));
61  
62              if (is == null) {
63                  _log.error("Default spacer is not available");
64              }
65  
66              _defaultSpacer = getImage(is);
67          }
68          catch (IOException ioe) {
69              _log.error(
70                  "Unable to configure the default spacer: " + ioe.getMessage());
71          }
72  
73          try {
74              InputStream is = classLoader.getResourceAsStream(
75                  PropsUtil.get(PropsKeys.IMAGE_DEFAULT_COMPANY_LOGO));
76  
77              if (is == null) {
78                  _log.error("Default company logo is not available");
79              }
80  
81              _defaultCompanyLogo = getImage(is);
82          }
83          catch (IOException ioe) {
84              _log.error(
85                  "Unable to configure the default company logo: " +
86                      ioe.getMessage());
87          }
88  
89          try {
90              InputStream is = classLoader.getResourceAsStream(
91                  PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_FEMALE_PORTRAIT));
92  
93              if (is == null) {
94                  _log.error("Default user female portrait is not available");
95              }
96  
97              _defaultUserFemalePortrait = getImage(is);
98          }
99          catch (IOException ioe) {
100             _log.error(
101                 "Unable to configure the default user female portrait: " +
102                     ioe.getMessage());
103         }
104 
105         try {
106             InputStream is = classLoader.getResourceAsStream(
107                 PropsUtil.get(PropsKeys.IMAGE_DEFAULT_USER_MALE_PORTRAIT));
108 
109             if (is == null) {
110                 _log.error("Default user male portrait is not available");
111             }
112 
113             _defaultUserMalePortrait = getImage(is);
114         }
115         catch (IOException ioe) {
116             _log.error(
117                 "Unable to configure the default user male portrait: " +
118                     ioe.getMessage());
119         }
120     }
121 
122     public void deleteImage(long imageId) throws SystemException {
123         try {
124             if (imageId > 0) {
125                 imagePersistence.remove(imageId);
126             }
127         }
128         catch (NoSuchImageException nsie) {
129         }
130     }
131 
132     public Image getCompanyLogo(long imageId) {
133         Image image = getImage(imageId);
134 
135         if (image == null) {
136             image = getDefaultCompanyLogo();
137         }
138 
139         return image;
140     }
141 
142     public Image getDefaultCompanyLogo() {
143         return _defaultCompanyLogo;
144     }
145 
146     public Image getDefaultSpacer() {
147         return _defaultSpacer;
148     }
149 
150     public Image getDefaultUserFemalePortrait() {
151         return _defaultUserFemalePortrait;
152     }
153 
154     public Image getDefaultUserMalePortrait() {
155         return _defaultUserMalePortrait;
156     }
157 
158     public Image getImage(long imageId) {
159         try {
160             if (imageId > 0) {
161                 return imagePersistence.findByPrimaryKey(imageId);
162             }
163         }
164         catch (Exception e) {
165             if (_log.isWarnEnabled()) {
166                 _log.warn(
167                     "Unable to get image " + imageId + ": " + e.getMessage());
168             }
169         }
170 
171         return null;
172     }
173 
174     public Image getImage(byte[] bytes) throws IOException {
175         return getImage(null, bytes);
176     }
177 
178     public Image getImage(File file) throws IOException {
179         return getImage(new FileInputStream(file));
180     }
181 
182     public Image getImage(InputStream is) throws IOException {
183         return getImage(is, null);
184     }
185 
186     public Image getImageOrDefault(long imageId) {
187         Image image = getImage(imageId);
188 
189         if (image == null) {
190             image = getDefaultSpacer();
191         }
192 
193         return image;
194     }
195 
196     public List<Image> getImages() throws SystemException {
197         return imagePersistence.findAll();
198     }
199 
200     public List<Image> getImages(int start, int end) throws SystemException {
201         return imagePersistence.findAll(start, end);
202     }
203 
204     public List<Image> getImagesBySize(int size) throws SystemException {
205         return imagePersistence.findBySize(size);
206     }
207 
208     public boolean isNullOrDefaultSpacer(byte[] bytes) {
209         if ((bytes == null) || (bytes.length == 0) ||
210             (Arrays.equals(bytes, getDefaultSpacer().getTextObj()))) {
211 
212             return true;
213         }
214         else {
215             return false;
216         }
217     }
218 
219     public Image updateImage(long imageId, byte[] bytes)
220         throws SystemException {
221 
222         try {
223             Image image = getImage(bytes);
224 
225             return updateImage(
226                 imageId, image.getTextObj(), image.getType(), image.getHeight(),
227                 image.getWidth(), image.getSize());
228         }
229         catch (IOException ioe) {
230             throw new SystemException(ioe);
231         }
232     }
233 
234     public Image updateImage(long imageId, File file)
235         throws SystemException {
236 
237         try {
238             Image image = getImage(file);
239 
240             return updateImage(
241                 imageId, image.getTextObj(), image.getType(), image.getHeight(),
242                 image.getWidth(), image.getSize());
243         }
244         catch (IOException ioe) {
245             throw new SystemException(ioe);
246         }
247     }
248 
249     public Image updateImage(long imageId, InputStream is)
250         throws SystemException {
251 
252         try {
253             Image image = getImage(is);
254 
255             return updateImage(
256                 imageId, image.getTextObj(), image.getType(), image.getHeight(),
257                 image.getWidth(), image.getSize());
258         }
259         catch (IOException ioe) {
260             throw new SystemException(ioe);
261         }
262     }
263 
264     public Image updateImage(
265             long imageId, byte[] bytes, String type, int height, int width,
266             int size)
267         throws SystemException {
268 
269         Image image = imagePersistence.fetchByPrimaryKey(imageId);
270 
271         if (image == null) {
272             image = imagePersistence.create(imageId);
273         }
274 
275         image.setModifiedDate(new Date());
276         image.setTextObj(bytes);
277         image.setType(type);
278         image.setHeight(height);
279         image.setWidth(width);
280         image.setSize(size);
281 
282         imagePersistence.update(image, false);
283 
284         ImageServletTokenUtil.resetToken(imageId);
285 
286         return image;
287     }
288 
289     protected Image getImage(InputStream is, byte[] bytes) throws IOException {
290         try {
291             if (is != null) {
292                 bytes = FileUtil.getBytes(is);
293             }
294 
295             ImageBag imageBag = ImageProcessorUtil.read(bytes);
296 
297             RenderedImage renderedImage = imageBag.getRenderedImage();
298             String type = imageBag.getType();
299 
300             if (renderedImage == null) {
301                 throw new IOException(
302                     "Unable to retreive rendered image from input stream " +
303                         "with type " + type);
304             }
305 
306             int height = renderedImage.getHeight();
307             int width = renderedImage.getWidth();
308             int size = bytes.length;
309 
310             Image image = new ImageImpl();
311 
312             image.setTextObj(bytes);
313             image.setType(type);
314             image.setHeight(height);
315             image.setWidth(width);
316             image.setSize(size);
317 
318             return image;
319         }
320         finally {
321             if (is != null) {
322                 try {
323                     is.close();
324                 }
325                 catch (IOException ioe) {
326                     if (_log.isWarnEnabled()) {
327                         _log.warn(ioe);
328                     }
329                 }
330             }
331         }
332     }
333 
334     private static Log _log =
335          LogFactoryUtil.getLog(ImageLocalServiceImpl.class);
336 
337     private Image _defaultSpacer;
338     private Image _defaultCompanyLogo;
339     private Image _defaultUserFemalePortrait;
340     private Image _defaultUserMalePortrait;
341 
342 }