001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatalists.util;
016    
017    import com.liferay.portal.kernel.util.OrderByComparator;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.xml.Document;
020    import com.liferay.portal.kernel.xml.Element;
021    import com.liferay.portal.kernel.xml.SAXReaderUtil;
022    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
023    import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
024    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
025    import com.liferay.portlet.dynamicdatamapping.model.DDMFormField;
026    import com.liferay.portlet.dynamicdatamapping.model.LocalizedValue;
027    import com.liferay.portlet.dynamicdatamapping.storage.Field;
028    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
029    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
030    
031    import java.io.Serializable;
032    
033    import java.util.List;
034    
035    /**
036     * @author Marcellus Tavares
037     * @author Manuel de la Pe??a
038     */
039    public class DDLXMLExporter extends BaseDDLExporter {
040    
041            protected void addFieldElement(
042                    Element fieldsElement, String label, Serializable value, int status) {
043    
044                    Element fieldElement = fieldsElement.addElement("field");
045    
046                    Element labelElement = fieldElement.addElement("label");
047    
048                    labelElement.addText(label);
049    
050                    Element valueElement = fieldElement.addElement("value");
051    
052                    valueElement.addText(String.valueOf(value));
053    
054                    Element statusElement = fieldElement.addElement("status");
055    
056                    statusElement.addText(getStatusMessage(status));
057            }
058    
059            @Override
060            protected byte[] doExport(
061                            long recordSetId, int status, int start, int end,
062                            OrderByComparator<DDLRecord> orderByComparator)
063                    throws Exception {
064    
065                    List<DDMFormField> ddmFormFields = getDDMFormFields(recordSetId);
066    
067                    Document document = SAXReaderUtil.createDocument();
068    
069                    Element rootElement = document.addElement("root");
070    
071                    List<DDLRecord> records = DDLRecordLocalServiceUtil.getRecords(
072                            recordSetId, status, start, end, orderByComparator);
073    
074                    for (DDLRecord record : records) {
075                            Element fieldsElement = rootElement.addElement("fields");
076    
077                            DDLRecordVersion recordVersion = record.getRecordVersion();
078    
079                            Fields fields = StorageEngineUtil.getFields(
080                                    recordVersion.getDDMStorageId());
081    
082                            for (DDMFormField ddmFormField : ddmFormFields) {
083                                    LocalizedValue label = ddmFormField.getLabel();
084    
085                                    String name = ddmFormField.getName();
086    
087                                    String value = StringPool.BLANK;
088    
089                                    if (fields.contains(name)) {
090                                            Field field = fields.get(name);
091    
092                                            value = field.getRenderedValue(getLocale());
093                                    }
094    
095                                    addFieldElement(
096                                            fieldsElement, label.getString(getLocale()), value,
097                                            recordVersion.getStatus());
098                            }
099                    }
100    
101                    String xml = document.asXML();
102    
103                    return xml.getBytes();
104            }
105    
106    }