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