IDEMPIERE-454 Easy import / First a CSV exporter with the format expected for the import
This commit is contained in:
parent
cc94264d83
commit
a15c08f66e
|
@ -11,6 +11,7 @@
|
|||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry exported="true" kind="lib" path="groovy-all-1.7.5.jar"/>
|
||||
<classpathentry kind="lib" path="super-csv-2.0.0-beta-1.jar" sourcepath="/home/carlos/libsources/super-csv-2.0.0-beta-1-sources.jar"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
|
||||
<accessrules>
|
||||
<accessrule kind="accessible" pattern="com/sun/rowset/*"/>
|
||||
|
|
|
@ -17,7 +17,8 @@ Bundle-ClassPath: base.jar,
|
|||
jnlp.jar,
|
||||
groovy-all-1.7.5.jar,
|
||||
vt-dictionary-3.0.jar,
|
||||
vt-password-3.1.1.jar
|
||||
vt-password-3.1.1.jar,
|
||||
super-csv-2.0.0-beta-1.jar
|
||||
Export-Package: bsh,
|
||||
bsh.classpath,
|
||||
bsh.collection,
|
||||
|
@ -237,7 +238,14 @@ Export-Package: bsh,
|
|||
org.jfree.ui.about.resources,
|
||||
org.jfree.ui.action,
|
||||
org.jfree.ui.tabbedui,
|
||||
org.jfree.util
|
||||
org.jfree.util,
|
||||
org.supercsv.cellprocessor,
|
||||
org.supercsv.cellprocessor.constraint,
|
||||
org.supercsv.cellprocessor.ift,
|
||||
org.supercsv.exception,
|
||||
org.supercsv.io,
|
||||
org.supercsv.prefs,
|
||||
org.supercsv.util
|
||||
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
|
||||
Import-Package: com.sun.mail.smtp;version="1.4.0",
|
||||
javax.jms;version="1.1.0",
|
||||
|
|
|
@ -15,7 +15,8 @@ bin.includes = META-INF/,\
|
|||
OSGI-INF/,\
|
||||
groovy-all-1.7.5.jar,\
|
||||
vt-dictionary-3.0.jar,\
|
||||
vt-password-3.1.1.jar
|
||||
vt-password-3.1.1.jar,\
|
||||
super-csv-2.0.0-beta-1.jar
|
||||
output.base.jar = build/
|
||||
source.base.jar = src/
|
||||
src.includes = schema/
|
||||
|
|
|
@ -42,6 +42,15 @@
|
|||
priority="0">
|
||||
</exporter>
|
||||
</extension>
|
||||
<extension
|
||||
id="org.adempiere.impexp.GridTabCSVExporter"
|
||||
name="Grid data CSV exporter"
|
||||
point="org.adempiere.base.IGridTabExporter">
|
||||
<exporter
|
||||
class="org.adempiere.impexp.GridTabCSVExporter"
|
||||
priority="0">
|
||||
</exporter>
|
||||
</extension>
|
||||
<extension
|
||||
id="org.adempiere.base.ModelGeneratorApplication"
|
||||
name="Model Generator"
|
||||
|
|
|
@ -0,0 +1,256 @@
|
|||
/******************************************************************************
|
||||
* Product: iDempiere ERP & CRM Smart Business Solution *
|
||||
* Copyright (C) 2012 Carlos Ruiz *
|
||||
* Copyright (C) 2012 Trek Global *
|
||||
* This program is free software; you can redistribute it and/or modify it *
|
||||
* under the terms version 2 of the GNU General Public License as published *
|
||||
* by the Free Software Foundation. This program is distributed in the hope *
|
||||
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
|
||||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
||||
* See the GNU General Public License for more details. *
|
||||
* You should have received a copy of the GNU General Public License along *
|
||||
* with this program; if not, write to the Free Software Foundation, Inc., *
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
|
||||
*****************************************************************************/
|
||||
package org.adempiere.impexp;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.adempiere.base.IGridTabExporter;
|
||||
import org.adempiere.model.MTabCustomization;
|
||||
import org.compiere.model.GridField;
|
||||
import org.compiere.model.GridTab;
|
||||
import org.compiere.model.GridTable;
|
||||
import org.compiere.model.MColumn;
|
||||
import org.compiere.model.MRefList;
|
||||
import org.compiere.model.MTable;
|
||||
import org.compiere.util.DB;
|
||||
import org.compiere.util.DisplayType;
|
||||
import org.compiere.util.Env;
|
||||
import org.compiere.util.Msg;
|
||||
import org.compiere.util.Util;
|
||||
import org.supercsv.cellprocessor.FmtBool;
|
||||
import org.supercsv.cellprocessor.FmtDate;
|
||||
import org.supercsv.cellprocessor.FmtNumber;
|
||||
import org.supercsv.cellprocessor.Optional;
|
||||
import org.supercsv.cellprocessor.ift.CellProcessor;
|
||||
import org.supercsv.io.CsvMapWriter;
|
||||
import org.supercsv.io.ICsvMapWriter;
|
||||
import org.supercsv.prefs.CsvPreference;
|
||||
|
||||
/**
|
||||
* CSV Exporter for GridTab
|
||||
* @author Carlos Ruiz
|
||||
*/
|
||||
public class GridTabCSVExporter implements IGridTabExporter
|
||||
{
|
||||
@Override
|
||||
public void export(GridTab gridTab, List<GridTab> childs, boolean currentRowOnly, File file) {
|
||||
|
||||
ICsvMapWriter mapWriter = null;
|
||||
try {
|
||||
mapWriter = new CsvMapWriter(new FileWriter(file), CsvPreference.STANDARD_PREFERENCE);
|
||||
GridTable gt = gridTab.getTableModel();
|
||||
GridField[] gridFields = getFields(gridTab);
|
||||
List<String> headArray = new ArrayList<String>();
|
||||
List<String> colsArray = new ArrayList<String>();
|
||||
List<CellProcessor> procArray = new ArrayList<CellProcessor>();
|
||||
MTable table = MTable.get(Env.getCtx(), gridTab.getTableName());
|
||||
for (int idxfld = 0; idxfld < gridFields.length; idxfld++) {
|
||||
GridField field = gridFields[idxfld];
|
||||
colsArray.add(field.getColumnName());
|
||||
MColumn column = MColumn.get(Env.getCtx(), field.getAD_Column_ID());
|
||||
String headName = resolveColumnName(table, column);
|
||||
headArray.add(headName);
|
||||
if (DisplayType.Date == column.getAD_Reference_ID()) {
|
||||
procArray.add(new Optional(new FmtDate(DisplayType.DEFAULT_DATE_FORMAT)));
|
||||
} else if (DisplayType.DateTime == column.getAD_Reference_ID()) {
|
||||
procArray.add(new Optional(new FmtDate(DisplayType.DEFAULT_TIMESTAMP_FORMAT)));
|
||||
} else if (DisplayType.Time == column.getAD_Reference_ID()) {
|
||||
procArray.add(new Optional(new FmtDate("DisplayType.DEFAULT_TIME_FORMAT")));
|
||||
} else if (DisplayType.Integer == column.getAD_Reference_ID() || DisplayType.isNumeric(column.getAD_Reference_ID())) {
|
||||
procArray.add(new Optional(new FmtNumber(DisplayType.getNumberFormat(column.getAD_Reference_ID()))));
|
||||
} else if (DisplayType.YesNo == column.getAD_Reference_ID()) {
|
||||
procArray.add(new Optional(new FmtBool("Y", "N")));
|
||||
} else { // lookups and text
|
||||
procArray.add(null);
|
||||
}
|
||||
}
|
||||
// the header elements are used to map the bean values to each column (names must match)
|
||||
String[] header = headArray.toArray(new String[headArray.size()]);
|
||||
CellProcessor[] processors = procArray.toArray(new CellProcessor[procArray.size()]);
|
||||
// write the header
|
||||
mapWriter.writeHeader(header);
|
||||
// write the beans
|
||||
int start = 0;
|
||||
int end = 0;
|
||||
if (currentRowOnly) {
|
||||
start = gridTab.getCurrentRow();
|
||||
end = start + 1;
|
||||
} else {
|
||||
end = gt.getRowCount();
|
||||
}
|
||||
for (int idxrow = start; idxrow < end; idxrow++) {
|
||||
Map<String, Object> row = new HashMap<String, Object>();
|
||||
for (int idxfld = 0; idxfld < header.length; idxfld++) {
|
||||
GridField field = gridFields[idxfld];
|
||||
MColumn column = MColumn.get(Env.getCtx(), field.getAD_Column_ID());
|
||||
String headName = header[idxfld];
|
||||
Object value = resolveValue(gridTab, table, column, idxrow, headName);
|
||||
row.put(headName, value);
|
||||
}
|
||||
mapWriter.write(row, header, processors);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (mapWriter != null) {
|
||||
try {
|
||||
mapWriter.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Object resolveValue(GridTab gridTab, MTable table, MColumn column, int i, String headName) {
|
||||
Object value = null;
|
||||
if (headName.contains("[") && headName.endsWith("]")) {
|
||||
String foreignTable = column.getReferenceTableName();
|
||||
Object idO = gridTab.getValue(i, column.getColumnName());
|
||||
if (idO != null) {
|
||||
if (foreignTable.equals("AD_Ref_List")) {
|
||||
String ref = (String) idO;
|
||||
value = MRefList.getListName(Env.getCtx(), column.getAD_Reference_Value_ID(), ref);
|
||||
} else {
|
||||
int id = (Integer) idO;
|
||||
int start = headName.indexOf("[")+1;
|
||||
int end = headName.length()-1;
|
||||
String foreignColumn = headName.substring(start, end);
|
||||
StringBuilder select = new StringBuilder("SELECT ")
|
||||
.append(foreignColumn).append(" FROM ")
|
||||
.append(foreignTable).append(" WHERE ")
|
||||
.append(foreignTable).append("_ID=?");
|
||||
value = DB.getSQLValueStringEx(null, select.toString(), id);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
value = gridTab.getValue(i, headName);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private String resolveColumnName(MTable table, MColumn column) {
|
||||
StringBuilder name = new StringBuilder(column.getColumnName());
|
||||
if (DisplayType.isLookup(column.getAD_Reference_ID())) {
|
||||
// resolve to identifier - search for value first, if not search for name - if not use the ID
|
||||
String foreignTable = column.getReferenceTableName();
|
||||
if ( ! ("AD_Language".equals(foreignTable) || "AD_EntityType".equals(foreignTable))) {
|
||||
MTable fTable = MTable.get(Env.getCtx(), foreignTable);
|
||||
// Hardcoded / do not check for Value on AD_Org and AD_User, must use name for these two tables
|
||||
if (! ("AD_Org".equals(foreignTable) || "AD_User".equals(foreignTable))
|
||||
&& fTable.getColumn("Value") != null) {
|
||||
name.append("[Value]"); // fully qualified
|
||||
} else if (fTable.getColumn("Name") != null) {
|
||||
name.append("[Name]");
|
||||
}
|
||||
}
|
||||
}
|
||||
return name.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileExtension() {
|
||||
return "csv";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileExtensionLabel() {
|
||||
return Msg.getMsg(Env.getCtx(), "FileCSV");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() {
|
||||
return "application/csv";
|
||||
}
|
||||
|
||||
private GridField[] getFields (GridTab gridTab) {
|
||||
GridTable tableModel = gridTab.getTableModel();
|
||||
GridField[] tmpFields = tableModel.getFields();
|
||||
MTabCustomization tabCustomization = MTabCustomization.get(Env.getCtx(), Env.getAD_User_ID(Env.getCtx()), gridTab.getAD_Tab_ID(), null);
|
||||
GridField[] gridFields = null;
|
||||
if (tabCustomization != null
|
||||
&& tabCustomization.getAD_Tab_Customization_ID() > 0
|
||||
&& !Util.isEmpty(tabCustomization.getCustom(), true))
|
||||
{
|
||||
String custom = tabCustomization.getCustom().trim();
|
||||
String[] customComponent = custom.split(";");
|
||||
String[] fieldIds = customComponent[0].split("[,]");
|
||||
List<GridField> fieldList = new ArrayList<GridField>();
|
||||
for(String fieldIdStr : fieldIds)
|
||||
{
|
||||
fieldIdStr = fieldIdStr.trim();
|
||||
if (fieldIdStr.length() == 0) continue;
|
||||
int AD_Field_ID = Integer.parseInt(fieldIdStr);
|
||||
for(GridField gridField : tmpFields)
|
||||
{
|
||||
if (gridField.getAD_Field_ID() == AD_Field_ID)
|
||||
{
|
||||
if(gridField.isDisplayedGrid())
|
||||
fieldList.add(gridField);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
gridFields = fieldList.toArray(new GridField[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
ArrayList<GridField> gridFieldList = new ArrayList<GridField>();
|
||||
|
||||
for(GridField field:tmpFields)
|
||||
{
|
||||
if(field.isDisplayedGrid())
|
||||
gridFieldList.add(field);
|
||||
}
|
||||
|
||||
Collections.sort(gridFieldList, new Comparator<GridField>() {
|
||||
@Override
|
||||
public int compare(GridField o1, GridField o2) {
|
||||
return o1.getSeqNoGrid()-o2.getSeqNoGrid();
|
||||
}
|
||||
});
|
||||
|
||||
gridFields = new GridField[gridFieldList.size()];
|
||||
gridFieldList.toArray(gridFields);
|
||||
}
|
||||
return gridFields;
|
||||
}
|
||||
|
||||
public boolean isColumnPrinted(GridTab tab, int col)
|
||||
{
|
||||
GridField field = tab.getField(col);
|
||||
// field not displayed
|
||||
if (!field.isDisplayed())
|
||||
return false;
|
||||
// field encrypted
|
||||
if (field.isEncrypted())
|
||||
return false;
|
||||
// button without a reference value
|
||||
if (field.getDisplayType() == DisplayType.Button && field.getAD_Reference_Value_ID() == 0)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -537,7 +537,7 @@ public class GridTable extends AbstractTableModel
|
|||
} // getColumn
|
||||
|
||||
/**
|
||||
* Return Columns with Indentifier (ColumnName)
|
||||
* Return Columns with Identifier (ColumnName)
|
||||
* @param identifier column name
|
||||
* @return MField
|
||||
*/
|
||||
|
|
|
@ -603,4 +603,24 @@ public class MColumn extends X_AD_Column
|
|||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
public String getReferenceTableName() {
|
||||
String foreignTable = null;
|
||||
if (DisplayType.TableDir == getAD_Reference_ID()
|
||||
|| (DisplayType.Search == getAD_Reference_ID() && getAD_Reference_Value_ID() == 0)) {
|
||||
foreignTable = getColumnName().substring(0, getColumnName().length()-3);
|
||||
} else if (DisplayType.Table == getAD_Reference_ID() || DisplayType.Search == getAD_Reference_ID()) {
|
||||
X_AD_Reference ref = new X_AD_Reference(getCtx(), getAD_Reference_Value_ID(), get_TrxName());
|
||||
if (X_AD_Reference.VALIDATIONTYPE_TableValidation.equals(ref.getValidationType())) {
|
||||
MRefTable rt = new MRefTable(getCtx(), getAD_Reference_Value_ID(), get_TrxName());
|
||||
if (rt != null)
|
||||
foreignTable = rt.getAD_Table().getTableName();
|
||||
}
|
||||
} else if (DisplayType.List == getAD_Reference_ID()) {
|
||||
foreignTable = "AD_Ref_List";
|
||||
}
|
||||
|
||||
return foreignTable;
|
||||
}
|
||||
|
||||
} // MColumn
|
||||
|
|
|
@ -130,6 +130,10 @@ public final class DisplayType
|
|||
/** Default Amount Precision */
|
||||
private static final int AMOUNT_FRACTION = 2;
|
||||
|
||||
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
|
||||
public static final String DEFAULT_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
|
||||
|
||||
/** Logger */
|
||||
private static CLogger s_log = CLogger.getCLogger (DisplayType.class);
|
||||
|
||||
|
@ -398,7 +402,7 @@ public final class DisplayType
|
|||
*/
|
||||
static public SimpleDateFormat getDateFormat_JDBC()
|
||||
{
|
||||
return new SimpleDateFormat ("yyyy-MM-dd");
|
||||
return new SimpleDateFormat (DEFAULT_DATE_FORMAT);
|
||||
} // getDateFormat_JDBC
|
||||
|
||||
/**
|
||||
|
@ -407,9 +411,14 @@ public final class DisplayType
|
|||
*/
|
||||
static public SimpleDateFormat getTimestampFormat_Default()
|
||||
{
|
||||
return new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
|
||||
return new SimpleDateFormat (DEFAULT_TIMESTAMP_FORMAT);
|
||||
} // getTimestampFormat_JDBC
|
||||
|
||||
static public SimpleDateFormat getTimeFormat_Default()
|
||||
{
|
||||
return new SimpleDateFormat (DEFAULT_TIME_FORMAT);
|
||||
} // getTimeFormat_Default
|
||||
|
||||
/**
|
||||
* Return Storage Class.
|
||||
* (used for MiniTable)
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry exported="true" kind="lib" path="SuperCSV-with_src-1.52.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="spiffy-with_source-all-0.05.jar"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
|
||||
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="lib" path="/org.adempiere.base/super-csv-2.0.0-beta-1.jar" sourcepath="/home/carlos/libsources/super-csv-2.0.0-beta-1-sources.jar"/>
|
||||
<classpathentry kind="output" path="build"/>
|
||||
</classpath>
|
||||
|
|
|
@ -4,12 +4,13 @@ Bundle-Name: org.adempiere.extend
|
|||
Bundle-SymbolicName: org.adempiere.extend;singleton:=true
|
||||
Bundle-Version: 1.0.0.qualifier
|
||||
Bundle-ClassPath: extend.jar,
|
||||
spiffy-with_source-all-0.05.jar,
|
||||
SuperCSV-with_src-1.52.jar
|
||||
spiffy-with_source-all-0.05.jar
|
||||
Eclipse-RegisterBuddy: org.adempiere.tools
|
||||
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
|
||||
Export-Package: compiere.model,
|
||||
test
|
||||
Fragment-Host: org.adempiere.base;bundle-version="0.0.0"
|
||||
Eclipse-PatchFragment: true
|
||||
Import-Package: junit.framework;version="3.8.2"
|
||||
Import-Package: junit.framework;version="3.8.2",
|
||||
org.supercsv.io,
|
||||
org.supercsv.prefs
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
output.extend.jar = build/
|
||||
bin.includes = META-INF/,\
|
||||
extend.jar,\
|
||||
spiffy-with_source-all-0.05.jar,\
|
||||
SuperCSV-with_src-1.52.jar
|
||||
spiffy-with_source-all-0.05.jar
|
||||
jars.compile.order = extend.jar
|
||||
source.extend.jar = src/
|
||||
|
||||
|
|
|
@ -80,7 +80,7 @@ public class CSVFactory
|
|||
|
||||
private String[] getCSVHeader() throws IOException
|
||||
{
|
||||
String[] header = reader.getCSVHeader(true);
|
||||
String[] header = reader.getHeader(true);
|
||||
for (int i = 0; i < header.length; i++)
|
||||
{
|
||||
header[i] = header[i].trim().replaceAll("\\s", "");
|
||||
|
|
Loading…
Reference in New Issue