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.Iterator;
033    import java.util.List;
034    
035    /**
036     * @author Marcellus Tavares
037     * @author Manuel de la Pe??a
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<DDLRecord> orderByComparator)
045                    throws Exception {
046    
047                    StringBundler sb = new StringBundler();
048    
049                    List<DDMFormField> ddmFormFields = getDDMFormFields(recordSetId);
050    
051                    for (DDMFormField ddmFormField : ddmFormFields) {
052                            LocalizedValue label = ddmFormField.getLabel();
053    
054                            sb.append(label.getString(getLocale()));
055                            sb.append(CharPool.COMMA);
056                    }
057    
058                    sb.append(LanguageUtil.get(getLocale(), "status"));
059                    sb.append(StringPool.NEW_LINE);
060    
061                    List<DDLRecord> records = DDLRecordLocalServiceUtil.getRecords(
062                            recordSetId, status, start, end, orderByComparator);
063    
064                    Iterator<DDLRecord> iterator = records.iterator();
065    
066                    while (iterator.hasNext()) {
067                            DDLRecord record = iterator.next();
068    
069                            DDLRecordVersion recordVersion = record.getRecordVersion();
070    
071                            Fields fields = StorageEngineUtil.getFields(
072                                    recordVersion.getDDMStorageId());
073    
074                            for (DDMFormField ddmFormField : ddmFormFields) {
075                                    String name = ddmFormField.getName();
076                                    String value = StringPool.BLANK;
077    
078                                    if (fields.contains(name)) {
079                                            Field field = fields.get(name);
080    
081                                            value = field.getRenderedValue(getLocale());
082                                    }
083    
084                                    sb.append(CSVUtil.encode(value));
085                                    sb.append(CharPool.COMMA);
086                            }
087    
088                            sb.append(getStatusMessage(recordVersion.getStatus()));
089    
090                            if (iterator.hasNext()) {
091                                    sb.append(StringPool.NEW_LINE);
092                            }
093                    }
094    
095                    String csv = sb.toString();
096    
097                    return csv.getBytes();
098            }
099    
100    }