001    /**
002     * Copyright (c) 2000-2010 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.portlet.imagegallery.model.impl;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.Image;
022    import com.liferay.portal.service.ImageLocalServiceUtil;
023    import com.liferay.portlet.imagegallery.model.IGFolder;
024    import com.liferay.portlet.imagegallery.model.IGImage;
025    import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class IGImageImpl extends IGImageModelImpl implements IGImage {
031    
032            public static String getNameWithExtension(String name, String type) {
033                    if (Validator.isNotNull(type)) {
034                            name += StringPool.PERIOD + type;
035                    }
036    
037                    return name;
038            }
039    
040            public IGImageImpl() {
041            }
042    
043            public IGFolder getFolder() {
044                    IGFolder folder = null;
045    
046                    if (getFolderId() > 0) {
047                            try {
048                                    folder = IGFolderLocalServiceUtil.getFolder(getFolderId());
049                            }
050                            catch (Exception e) {
051                                    folder = new IGFolderImpl();
052    
053                                    _log.error(e);
054                            }
055                    }
056                    else {
057                            folder = new IGFolderImpl();
058                    }
059    
060                    return folder;
061            }
062    
063            public int getImageSize() {
064                    if (_imageSize == null) {
065                            try {
066                                    Image largeImage = ImageLocalServiceUtil.getImage(
067                                            getLargeImageId());
068    
069                                    _imageSize = new Integer(largeImage.getSize());
070                            }
071                            catch (Exception e) {
072                                    _imageSize = new Integer(0);
073    
074                                    _log.error(e);
075                            }
076                    }
077    
078                    return _imageSize.intValue();
079            }
080    
081            public String getImageType() {
082                    if (_imageType == null) {
083                            try {
084                                    Image largeImage = ImageLocalServiceUtil.getImage(
085                                            getLargeImageId());
086    
087                                    _imageType = largeImage.getType();
088                            }
089                            catch (Exception e) {
090                                    _imageType = StringPool.BLANK;
091    
092                                    _log.error(e);
093                            }
094                    }
095    
096                    return _imageType;
097            }
098    
099            public String getNameWithExtension() {
100                    String nameWithExtension = getName();
101    
102                    if (Validator.isNull(nameWithExtension)) {
103                            nameWithExtension = String.valueOf(getImageId());
104                    }
105    
106                    String type = getImageType();
107    
108                    return getNameWithExtension(nameWithExtension, type);
109            }
110    
111            public void setImageType(String imageType) {
112                    _imageType = imageType;
113            }
114    
115            private static Log _log = LogFactoryUtil.getLog(IGImageImpl.class);
116    
117            private Integer _imageSize;
118            private String _imageType;
119    
120    }