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