1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ImageTextUpgradeColumnImpl extends BaseUpgradeColumnImpl {
40  
41      public ImageTextUpgradeColumnImpl(UpgradeColumn imageIdColumn) {
42          super("text_");
43  
44          _imageIdColumn = imageIdColumn;
45      }
46  
47      public Object getNewValue(Object oldValue) throws Exception {
48          _type = null;
49          _height = null;
50          _width = null;
51          _size = null;
52  
53          String text = (String)oldValue;
54  
55          byte[] bytes = (byte[])Base64.stringToObject(text);
56  
57          try {
58              Image image = ImageLocalServiceUtil.getImage(bytes);
59  
60              _type = image.getType();
61              _height = new Integer(image.getHeight());
62              _width = new Integer(image.getWidth());
63              _size = new Integer(image.getSize());
64          }
65          catch (Exception e) {
66              if (_log.isWarnEnabled()) {
67                  String imageId = (String)_imageIdColumn.getOldValue();
68  
69                  if (_log.isWarnEnabled()) {
70                      _log.warn(
71                          "Unable to get image data for " + imageId + ": " +
72                              e.getMessage());
73                  }
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         LogFactoryUtil.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 }