001    /**
002     * Copyright (c) 2000-2012 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.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            public JournalArticleResource getArticleResource()
057                    throws PortalException, SystemException {
058    
059                    return JournalArticleResourceLocalServiceUtil.getArticleResource(
060                            getResourcePrimKey());
061            }
062    
063            public String getArticleResourceUuid()
064                    throws PortalException, SystemException {
065    
066                    JournalArticleResource articleResource = getArticleResource();
067    
068                    return articleResource.getUuid();
069            }
070    
071            public String[] getAvailableLocales() {
072                    Set<String> availableLocales = new TreeSet<String>();
073    
074                    // Title
075    
076                    Map<Locale, String> titleMap = getTitleMap();
077    
078                    for (Map.Entry<Locale, String> entry : titleMap.entrySet()) {
079                            Locale locale = entry.getKey();
080                            String value = entry.getValue();
081    
082                            if (Validator.isNotNull(value)) {
083                                    availableLocales.add(locale.toString());
084                            }
085                    }
086    
087                    // Description
088    
089                    Map<Locale, String> descriptionMap = getDescriptionMap();
090    
091                    for (Map.Entry<Locale, String> entry : descriptionMap.entrySet()) {
092                            Locale locale = entry.getKey();
093                            String value = entry.getValue();
094    
095                            if (Validator.isNotNull(value)) {
096                                    availableLocales.add(locale.toString());
097                            }
098                    }
099    
100                    // Content
101    
102                    String[] availableLocalesArray = LocalizationUtil.getAvailableLocales(
103                            getContent());
104    
105                    for (String availableLocale : availableLocalesArray) {
106                            availableLocales.add(availableLocale);
107                    }
108    
109                    return availableLocales.toArray(new String[availableLocales.size()]);
110            }
111    
112            public String getContentByLocale(String languageId) {
113                    return getContentByLocale(getContent(), isTemplateDriven(), languageId);
114            }
115    
116            public String getDefaultLocale() {
117                    String xml = getContent();
118    
119                    if (xml == null) {
120                            return StringPool.BLANK;
121                    }
122    
123                    String defaultLanguageId = LocalizationUtil.getDefaultLocale(xml);
124    
125                    if (isTemplateDriven() && Validator.isNull(defaultLanguageId)) {
126                            defaultLanguageId = LocaleUtil.toLanguageId(
127                                    LocaleUtil.getDefault());
128                    }
129    
130                    return defaultLanguageId;
131            }
132    
133            public String getSmallImageType() throws PortalException, SystemException {
134                    if ((_smallImageType == null) && isSmallImage()) {
135                            Image smallImage = ImageLocalServiceUtil.getImage(
136                                    getSmallImageId());
137    
138                            _smallImageType = smallImage.getType();
139                    }
140    
141                    return _smallImageType;
142            }
143    
144            @Override
145            public Map<Locale, String> getTitleMap() {
146                    Locale defaultLocale = LocaleThreadLocal.getDefaultLocale();
147    
148                    try {
149                            Locale articleDefaultLocale = LocaleUtil.fromLanguageId(
150                                    getDefaultLocale());
151    
152                            LocaleThreadLocal.setDefaultLocale(articleDefaultLocale);
153    
154                            return super.getTitleMap();
155                    }
156                    finally {
157                            LocaleThreadLocal.setDefaultLocale(defaultLocale);
158                    }
159            }
160    
161            public boolean isTemplateDriven() {
162                    if (Validator.isNull(getStructureId())) {
163                            return false;
164                    }
165                    else {
166                            return true;
167                    }
168            }
169    
170            @Override
171            @SuppressWarnings("unused")
172            public void prepareLocalizedFieldsForImport(Locale defaultImportLocale)
173                    throws LocaleException {
174            }
175    
176            public void setSmallImageType(String smallImageType) {
177                    _smallImageType = smallImageType;
178            }
179    
180            private String _smallImageType;
181    
182    }