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.lar.StagedModelType;
021    import com.liferay.portal.kernel.templateparser.TransformerListener;
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.SetUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
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.NoSuchFolderException;
033    import com.liferay.portlet.journal.model.JournalArticle;
034    import com.liferay.portlet.journal.model.JournalArticleResource;
035    import com.liferay.portlet.journal.model.JournalFolder;
036    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
037    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
038    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
039    import com.liferay.portlet.journal.util.LocaleTransformerListener;
040    
041    import java.util.Locale;
042    import java.util.Map;
043    import java.util.Set;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Wesley Gong
048     */
049    public class JournalArticleImpl extends JournalArticleBaseImpl {
050    
051            public static String getContentByLocale(
052                    String content, boolean templateDriven, String languageId) {
053    
054                    TransformerListener transformerListener =
055                            new LocaleTransformerListener();
056    
057                    return transformerListener.onXml(content, languageId, null);
058            }
059    
060            public JournalArticleImpl() {
061            }
062    
063            @Override
064            public String getArticleImageURL(ThemeDisplay themeDisplay) {
065                    if (!isSmallImage()) {
066                            return null;
067                    }
068    
069                    if (Validator.isNotNull(getSmallImageURL())) {
070                            return getSmallImageURL();
071                    }
072    
073                    return
074                            themeDisplay.getPathImage() + "/journal/article?img_id=" +
075                                    getSmallImageId() + "&t=" +
076                                            WebServerServletTokenUtil.getToken(getSmallImageId());
077            }
078    
079            @Override
080            public JournalArticleResource getArticleResource()
081                    throws PortalException, SystemException {
082    
083                    return JournalArticleResourceLocalServiceUtil.getArticleResource(
084                            getResourcePrimKey());
085            }
086    
087            @Override
088            public String getArticleResourceUuid()
089                    throws PortalException, SystemException {
090    
091                    JournalArticleResource articleResource = getArticleResource();
092    
093                    return articleResource.getUuid();
094            }
095    
096            @Override
097            public String[] getAvailableLanguageIds() {
098                    Set<String> availableLanguageIds = SetUtil.fromArray(
099                            super.getAvailableLanguageIds());
100    
101                    String[] contentAvailableLanguageIds =
102                            LocalizationUtil.getAvailableLanguageIds(getContent());
103    
104                    for (String availableLanguageId : contentAvailableLanguageIds) {
105                            availableLanguageIds.add(availableLanguageId);
106                    }
107    
108                    return availableLanguageIds.toArray(
109                            new String[availableLanguageIds.size()]);
110            }
111    
112            /**
113             * @deprecated As of 6.2.0, replaced by {@link #getAvailableLanguageIds}
114             */
115            @Override
116            public String[] getAvailableLocales() {
117                    return getAvailableLanguageIds();
118            }
119    
120            @Override
121            public String getContentByLocale(String languageId) {
122                    return getContentByLocale(getContent(), isTemplateDriven(), languageId);
123            }
124    
125            @Override
126            public String getDefaultLanguageId() {
127                    String defaultLanguageId = super.getDefaultLanguageId();
128    
129                    if (isTemplateDriven() && Validator.isNull(defaultLanguageId)) {
130                            defaultLanguageId = LocaleUtil.toLanguageId(
131                                    LocaleUtil.getSiteDefault());
132                    }
133    
134                    return defaultLanguageId;
135            }
136    
137            /**
138             * @deprecated As of 6.2.0, replaced by {@link #getDefaultLanguageId}
139             */
140            @Override
141            public String getDefaultLocale() {
142                    return getDefaultLanguageId();
143            }
144    
145            @Override
146            public JournalFolder getFolder() throws PortalException, SystemException {
147                    if (getFolderId() <= 0) {
148                            return new JournalFolderImpl();
149                    }
150    
151                    return JournalFolderLocalServiceUtil.getFolder(getFolderId());
152            }
153    
154            @Override
155            public String getSmallImageType() throws PortalException, SystemException {
156                    if ((_smallImageType == null) && isSmallImage()) {
157                            Image smallImage = ImageLocalServiceUtil.getImage(
158                                    getSmallImageId());
159    
160                            _smallImageType = smallImage.getType();
161                    }
162    
163                    return _smallImageType;
164            }
165    
166            @Override
167            public StagedModelType getStagedModelType() {
168                    return new StagedModelType(JournalArticle.class);
169            }
170    
171            @Override
172            public Map<Locale, String> getTitleMap() {
173                    Locale defaultLocale = LocaleThreadLocal.getDefaultLocale();
174    
175                    try {
176                            Locale articleDefaultLocale = LocaleUtil.fromLanguageId(
177                                    getDefaultLanguageId());
178    
179                            LocaleThreadLocal.setDefaultLocale(articleDefaultLocale);
180    
181                            return super.getTitleMap();
182                    }
183                    finally {
184                            LocaleThreadLocal.setDefaultLocale(defaultLocale);
185                    }
186            }
187    
188            @Override
189            public JournalFolder getTrashContainer()
190                    throws PortalException, SystemException {
191    
192                    JournalFolder folder = null;
193    
194                    try {
195                            folder = getFolder();
196                    }
197                    catch (NoSuchFolderException nsfe) {
198                            return null;
199                    }
200    
201                    if (folder.isInTrash()) {
202                            return folder;
203                    }
204    
205                    return folder.getTrashContainer();
206            }
207    
208            @Override
209            public boolean hasApprovedVersion() throws SystemException {
210                    JournalArticle article =
211                            JournalArticleLocalServiceUtil.fetchLatestArticle(
212                                    getGroupId(), getArticleId(),
213                                    WorkflowConstants.STATUS_APPROVED);
214    
215                    if (article == null) {
216                            return false;
217                    }
218    
219                    return true;
220            }
221    
222            @Override
223            public boolean isInTrashContainer()
224                    throws PortalException, SystemException {
225    
226                    if (getTrashContainer() != null) {
227                            return true;
228                    }
229                    else {
230                            return false;
231                    }
232            }
233    
234            @Override
235            public boolean isTemplateDriven() {
236                    if (Validator.isNull(getStructureId())) {
237                            return false;
238                    }
239                    else {
240                            return true;
241                    }
242            }
243    
244            /**
245             * @param  defaultImportLocale the default imported locale
246             * @throws LocaleException if a locale exception occurred
247             */
248            @Override
249            public void prepareLocalizedFieldsForImport(Locale defaultImportLocale)
250                    throws LocaleException {
251            }
252    
253            @Override
254            public void setSmallImageType(String smallImageType) {
255                    _smallImageType = smallImageType;
256            }
257    
258            private String _smallImageType;
259    
260    }