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            public JournalArticleImpl() {
099            }
100    
101            @Override
102            public String buildTreePath() throws PortalException {
103                    if (getFolderId() == JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
104                            return StringPool.SLASH;
105                    }
106    
107                    JournalFolder folder = getFolder();
108    
109                    return folder.buildTreePath();
110            }
111    
112            @Override
113            public long getArticleImageId(
114                    String elInstanceId, String elName, String languageId) {
115    
116                    return JournalArticleImageLocalServiceUtil.getArticleImageId(
117                            getGroupId(), getArticleId(), getVersion(), elInstanceId, elName,
118                            languageId);
119            }
120    
121            @Override
122            public String getArticleImageURL(ThemeDisplay themeDisplay) {
123                    if (!isSmallImage()) {
124                            return null;
125                    }
126    
127                    if (Validator.isNotNull(getSmallImageURL())) {
128                            return getSmallImageURL();
129                    }
130    
131                    return
132                            themeDisplay.getPathImage() + "/journal/article?img_id=" +
133                                    getSmallImageId() + "&t=" +
134                                            WebServerServletTokenUtil.getToken(getSmallImageId());
135            }
136    
137            @Override
138            public JournalArticleResource getArticleResource() throws PortalException {
139                    return JournalArticleResourceLocalServiceUtil.getArticleResource(
140                            getResourcePrimKey());
141            }
142    
143            @Override
144            public String getArticleResourceUuid() throws PortalException {
145                    JournalArticleResource articleResource = getArticleResource();
146    
147                    return articleResource.getUuid();
148            }
149    
150            @Override
151            public String[] getAvailableLanguageIds() {
152                    Set<String> availableLanguageIds = SetUtil.fromArray(
153                            super.getAvailableLanguageIds());
154    
155                    Document document = getDocument();
156    
157                    if (document != null) {
158                            for (String availableLanguageId :
159                                            LocalizationUtil.getAvailableLanguageIds(document)) {
160    
161                                    availableLanguageIds.add(availableLanguageId);
162                            }
163                    }
164    
165                    return availableLanguageIds.toArray(
166                            new String[availableLanguageIds.size()]);
167            }
168    
169            /**
170             * @deprecated As of 6.2.0, replaced by {@link #getAvailableLanguageIds}
171             */
172            @Deprecated
173            @Override
174            public String[] getAvailableLocales() {
175                    return getAvailableLanguageIds();
176            }
177    
178            @Override
179            public String getContentByLocale(String languageId) {
180                    return getContentByLocale(getDocument(), languageId);
181            }
182    
183            @Override
184            public DDMStructure getDDMStructure() throws PortalException {
185                    return DDMStructureLocalServiceUtil.fetchStructure(
186                            PortalUtil.getSiteGroupId(getGroupId()),
187                            ClassNameLocalServiceUtil.getClassNameId(JournalArticle.class),
188                            getStructureId(), true);
189            }
190    
191            @Override
192            public DDMTemplate getDDMTemplate() throws PortalException {
193                    return DDMTemplateLocalServiceUtil.fetchTemplate(
194                            PortalUtil.getSiteGroupId(getGroupId()),
195                            ClassNameLocalServiceUtil.getClassNameId(JournalArticle.class),
196                            getStructureId(), true);
197            }
198    
199            @Override
200            public String getDefaultLanguageId() {
201                    if (_defaultLanguageId == null) {
202                            _defaultLanguageId = super.getDefaultLanguageId();
203    
204                            if (Validator.isNull(_defaultLanguageId)) {
205                                    _defaultLanguageId = LocaleUtil.toLanguageId(
206                                            LocaleUtil.getSiteDefault());
207                            }
208                    }
209    
210                    return _defaultLanguageId;
211            }
212    
213            /**
214             * @deprecated As of 6.2.0, replaced by {@link #getDefaultLanguageId}
215             */
216            @Deprecated
217            @Override
218            public String getDefaultLocale() {
219                    return getDefaultLanguageId();
220            }
221    
222            @Override
223            public Document getDocument() {
224                    if (_document == null) {
225                            try {
226                                    _document = SAXReaderUtil.read(getContent());
227                            }
228                            catch (DocumentException de) {
229                                    if (_log.isWarnEnabled()) {
230                                            _log.warn(de, de);
231                                    }
232                            }
233                    }
234    
235                    return _document;
236            }
237    
238            @Override
239            public JournalFolder getFolder() throws PortalException {
240                    if (getFolderId() <= 0) {
241                            return new JournalFolderImpl();
242                    }
243    
244                    return JournalFolderLocalServiceUtil.getFolder(getFolderId());
245            }
246    
247            @Override
248            public Layout getLayout() {
249                    String layoutUuid = getLayoutUuid();
250    
251                    if (Validator.isNull(layoutUuid)) {
252                            return null;
253                    }
254    
255                    return JournalUtil.getArticleLayout(layoutUuid, getGroupId());
256            }
257    
258            @Override
259            public String getSmallImageType() throws PortalException {
260                    if ((_smallImageType == null) && isSmallImage()) {
261                            Image smallImage = ImageLocalServiceUtil.getImage(
262                                    getSmallImageId());
263    
264                            _smallImageType = smallImage.getType();
265                    }
266    
267                    return _smallImageType;
268            }
269    
270            @Override
271            public StagedModelType getStagedModelType() {
272                    return new StagedModelType(JournalArticle.class);
273            }
274    
275            @Override
276            public Map<Locale, String> getTitleMap() {
277                    Locale defaultLocale = LocaleThreadLocal.getDefaultLocale();
278    
279                    try {
280                            Locale articleDefaultLocale = LocaleUtil.fromLanguageId(
281                                    getDefaultLanguageId());
282    
283                            LocaleThreadLocal.setDefaultLocale(articleDefaultLocale);
284    
285                            return super.getTitleMap();
286                    }
287                    finally {
288                            LocaleThreadLocal.setDefaultLocale(defaultLocale);
289                    }
290            }
291    
292            @Override
293            public long getTrashEntryClassPK() {
294                    return getResourcePrimKey();
295            }
296    
297            @Override
298            public boolean hasApprovedVersion() {
299                    JournalArticle article =
300                            JournalArticleLocalServiceUtil.fetchLatestArticle(
301                                    getGroupId(), getArticleId(),
302                                    WorkflowConstants.STATUS_APPROVED);
303    
304                    if (article == null) {
305                            return false;
306                    }
307    
308                    return true;
309            }
310    
311            /**
312             * @deprecated As of 7.0.0, with no direct replacement
313             */
314            @Deprecated
315            @Override
316            public boolean isTemplateDriven() {
317                    return true;
318            }
319    
320            /**
321             * @param  defaultImportLocale the default imported locale
322             * @throws LocaleException if a locale exception occurred
323             */
324            @Override
325            public void prepareLocalizedFieldsForImport(Locale defaultImportLocale)
326                    throws LocaleException {
327    
328                    super.prepareLocalizedFieldsForImport(defaultImportLocale);
329    
330                    String content = StringPool.BLANK;
331    
332                    try {
333                            content = JournalUtil.prepareLocalizedContentForImport(
334                                    getContent(), defaultImportLocale);
335                    }
336                    catch (Exception e) {
337                            throw new LocaleException(LocaleException.TYPE_DEFAULT, e);
338                    }
339    
340                    setContent(content);
341            }
342    
343            @Override
344            public void setContent(String content) {
345                    super.setContent(content);
346    
347                    _document = null;
348            }
349    
350            @Override
351            public void setDefaultLanguageId(String defaultLanguageId) {
352                    _defaultLanguageId = defaultLanguageId;
353            }
354    
355            @Override
356            public void setDocument(Document document) {
357                    _document = document;
358            }
359    
360            @Override
361            public void setSmallImageType(String smallImageType) {
362                    _smallImageType = smallImageType;
363            }
364    
365            @Override
366            public void setTitle(String title) {
367                    super.setTitle(title);
368    
369                    _defaultLanguageId = null;
370            }
371    
372            private static final Log _log = LogFactoryUtil.getLog(
373                    JournalArticleImpl.class);
374    
375            @CacheField
376            private String _defaultLanguageId;
377    
378            @CacheField
379            private Document _document;
380    
381            private String _smallImageType;
382    
383    }