001    /**
002     * Copyright (c) 2000-2013 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.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    }