001    /**
002     * Copyright (c) 2000-2013 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.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.FileUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
022    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
023    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
024    
025    import java.io.InputStream;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class ImageImpl extends ImageBaseImpl {
031    
032            public ImageImpl() {
033            }
034    
035            public byte[] getTextObj() {
036                    if (_textObj != null) {
037                            return _textObj;
038                    }
039    
040                    long imageId = getImageId();
041    
042                    try {
043                            DLFileEntry dlFileEntry =
044                                    DLFileEntryLocalServiceUtil.fetchFileEntryByAnyImageId(imageId);
045    
046                            InputStream is = null;
047    
048                            if ((dlFileEntry != null) &&
049                                    (dlFileEntry.getLargeImageId() == imageId)) {
050    
051                                    is = DLStoreUtil.getFileAsStream(
052                                            dlFileEntry.getCompanyId(),
053                                            dlFileEntry.getDataRepositoryId(), dlFileEntry.getName());
054                            }
055                            else {
056                                    is = DLStoreUtil.getFileAsStream(
057                                            _DEFAULT_COMPANY_ID, _DEFAULT_REPOSITORY_ID, getFileName());
058                            }
059    
060                            byte[] bytes = FileUtil.getBytes(is);
061    
062                            _textObj = bytes;
063                    }
064                    catch (Exception e) {
065                            _log.error("Error reading image " + imageId, e);
066                    }
067    
068                    return _textObj;
069            }
070    
071            public void setTextObj(byte[] textObj) {
072                    _textObj = textObj;
073            }
074    
075            protected String getFileName() {
076                    return getImageId() + StringPool.PERIOD + getType();
077            }
078    
079            private static final long _DEFAULT_COMPANY_ID = 0;
080    
081            private static final long _DEFAULT_REPOSITORY_ID = 0;
082    
083            private static Log _log = LogFactoryUtil.getLog(ImageImpl.class);
084    
085            private byte[] _textObj;
086    
087    }