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.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.JournalArticleResource;
026    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
027    import com.liferay.portlet.journal.util.LocaleTransformerListener;
028    
029    import java.util.Locale;
030    import java.util.Map;
031    import java.util.Set;
032    import java.util.TreeSet;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     * @author Wesley Gong
037     */
038    public class JournalArticleImpl extends JournalArticleBaseImpl {
039    
040            public static String getContentByLocale(
041                    String content, boolean templateDriven, String languageId) {
042    
043                    LocaleTransformerListener listener = new LocaleTransformerListener();
044    
045                    listener.setTemplateDriven(templateDriven);
046                    listener.setLanguageId(languageId);
047    
048                    return listener.onXml(content);
049            }
050    
051            public JournalArticleImpl() {
052            }
053    
054            public JournalArticleResource getArticleResource()
055                    throws PortalException, SystemException {
056    
057                    return JournalArticleResourceLocalServiceUtil.getArticleResource(
058                            getResourcePrimKey());
059            }
060    
061            public String getArticleResourceUuid()
062                    throws PortalException, SystemException {
063    
064                    JournalArticleResource articleResource = getArticleResource();
065    
066                    return articleResource.getUuid();
067            }
068    
069            public String[] getAvailableLocales() {
070                    Set<String> availableLocales = new TreeSet<String>();
071    
072                    // Title
073    
074                    Map<Locale, String> titleMap = getTitleMap();
075    
076                    for (Map.Entry<Locale, String> entry : titleMap.entrySet()) {
077                            Locale locale = entry.getKey();
078                            String value = entry.getValue();
079    
080                            if (Validator.isNotNull(value)) {
081                                    availableLocales.add(locale.toString());
082                            }
083                    }
084    
085                    // Description
086    
087                    Map<Locale, String> descriptionMap = getDescriptionMap();
088    
089                    for (Map.Entry<Locale, String> entry : descriptionMap.entrySet()) {
090                            Locale locale = entry.getKey();
091                            String value = entry.getValue();
092    
093                            if (Validator.isNotNull(value)) {
094                                    availableLocales.add(locale.toString());
095                            }
096                    }
097    
098                    // Content
099    
100                    String[] availableLocalesArray = LocalizationUtil.getAvailableLocales(
101                            getContent());
102    
103                    for (String availableLocale : availableLocalesArray) {
104                            availableLocales.add(availableLocale);
105                    }
106    
107                    return availableLocales.toArray(new String[availableLocales.size()]);
108            }
109    
110            public String getContentByLocale(String languageId) {
111                    return getContentByLocale(getContent(), isTemplateDriven(), languageId);
112            }
113    
114            public String getDefaultLocale() {
115                    String xml = getContent();
116    
117                    if (xml == null) {
118                            return StringPool.BLANK;
119                    }
120    
121                    String defaultLanguageId = LocalizationUtil.getDefaultLocale(xml);
122    
123                    if (isTemplateDriven() && Validator.isNull(defaultLanguageId)) {
124                            defaultLanguageId = LocaleUtil.toLanguageId(
125                                    LocaleUtil.getDefault());
126                    }
127    
128                    return defaultLanguageId;
129            }
130    
131            public String getSmallImageType() throws PortalException, SystemException {
132                    if ((_smallImageType == null) && isSmallImage()) {
133                            Image smallImage = ImageLocalServiceUtil.getImage(
134                                    getSmallImageId());
135    
136                            _smallImageType = smallImage.getType();
137                    }
138    
139                    return _smallImageType;
140            }
141    
142            public boolean isTemplateDriven() {
143                    if (Validator.isNull(getStructureId())) {
144                            return false;
145                    }
146                    else {
147                            return true;
148                    }
149            }
150    
151            public void setSmallImageType(String smallImageType) {
152                    _smallImageType = smallImageType;
153            }
154    
155            private String _smallImageType;
156    
157    }