001    /**
002     * Copyright (c) 2000-present 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.lar.StagedModelType;
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.SetUtil;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.kernel.xml.Document;
031    import com.liferay.portal.kernel.xml.DocumentException;
032    import com.liferay.portal.kernel.xml.SAXReaderUtil;
033    import com.liferay.portal.model.CacheField;
034    import com.liferay.portal.model.Image;
035    import com.liferay.portal.model.Layout;
036    import com.liferay.portal.service.ClassNameLocalServiceUtil;
037    import com.liferay.portal.service.ImageLocalServiceUtil;
038    import com.liferay.portal.theme.ThemeDisplay;
039    import com.liferay.portal.util.PortalUtil;
040    import com.liferay.portal.webserver.WebServerServletTokenUtil;
041    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
042    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
043    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
044    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
045    import com.liferay.portlet.journal.model.JournalArticle;
046    import com.liferay.portlet.journal.model.JournalArticleResource;
047    import com.liferay.portlet.journal.model.JournalFolder;
048    import com.liferay.portlet.journal.model.JournalFolderConstants;
049    import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
050    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
051    import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
052    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
053    import com.liferay.portlet.journal.util.JournalUtil;
054    import com.liferay.portlet.journal.util.LocaleTransformerListener;
055    
056    import java.util.Locale;
057    import java.util.Map;
058    import java.util.Set;
059    
060    /**
061     * @author Brian Wing Shun Chan
062     * @author Wesley Gong
063     */
064    public class JournalArticleImpl extends JournalArticleBaseImpl {
065    
066            public static String getContentByLocale(
067                    Document document, String languageId) {
068    
069                    TransformerListener transformerListener =
070                            new LocaleTransformerListener();
071    
072                    document = transformerListener.onXml(
073                            document.clone(), languageId, null);
074    
075                    return document.asXML();
076            }
077    
078            /**
079             * @deprecated As of 7.0.0, replaced by {@link #getContentByLocale(Document,
080             *             String)}
081             */
082            @Deprecated
083            public static String getContentByLocale(
084                    String content, boolean templateDriven, String languageId) {
085    
086                    try {
087                            return getContentByLocale(SAXReaderUtil.read(content), languageId);
088                    }
089                    catch (DocumentException de) {
090                            if (_log.isWarnEnabled()) {
091                                    _log.warn(de, de);
092                            }
093    
094                            return content;
095                    }
096            }
097    
098            @Override
099            public String buildTreePath() throws PortalException {
100                    if (getFolderId() == JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
101                            return StringPool.SLASH;
102                    }
103    
104                    JournalFolder folder = getFolder();
105    
106                    return folder.buildTreePath();
107            }
108    
109            @Override
110            public long getArticleImageId(
111                    String elInstanceId, String elName, String languageId) {
112    
113                    return JournalArticleImageLocalServiceUtil.getArticleImageId(
114                            getGroupId(), getArticleId(), getVersion(), elInstanceId, elName,
115                            languageId);
116            }
117    
118            @Override
119            public String getArticleImageURL(ThemeDisplay themeDisplay) {
120                    if (!isSmallImage()) {
121                            return null;
122                    }
123    
124                    if (Validator.isNotNull(getSmallImageURL())) {
125                            return getSmallImageURL();
126                    }
127    
128                    return
129                            themeDisplay.getPathImage() + "/journal/article?img_id=" +
130                                    getSmallImageId() + "&t=" +
131                                            WebServerServletTokenUtil.getToken(getSmallImageId());
132            }
133    
134            @Override
135            public JournalArticleResource getArticleResource() throws PortalException {
136                    return JournalArticleResourceLocalServiceUtil.getArticleResource(
137                            getResourcePrimKey());
138            }
139    
140            @Override
141            public String getArticleResourceUuid() throws PortalException {
142                    JournalArticleResource articleResource = getArticleResource();
143    
144                    return articleResource.getUuid();
145            }
146    
147            @Override
148            public String[] getAvailableLanguageIds() {
149                    Set<String> availableLanguageIds = SetUtil.fromArray(
150                            super.getAvailableLanguageIds());
151    
152                    Document document = getDocument();
153    
154                    if (document != null) {
155                            for (String availableLanguageId :
156                                            LocalizationUtil.getAvailableLanguageIds(document)) {
157    
158                                    availableLanguageIds.add(availableLanguageId);
159                            }
160                    }
161    
162                    return availableLanguageIds.toArray(
163                            new String[availableLanguageIds.size()]);
164            }
165    
166            /**
167             * @deprecated As of 6.2.0, replaced by {@link #getAvailableLanguageIds}
168             */
169            @Deprecated
170            @Override
171            public String[] getAvailableLocales() {
172                    return getAvailableLanguageIds();
173            }
174    
175            @Override
176            public String getContentByLocale(String languageId) {
177                    return getContentByLocale(getDocument(), languageId);
178            }
179    
180            @Override
181            public DDMStructure getDDMStructure() throws PortalException {
182                    return DDMStructureLocalServiceUtil.fetchStructure(
183                            PortalUtil.getSiteGroupId(getGroupId()),
184                            ClassNameLocalServiceUtil.getClassNameId(JournalArticle.class),
185                            getDDMStructureKey(), true);
186            }
187    
188            @Override
189            public DDMTemplate getDDMTemplate() throws PortalException {
190                    return DDMTemplateLocalServiceUtil.fetchTemplate(
191                            PortalUtil.getSiteGroupId(getGroupId()),
192                            ClassNameLocalServiceUtil.getClassNameId(JournalArticle.class),
193                            getDDMStructureKey(), true);
194            }
195    
196            @Override
197            public String getDefaultLanguageId() {
198                    if (_defaultLanguageId == null) {
199                            _defaultLanguageId = super.getDefaultLanguageId();
200    
201                            if (Validator.isNull(_defaultLanguageId)) {
202                                    _defaultLanguageId = LocaleUtil.toLanguageId(
203                                            LocaleUtil.getSiteDefault());
204                            }
205                    }
206    
207                    return _defaultLanguageId;
208            }
209    
210            /**
211             * @deprecated As of 6.2.0, replaced by {@link #getDefaultLanguageId}
212             */
213            @Deprecated
214            @Override
215            public String getDefaultLocale() {
216                    return getDefaultLanguageId();
217            }
218    
219            @Override
220            public Document getDocument() {
221                    if (_document == null) {
222                            try {
223                                    _document = SAXReaderUtil.read(getContent());
224                            }
225                            catch (DocumentException de) {
226                                    if (_log.isWarnEnabled()) {
227                                            _log.warn(de, de);
228                                    }
229                            }
230                    }
231    
232                    return _document;
233            }
234    
235            @Override
236            public JournalFolder getFolder() throws PortalException {
237                    if (getFolderId() <= 0) {
238                            return new JournalFolderImpl();
239                    }
240    
241                    return JournalFolderLocalServiceUtil.getFolder(getFolderId());
242            }
243    
244            @Override
245            public Layout getLayout() {
246                    String layoutUuid = getLayoutUuid();
247    
248                    if (Validator.isNull(layoutUuid)) {
249                            return null;
250                    }
251    
252                    return JournalUtil.getArticleLayout(layoutUuid, getGroupId());
253            }
254    
255            @Override
256            public String getSmallImageType() throws PortalException {
257                    if ((_smallImageType == null) && isSmallImage()) {
258                            Image smallImage = ImageLocalServiceUtil.getImage(
259                                    getSmallImageId());
260    
261                            _smallImageType = smallImage.getType();
262                    }
263    
264                    return _smallImageType;
265            }
266    
267            @Override
268            public StagedModelType getStagedModelType() {
269                    return new StagedModelType(JournalArticle.class);
270            }
271    
272            /**
273             * @deprecated As of 7.0.0, replaced by {@link #getDDMStructureKey()}
274             */
275            @Override
276            public String getStructureId() {
277                    return getDDMStructureKey();
278            }
279    
280            /**
281             * @deprecated As of 7.0.0, replaced by {@link #getDDMTemplateKey()}
282             */
283            @Override
284            public String getTemplateId() {
285                    return getDDMTemplateKey();
286            }
287    
288            @Override
289            public Map<Locale, String> getTitleMap() {
290                    Locale defaultLocale = LocaleThreadLocal.getDefaultLocale();
291    
292                    try {
293                            Locale articleDefaultLocale = LocaleUtil.fromLanguageId(
294                                    getDefaultLanguageId());
295    
296                            LocaleThreadLocal.setDefaultLocale(articleDefaultLocale);
297    
298                            return super.getTitleMap();
299                    }
300                    finally {
301                            LocaleThreadLocal.setDefaultLocale(defaultLocale);
302                    }
303            }
304    
305            @Override
306            public long getTrashEntryClassPK() {
307                    return getResourcePrimKey();
308            }
309    
310            @Override
311            public boolean hasApprovedVersion() {
312                    JournalArticle article =
313                            JournalArticleLocalServiceUtil.fetchLatestArticle(
314                                    getGroupId(), getArticleId(),
315                                    WorkflowConstants.STATUS_APPROVED);
316    
317                    if (article == null) {
318                            return false;
319                    }
320    
321                    return true;
322            }
323    
324            /**
325             * @deprecated As of 7.0.0, with no direct replacement
326             */
327            @Deprecated
328            @Override
329            public boolean isTemplateDriven() {
330                    return true;
331            }
332    
333            /**
334             * @param  defaultImportLocale the default imported locale
335             * @throws LocaleException if a locale exception occurred
336             */
337            @Override
338            public void prepareLocalizedFieldsForImport(Locale defaultImportLocale)
339                    throws LocaleException {
340    
341                    super.prepareLocalizedFieldsForImport(defaultImportLocale);
342    
343                    String content = StringPool.BLANK;
344    
345                    try {
346                            content = JournalUtil.prepareLocalizedContentForImport(
347                                    getContent(), defaultImportLocale);
348                    }
349                    catch (Exception e) {
350                            throw new LocaleException(LocaleException.TYPE_DEFAULT, e);
351                    }
352    
353                    setContent(content);
354            }
355    
356            @Override
357            public void setContent(String content) {
358                    super.setContent(content);
359    
360                    _document = null;
361            }
362    
363            @Override
364            public void setDefaultLanguageId(String defaultLanguageId) {
365                    _defaultLanguageId = defaultLanguageId;
366            }
367    
368            @Override
369            public void setDocument(Document document) {
370                    _document = document;
371            }
372    
373            @Override
374            public void setSmallImageType(String smallImageType) {
375                    _smallImageType = smallImageType;
376            }
377    
378            /**
379             * @deprecated As of 7.0.0, replaced by {@link #setDDMStructureKey(String)}
380             */
381            @Override
382            public void setStructureId(String ddmStructureKey) {
383                    setDDMStructureKey(ddmStructureKey);
384            }
385    
386            /**
387             * @deprecated As of 7.0.0, replaced by {@link #setDDMTemplateKey(String)}
388             */
389            @Override
390            public void setTemplateId(String ddmTemplateKey) {
391                    setDDMTemplateKey(ddmTemplateKey);
392            }
393    
394            @Override
395            public void setTitle(String title) {
396                    super.setTitle(title);
397    
398                    _defaultLanguageId = null;
399            }
400    
401            private static final Log _log = LogFactoryUtil.getLog(
402                    JournalArticleImpl.class);
403    
404            @CacheField
405            private String _defaultLanguageId;
406    
407            @CacheField
408            private Document _document;
409    
410            private String _smallImageType;
411    
412    }