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.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.OrderByComparator;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.xml.Document;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.kernel.xml.SAXReaderUtil;
023    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
024    import com.liferay.portlet.dynamicdatalists.model.DDLRecordVersion;
025    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
026    import com.liferay.portlet.dynamicdatamapping.model.DDMFormField;
027    import com.liferay.portlet.dynamicdatamapping.model.LocalizedValue;
028    import com.liferay.portlet.dynamicdatamapping.storage.Field;
029    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
030    import com.liferay.portlet.dynamicdatamapping.storage.StorageEngineUtil;
031    
032    import java.io.Serializable;
033    
034    import java.util.List;
035    
036    /**
037     * @author Marcellus Tavares
038     * @author Manuel de la Pe??a
039     */
040    public class DDLXMLExporter extends BaseDDLExporter {
041    
042            protected void addFieldElement(
043                    Element fieldsElement, String label, Serializable value) {
044    
045                    Element fieldElement = fieldsElement.addElement("field");
046    
047                    Element labelElement = fieldElement.addElement("label");
048    
049                    labelElement.addText(label);
050    
051                    Element valueElement = fieldElement.addElement("value");
052    
053                    valueElement.addText(String.valueOf(value));
054            }
055    
056            @Override
057            protected byte[] doExport(
058                            long recordSetId, int status, int start, int end,
059                            OrderByComparator<DDLRecord> orderByComparator)
060                    throws Exception {
061    
062                    List<DDMFormField> ddmFormFields = getDDMFormFields(recordSetId);
063    
064                    Document document = SAXReaderUtil.createDocument();
065    
066                    Element rootElement = document.addElement("root");
067    
068                    List<DDLRecord> records = DDLRecordLocalServiceUtil.getRecords(
069                            recordSetId, status, start, end, orderByComparator);
070    
071                    for (DDLRecord record : records) {
072                            Element fieldsElement = rootElement.addElement("fields");
073    
074                            DDLRecordVersion recordVersion = record.getRecordVersion();
075    
076                            Fields fields = StorageEngineUtil.getFields(
077                                    recordVersion.getDDMStorageId());
078    
079                            for (DDMFormField ddmFormField : ddmFormFields) {
080                                    LocalizedValue label = ddmFormField.getLabel();
081    
082                                    String name = ddmFormField.getName();
083    
084                                    String value = StringPool.BLANK;
085    
086                                    if (fields.contains(name)) {
087                                            Field field = fields.get(name);
088    
089                                            value = field.getRenderedValue(getLocale());
090                                    }
091    
092                                    addFieldElement(
093                                            fieldsElement, label.getString(getLocale()), value);
094                            }
095    
096                            addFieldElement(
097                                    fieldsElement, LanguageUtil.get(getLocale(), "status"),
098                                    getStatusMessage(recordVersion.getStatus()));
099                    }
100    
101                    String xml = document.asXML();
102    
103                    return xml.getBytes();
104            }
105    
106    }