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