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.Base64;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.kernel.util.StringPool;
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 =
046                                    DLFileEntryLocalServiceUtil.fetchFileEntryByAnyImageId(imageId);
047    
048                            InputStream is = null;
049    
050                            if ((dlFileEntry != null) &&
051                                    (dlFileEntry.getLargeImageId() == imageId)) {
052    
053                                    is = DLStoreUtil.getFileAsStream(
054                                            dlFileEntry.getCompanyId(),
055                                            dlFileEntry.getDataRepositoryId(), dlFileEntry.getName());
056                            }
057                            else {
058                                    is = DLStoreUtil.getFileAsStream(
059                                            _DEFAULT_COMPANY_ID, _DEFAULT_REPOSITORY_ID, getFileName());
060                            }
061    
062                            byte[] bytes = FileUtil.getBytes(is);
063    
064                            _textObj = bytes;
065                    }
066                    catch (Exception e) {
067                            _log.error("Error reading image " + imageId, e);
068                    }
069    
070                    return _textObj;
071            }
072    
073            @Override
074            public void setTextObj(byte[] textObj) {
075                    _textObj = textObj;
076    
077                    super.setText(Base64.objectToString(textObj));
078            }
079    
080            protected String getFileName() {
081                    return getImageId() + StringPool.PERIOD + getType();
082            }
083    
084            private static final long _DEFAULT_COMPANY_ID = 0;
085    
086            private static final long _DEFAULT_REPOSITORY_ID = 0;
087    
088            private static Log _log = LogFactoryUtil.getLog(ImageImpl.class);
089    
090            private byte[] _textObj;
091    
092    }