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.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.impl.ImageImpl;
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 = ImageImpl.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 }