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