001    /**
002     * Copyright (c) 2000-2011 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.action;
016    
017    import com.liferay.portal.kernel.portlet.LiferayWindowState;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.upload.UploadPortletRequest;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.LocaleUtil;
024    import com.liferay.portal.kernel.util.LocalizationUtil;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.kernel.workflow.WorkflowConstants;
030    import com.liferay.portal.model.Layout;
031    import com.liferay.portal.security.auth.PrincipalException;
032    import com.liferay.portal.service.ServiceContext;
033    import com.liferay.portal.service.ServiceContextFactory;
034    import com.liferay.portal.struts.PortletAction;
035    import com.liferay.portal.theme.ThemeDisplay;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.portal.util.WebKeys;
038    import com.liferay.portlet.PortletPreferencesFactoryUtil;
039    import com.liferay.portlet.PortletURLImpl;
040    import com.liferay.portlet.asset.AssetCategoryException;
041    import com.liferay.portlet.asset.AssetTagException;
042    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
043    import com.liferay.portlet.journal.ArticleContentException;
044    import com.liferay.portlet.journal.ArticleContentSizeException;
045    import com.liferay.portlet.journal.ArticleDisplayDateException;
046    import com.liferay.portlet.journal.ArticleExpirationDateException;
047    import com.liferay.portlet.journal.ArticleIdException;
048    import com.liferay.portlet.journal.ArticleSmallImageNameException;
049    import com.liferay.portlet.journal.ArticleSmallImageSizeException;
050    import com.liferay.portlet.journal.ArticleTitleException;
051    import com.liferay.portlet.journal.ArticleTypeException;
052    import com.liferay.portlet.journal.ArticleVersionException;
053    import com.liferay.portlet.journal.DuplicateArticleIdException;
054    import com.liferay.portlet.journal.NoSuchArticleException;
055    import com.liferay.portlet.journal.NoSuchStructureException;
056    import com.liferay.portlet.journal.NoSuchTemplateException;
057    import com.liferay.portlet.journal.model.JournalArticle;
058    import com.liferay.portlet.journal.model.JournalStructure;
059    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
060    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
061    import com.liferay.portlet.journal.service.JournalStructureLocalServiceUtil;
062    import com.liferay.portlet.journal.util.JournalUtil;
063    
064    import java.io.File;
065    
066    import java.util.Calendar;
067    import java.util.Enumeration;
068    import java.util.HashMap;
069    import java.util.Locale;
070    import java.util.Map;
071    
072    import javax.portlet.ActionRequest;
073    import javax.portlet.ActionResponse;
074    import javax.portlet.PortletConfig;
075    import javax.portlet.PortletContext;
076    import javax.portlet.PortletPreferences;
077    import javax.portlet.PortletRequest;
078    import javax.portlet.PortletRequestDispatcher;
079    import javax.portlet.RenderRequest;
080    import javax.portlet.RenderResponse;
081    import javax.portlet.ResourceRequest;
082    import javax.portlet.ResourceResponse;
083    import javax.portlet.WindowState;
084    
085    import org.apache.struts.action.ActionForm;
086    import org.apache.struts.action.ActionForward;
087    import org.apache.struts.action.ActionMapping;
088    
089    /**
090     * @author Brian Wing Shun Chan
091     * @author Raymond Augé
092     * @author Eduardo Lundgren
093     * @author Juan Fernández
094     */
095    public class EditArticleAction extends PortletAction {
096    
097            public static final String VERSION_SEPARATOR = "_version_";
098    
099            @Override
100            public void processAction(
101                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
102                            ActionRequest actionRequest, ActionResponse actionResponse)
103                    throws Exception {
104    
105                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
106    
107                    JournalArticle article = null;
108                    String oldUrlTitle = StringPool.BLANK;
109    
110                    try {
111                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.TRANSLATE) ||
112                                    cmd.equals(Constants.UPDATE)) {
113    
114                                    Object[] returnValue = updateArticle(actionRequest);
115    
116                                    article = (JournalArticle)returnValue[0];
117                                    oldUrlTitle = ((String)returnValue[1]);
118                            }
119                            else if (cmd.equals(Constants.DELETE)) {
120                                    deleteArticles(actionRequest);
121                            }
122                            else if (cmd.equals(Constants.DELETE_TRANSLATION)) {
123                                    removeArticlesLocale(actionRequest);
124                            }
125                            else if (cmd.equals(Constants.EXPIRE)) {
126                                    expireArticles(actionRequest);
127                            }
128                            else if (cmd.equals(Constants.SUBSCRIBE)) {
129                                    subscribeArticles(actionRequest);
130                            }
131                            else if (cmd.equals(Constants.UNSUBSCRIBE)) {
132                                    unsubscribeArticles(actionRequest);
133                            }
134    
135                            if (Validator.isNotNull(cmd)) {
136                                    String redirect = ParamUtil.getString(
137                                            actionRequest, "redirect");
138    
139                                    int workflowAction = ParamUtil.getInteger(
140                                            actionRequest, "workflowAction",
141                                            WorkflowConstants.ACTION_PUBLISH);
142    
143                                    if ((article != null) &&
144                                            (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT)) {
145    
146                                            redirect = getSaveAndContinueRedirect(
147                                                    portletConfig, actionRequest, article, redirect);
148                                    }
149    
150                                    if (redirect.contains("/content/" + oldUrlTitle + "?")) {
151                                            int pos = redirect.indexOf("?");
152    
153                                            if (pos == -1) {
154                                                    pos = redirect.length();
155                                            }
156    
157                                            String newRedirect = redirect.substring(
158                                                    0, pos - oldUrlTitle.length());
159    
160                                            newRedirect += article.getUrlTitle();
161    
162                                            if (oldUrlTitle.contains("/maximized")) {
163                                                    newRedirect += "/maximized";
164                                            }
165    
166                                            if (pos < redirect.length()) {
167                                                    newRedirect +=
168                                                            "?" +
169                                                                    redirect.substring(pos + 1, redirect.length());
170                                            }
171    
172                                            redirect = newRedirect;
173                                    }
174    
175                                    WindowState windowState = actionRequest.getWindowState();
176    
177                                    ThemeDisplay themeDisplay =
178                                            (ThemeDisplay)actionRequest.getAttribute(
179                                                    WebKeys.THEME_DISPLAY);
180    
181                                    Layout layout = themeDisplay.getLayout();
182    
183                                    if (cmd.equals(Constants.DELETE_TRANSLATION) ||
184                                            cmd.equals(Constants.TRANSLATE)) {
185    
186                                            setForward(
187                                                    actionRequest,
188                                                    "portlet.journal.update_translation_redirect");
189                                    }
190                                    else if (!windowState.equals(LiferayWindowState.POP_UP) &&
191                                                     layout.isTypeControlPanel()) {
192    
193                                            sendRedirect(actionRequest, actionResponse, redirect);
194                                    }
195                                    else {
196                                            redirect = PortalUtil.escapeRedirect(redirect);
197    
198                                            if (Validator.isNotNull(redirect)) {
199                                                    actionResponse.sendRedirect(redirect);
200                                            }
201                                    }
202                            }
203                    }
204                    catch (Exception e) {
205                            if (e instanceof NoSuchArticleException ||
206                                    e instanceof NoSuchStructureException ||
207                                    e instanceof NoSuchTemplateException ||
208                                    e instanceof PrincipalException) {
209    
210                                    SessionErrors.add(actionRequest, e.getClass().getName());
211    
212                                    setForward(actionRequest, "portlet.journal.error");
213                            }
214                            else if (e instanceof ArticleContentException ||
215                                             e instanceof ArticleContentSizeException ||
216                                             e instanceof ArticleDisplayDateException ||
217                                             e instanceof ArticleExpirationDateException ||
218                                             e instanceof ArticleIdException ||
219                                             e instanceof ArticleSmallImageNameException ||
220                                             e instanceof ArticleSmallImageSizeException ||
221                                             e instanceof ArticleTitleException ||
222                                             e instanceof ArticleTypeException ||
223                                             e instanceof ArticleVersionException ||
224                                             e instanceof DuplicateArticleIdException) {
225    
226                                    SessionErrors.add(actionRequest, e.getClass().getName());
227                            }
228                            else if (e instanceof AssetCategoryException ||
229                                             e instanceof AssetTagException) {
230    
231                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
232                            }
233                            else {
234                                    throw e;
235                            }
236                    }
237            }
238    
239            @Override
240            public ActionForward render(
241                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
242                            RenderRequest renderRequest, RenderResponse renderResponse)
243                    throws Exception {
244    
245                    try {
246                            ActionUtil.getArticle(renderRequest);
247                    }
248                    catch (NoSuchArticleException nsse) {
249    
250                            // Let this slide because the user can manually input a article id
251                            // for a new article that does not yet exist.
252    
253                    }
254                    catch (Exception e) {
255                            if (//e instanceof NoSuchArticleException ||
256                                    e instanceof PrincipalException) {
257    
258                                    SessionErrors.add(renderRequest, e.getClass().getName());
259    
260                                    return mapping.findForward("portlet.journal.error");
261                            }
262                            else {
263                                    throw e;
264                            }
265                    }
266    
267                    return mapping.findForward(
268                            getForward(renderRequest, "portlet.journal.edit_article"));
269            }
270    
271            @Override
272            public void serveResource(
273                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
274                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
275                    throws Exception {
276    
277                    PortletContext portletContext = portletConfig.getPortletContext();
278    
279                    PortletRequestDispatcher portletRequestDispatcher =
280                            portletContext.getRequestDispatcher(
281                                    "/html/portlet/journal/editor.jsp");
282    
283                    portletRequestDispatcher.include(resourceRequest, resourceResponse);
284            }
285    
286            protected void deleteArticles(ActionRequest actionRequest)
287                    throws Exception {
288    
289                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
290    
291                    String[] deleteArticleIds = StringUtil.split(
292                            ParamUtil.getString(actionRequest, "deleteArticleIds"));
293    
294                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
295                            JournalArticle.class.getName(), actionRequest);
296    
297                    for (int i = 0; i < deleteArticleIds.length; i++) {
298                            int pos = deleteArticleIds[i].lastIndexOf(VERSION_SEPARATOR);
299    
300                            String articleId = deleteArticleIds[i];
301    
302                            String articleURL = ParamUtil.getString(
303                                    actionRequest, "articleURL");
304    
305                            double version = 0;
306    
307                            if (pos == -1) {
308                                    JournalArticleServiceUtil.deleteArticle(
309                                            groupId, articleId, articleURL, serviceContext);
310                            }
311                            else {
312                                    articleId = articleId.substring(0, pos);
313                                    version = GetterUtil.getDouble(
314                                            deleteArticleIds[i].substring(
315                                                    pos + VERSION_SEPARATOR.length()));
316    
317                                    JournalArticleServiceUtil.deleteArticle(
318                                            groupId, articleId, version, articleURL, serviceContext);
319                            }
320    
321                            JournalUtil.removeRecentArticle(actionRequest, articleId, version);
322                    }
323            }
324    
325            protected void expireArticles(ActionRequest actionRequest)
326                    throws Exception {
327    
328                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
329    
330                    String[] expireArticleIds = StringUtil.split(
331                            ParamUtil.getString(actionRequest, "expireArticleIds"));
332    
333                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
334                            JournalArticle.class.getName(), actionRequest);
335    
336                    for (int i = 0; i < expireArticleIds.length; i++) {
337                            int pos = expireArticleIds[i].lastIndexOf(VERSION_SEPARATOR);
338    
339                            String articleId = expireArticleIds[i];
340    
341                            String articleURL = ParamUtil.getString(
342                                    actionRequest, "articleURL");
343    
344                            double version = 0;
345    
346                            if (pos == -1) {
347                                    JournalArticleServiceUtil.expireArticle(
348                                            groupId, articleId, articleURL, serviceContext);
349                            }
350                            else {
351                                    articleId = articleId.substring(0, pos);
352                                    version = GetterUtil.getDouble(
353                                            expireArticleIds[i].substring(
354                                                    pos + VERSION_SEPARATOR.length()));
355    
356                                    JournalArticleServiceUtil.expireArticle(
357                                            groupId, articleId, version, articleURL, serviceContext);
358                            }
359                    }
360            }
361    
362            protected Map<String, byte[]> getImages(
363                            UploadPortletRequest uploadPortletRequest)
364                    throws Exception {
365    
366                    Map<String, byte[]> images = new HashMap<String, byte[]>();
367    
368                    String imagePrefix = "structure_image_";
369    
370                    Enumeration<String> enu = uploadPortletRequest.getParameterNames();
371    
372                    while (enu.hasMoreElements()) {
373                            String name = enu.nextElement();
374    
375                            if (name.startsWith(imagePrefix)) {
376                                    File file = uploadPortletRequest.getFile(name);
377                                    byte[] bytes = FileUtil.getBytes(file);
378    
379                                    if ((bytes != null) && (bytes.length > 0)) {
380                                            name = name.substring(imagePrefix.length(), name.length());
381    
382                                            images.put(name, bytes);
383                                    }
384                            }
385                    }
386    
387                    return images;
388            }
389    
390            protected String getSaveAndContinueRedirect(
391                            PortletConfig portletConfig, ActionRequest actionRequest,
392                            JournalArticle article, String redirect)
393                    throws Exception {
394    
395                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
396                            WebKeys.THEME_DISPLAY);
397    
398                    String originalRedirect = ParamUtil.getString(
399                            actionRequest, "originalRedirect");
400    
401                    String languageId = ParamUtil.getString(actionRequest, "languageId");
402    
403                    PortletURLImpl portletURL = new PortletURLImpl(
404                            actionRequest, portletConfig.getPortletName(),
405                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
406    
407                    portletURL.setWindowState(actionRequest.getWindowState());
408    
409                    portletURL.setParameter("struts_action", "/journal/edit_article");
410                    portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
411                    portletURL.setParameter("redirect", redirect, false);
412                    portletURL.setParameter("originalRedirect", originalRedirect, false);
413                    portletURL.setParameter(
414                            "groupId", String.valueOf(article.getGroupId()), false);
415                    portletURL.setParameter("articleId", article.getArticleId(), false);
416                    portletURL.setParameter(
417                            "version", String.valueOf(article.getVersion()), false);
418                    portletURL.setParameter("languageId", languageId, false);
419    
420                    return portletURL.toString();
421            }
422    
423            protected void removeArticlesLocale(ActionRequest actionRequest)
424                    throws Exception {
425    
426                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
427    
428                    String[] removeArticleLocaleIds = StringUtil.split(
429                            ParamUtil.getString(actionRequest, "deleteArticleIds"));
430    
431                    for (int i = 0; i < removeArticleLocaleIds.length; i++) {
432                            int pos = removeArticleLocaleIds[i].lastIndexOf(VERSION_SEPARATOR);
433    
434                            String articleId = removeArticleLocaleIds[i].substring(0, pos);
435                            double version = GetterUtil.getDouble(
436                                    removeArticleLocaleIds[i].substring(
437                                            pos + VERSION_SEPARATOR.length()));
438                            String languageId = ParamUtil.getString(
439                                    actionRequest, "languageId");
440    
441                            JournalArticleServiceUtil.removeArticleLocale(
442                                    groupId, articleId, version, languageId);
443                    }
444            }
445    
446            protected void subscribeArticles(ActionRequest actionRequest)
447                    throws Exception {
448    
449                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
450                            WebKeys.THEME_DISPLAY);
451    
452                    JournalArticleServiceUtil.subscribe(themeDisplay.getScopeGroupId());
453            }
454    
455            protected void unsubscribeArticles(ActionRequest actionRequest)
456                    throws Exception {
457    
458                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
459                            WebKeys.THEME_DISPLAY);
460    
461                    JournalArticleServiceUtil.unsubscribe(themeDisplay.getScopeGroupId());
462            }
463    
464            protected Object[] updateArticle(ActionRequest actionRequest)
465                    throws Exception {
466    
467                    UploadPortletRequest uploadPortletRequest =
468                            PortalUtil.getUploadPortletRequest(actionRequest);
469    
470                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
471                            WebKeys.THEME_DISPLAY);
472    
473                    String cmd = ParamUtil.getString(uploadPortletRequest, Constants.CMD);
474    
475                    long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
476    
477                    long classNameId = ParamUtil.getLong(
478                            uploadPortletRequest, "classNameId");
479                    long classPK = ParamUtil.getLong(uploadPortletRequest, "classPK");
480    
481                    String articleId = ParamUtil.getString(
482                            uploadPortletRequest, "articleId");
483                    boolean autoArticleId = ParamUtil.getBoolean(
484                            uploadPortletRequest, "autoArticleId");
485    
486                    double version = ParamUtil.getDouble(uploadPortletRequest, "version");
487    
488                    boolean localized = ParamUtil.getBoolean(
489                            uploadPortletRequest, "localized");
490    
491                    String defaultLanguageId = ParamUtil.getString(
492                            uploadPortletRequest, "defaultLanguageId");
493    
494                    Locale defaultLocale = LocaleUtil.fromLanguageId(defaultLanguageId);
495    
496                    String toLanguageId = ParamUtil.getString(
497                            uploadPortletRequest, "toLanguageId");
498    
499                    Locale toLocale = null;
500    
501                    String title = StringPool.BLANK;
502                    String description = StringPool.BLANK;
503    
504                    if (Validator.isNull(toLanguageId)) {
505                            title = ParamUtil.getString(
506                                    uploadPortletRequest, "title_" + defaultLanguageId);
507                            description = ParamUtil.getString(
508                                    uploadPortletRequest, "description_" + defaultLanguageId);
509                    }
510                    else{
511                            toLocale = LocaleUtil.fromLanguageId(toLanguageId);
512    
513                            title = ParamUtil.getString(
514                                    uploadPortletRequest, "title_" + toLanguageId);
515                            description = ParamUtil.getString(
516                                    uploadPortletRequest, "description_" + toLanguageId);
517                    }
518    
519                    String content = ParamUtil.getString(uploadPortletRequest, "content");
520    
521                    Boolean fileItemThresholdSizeExceeded =
522                                    (Boolean)uploadPortletRequest.getAttribute(
523                            WebKeys.FILE_ITEM_THRESHOLD_SIZE_EXCEEDED);
524    
525                    if ((fileItemThresholdSizeExceeded != null) &&
526                            fileItemThresholdSizeExceeded.booleanValue()) {
527    
528                            throw new ArticleContentSizeException();
529                    }
530    
531                    String type = ParamUtil.getString(uploadPortletRequest, "type");
532                    String structureId = ParamUtil.getString(
533                            uploadPortletRequest, "structureId");
534                    String templateId = ParamUtil.getString(
535                            uploadPortletRequest, "templateId");
536                    String layoutUuid = ParamUtil.getString(
537                            uploadPortletRequest, "layoutUuid");
538    
539                    int displayDateMonth = ParamUtil.getInteger(
540                            uploadPortletRequest, "displayDateMonth");
541                    int displayDateDay = ParamUtil.getInteger(
542                            uploadPortletRequest, "displayDateDay");
543                    int displayDateYear = ParamUtil.getInteger(
544                            uploadPortletRequest, "displayDateYear");
545                    int displayDateHour = ParamUtil.getInteger(
546                            uploadPortletRequest, "displayDateHour");
547                    int displayDateMinute = ParamUtil.getInteger(
548                            uploadPortletRequest, "displayDateMinute");
549                    int displayDateAmPm = ParamUtil.getInteger(
550                            uploadPortletRequest, "displayDateAmPm");
551    
552                    if (displayDateAmPm == Calendar.PM) {
553                            displayDateHour += 12;
554                    }
555    
556                    int expirationDateMonth = ParamUtil.getInteger(
557                            uploadPortletRequest, "expirationDateMonth");
558                    int expirationDateDay = ParamUtil.getInteger(
559                            uploadPortletRequest, "expirationDateDay");
560                    int expirationDateYear = ParamUtil.getInteger(
561                            uploadPortletRequest, "expirationDateYear");
562                    int expirationDateHour = ParamUtil.getInteger(
563                            uploadPortletRequest, "expirationDateHour");
564                    int expirationDateMinute = ParamUtil.getInteger(
565                            uploadPortletRequest, "expirationDateMinute");
566                    int expirationDateAmPm = ParamUtil.getInteger(
567                            uploadPortletRequest, "expirationDateAmPm");
568                    boolean neverExpire = ParamUtil.getBoolean(
569                            uploadPortletRequest, "neverExpire");
570    
571                    if (expirationDateAmPm == Calendar.PM) {
572                            expirationDateHour += 12;
573                    }
574    
575                    int reviewDateMonth = ParamUtil.getInteger(
576                            uploadPortletRequest, "reviewDateMonth");
577                    int reviewDateDay = ParamUtil.getInteger(
578                            uploadPortletRequest, "reviewDateDay");
579                    int reviewDateYear = ParamUtil.getInteger(
580                            uploadPortletRequest, "reviewDateYear");
581                    int reviewDateHour = ParamUtil.getInteger(
582                            uploadPortletRequest, "reviewDateHour");
583                    int reviewDateMinute = ParamUtil.getInteger(
584                            uploadPortletRequest, "reviewDateMinute");
585                    int reviewDateAmPm = ParamUtil.getInteger(
586                            uploadPortletRequest, "reviewDateAmPm");
587                    boolean neverReview = ParamUtil.getBoolean(
588                            uploadPortletRequest, "neverReview");
589    
590                    if (reviewDateAmPm == Calendar.PM) {
591                            reviewDateHour += 12;
592                    }
593    
594                    boolean indexable = ParamUtil.getBoolean(
595                            uploadPortletRequest, "indexable");
596    
597                    boolean smallImage = ParamUtil.getBoolean(
598                            uploadPortletRequest, "smallImage");
599                    String smallImageURL = ParamUtil.getString(
600                            uploadPortletRequest, "smallImageURL");
601                    File smallFile = uploadPortletRequest.getFile("smallFile");
602    
603                    Map<String, byte[]> images = getImages(uploadPortletRequest);
604    
605                    String articleURL = ParamUtil.getString(
606                            uploadPortletRequest, "articleURL");
607    
608                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
609                            JournalArticle.class.getName(), actionRequest);
610    
611                    serviceContext.setAttribute("defaultLanguageId", defaultLanguageId);
612    
613                    JournalArticle article = null;
614                    String oldUrlTitle = StringPool.BLANK;
615    
616                    if (cmd.equals(Constants.ADD)) {
617                            Map<Locale, String> titleMap = new HashMap<Locale, String>();
618    
619                            titleMap.put(defaultLocale, title);
620    
621                            Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
622    
623                            descriptionMap.put(defaultLocale, description);
624    
625                            if (Validator.isNull(structureId)) {
626                                    content = LocalizationUtil.updateLocalization(
627                                            StringPool.BLANK, "static-content", content,
628                                            defaultLanguageId, defaultLanguageId, true, localized);
629                            }
630    
631                            // Add article
632    
633                            article = JournalArticleServiceUtil.addArticle(
634                                    groupId, classNameId, classPK, articleId, autoArticleId,
635                                    titleMap, descriptionMap, content, type, structureId,
636                                    templateId, layoutUuid, displayDateMonth, displayDateDay,
637                                    displayDateYear, displayDateHour, displayDateMinute,
638                                    expirationDateMonth, expirationDateDay, expirationDateYear,
639                                    expirationDateHour, expirationDateMinute, neverExpire,
640                                    reviewDateMonth, reviewDateDay, reviewDateYear, reviewDateHour,
641                                    reviewDateMinute, neverReview, indexable, smallImage,
642                                    smallImageURL, smallFile, images, articleURL, serviceContext);
643    
644                            AssetPublisherUtil.addAndStoreSelection(
645                                    actionRequest, JournalArticle.class.getName(),
646                                    article.getResourcePrimKey(), -1);
647                    }
648                    else {
649    
650                            // Merge current content with new content
651    
652                            JournalArticle curArticle = JournalArticleServiceUtil.getArticle(
653                                    groupId, articleId, version);
654    
655                            if (Validator.isNull(structureId)) {
656                                    if (!curArticle.isTemplateDriven()) {
657                                            String curContent = StringPool.BLANK;
658    
659                                            curContent = curArticle.getContent();
660    
661                                            if (cmd.equals(Constants.TRANSLATE)) {
662                                                    content = LocalizationUtil.updateLocalization(
663                                                            curContent, "static-content", content, toLanguageId,
664                                                            defaultLanguageId, true, true);
665                                            }
666                                            else {
667                                                    content = LocalizationUtil.updateLocalization(
668                                                            curContent, "static-content", content,
669                                                            defaultLanguageId, defaultLanguageId, true,
670                                                            localized);
671                                            }
672                                    }
673                            }
674                            else {
675                                    if (curArticle.isTemplateDriven()) {
676                                            JournalStructure structure = null;
677    
678                                            try {
679                                                    structure =
680                                                            JournalStructureLocalServiceUtil.getStructure(
681                                                                    groupId, structureId);
682                                            }
683                                            catch (NoSuchStructureException nsse) {
684                                                    structure =
685                                                            JournalStructureLocalServiceUtil.getStructure(
686                                                                    themeDisplay.getCompanyGroupId(), structureId);
687                                            }
688    
689                                            if (!cmd.equals(Constants.TRANSLATE)) {
690                                                    content = JournalUtil.mergeArticleContent(
691                                                            curArticle.getContent(), content, true);
692                                                    content = JournalUtil.removeOldContent(
693                                                            content, structure.getMergedXsd());
694                                            }
695                                    }
696                            }
697    
698                            // Update article
699    
700                            article = JournalArticleServiceUtil.getArticle(
701                                    groupId, articleId, version);
702    
703                            Map<Locale, String> titleMap = article.getTitleMap();
704                            Map<Locale, String> descriptionMap =
705                                    article.getDescriptionMap();
706    
707                            String tempOldUrlTitle = article.getUrlTitle();
708    
709                            if (cmd.equals(Constants.UPDATE)) {
710                                    titleMap.put(defaultLocale, title);
711                                    descriptionMap.put(defaultLocale, description);
712    
713                                    article = JournalArticleServiceUtil.updateArticle(
714                                            groupId, articleId, version, titleMap, descriptionMap,
715                                            content, type, structureId, templateId, layoutUuid,
716                                            displayDateMonth, displayDateDay, displayDateYear,
717                                            displayDateHour, displayDateMinute, expirationDateMonth,
718                                            expirationDateDay, expirationDateYear, expirationDateHour,
719                                            expirationDateMinute, neverExpire, reviewDateMonth,
720                                            reviewDateDay, reviewDateYear, reviewDateHour,
721                                            reviewDateMinute, neverReview, indexable, smallImage,
722                                            smallImageURL, smallFile, images, articleURL,
723                                            serviceContext);
724                            }
725                            else if (cmd.equals(Constants.TRANSLATE)) {
726                                    article = JournalArticleServiceUtil.updateArticleTranslation(
727                                            groupId, articleId, version, toLocale, title, description,
728                                            content);
729                            }
730    
731                            if (!tempOldUrlTitle.equals(article.getUrlTitle())) {
732                                    oldUrlTitle = tempOldUrlTitle;
733                            }
734                    }
735    
736                    // Recent articles
737    
738                    JournalUtil.addRecentArticle(actionRequest, article);
739    
740                    // Journal content
741    
742                    String portletResource = ParamUtil.getString(
743                            uploadPortletRequest, "portletResource");
744    
745                    if (Validator.isNotNull(portletResource)) {
746                            PortletPreferences preferences =
747                                    PortletPreferencesFactoryUtil.getPortletSetup(
748                                            uploadPortletRequest, portletResource);
749    
750                            preferences.setValue(
751                                    "groupId", String.valueOf(article.getGroupId()));
752                            preferences.setValue("articleId", article.getArticleId());
753    
754                            preferences.store();
755    
756                            updateContentSearch(
757                                    actionRequest, portletResource, article.getArticleId());
758                    }
759    
760                    return new Object[] {article, oldUrlTitle};
761            }
762    
763            protected void updateContentSearch(
764                            ActionRequest actionRequest, String portletResource,
765                            String articleId)
766                    throws Exception {
767    
768                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
769                            WebKeys.THEME_DISPLAY);
770    
771                    Layout layout = themeDisplay.getLayout();
772    
773                    JournalContentSearchLocalServiceUtil.updateContentSearch(
774                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
775                            portletResource, articleId);
776            }
777    
778    }