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.action;
016    
017    import com.liferay.portal.kernel.diff.CompareVersionsException;
018    import com.liferay.portal.kernel.portlet.PortletRequestModel;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.journal.NoSuchArticleException;
027    import com.liferay.portlet.journal.model.JournalArticle;
028    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
029    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
030    import com.liferay.portlet.journal.util.JournalUtil;
031    import com.liferay.portlet.journal.util.comparator.ArticleVersionComparator;
032    
033    import java.util.HashSet;
034    import java.util.List;
035    import java.util.Locale;
036    import java.util.Set;
037    
038    import javax.portlet.PortletConfig;
039    import javax.portlet.PortletContext;
040    import javax.portlet.PortletRequestDispatcher;
041    import javax.portlet.RenderRequest;
042    import javax.portlet.RenderResponse;
043    import javax.portlet.ResourceRequest;
044    import javax.portlet.ResourceResponse;
045    
046    import org.apache.struts.action.ActionForm;
047    import org.apache.struts.action.ActionForward;
048    import org.apache.struts.action.ActionMapping;
049    
050    /**
051     * @author Eudaldo Alonso
052     */
053    public class CompareVersionsAction extends PortletAction {
054    
055            @Override
056            public ActionForward render(
057                            ActionMapping actionMapping, ActionForm actionForm,
058                            PortletConfig portletConfig, RenderRequest renderRequest,
059                            RenderResponse renderResponse)
060                    throws Exception {
061    
062                    try {
063                            ActionUtil.getArticle(renderRequest);
064    
065                            compareVersions(renderRequest, renderResponse);
066                    }
067                    catch (Exception e) {
068                            if (e instanceof NoSuchArticleException) {
069                                    SessionErrors.add(renderRequest, e.getClass());
070    
071                                    return actionMapping.findForward("portlet.journal.error");
072                            }
073                            else {
074                                    throw e;
075                            }
076                    }
077    
078                    return actionMapping.findForward("portlet.journal.compare_versions");
079            }
080    
081            @Override
082            public void serveResource(
083                            ActionMapping actionMapping, ActionForm actionForm,
084                            PortletConfig portletConfig, ResourceRequest resourceRequest,
085                            ResourceResponse resourceResponse)
086                    throws Exception {
087    
088                    ThemeDisplay themeDisplay = (ThemeDisplay)resourceRequest.getAttribute(
089                            WebKeys.THEME_DISPLAY);
090    
091                    long groupId = ParamUtil.getLong(resourceRequest, "groupId");
092                    String articleId = ParamUtil.getString(resourceRequest, "articleId");
093                    double sourceVersion = ParamUtil.getDouble(
094                            resourceRequest, "filterSourceVersion");
095                    double targetVersion = ParamUtil.getDouble(
096                            resourceRequest, "filterTargetVersion");
097                    String languageId = ParamUtil.getString(resourceRequest, "languageId");
098    
099                    String diffHtmlResults = null;
100    
101                    try {
102                            diffHtmlResults = JournalUtil.diffHtml(
103                                    groupId, articleId, sourceVersion, targetVersion, languageId,
104                                    new PortletRequestModel(resourceRequest, resourceResponse),
105                                    themeDisplay);
106                    }
107                    catch (CompareVersionsException cve) {
108                            resourceRequest.setAttribute(
109                                    WebKeys.DIFF_VERSION, cve.getVersion());
110                    }
111    
112                    resourceRequest.setAttribute(
113                            WebKeys.DIFF_HTML_RESULTS, diffHtmlResults);
114    
115                    PortletContext portletContext = portletConfig.getPortletContext();
116    
117                    PortletRequestDispatcher portletRequestDispatcher =
118                            portletContext.getRequestDispatcher(
119                                    "/html/taglib/ui/diff_version_comparator/diff_html.jsp");
120    
121                    portletRequestDispatcher.include(resourceRequest, resourceResponse);
122            }
123    
124            protected void compareVersions(
125                            RenderRequest renderRequest, RenderResponse renderResponse)
126                    throws Exception {
127    
128                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
129                            WebKeys.THEME_DISPLAY);
130    
131                    long groupId = ParamUtil.getLong(renderRequest, "groupId");
132                    String articleId = ParamUtil.getString(renderRequest, "articleId");
133    
134                    String sourceArticleId = ParamUtil.getString(
135                            renderRequest, "sourceVersion");
136    
137                    int index = sourceArticleId.lastIndexOf(
138                            EditArticleAction.VERSION_SEPARATOR);
139    
140                    if (index != -1) {
141                            sourceArticleId =
142                                    sourceArticleId.substring(
143                                            index + EditArticleAction.VERSION_SEPARATOR.length(),
144                                            sourceArticleId.length());
145                    }
146    
147                    double sourceVersion = GetterUtil.getDouble(sourceArticleId);
148    
149                    String targetArticleId = ParamUtil.getString(
150                            renderRequest, "targetVersion");
151    
152                    index = targetArticleId.lastIndexOf(
153                            EditArticleAction.VERSION_SEPARATOR);
154    
155                    if (index != -1) {
156                            targetArticleId =
157                                    targetArticleId.substring(
158                                            index + EditArticleAction.VERSION_SEPARATOR.length(),
159                                            targetArticleId.length());
160                    }
161    
162                    double targetVersion = GetterUtil.getDouble(targetArticleId);
163    
164                    if ((sourceVersion == 0) && (targetVersion == 0)) {
165                            List<JournalArticle> sourceArticles =
166                                    JournalArticleServiceUtil.getArticlesByArticleId(
167                                            groupId, articleId, 0, 1,
168                                            new ArticleVersionComparator(false));
169    
170                            JournalArticle sourceArticle = sourceArticles.get(0);
171    
172                            sourceVersion = sourceArticle.getVersion();
173    
174                            List<JournalArticle> targetArticles =
175                                    JournalArticleServiceUtil.getArticlesByArticleId(
176                                            groupId, articleId, 0, 1,
177                                            new ArticleVersionComparator(true));
178    
179                            JournalArticle targetArticle = targetArticles.get(0);
180    
181                            targetVersion = targetArticle.getVersion();
182                    }
183    
184                    if (sourceVersion > targetVersion) {
185                            double tempVersion = targetVersion;
186    
187                            targetVersion = sourceVersion;
188                            sourceVersion = tempVersion;
189                    }
190    
191                    String languageId = getLanguageId(
192                            renderRequest, groupId, articleId, sourceVersion, targetVersion);
193    
194                    String diffHtmlResults = null;
195    
196                    try {
197                            diffHtmlResults = JournalUtil.diffHtml(
198                                    groupId, articleId, sourceVersion, targetVersion, languageId,
199                                    new PortletRequestModel(renderRequest, renderResponse),
200                                    themeDisplay);
201                    }
202                    catch (CompareVersionsException cve) {
203                            renderRequest.setAttribute(WebKeys.DIFF_VERSION, cve.getVersion());
204                    }
205    
206                    renderRequest.setAttribute(WebKeys.DIFF_HTML_RESULTS, diffHtmlResults);
207                    renderRequest.setAttribute(WebKeys.SOURCE_VERSION, sourceVersion);
208                    renderRequest.setAttribute(WebKeys.TARGET_VERSION, targetVersion);
209            }
210    
211            protected String getLanguageId(
212                            RenderRequest renderRequest, long groupId, String articleId,
213                            double sourceVersion, double targetVersion)
214                    throws Exception {
215    
216                    JournalArticle sourceArticle =
217                            JournalArticleLocalServiceUtil.fetchArticle(
218                                    groupId, articleId, sourceVersion);
219    
220                    JournalArticle targetArticle =
221                            JournalArticleLocalServiceUtil.fetchArticle(
222                                    groupId, articleId, targetVersion);
223    
224                    Set<Locale> locales = new HashSet<Locale>();
225    
226                    for (String locale : sourceArticle.getAvailableLanguageIds()) {
227                            locales.add(LocaleUtil.fromLanguageId(locale));
228                    }
229    
230                    for (String locale : targetArticle.getAvailableLanguageIds()) {
231                            locales.add(LocaleUtil.fromLanguageId(locale));
232                    }
233    
234                    String languageId = ParamUtil.get(
235                            renderRequest, "languageId", targetArticle.getDefaultLanguageId());
236    
237                    Locale locale = LocaleUtil.fromLanguageId(languageId);
238    
239                    if (!locales.contains(locale)) {
240                            languageId = targetArticle.getDefaultLanguageId();
241                    }
242    
243                    renderRequest.setAttribute(WebKeys.AVAILABLE_LOCALES, locales);
244                    renderRequest.setAttribute(WebKeys.LANGUAGE_ID, languageId);
245    
246                    return languageId;
247            }
248    
249    }