001
014
015 package com.liferay.portlet.journal.lar;
016
017 import com.liferay.portal.kernel.lar.DataLevel;
018 import com.liferay.portal.kernel.lar.PortletDataContext;
019 import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
020 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.MapUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.Validator;
027 import com.liferay.portal.kernel.workflow.WorkflowConstants;
028 import com.liferay.portal.kernel.xml.Element;
029 import com.liferay.portal.model.Layout;
030 import com.liferay.portal.service.LayoutLocalServiceUtil;
031 import com.liferay.portal.util.PortalUtil;
032 import com.liferay.portal.util.PropsValues;
033 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
034 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
035 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
036 import com.liferay.portlet.journal.model.JournalArticle;
037 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
038 import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
039 import com.liferay.portlet.journal.service.permission.JournalPermission;
040
041 import java.util.List;
042 import java.util.Map;
043
044 import javax.portlet.PortletPreferences;
045
046
073 public class JournalContentPortletDataHandler
074 extends JournalPortletDataHandler {
075
076 public JournalContentPortletDataHandler() {
077 setDataLevel(DataLevel.PORTLET_INSTANCE);
078 setDataPortletPreferences("articleId", "ddmTemplateKey", "groupId");
079 setExportControls(new PortletDataHandlerControl[0]);
080 setPublishToLiveByDefault(
081 PropsValues.JOURNAL_CONTENT_PUBLISH_TO_LIVE_BY_DEFAULT);
082 }
083
084 @Override
085 protected PortletPreferences doDeleteData(
086 PortletDataContext portletDataContext, String portletId,
087 PortletPreferences portletPreferences)
088 throws Exception {
089
090 if (portletPreferences == null) {
091 return portletPreferences;
092 }
093
094 portletPreferences.setValue("articleId", StringPool.BLANK);
095 portletPreferences.setValue("ddmTemplateKey", StringPool.BLANK);
096 portletPreferences.setValue("groupId", StringPool.BLANK);
097
098 return portletPreferences;
099 }
100
101 @Override
102 protected PortletPreferences doProcessExportPortletPreferences(
103 PortletDataContext portletDataContext, String portletId,
104 PortletPreferences portletPreferences)
105 throws Exception {
106
107 portletDataContext.addPortletPermissions(
108 JournalPermission.RESOURCE_NAME);
109
110 String articleId = portletPreferences.getValue("articleId", null);
111
112 if (articleId == null) {
113 if (_log.isDebugEnabled()) {
114 _log.debug(
115 "No article id found in preferences of portlet " +
116 portletId);
117 }
118
119 return portletPreferences;
120 }
121
122 long articleGroupId = GetterUtil.getLong(
123 portletPreferences.getValue("groupId", StringPool.BLANK));
124
125 if (articleGroupId <= 0) {
126 if (_log.isWarnEnabled()) {
127 _log.warn(
128 "No group id found in preferences of portlet " + portletId);
129 }
130
131 return portletPreferences;
132 }
133
134 long previousScopeGroupId = portletDataContext.getScopeGroupId();
135
136 if (articleGroupId != previousScopeGroupId) {
137 portletDataContext.setScopeGroupId(articleGroupId);
138 }
139
140 JournalArticle article = null;
141
142 article = JournalArticleLocalServiceUtil.fetchLatestArticle(
143 articleGroupId, articleId, WorkflowConstants.STATUS_APPROVED);
144
145 if (article == null) {
146 article = JournalArticleLocalServiceUtil.fetchLatestArticle(
147 articleGroupId, articleId, WorkflowConstants.STATUS_EXPIRED);
148 }
149
150 if (article == null) {
151 portletDataContext.setScopeGroupId(previousScopeGroupId);
152
153 return portletPreferences;
154 }
155
156 StagedModelDataHandlerUtil.exportReferenceStagedModel(
157 portletDataContext, portletId, article);
158
159 String defaultTemplateId = article.getTemplateId();
160 String preferenceTemplateId = portletPreferences.getValue(
161 "ddmTemplateKey", null);
162
163 if (Validator.isNotNull(defaultTemplateId) &&
164 Validator.isNotNull(preferenceTemplateId) &&
165 !defaultTemplateId.equals(preferenceTemplateId)) {
166
167 DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(
168 article.getGroupId(),
169 PortalUtil.getClassNameId(DDMStructure.class),
170 preferenceTemplateId, true);
171
172 StagedModelDataHandlerUtil.exportReferenceStagedModel(
173 portletDataContext, article, ddmTemplate,
174 PortletDataContext.REFERENCE_TYPE_STRONG);
175 }
176
177 portletDataContext.setScopeGroupId(previousScopeGroupId);
178
179 return portletPreferences;
180 }
181
182 @Override
183 protected PortletPreferences doProcessImportPortletPreferences(
184 PortletDataContext portletDataContext, String portletId,
185 PortletPreferences portletPreferences)
186 throws Exception {
187
188 portletDataContext.importPortletPermissions(
189 JournalPermission.RESOURCE_NAME);
190
191 long previousScopeGroupId = portletDataContext.getScopeGroupId();
192
193 long importGroupId = GetterUtil.getLong(
194 portletPreferences.getValue("groupId", null));
195
196 if (importGroupId == portletDataContext.getSourceGroupId()) {
197 portletDataContext.setScopeGroupId(portletDataContext.getGroupId());
198 }
199
200 Element ddmStructuresElement =
201 portletDataContext.getImportDataGroupElement(DDMStructure.class);
202
203 List<Element> ddmStructureElements = ddmStructuresElement.elements();
204
205 for (Element ddmStructureElement : ddmStructureElements) {
206 StagedModelDataHandlerUtil.importReferenceStagedModel(
207 portletDataContext, ddmStructureElement);
208 }
209
210 Element ddmTemplatesElement =
211 portletDataContext.getImportDataGroupElement(DDMTemplate.class);
212
213 List<Element> ddmTemplateElements = ddmTemplatesElement.elements();
214
215 for (Element ddmTemplateElement : ddmTemplateElements) {
216 StagedModelDataHandlerUtil.importReferenceStagedModel(
217 portletDataContext, ddmTemplateElement);
218 }
219
220 Element articlesElement = portletDataContext.getImportDataGroupElement(
221 JournalArticle.class);
222
223 List<Element> articleElements = articlesElement.elements();
224
225 if (!articleElements.isEmpty()) {
226 StagedModelDataHandlerUtil.importReferenceStagedModel(
227 portletDataContext, articleElements.get(0));
228 }
229
230 String articleId = portletPreferences.getValue("articleId", null);
231
232 if (Validator.isNotNull(articleId)) {
233 Map<String, String> articleIds =
234 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
235 JournalArticle.class + ".articleId");
236
237 articleId = MapUtil.getString(articleIds, articleId, articleId);
238
239 portletPreferences.setValue("articleId", articleId);
240
241 String importedArticleGroupId = String.valueOf(
242 portletDataContext.getScopeGroupId());
243
244 portletPreferences.setValue("groupId", importedArticleGroupId);
245
246 Layout layout = LayoutLocalServiceUtil.getLayout(
247 portletDataContext.getPlid());
248
249 JournalContentSearchLocalServiceUtil.updateContentSearch(
250 portletDataContext.getScopeGroupId(), layout.isPrivateLayout(),
251 layout.getLayoutId(), portletId, articleId, true);
252 }
253 else {
254 portletPreferences.setValue("groupId", StringPool.BLANK);
255 portletPreferences.setValue("articleId", StringPool.BLANK);
256 }
257
258 String ddmTemplateKey = portletPreferences.getValue(
259 "ddmTemplateKey", null);
260
261 if (Validator.isNotNull(ddmTemplateKey)) {
262 Map<String, String> ddmTemplateKeys =
263 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
264 DDMTemplate.class + ".ddmTemplateKey");
265
266 ddmTemplateKey = MapUtil.getString(
267 ddmTemplateKeys, ddmTemplateKey, ddmTemplateKey);
268
269 portletPreferences.setValue("ddmTemplateKey", ddmTemplateKey);
270 }
271 else {
272 portletPreferences.setValue("ddmTemplateKey", StringPool.BLANK);
273 }
274
275 portletDataContext.setScopeGroupId(previousScopeGroupId);
276
277 return portletPreferences;
278 }
279
280 private static Log _log = LogFactoryUtil.getLog(
281 JournalContentPortletDataHandler.class);
282
283 }