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