001    /**
002     * Copyright (c) 2000-2010 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.portlet.journal.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.LocalizationUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Image;
024    import com.liferay.portal.service.ImageLocalServiceUtil;
025    import com.liferay.portlet.journal.model.JournalArticle;
026    import com.liferay.portlet.journal.util.LocaleTransformerListener;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Wesley Gong
031     */
032    public class JournalArticleImpl
033            extends JournalArticleModelImpl implements JournalArticle {
034    
035            public static String getContentByLocale(
036                    String content, boolean templateDriven, String languageId) {
037    
038                    LocaleTransformerListener listener = new LocaleTransformerListener();
039    
040                    listener.setTemplateDriven(templateDriven);
041                    listener.setLanguageId(languageId);
042    
043                    return listener.onXml(content);
044            }
045    
046            public JournalArticleImpl() {
047            }
048    
049            public String[] getAvailableLocales() {
050                    return LocalizationUtil.getAvailableLocales(getContent());
051            }
052    
053            public String getContentByLocale(String languageId) {
054                    return getContentByLocale(getContent(), isTemplateDriven(), languageId);
055            }
056    
057            public String getDefaultLocale() {
058                    String xml = getContent();
059    
060                    if (xml == null) {
061                            return StringPool.BLANK;
062                    }
063    
064                    String defaultLanguageId = LocalizationUtil.getDefaultLocale(xml);
065    
066                    if (isTemplateDriven() && Validator.isNull(defaultLanguageId)) {
067                            defaultLanguageId = LocaleUtil.toLanguageId(
068                                    LocaleUtil.getDefault());
069                    }
070    
071                    return defaultLanguageId;
072            }
073    
074            public String getSmallImageType() throws PortalException, SystemException {
075                    if (_smallImageType == null && isSmallImage()) {
076                            Image smallImage =  ImageLocalServiceUtil.getImage(
077                                    getSmallImageId());
078    
079                            _smallImageType = smallImage.getType();
080                    }
081    
082                    return _smallImageType;
083            }
084    
085            public boolean isTemplateDriven() {
086                    if (Validator.isNull(getStructureId())) {
087                            return false;
088                    }
089                    else {
090                            return true;
091                    }
092            }
093    
094            public void setSmallImageType(String smallImageType) {
095                    _smallImageType = smallImageType;
096            }
097    
098            private String _smallImageType;
099    
100    }