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