001
014
015 package com.liferay.portlet.mobiledevicerules.lar;
016
017 import com.liferay.portal.kernel.dao.orm.QueryUtil;
018 import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
019 import com.liferay.portal.kernel.lar.ExportImportPathUtil;
020 import com.liferay.portal.kernel.lar.PortletDataContext;
021 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
022 import com.liferay.portal.kernel.lar.StagedModelModifiedDateComparator;
023 import com.liferay.portal.kernel.log.Log;
024 import com.liferay.portal.kernel.log.LogFactoryUtil;
025 import com.liferay.portal.kernel.util.GetterUtil;
026 import com.liferay.portal.kernel.util.ListUtil;
027 import com.liferay.portal.kernel.util.MapUtil;
028 import com.liferay.portal.kernel.util.StringBundler;
029 import com.liferay.portal.kernel.util.UnicodeProperties;
030 import com.liferay.portal.kernel.util.Validator;
031 import com.liferay.portal.kernel.xml.Element;
032 import com.liferay.portal.mobile.device.rulegroup.action.impl.SiteRedirectActionHandler;
033 import com.liferay.portal.model.Layout;
034 import com.liferay.portal.service.LayoutLocalServiceUtil;
035 import com.liferay.portal.service.ServiceContext;
036 import com.liferay.portlet.mobiledevicerules.model.MDRAction;
037 import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroupInstance;
038 import com.liferay.portlet.mobiledevicerules.service.MDRActionLocalServiceUtil;
039 import com.liferay.portlet.mobiledevicerules.service.MDRRuleGroupInstanceLocalServiceUtil;
040
041 import java.util.List;
042 import java.util.Map;
043
044
047 public class MDRActionStagedModelDataHandler
048 extends BaseStagedModelDataHandler<MDRAction> {
049
050 public static final String[] CLASS_NAMES = {MDRAction.class.getName()};
051
052 @Override
053 public void deleteStagedModel(
054 String uuid, long groupId, String className, String extraData) {
055
056 MDRAction action = fetchStagedModelByUuidAndGroupId(uuid, groupId);
057
058 if (action != null) {
059 MDRActionLocalServiceUtil.deleteAction(action);
060 }
061 }
062
063 @Override
064 public MDRAction fetchStagedModelByUuidAndCompanyId(
065 String uuid, long companyId) {
066
067 List<MDRAction> actions =
068 MDRActionLocalServiceUtil.getMDRActionsByUuidAndCompanyId(
069 uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
070 new StagedModelModifiedDateComparator<MDRAction>());
071
072 if (ListUtil.isEmpty(actions)) {
073 return null;
074 }
075
076 return actions.get(0);
077 }
078
079 @Override
080 public MDRAction fetchStagedModelByUuidAndGroupId(
081 String uuid, long groupId) {
082
083 return MDRActionLocalServiceUtil.fetchMDRActionByUuidAndGroupId(
084 uuid, groupId);
085 }
086
087 @Override
088 public String[] getClassNames() {
089 return CLASS_NAMES;
090 }
091
092 @Override
093 public String getDisplayName(MDRAction action) {
094 return action.getNameCurrentValue();
095 }
096
097 @Override
098 protected void doExportStagedModel(
099 PortletDataContext portletDataContext, MDRAction action)
100 throws Exception {
101
102 MDRRuleGroupInstance ruleGroupInstance =
103 MDRRuleGroupInstanceLocalServiceUtil.getRuleGroupInstance(
104 action.getRuleGroupInstanceId());
105
106 StagedModelDataHandlerUtil.exportReferenceStagedModel(
107 portletDataContext, action, ruleGroupInstance,
108 PortletDataContext.REFERENCE_TYPE_PARENT);
109
110 Element actionElement = portletDataContext.getExportDataElement(action);
111
112 String type = action.getType();
113
114 if (type.equals(SiteRedirectActionHandler.class.getName())) {
115 UnicodeProperties typeSettingsProperties =
116 action.getTypeSettingsProperties();
117
118 long plid = GetterUtil.getLong(
119 typeSettingsProperties.getProperty("plid"));
120
121 try {
122 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
123
124 actionElement.addAttribute("layout-uuid", layout.getUuid());
125 }
126 catch (Exception e) {
127 if (_log.isWarnEnabled()) {
128 _log.warn(
129 "Unable to set the layout uuid of layout " + plid +
130 ". Site redirect may not match after import.",
131 e);
132 }
133 }
134 }
135
136 portletDataContext.addClassedModel(
137 actionElement, ExportImportPathUtil.getModelPath(action), action);
138 }
139
140 @Override
141 protected void doImportStagedModel(
142 PortletDataContext portletDataContext, MDRAction action)
143 throws Exception {
144
145 Map<Long, Long> ruleGroupInstanceIds =
146 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
147 MDRRuleGroupInstance.class);
148
149 long ruleGroupInstanceId = MapUtil.getLong(
150 ruleGroupInstanceIds, action.getRuleGroupInstanceId(),
151 action.getRuleGroupInstanceId());
152
153 ServiceContext serviceContext = portletDataContext.createServiceContext(
154 action);
155
156 serviceContext.setUserId(
157 portletDataContext.getUserId(action.getUserUuid()));
158
159 Element element = portletDataContext.getImportDataStagedModelElement(
160 action);
161
162 validateLayout(element, action);
163
164 MDRAction importedAction = null;
165
166 if (portletDataContext.isDataStrategyMirror()) {
167 MDRAction existingAction = fetchStagedModelByUuidAndGroupId(
168 action.getUuid(), portletDataContext.getScopeGroupId());
169
170 if (existingAction == null) {
171 serviceContext.setUuid(action.getUuid());
172
173 importedAction = MDRActionLocalServiceUtil.addAction(
174 ruleGroupInstanceId, action.getNameMap(),
175 action.getDescriptionMap(), action.getType(),
176 action.getTypeSettingsProperties(), serviceContext);
177 }
178 else {
179 importedAction = MDRActionLocalServiceUtil.updateAction(
180 existingAction.getActionId(), action.getNameMap(),
181 action.getDescriptionMap(), action.getType(),
182 action.getTypeSettingsProperties(), serviceContext);
183 }
184 }
185 else {
186 importedAction = MDRActionLocalServiceUtil.addAction(
187 ruleGroupInstanceId, action.getNameMap(),
188 action.getDescriptionMap(), action.getType(),
189 action.getTypeSettingsProperties(), serviceContext);
190 }
191
192 portletDataContext.importClassedModel(action, importedAction);
193 }
194
195 protected void validateLayout(Element actionElement, MDRAction action) {
196 String type = action.getType();
197
198 if (!type.equals(SiteRedirectActionHandler.class.getName())) {
199 return;
200 }
201
202 String layoutUuid = actionElement.attributeValue("layout-uuid");
203
204 if (Validator.isNull(layoutUuid)) {
205 return;
206 }
207
208 UnicodeProperties typeSettingsProperties =
209 action.getTypeSettingsProperties();
210
211 long groupId = GetterUtil.getLong(
212 typeSettingsProperties.getProperty("groupId"));
213 boolean privateLayout = GetterUtil.getBoolean(
214 actionElement.attributeValue("private-layout"));
215
216 try {
217 Layout layout = LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
218 layoutUuid, groupId, privateLayout);
219
220 typeSettingsProperties.setProperty(
221 "plid", String.valueOf(layout.getPlid()));
222 }
223 catch (Exception e) {
224 if (_log.isWarnEnabled()) {
225 StringBundler sb = new StringBundler(5);
226
227 sb.append("Unable to find layout with uuid ");
228 sb.append(layoutUuid);
229 sb.append(" in group ");
230 sb.append(groupId);
231 sb.append(". Site redirect may not match the target layout.");
232
233 _log.warn(sb.toString(), e);
234 }
235 }
236 }
237
238 private static final Log _log = LogFactoryUtil.getLog(
239 MDRActionStagedModelDataHandler.class);
240
241 }