001    /**
002     * Copyright (c) 2000-2013 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.LocaleException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.LocaleThreadLocal;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.LocalizationUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.Image;
026    import com.liferay.portal.service.ImageLocalServiceUtil;
027    import com.liferay.portlet.journal.model.JournalArticleResource;
028    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
029    import com.liferay.portlet.journal.util.LocaleTransformerListener;
030    
031    import java.util.Locale;
032    import java.util.Map;
033    import java.util.Set;
034    import java.util.TreeSet;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Wesley Gong
039     */
040    public class JournalArticleImpl extends JournalArticleBaseImpl {
041    
042            public static String getContentByLocale(
043                    String content, boolean templateDriven, String languageId) {
044    
045                    LocaleTransformerListener listener = new LocaleTransformerListener();
046    
047                    listener.setTemplateDriven(templateDriven);
048                    listener.setLanguageId(languageId);
049    
050                    return listener.onXml(content);
051            }
052    
053            public JournalArticleImpl() {
054            }
055    
056            @Override
057            public JournalArticleResource getArticleResource()
058                    throws PortalException, SystemException {
059    
060                    return JournalArticleResourceLocalServiceUtil.getArticleResource(
061                            getResourcePrimKey());
062            }
063    
064            @Override
065            public String getArticleResourceUuid()
066                    throws PortalException, SystemException {
067    
068                    JournalArticleResource articleResource = getArticleResource();
069    
070                    return articleResource.getUuid();
071            }
072    
073            @Override
074            public String[] getAvailableLocales() {
075                    Set<String> availableLocales = new TreeSet<String>();
076    
077                    // Title
078    
079                    Map<Locale, String> titleMap = getTitleMap();
080    
081                    for (Map.Entry<Locale, String> entry : titleMap.entrySet()) {
082                            Locale locale = entry.getKey();
083                            String value = entry.getValue();
084    
085                            if (Validator.isNotNull(value)) {
086                                    availableLocales.add(locale.toString());
087                            }
088                    }
089    
090                    // Description
091    
092                    Map<Locale, String> descriptionMap = getDescriptionMap();
093    
094                    for (Map.Entry<Locale, String> entry : descriptionMap.entrySet()) {
095                            Locale locale = entry.getKey();
096                            String value = entry.getValue();
097    
098                            if (Validator.isNotNull(value)) {
099                                    availableLocales.add(locale.toString());
100                            }
101                    }
102    
103                    // Content
104    
105                    String[] availableLocalesArray = LocalizationUtil.getAvailableLocales(
106                            getContent());
107    
108                    for (String availableLocale : availableLocalesArray) {
109                            availableLocales.add(availableLocale);
110                    }
111    
112                    return availableLocales.toArray(new String[availableLocales.size()]);
113            }
114    
115            @Override
116            public String getContentByLocale(String languageId) {
117                    return getContentByLocale(getContent(), isTemplateDriven(), languageId);
118            }
119    
120            @Override
121            public String getDefaultLocale() {
122                    String xml = getContent();
123    
124                    if (xml == null) {
125                            return StringPool.BLANK;
126                    }
127    
128                    String defaultLanguageId = LocalizationUtil.getDefaultLocale(xml);
129    
130                    if (isTemplateDriven() && Validator.isNull(defaultLanguageId)) {
131                            defaultLanguageId = LocaleUtil.toLanguageId(
132                                    LocaleUtil.getDefault());
133                    }
134    
135                    return defaultLanguageId;
136            }
137    
138            @Override
139            public String getSmallImageType() throws PortalException, SystemException {
140                    if ((_smallImageType == null) && isSmallImage()) {
141                            Image smallImage = ImageLocalServiceUtil.getImage(
142                                    getSmallImageId());
143    
144                            _smallImageType = smallImage.getType();
145                    }
146    
147                    return _smallImageType;
148            }
149    
150            @Override
151            public Map<Locale, String> getTitleMap() {
152                    Locale defaultLocale = LocaleThreadLocal.getDefaultLocale();
153    
154                    try {
155                            Locale articleDefaultLocale = LocaleUtil.fromLanguageId(
156                                    getDefaultLocale());
157    
158                            LocaleThreadLocal.setDefaultLocale(articleDefaultLocale);
159    
160                            return super.getTitleMap();
161                    }
162                    finally {
163                            LocaleThreadLocal.setDefaultLocale(defaultLocale);
164                    }
165            }
166    
167            @Override
168            public boolean isTemplateDriven() {
169                    if (Validator.isNull(getStructureId())) {
170                            return false;
171                    }
172                    else {
173                            return true;
174                    }
175            }
176    
177            /**
178             * @throws LocaleException
179             */
180            @Override
181            public void prepareLocalizedFieldsForImport(Locale defaultImportLocale)
182                    throws LocaleException {
183            }
184    
185            @Override
186            public void setSmallImageType(String smallImageType) {
187                    _smallImageType = smallImageType;
188            }
189    
190            private String _smallImageType;
191    
192    }