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