/modules/apps/forms-and-workflow/dynamic-data-lists/dynamic-data-lists-service/src/main/java/com/liferay/dynamic/data/lists/exporter/impl/DDLXMLExporter.java

http://github.com/liferay/liferay-portal · Java · 159 lines · 100 code · 42 blank · 17 comment · 3 complexity · 1b454d3f4d02f214b02a2691d32a2b79 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.dynamic.data.lists.exporter.impl;
  15. import com.liferay.dynamic.data.lists.exporter.DDLExporter;
  16. import com.liferay.dynamic.data.lists.model.DDLRecord;
  17. import com.liferay.dynamic.data.lists.model.DDLRecordSet;
  18. import com.liferay.dynamic.data.lists.model.DDLRecordVersion;
  19. import com.liferay.dynamic.data.lists.service.DDLRecordLocalService;
  20. import com.liferay.dynamic.data.lists.service.DDLRecordSetService;
  21. import com.liferay.dynamic.data.mapping.model.DDMFormField;
  22. import com.liferay.dynamic.data.mapping.model.DDMStructure;
  23. import com.liferay.dynamic.data.mapping.model.LocalizedValue;
  24. import com.liferay.dynamic.data.mapping.storage.DDMFormValues;
  25. import com.liferay.dynamic.data.mapping.storage.Field;
  26. import com.liferay.dynamic.data.mapping.storage.Fields;
  27. import com.liferay.dynamic.data.mapping.storage.StorageEngine;
  28. import com.liferay.dynamic.data.mapping.util.DDMFormValuesToFieldsConverter;
  29. import com.liferay.portal.kernel.language.LanguageUtil;
  30. import com.liferay.portal.kernel.util.OrderByComparator;
  31. import com.liferay.portal.kernel.util.StringPool;
  32. import com.liferay.portal.kernel.xml.Document;
  33. import com.liferay.portal.kernel.xml.Element;
  34. import com.liferay.portal.kernel.xml.SAXReaderUtil;
  35. import java.io.Serializable;
  36. import java.util.List;
  37. import org.osgi.service.component.annotations.Component;
  38. import org.osgi.service.component.annotations.Reference;
  39. /**
  40. * @author Marcellus Tavares
  41. * @author Manuel de la PeĂąa
  42. */
  43. @Component(immediate = true, service = DDLExporter.class)
  44. public class DDLXMLExporter extends BaseDDLExporter {
  45. @Override
  46. public String getFormat() {
  47. return "xml";
  48. }
  49. protected void addFieldElement(
  50. Element fieldsElement, String label, Serializable value) {
  51. Element fieldElement = fieldsElement.addElement("field");
  52. Element labelElement = fieldElement.addElement("label");
  53. labelElement.addText(label);
  54. Element valueElement = fieldElement.addElement("value");
  55. valueElement.addText(String.valueOf(value));
  56. }
  57. @Override
  58. protected byte[] doExport(
  59. long recordSetId, int status, int start, int end,
  60. OrderByComparator<DDLRecord> orderByComparator)
  61. throws Exception {
  62. DDLRecordSet recordSet = _ddlRecordSetService.getRecordSet(recordSetId);
  63. DDMStructure ddmStructure = recordSet.getDDMStructure();
  64. List<DDMFormField> ddmFormFields = getDDMFormFields(ddmStructure);
  65. Document document = SAXReaderUtil.createDocument();
  66. Element rootElement = document.addElement("root");
  67. List<DDLRecord> records = _ddlRecordLocalService.getRecords(
  68. recordSetId, status, start, end, orderByComparator);
  69. for (DDLRecord record : records) {
  70. Element fieldsElement = rootElement.addElement("fields");
  71. DDLRecordVersion recordVersion = record.getRecordVersion();
  72. DDMFormValues ddmFormValues = _storageEngine.getDDMFormValues(
  73. recordVersion.getDDMStorageId());
  74. Fields fields = _ddmFormValuesToFieldsConverter.convert(
  75. ddmStructure, ddmFormValues);
  76. for (DDMFormField ddmFormField : ddmFormFields) {
  77. LocalizedValue label = ddmFormField.getLabel();
  78. String name = ddmFormField.getName();
  79. String value = StringPool.BLANK;
  80. if (fields.contains(name)) {
  81. Field field = fields.get(name);
  82. value = field.getRenderedValue(getLocale());
  83. }
  84. addFieldElement(
  85. fieldsElement, label.getString(getLocale()), value);
  86. }
  87. addFieldElement(
  88. fieldsElement, LanguageUtil.get(getLocale(), "status"),
  89. getStatusMessage(recordVersion.getStatus()));
  90. }
  91. String xml = document.asXML();
  92. return xml.getBytes();
  93. }
  94. @Reference(unbind = "-")
  95. protected void setDDLRecordLocalService(
  96. DDLRecordLocalService ddlRecordLocalService) {
  97. _ddlRecordLocalService = ddlRecordLocalService;
  98. }
  99. @Reference(unbind = "-")
  100. protected void setDDLRecordSetService(
  101. DDLRecordSetService ddlRecordSetService) {
  102. _ddlRecordSetService = ddlRecordSetService;
  103. }
  104. @Reference(unbind = "-")
  105. protected void setDDMFormValuesToFieldsConverter(
  106. DDMFormValuesToFieldsConverter ddmFormValuesToFieldsConverter) {
  107. _ddmFormValuesToFieldsConverter = ddmFormValuesToFieldsConverter;
  108. }
  109. @Reference(unbind = "-")
  110. protected void setStorageEngine(StorageEngine storageEngine) {
  111. _storageEngine = storageEngine;
  112. }
  113. private DDLRecordLocalService _ddlRecordLocalService;
  114. private DDLRecordSetService _ddlRecordSetService;
  115. private DDMFormValuesToFieldsConverter _ddmFormValuesToFieldsConverter;
  116. private StorageEngine _storageEngine;
  117. }