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