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.CSVUtil;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.OrderByComparator;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
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.util.List;
033    
034    /**
035     * @author Marcellus Tavares
036     * @author Manuel de la Pe??a
037     */
038    public class DDLCSVExporter extends BaseDDLExporter {
039    
040            @Override
041            protected byte[] doExport(
042                            long recordSetId, int status, int start, int end,
043                            OrderByComparator<DDLRecord> orderByComparator)
044                    throws Exception {
045    
046                    StringBundler sb = new StringBundler();
047    
048                    List<DDMFormField> ddmFormFields = getDDMFormFields(recordSetId);
049    
050                    for (DDMFormField ddmFormField : ddmFormFields) {
051                            LocalizedValue label = ddmFormField.getLabel();
052    
053                            sb.append(label.getString(getLocale()));
054                            sb.append(CharPool.COMMA);
055                    }
056    
057                    sb.append(LanguageUtil.get(getLocale(), "status"));
058                    sb.append(StringPool.NEW_LINE);
059    
060                    List<DDLRecord> records = DDLRecordLocalServiceUtil.getRecords(
061                            recordSetId, status, start, end, orderByComparator);
062    
063                    for (DDLRecord record : records) {
064                            DDLRecordVersion recordVersion = record.getRecordVersion();
065    
066                            Fields fields = StorageEngineUtil.getFields(
067                                    recordVersion.getDDMStorageId());
068    
069                            for (DDMFormField ddmFormField : ddmFormFields) {
070                                    String name = ddmFormField.getName();
071                                    String value = StringPool.BLANK;
072    
073                                    if (fields.contains(name)) {
074                                            Field field = fields.get(name);
075    
076                                            value = field.getRenderedValue(getLocale());
077                                    }
078    
079                                    sb.append(CSVUtil.encode(value));
080                                    sb.append(CharPool.COMMA);
081                            }
082    
083                            sb.append(getStatusMessage(recordVersion.getStatus()));
084                            sb.append(StringPool.NEW_LINE);
085                    }
086    
087                    String csv = sb.toString();
088    
089                    return csv.getBytes();
090            }
091    
092    }