001
014
015 package com.liferay.portlet.test.lar;
016
017 import com.liferay.portal.kernel.lar.BasePortletDataHandler;
018 import com.liferay.portal.kernel.lar.DataLevel;
019 import com.liferay.portal.kernel.lar.PortletDataContext;
020 import com.liferay.portal.kernel.lar.PortletDataException;
021 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
022 import com.liferay.portal.kernel.log.Log;
023 import com.liferay.portal.kernel.log.LogFactoryUtil;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.MapUtil;
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.Group;
030 import com.liferay.portal.model.Layout;
031 import com.liferay.portal.service.LayoutLocalServiceUtil;
032 import com.liferay.portlet.journal.model.JournalArticle;
033 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
034 import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
035
036 import java.util.Map;
037
038 import javax.portlet.PortletPreferences;
039 import javax.portlet.ReadOnlyException;
040
041
044 public class TestPortletDataHandler extends BasePortletDataHandler {
045
046 public TestPortletDataHandler() {
047 setDataLevel(DataLevel.PORTLET_INSTANCE);
048 setDataPortletPreferences("articleId", "groupId");
049 setPublishToLiveByDefault(true);
050 }
051
052 @Override
053 protected PortletPreferences doDeleteData(
054 PortletDataContext portletDataContext, String portletId,
055 PortletPreferences portletPreferences)
056 throws ReadOnlyException {
057
058 if (portletPreferences == null) {
059 return portletPreferences;
060 }
061
062 portletPreferences.setValue("articleId", StringPool.BLANK);
063 portletPreferences.setValue("groupId", StringPool.BLANK);
064
065 return portletPreferences;
066 }
067
068 @Override
069 protected PortletPreferences doProcessExportPortletPreferences(
070 PortletDataContext portletDataContext, String portletId,
071 PortletPreferences portletPreferences)
072 throws PortletDataException {
073
074 String articleId = portletPreferences.getValue("articleId", null);
075
076 if (articleId == null) {
077 if (_log.isDebugEnabled()) {
078 _log.debug(
079 "No article ID found in preferences of portlet " +
080 portletId);
081 }
082
083 return portletPreferences;
084 }
085
086 long articleGroupId = GetterUtil.getLong(
087 portletPreferences.getValue("groupId", StringPool.BLANK));
088
089 if (articleGroupId <= 0) {
090 if (_log.isWarnEnabled()) {
091 _log.warn(
092 "No group ID found in preferences of portlet " + portletId);
093 }
094
095 return portletPreferences;
096 }
097
098 long previousScopeGroupId = portletDataContext.getScopeGroupId();
099
100 if (articleGroupId != previousScopeGroupId) {
101 portletDataContext.setScopeGroupId(articleGroupId);
102 }
103
104 JournalArticle journalArticle =
105 JournalArticleLocalServiceUtil.fetchLatestArticle(
106 articleGroupId, articleId, WorkflowConstants.STATUS_APPROVED);
107
108 if (journalArticle == null) {
109 journalArticle = JournalArticleLocalServiceUtil.fetchLatestArticle(
110 articleGroupId, articleId, WorkflowConstants.STATUS_EXPIRED);
111 }
112
113 if (journalArticle == null) {
114 if (_log.isWarnEnabled()) {
115 _log.warn(
116 "Portlet " + portletId +
117 " refers to an invalid article ID " + articleId);
118 }
119
120 portletDataContext.setScopeGroupId(previousScopeGroupId);
121
122 return portletPreferences;
123 }
124
125 StagedModelDataHandlerUtil.exportReferenceStagedModel(
126 portletDataContext, portletId, journalArticle);
127
128 portletDataContext.setScopeGroupId(previousScopeGroupId);
129
130 return portletPreferences;
131 }
132
133 @Override
134 protected PortletPreferences doProcessImportPortletPreferences(
135 PortletDataContext portletDataContext, String portletId,
136 PortletPreferences portletPreferences)
137 throws Exception {
138
139 long previousScopeGroupId = portletDataContext.getScopeGroupId();
140
141 Map<Long, Long> groupIds =
142 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
143 Group.class);
144
145 long importGroupId = GetterUtil.getLong(
146 portletPreferences.getValue("groupId", null));
147
148 long groupId = MapUtil.getLong(groupIds, importGroupId, importGroupId);
149
150 portletDataContext.setScopeGroupId(groupId);
151
152 StagedModelDataHandlerUtil.importReferenceStagedModels(
153 portletDataContext, JournalArticle.class);
154
155 String articleId = portletPreferences.getValue("articleId", null);
156
157 if (Validator.isNotNull(articleId)) {
158 Map<String, String> articleIds =
159 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
160 JournalArticle.class + ".articleId");
161
162 articleId = MapUtil.getString(articleIds, articleId, articleId);
163
164 portletPreferences.setValue("articleId", articleId);
165
166 portletPreferences.setValue("groupId", String.valueOf(groupId));
167
168 Layout layout = LayoutLocalServiceUtil.getLayout(
169 portletDataContext.getPlid());
170
171 JournalContentSearchLocalServiceUtil.updateContentSearch(
172 layout.getGroupId(), layout.isPrivateLayout(),
173 layout.getLayoutId(), portletId, articleId, true);
174 }
175 else {
176 portletPreferences.setValue("groupId", StringPool.BLANK);
177 portletPreferences.setValue("articleId", StringPool.BLANK);
178 }
179
180 portletDataContext.setScopeGroupId(previousScopeGroupId);
181
182 return portletPreferences;
183 }
184
185 private static final Log _log = LogFactoryUtil.getLog(
186 TestPortletDataHandler.class);
187
188 }