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