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