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.upgrade.v4_3_0.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
20  import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
21  import com.liferay.portal.kernel.util.Base64;
22  import com.liferay.portal.model.Image;
23  import com.liferay.portal.model.ImageConstants;
24  import com.liferay.portal.service.ImageLocalServiceUtil;
25  
26  /**
27   * <a href="ImageTextUpgradeColumnImpl.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class ImageTextUpgradeColumnImpl extends BaseUpgradeColumnImpl {
32  
33      public ImageTextUpgradeColumnImpl(UpgradeColumn imageIdColumn) {
34          super("text_");
35  
36          _imageIdColumn = imageIdColumn;
37      }
38  
39      public Object getNewValue(Object oldValue) throws Exception {
40          _type = null;
41          _height = null;
42          _width = null;
43          _size = null;
44  
45          String text = (String)oldValue;
46  
47          byte[] bytes = (byte[])Base64.stringToObject(text);
48  
49          try {
50              Image image = ImageLocalServiceUtil.getImage(bytes);
51  
52              _type = image.getType();
53              _height = new Integer(image.getHeight());
54              _width = new Integer(image.getWidth());
55              _size = new Integer(image.getSize());
56          }
57          catch (Exception e) {
58              if (_log.isWarnEnabled()) {
59                  String imageId = (String)_imageIdColumn.getOldValue();
60  
61                  if (_log.isWarnEnabled()) {
62                      _log.warn(
63                          "Unable to get image data for " + imageId + ": " +
64                              e.getMessage());
65                  }
66              }
67  
68              _type = ImageConstants.TYPE_NOT_AVAILABLE;
69              _height = null;
70              _width = null;
71              _size = new Integer(bytes.length);
72          }
73  
74          return oldValue;
75      }
76  
77      public String getType() {
78          return _type;
79      }
80  
81      public Integer getHeight() {
82          return _height;
83      }
84  
85      public Integer getWidth() {
86          return _width;
87      }
88  
89      public Integer getSize() {
90          return _size;
91      }
92  
93      private static Log _log = LogFactoryUtil.getLog(
94          ImageTextUpgradeColumnImpl.class);
95  
96      private UpgradeColumn _imageIdColumn;
97      private String _type;
98      private Integer _height;
99      private Integer _width;
100     private Integer _size;
101 
102 }