001    /**
002     * Copyright (c) 2000-2012 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.CSVUtil;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.OrderByComparator;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
023    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
024    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
025    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetServiceUtil;
026    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
027    import com.liferay.portlet.dynamicdatamapping.storage.Field;
028    import com.liferay.portlet.dynamicdatamapping.storage.FieldConstants;
029    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
030    
031    import java.io.Serializable;
032    
033    import java.util.List;
034    import java.util.Map;
035    
036    /**
037     * @author Marcellus Tavares
038     */
039    public class DDLCSVExporter extends BaseDDLExporter {
040    
041            @Override
042            protected byte[] doExport(
043                            long recordSetId, int status, int start, int end,
044                            OrderByComparator orderByComparator)
045                    throws Exception {
046    
047                    DDLRecordSet recordSet = DDLRecordSetServiceUtil.getRecordSet(
048                            recordSetId);
049    
050                    DDMStructure ddmStructure = recordSet.getDDMStructure();
051    
052                    Map<String, Map<String, String>> fieldsMap =
053                            ddmStructure.getFieldsMap();
054    
055                    StringBundler sb = new StringBundler();
056    
057                    for (Map<String, String> fieldMap : fieldsMap.values()) {
058                            String label = fieldMap.get(FieldConstants.LABEL);
059    
060                            sb.append(label);
061                            sb.append(CharPool.COMMA);
062                    }
063    
064                    sb.setIndex(sb.index() - 1);
065                    sb.append(StringPool.NEW_LINE);
066    
067                    List<DDLRecord> records = DDLRecordLocalServiceUtil.getRecords(
068                            recordSetId, status, start, end, orderByComparator);
069    
070                    for (DDLRecord record : records) {
071                            Fields fields = record.getFields();
072    
073                            for (Map<String, String> fieldMap : fieldsMap.values()) {
074                                    String name = fieldMap.get(FieldConstants.NAME);
075    
076                                    Field field = fields.get(name);
077    
078                                    Serializable value = field.getValue();
079    
080                                    sb.append(CSVUtil.encode(value));
081                                    sb.append(CharPool.COMMA);
082                            }
083    
084                            sb.setIndex(sb.index() - 1);
085                            sb.append(StringPool.NEW_LINE);
086                    }
087    
088                    String csv = sb.toString();
089    
090                    return csv.getBytes();
091            }
092    
093    }