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