001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.upgrade.v4_3_0.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.upgrade.util.BaseUpgradeColumnImpl;
020    import com.liferay.portal.kernel.upgrade.util.UpgradeColumn;
021    import com.liferay.portal.kernel.util.Base64;
022    import com.liferay.portal.model.Image;
023    import com.liferay.portal.model.ImageConstants;
024    import com.liferay.portal.service.ImageLocalServiceUtil;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class ImageTextUpgradeColumnImpl extends BaseUpgradeColumnImpl {
030    
031            public ImageTextUpgradeColumnImpl(UpgradeColumn imageIdColumn) {
032                    super("text_");
033    
034                    _imageIdColumn = imageIdColumn;
035            }
036    
037            public Object getNewValue(Object oldValue) throws Exception {
038                    _type = null;
039                    _height = null;
040                    _width = null;
041                    _size = null;
042    
043                    String text = (String)oldValue;
044    
045                    byte[] bytes = (byte[])Base64.stringToObject(text);
046    
047                    try {
048                            Image image = ImageLocalServiceUtil.getImage(bytes);
049    
050                            _type = image.getType();
051                            _height = new Integer(image.getHeight());
052                            _width = new Integer(image.getWidth());
053                            _size = new Integer(image.getSize());
054                    }
055                    catch (Exception e) {
056                            if (_log.isWarnEnabled()) {
057                                    String imageId = (String)_imageIdColumn.getOldValue();
058    
059                                    if (_log.isWarnEnabled()) {
060                                            _log.warn(
061                                                    "Unable to get image data for " + imageId + ": " +
062                                                            e.getMessage());
063                                    }
064                            }
065    
066                            _type = ImageConstants.TYPE_NOT_AVAILABLE;
067                            _height = null;
068                            _width = null;
069                            _size = new Integer(bytes.length);
070                    }
071    
072                    return oldValue;
073            }
074    
075            public String getType() {
076                    return _type;
077            }
078    
079            public Integer getHeight() {
080                    return _height;
081            }
082    
083            public Integer getWidth() {
084                    return _width;
085            }
086    
087            public Integer getSize() {
088                    return _size;
089            }
090    
091            private static Log _log = LogFactoryUtil.getLog(
092                    ImageTextUpgradeColumnImpl.class);
093    
094            private UpgradeColumn _imageIdColumn;
095            private String _type;
096            private Integer _height;
097            private Integer _width;
098            private Integer _size;
099    
100    }