001
014
015 package com.liferay.portlet.dynamicdatamapping.lar;
016
017 import com.liferay.portal.kernel.dao.orm.QueryUtil;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.lar.BaseStagedModelDataHandler;
020 import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
021 import com.liferay.portal.kernel.lar.ExportImportPathUtil;
022 import com.liferay.portal.kernel.lar.PortletDataContext;
023 import com.liferay.portal.kernel.lar.PortletDataException;
024 import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
025 import com.liferay.portal.kernel.lar.StagedModelModifiedDateComparator;
026 import com.liferay.portal.kernel.util.FileUtil;
027 import com.liferay.portal.kernel.util.GetterUtil;
028 import com.liferay.portal.kernel.util.ListUtil;
029 import com.liferay.portal.kernel.util.MapUtil;
030 import com.liferay.portal.kernel.util.StringPool;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.kernel.xml.Element;
033 import com.liferay.portal.model.Group;
034 import com.liferay.portal.model.Image;
035 import com.liferay.portal.service.ImageLocalServiceUtil;
036 import com.liferay.portal.service.ServiceContext;
037 import com.liferay.portal.service.UserLocalServiceUtil;
038 import com.liferay.portal.util.PortalUtil;
039 import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
040 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
041 import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
042 import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil;
043
044 import java.io.File;
045
046 import java.util.HashMap;
047 import java.util.List;
048 import java.util.Map;
049
050
054 public class DDMTemplateStagedModelDataHandler
055 extends BaseStagedModelDataHandler<DDMTemplate> {
056
057 public static final String[] CLASS_NAMES = {DDMTemplate.class.getName()};
058
059 @Override
060 public void deleteStagedModel(
061 String uuid, long groupId, String className, String extraData)
062 throws PortalException {
063
064 DDMTemplate ddmTemplate = fetchStagedModelByUuidAndGroupId(
065 uuid, groupId);
066
067 if (ddmTemplate != null) {
068 DDMTemplateLocalServiceUtil.deleteTemplate(ddmTemplate);
069 }
070 }
071
072 @Override
073 public DDMTemplate fetchStagedModelByUuidAndCompanyId(
074 String uuid, long companyId) {
075
076 List<DDMTemplate> templates =
077 DDMTemplateLocalServiceUtil.getDDMTemplatesByUuidAndCompanyId(
078 uuid, companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
079 new StagedModelModifiedDateComparator<DDMTemplate>());
080
081 if (ListUtil.isEmpty(templates)) {
082 return null;
083 }
084
085 return templates.get(0);
086 }
087
088 @Override
089 public DDMTemplate fetchStagedModelByUuidAndGroupId(
090 String uuid, long groupId) {
091
092 return DDMTemplateLocalServiceUtil.fetchDDMTemplateByUuidAndGroupId(
093 uuid, groupId);
094 }
095
096 @Override
097 public String[] getClassNames() {
098 return CLASS_NAMES;
099 }
100
101 @Override
102 public String getDisplayName(DDMTemplate template) {
103 return template.getNameCurrentValue();
104 }
105
106 @Override
107 public Map<String, String> getReferenceAttributes(
108 PortletDataContext portletDataContext, DDMTemplate template) {
109
110 Map<String, String> referenceAttributes = new HashMap<String, String>();
111
112 referenceAttributes.put(
113 "referenced-class-name", template.getClassName());
114 referenceAttributes.put("template-key", template.getTemplateKey());
115
116 long defaultUserId = 0;
117
118 try {
119 defaultUserId = UserLocalServiceUtil.getDefaultUserId(
120 template.getCompanyId());
121 }
122 catch (Exception e) {
123 return referenceAttributes;
124 }
125
126 boolean preloaded = false;
127
128 if (defaultUserId == template.getUserId()) {
129 preloaded = true;
130 }
131
132 referenceAttributes.put("preloaded", String.valueOf(preloaded));
133
134 return referenceAttributes;
135 }
136
137 @Override
138 public void importMissingReference(
139 PortletDataContext portletDataContext, Element referenceElement)
140 throws PortletDataException {
141
142 importMissingGroupReference(portletDataContext, referenceElement);
143
144 String uuid = referenceElement.attributeValue("uuid");
145
146 Map<Long, Long> groupIds =
147 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
148 Group.class);
149
150 long liveGroupId = GetterUtil.getLong(
151 referenceElement.attributeValue("live-group-id"));
152
153 liveGroupId = MapUtil.getLong(groupIds, liveGroupId);
154
155 long classNameId = PortalUtil.getClassNameId(
156 referenceElement.attributeValue("referenced-class-name"));
157 String templateKey = referenceElement.attributeValue("template-key");
158 boolean preloaded = GetterUtil.getBoolean(
159 referenceElement.attributeValue("preloaded"));
160
161 DDMTemplate existingTemplate = null;
162
163 existingTemplate = fetchExistingTemplate(
164 uuid, liveGroupId, classNameId, templateKey, preloaded);
165
166 Map<Long, Long> templateIds =
167 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
168 DDMTemplate.class);
169
170 long templateId = GetterUtil.getLong(
171 referenceElement.attributeValue("class-pk"));
172
173 templateIds.put(templateId, existingTemplate.getTemplateId());
174
175 Map<String, String> templateKeys =
176 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
177 DDMTemplate.class + ".ddmTemplateKey");
178
179 templateKeys.put(templateKey, existingTemplate.getTemplateKey());
180 }
181
182 @Override
183 public boolean validateReference(
184 PortletDataContext portletDataContext, Element referenceElement) {
185
186 validateMissingGroupReference(portletDataContext, referenceElement);
187
188 String uuid = referenceElement.attributeValue("uuid");
189
190 Map<Long, Long> groupIds =
191 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
192 Group.class);
193
194 long liveGroupId = GetterUtil.getLong(
195 referenceElement.attributeValue("live-group-id"));
196
197 liveGroupId = MapUtil.getLong(groupIds, liveGroupId);
198
199 long classNameId = PortalUtil.getClassNameId(
200 referenceElement.attributeValue("referenced-class-name"));
201 String templateKey = referenceElement.attributeValue("template-key");
202 boolean preloaded = GetterUtil.getBoolean(
203 referenceElement.attributeValue("preloaded"));
204
205 DDMTemplate existingTemplate = fetchExistingTemplate(
206 uuid, liveGroupId, classNameId, templateKey, preloaded);
207
208 if (existingTemplate == null) {
209 return false;
210 }
211
212 return true;
213 }
214
215 @Override
216 protected void doExportStagedModel(
217 PortletDataContext portletDataContext, DDMTemplate template)
218 throws Exception {
219
220 Element templateElement = portletDataContext.getExportDataElement(
221 template);
222
223 DDMStructure structure = DDMStructureLocalServiceUtil.fetchStructure(
224 template.getClassPK());
225
226 if (structure != null) {
227 StagedModelDataHandlerUtil.exportReferenceStagedModel(
228 portletDataContext, template, structure,
229 PortletDataContext.REFERENCE_TYPE_STRONG);
230 }
231
232 if (template.isSmallImage()) {
233 Image smallImage = ImageLocalServiceUtil.fetchImage(
234 template.getSmallImageId());
235
236 if (Validator.isNotNull(template.getSmallImageURL())) {
237 String smallImageURL =
238 ExportImportHelperUtil.replaceExportContentReferences(
239 portletDataContext, template,
240 template.getSmallImageURL() + StringPool.SPACE, true);
241
242 template.setSmallImageURL(smallImageURL);
243 }
244 else if (smallImage != null) {
245 String smallImagePath = ExportImportPathUtil.getModelPath(
246 template,
247 smallImage.getImageId() + StringPool.PERIOD +
248 template.getSmallImageType());
249
250 templateElement.addAttribute(
251 "small-image-path", smallImagePath);
252
253 template.setSmallImageType(smallImage.getType());
254
255 portletDataContext.addZipEntry(
256 smallImagePath, smallImage.getTextObj());
257 }
258 }
259
260 String script = ExportImportHelperUtil.replaceExportContentReferences(
261 portletDataContext, template, template.getScript(),
262 portletDataContext.getBooleanParameter(
263 DDMPortletDataHandler.NAMESPACE, "referenced-content"));
264
265 template.setScript(script);
266
267 long defaultUserId = UserLocalServiceUtil.getDefaultUserId(
268 template.getCompanyId());
269
270 if (defaultUserId == template.getUserId()) {
271 templateElement.addAttribute("preloaded", "true");
272 }
273
274 portletDataContext.addClassedModel(
275 templateElement, ExportImportPathUtil.getModelPath(template),
276 template);
277 }
278
279 @Override
280 protected void doImportStagedModel(
281 PortletDataContext portletDataContext, DDMTemplate template)
282 throws Exception {
283
284 long userId = portletDataContext.getUserId(template.getUserUuid());
285
286 long classPK = template.getClassPK();
287
288 if (classPK > 0) {
289 Map<Long, Long> structureIds =
290 (Map<Long, Long>)portletDataContext.getNewPrimaryKeysMap(
291 DDMStructure.class);
292
293 classPK = MapUtil.getLong(structureIds, classPK, classPK);
294 }
295
296 File smallFile = null;
297
298 try {
299 if (template.isSmallImage()) {
300 Element element =
301 portletDataContext.getImportDataStagedModelElement(
302 template);
303
304 String smallImagePath = element.attributeValue(
305 "small-image-path");
306
307 if (Validator.isNotNull(template.getSmallImageURL())) {
308 String smallImageURL =
309 ExportImportHelperUtil.replaceImportContentReferences(
310 portletDataContext, template,
311 template.getSmallImageURL());
312
313 template.setSmallImageURL(smallImageURL);
314 }
315 else if (Validator.isNotNull(smallImagePath)) {
316 byte[] bytes = portletDataContext.getZipEntryAsByteArray(
317 smallImagePath);
318
319 if (bytes != null) {
320 smallFile = FileUtil.createTempFile(
321 template.getSmallImageType());
322
323 FileUtil.write(smallFile, bytes);
324 }
325 }
326 }
327
328 String script =
329 ExportImportHelperUtil.replaceImportContentReferences(
330 portletDataContext, template, template.getScript());
331
332 template.setScript(script);
333
334 ServiceContext serviceContext =
335 portletDataContext.createServiceContext(template);
336
337 DDMTemplate importedTemplate = null;
338
339 if (portletDataContext.isDataStrategyMirror()) {
340 Element element =
341 portletDataContext.getImportDataStagedModelElement(
342 template);
343
344 boolean preloaded = GetterUtil.getBoolean(
345 element.attributeValue("preloaded"));
346
347 DDMTemplate existingTemplate = fetchExistingTemplate(
348 template.getUuid(), portletDataContext.getScopeGroupId(),
349 template.getClassNameId(), template.getTemplateKey(),
350 preloaded);
351
352 if (existingTemplate == null) {
353 serviceContext.setUuid(template.getUuid());
354
355 importedTemplate = DDMTemplateLocalServiceUtil.addTemplate(
356 userId, portletDataContext.getScopeGroupId(),
357 template.getClassNameId(), classPK,
358 template.getTemplateKey(), template.getNameMap(),
359 template.getDescriptionMap(), template.getType(),
360 template.getMode(), template.getLanguage(),
361 template.getScript(), template.isCacheable(),
362 template.isSmallImage(), template.getSmallImageURL(),
363 smallFile, serviceContext);
364 }
365 else {
366 importedTemplate =
367 DDMTemplateLocalServiceUtil.updateTemplate(
368 existingTemplate.getTemplateId(),
369 template.getClassPK(), template.getNameMap(),
370 template.getDescriptionMap(), template.getType(),
371 template.getMode(), template.getLanguage(),
372 template.getScript(), template.isCacheable(),
373 template.isSmallImage(),
374 template.getSmallImageURL(), smallFile,
375 serviceContext);
376 }
377 }
378 else {
379 importedTemplate = DDMTemplateLocalServiceUtil.addTemplate(
380 userId, portletDataContext.getScopeGroupId(),
381 template.getClassNameId(), classPK, null,
382 template.getNameMap(), template.getDescriptionMap(),
383 template.getType(), template.getMode(),
384 template.getLanguage(), template.getScript(),
385 template.isCacheable(), template.isSmallImage(),
386 template.getSmallImageURL(), smallFile, serviceContext);
387 }
388
389 portletDataContext.importClassedModel(template, importedTemplate);
390
391 Map<String, String> ddmTemplateKeys =
392 (Map<String, String>)portletDataContext.getNewPrimaryKeysMap(
393 DDMTemplate.class + ".ddmTemplateKey");
394
395 ddmTemplateKeys.put(
396 template.getTemplateKey(), importedTemplate.getTemplateKey());
397 }
398 finally {
399 if (smallFile != null) {
400 smallFile.delete();
401 }
402 }
403 }
404
405 protected DDMTemplate fetchExistingTemplate(
406 String uuid, long groupId, long classNameId, String templateKey,
407 boolean preloaded) {
408
409 DDMTemplate existingTemplate = null;
410
411 if (!preloaded) {
412 existingTemplate = fetchStagedModelByUuidAndGroupId(uuid, groupId);
413 }
414 else {
415 existingTemplate = DDMTemplateLocalServiceUtil.fetchTemplate(
416 groupId, classNameId, templateKey);
417 }
418
419 return existingTemplate;
420 }
421
422 }