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 aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.OrderByComparator;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
025    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
026    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetServiceUtil;
027    import com.liferay.portlet.dynamicdatamapping.model.DDMFormField;
028    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    import java.util.Locale;
033    
034    /**
035     * @author Marcellus Tavares
036     * @author Manuel de la Pe??a
037     */
038    @ProviderType
039    public abstract class BaseDDLExporter implements DDLExporter {
040    
041            @Override
042            public byte[] export(long recordSetId) throws Exception {
043                    return doExport(
044                            recordSetId, WorkflowConstants.STATUS_ANY, QueryUtil.ALL_POS,
045                            QueryUtil.ALL_POS, null);
046            }
047    
048            @Override
049            public byte[] export(long recordSetId, int status) throws Exception {
050                    return doExport(
051                            recordSetId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
052            }
053    
054            @Override
055            public byte[] export(long recordSetId, int status, int start, int end)
056                    throws Exception {
057    
058                    return doExport(recordSetId, status, start, end, null);
059            }
060    
061            @Override
062            public byte[] export(
063                            long recordSetId, int status, int start, int end,
064                            OrderByComparator<DDLRecord> orderByComparator)
065                    throws Exception {
066    
067                    return doExport(recordSetId, status, start, end, orderByComparator);
068            }
069    
070            @Override
071            public Locale getLocale() {
072                    if (_locale == null) {
073                            _locale = LocaleUtil.getSiteDefault();
074                    }
075    
076                    return _locale;
077            }
078    
079            @Override
080            public void setLocale(Locale locale) {
081                    _locale = locale;
082            }
083    
084            protected abstract byte[] doExport(
085                            long recordSetId, int status, int start, int end,
086                            OrderByComparator<DDLRecord> orderByComparator)
087                    throws Exception;
088    
089            protected List<DDMFormField> getDDMFormFields(long recordSetId)
090                    throws Exception {
091    
092                    List<DDMFormField> ddmFormFields = new ArrayList<DDMFormField>();
093    
094                    DDLRecordSet recordSet = DDLRecordSetServiceUtil.getRecordSet(
095                            recordSetId);
096    
097                    DDMStructure ddmStructure = recordSet.getDDMStructure();
098    
099                    for (DDMFormField ddmFormField : ddmStructure.getDDMFormFields(false)) {
100                            if (ddmStructure.isFieldPrivate(ddmFormField.getName())) {
101                                    continue;
102                            }
103    
104                            ddmFormFields.add(ddmFormField);
105                    }
106    
107                    return ddmFormFields;
108            }
109    
110            protected String getStatusMessage(int status) {
111                    String statusLabel = WorkflowConstants.getStatusLabel(status);
112    
113                    return LanguageUtil.get(_locale, statusLabel);
114            }
115    
116            private Locale _locale;
117    
118    }