1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.upgrade.v4_3_0.util;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.Base64;
25  import com.liferay.portal.model.Image;
26  import com.liferay.portal.model.impl.ImageImpl;
27  import com.liferay.portal.service.ImageLocalServiceUtil;
28  import com.liferay.portal.upgrade.util.BaseUpgradeColumnImpl;
29  import com.liferay.portal.upgrade.util.UpgradeColumn;
30  
31  /**
32   * <a href="ImageTextUpgradeColumnImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class ImageTextUpgradeColumnImpl extends BaseUpgradeColumnImpl {
38  
39      public ImageTextUpgradeColumnImpl(UpgradeColumn imageIdColumn) {
40          super("text_");
41  
42          _imageIdColumn = imageIdColumn;
43      }
44  
45      public Object getNewValue(Object oldValue) throws Exception {
46          _type = null;
47          _height = null;
48          _width = null;
49          _size = null;
50  
51          String text = (String)oldValue;
52  
53          byte[] bytes = (byte[])Base64.stringToObject(text);
54  
55          try {
56              Image image = ImageLocalServiceUtil.getImage(bytes);
57  
58              _type = image.getType();
59              _height = new Integer(image.getHeight());
60              _width = new Integer(image.getWidth());
61              _size = new Integer(image.getSize());
62          }
63          catch (Exception e) {
64              if (_log.isWarnEnabled()) {
65                  String imageId = (String)_imageIdColumn.getOldValue();
66  
67                  _log.warn(
68                      "Unable to get image data for " + imageId + ": " +
69                          e.getMessage());
70              }
71  
72              _type = ImageImpl.TYPE_NOT_AVAILABLE;
73              _height = null;
74              _width = null;
75              _size = new Integer(bytes.length);
76          }
77  
78          return oldValue;
79      }
80  
81      public String getType() {
82          return _type;
83      }
84  
85      public Integer getHeight() {
86          return _height;
87      }
88  
89      public Integer getWidth() {
90          return _width;
91      }
92  
93      public Integer getSize() {
94          return _size;
95      }
96  
97      private static Log _log =
98          LogFactoryUtil.getLog(ImageTextUpgradeColumnImpl.class);
99  
100     private UpgradeColumn _imageIdColumn;
101     private String _type;
102     private Integer _height;
103     private Integer _width;
104     private Integer _size;
105 
106 }