001    /**
002     * Copyright (c) 2000-2013 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.journalcontent.action;
016    
017    import com.liferay.portal.kernel.portlet.DefaultConfigurationAction;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.xml.Document;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.kernel.xml.SAXReaderUtil;
027    import com.liferay.portal.model.Group;
028    import com.liferay.portal.model.Layout;
029    import com.liferay.portal.model.LayoutTypePortlet;
030    import com.liferay.portal.model.LayoutTypePortletConstants;
031    import com.liferay.portal.model.PortletConstants;
032    import com.liferay.portal.service.GroupLocalServiceUtil;
033    import com.liferay.portal.service.LayoutLocalServiceUtil;
034    import com.liferay.portal.theme.ThemeDisplay;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.portlet.journal.NoSuchArticleException;
037    import com.liferay.portlet.journal.NoSuchTemplateException;
038    import com.liferay.portlet.journal.model.JournalArticle;
039    import com.liferay.portlet.journal.model.JournalTemplate;
040    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
041    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
042    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
043    import com.liferay.portlet.layoutconfiguration.util.xml.PortletLogic;
044    
045    import javax.portlet.ActionRequest;
046    import javax.portlet.ActionResponse;
047    import javax.portlet.PortletConfig;
048    import javax.portlet.PortletRequest;
049    
050    /**
051     * @author Brian Wing Shun Chan
052     * @author Douglas Wong
053     */
054    public class ConfigurationActionImpl extends DefaultConfigurationAction {
055    
056            @Override
057            public void processAction(
058                            PortletConfig portletConfig, ActionRequest actionRequest,
059                            ActionResponse actionResponse)
060                    throws Exception {
061    
062                    String[] extensions = actionRequest.getParameterValues("extensions");
063    
064                    setPreference(actionRequest, "extensions", extensions);
065    
066                    super.processAction(portletConfig, actionRequest, actionResponse);
067    
068                    if (SessionErrors.isEmpty(actionRequest)) {
069                            updateContentSearch(actionRequest);
070    
071                            updateLayout(actionRequest);
072                    }
073            }
074    
075            protected String getArticleId(PortletRequest portletRequest) {
076                    String articleId = getParameter(portletRequest, "articleId");
077    
078                    return articleId.toUpperCase();
079            }
080    
081            protected String getRuntimePortletId(String xml) throws Exception {
082                    Document document = SAXReaderUtil.read(xml);
083    
084                    Element rootElement = document.getRootElement();
085    
086                    String instanceId = rootElement.attributeValue("instance");
087                    String portletId = rootElement.attributeValue("name");
088    
089                    if (Validator.isNotNull(instanceId)) {
090                            portletId += PortletConstants.INSTANCE_SEPARATOR + instanceId;
091                    }
092    
093                    return portletId;
094            }
095    
096            protected String getRuntimePortletIds(String content) throws Exception {
097                    StringBundler sb = new StringBundler();
098    
099                    for (int index = 0;;) {
100                            index = content.indexOf(PortletLogic.OPEN_TAG, index);
101    
102                            if (index == -1) {
103                                    break;
104                            }
105    
106                            int close1 = content.indexOf(PortletLogic.CLOSE_1_TAG, index);
107                            int close2 = content.indexOf(PortletLogic.CLOSE_2_TAG, index);
108    
109                            int closeIndex = -1;
110    
111                            if ((close2 == -1) || ((close1 != -1) && (close1 < close2))) {
112                                    closeIndex = close1 + PortletLogic.CLOSE_1_TAG.length();
113                            }
114                            else {
115                                    closeIndex = close2 + PortletLogic.CLOSE_2_TAG.length();
116                            }
117    
118                            if (closeIndex == -1) {
119                                    break;
120                            }
121    
122                            if (sb.length() > 0) {
123                                    sb.append(StringPool.COMMA);
124                            }
125    
126                            sb.append(
127                                    getRuntimePortletId(content.substring(index, closeIndex)));
128    
129                            index = closeIndex;
130                    }
131    
132                    if (sb.length() == 0) {
133                            return null;
134                    }
135    
136                    return sb.toString();
137            }
138    
139            protected String getRuntimePortletIds(
140                            ThemeDisplay themeDisplay, String articleId)
141                    throws Exception {
142    
143                    JournalArticle journalArticle = null;
144    
145                    Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
146                            themeDisplay.getCompanyId());
147    
148                    try {
149                            journalArticle = JournalArticleLocalServiceUtil.getDisplayArticle(
150                                    themeDisplay.getScopeGroupId(), articleId);
151                    }
152                    catch (NoSuchArticleException nsae) {
153                    }
154    
155                    if (journalArticle == null) {
156                            try {
157                                    journalArticle =
158                                            JournalArticleLocalServiceUtil.getDisplayArticle(
159                                                    companyGroup.getGroupId(), articleId);
160                            }
161                            catch (NoSuchArticleException nsae) {
162                                    return null;
163                            }
164                    }
165    
166                    String portletIds = getRuntimePortletIds(journalArticle.getContent());
167    
168                    if (Validator.isNotNull(journalArticle.getTemplateId())) {
169                            JournalTemplate journalTemplate = null;
170    
171                            try {
172                                    journalTemplate = JournalTemplateLocalServiceUtil.getTemplate(
173                                            themeDisplay.getScopeGroupId(),
174                                            journalArticle.getTemplateId());
175                            }
176                            catch (NoSuchTemplateException nste) {
177                                    journalTemplate = JournalTemplateLocalServiceUtil.getTemplate(
178                                            companyGroup.getGroupId(), journalArticle.getTemplateId());
179                            }
180    
181                            portletIds = StringUtil.add(
182                                    portletIds, getRuntimePortletIds(journalTemplate.getXsl()));
183                    }
184    
185                    return portletIds;
186            }
187    
188            protected void updateContentSearch(PortletRequest portletRequest)
189                    throws Exception {
190    
191                    String articleId = getArticleId(portletRequest);
192    
193                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
194                            WebKeys.THEME_DISPLAY);
195    
196                    Layout layout = themeDisplay.getLayout();
197    
198                    String portletResource = ParamUtil.getString(
199                            portletRequest, "portletResource");
200    
201                    JournalContentSearchLocalServiceUtil.updateContentSearch(
202                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
203                            portletResource, articleId, true);
204            }
205    
206            protected void updateLayout(PortletRequest portletRequest)
207                    throws Exception {
208    
209                    String articleId = getArticleId(portletRequest);
210    
211                    if (Validator.isNull(articleId)) {
212                            return;
213                    }
214    
215                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
216                            WebKeys.THEME_DISPLAY);
217    
218                    Layout layout = themeDisplay.getLayout();
219    
220                    LayoutTypePortlet layoutTypePortlet =
221                            (LayoutTypePortlet)layout.getLayoutType();
222    
223                    String portletResource = ParamUtil.getString(
224                            portletRequest, "portletResource");
225    
226                    layoutTypePortlet.setPortletIds(
227                            LayoutTypePortletConstants.RUNTIME_COLUMN_PREFIX +
228                                    portletResource,
229                            getRuntimePortletIds(themeDisplay, articleId));
230    
231                    LayoutLocalServiceUtil.updateLayout(
232                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
233                            layout.getTypeSettings());
234            }
235    
236    }