001    /**
002     * Copyright (c) 2000-2012 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.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            public byte[] getTextObj() {
037                    if (_textObj != null) {
038                            return _textObj;
039                    }
040    
041                    long imageId = getImageId();
042    
043                    try {
044                            DLFileEntry dlFileEntry =
045                                    DLFileEntryLocalServiceUtil.fetchFileEntryByAnyImageId(imageId);
046    
047                            InputStream is = null;
048    
049                            if ((dlFileEntry != null) &&
050                                    (dlFileEntry.getLargeImageId() == imageId)) {
051    
052                                    is = DLStoreUtil.getFileAsStream(
053                                            dlFileEntry.getCompanyId(),
054                                            dlFileEntry.getDataRepositoryId(), dlFileEntry.getName());
055                            }
056                            else {
057                                    is = DLStoreUtil.getFileAsStream(
058                                            _DEFAULT_COMPANY_ID, _DEFAULT_REPOSITORY_ID, getFileName());
059                            }
060    
061                            byte[] bytes = FileUtil.getBytes(is);
062    
063                            _textObj = bytes;
064                    }
065                    catch (Exception e) {
066                            _log.error("Error reading image " + imageId, e);
067                    }
068    
069                    return _textObj;
070            }
071    
072            public void setTextObj(byte[] textObj) {
073                    _textObj = textObj;
074    
075                    super.setText(Base64.objectToString(textObj));
076            }
077    
078            protected String getFileName() {
079                    return getImageId() + StringPool.PERIOD + getType();
080            }
081    
082            private static final long _DEFAULT_COMPANY_ID = 0;
083    
084            private static final long _DEFAULT_REPOSITORY_ID = 0;
085    
086            private static Log _log = LogFactoryUtil.getLog(ImageImpl.class);
087    
088            private byte[] _textObj;
089    
090    }