001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }