IDEMPIERE-454 Easy import / First a CSV exporter with the format expected for the import

This commit is contained in:
Carlos Ruiz 2012-10-10 22:56:16 -05:00
parent cc94264d83
commit a15c08f66e
12 changed files with 317 additions and 13 deletions

View File

@ -11,6 +11,7 @@
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry exported="true" kind="lib" path="groovy-all-1.7.5.jar"/> <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"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<accessrules> <accessrules>
<accessrule kind="accessible" pattern="com/sun/rowset/*"/> <accessrule kind="accessible" pattern="com/sun/rowset/*"/>

View File

@ -17,7 +17,8 @@ Bundle-ClassPath: base.jar,
jnlp.jar, jnlp.jar,
groovy-all-1.7.5.jar, groovy-all-1.7.5.jar,
vt-dictionary-3.0.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, Export-Package: bsh,
bsh.classpath, bsh.classpath,
bsh.collection, bsh.collection,
@ -237,7 +238,14 @@ Export-Package: bsh,
org.jfree.ui.about.resources, org.jfree.ui.about.resources,
org.jfree.ui.action, org.jfree.ui.action,
org.jfree.ui.tabbedui, 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 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: com.sun.mail.smtp;version="1.4.0", Import-Package: com.sun.mail.smtp;version="1.4.0",
javax.jms;version="1.1.0", javax.jms;version="1.1.0",

View File

@ -15,7 +15,8 @@ bin.includes = META-INF/,\
OSGI-INF/,\ OSGI-INF/,\
groovy-all-1.7.5.jar,\ groovy-all-1.7.5.jar,\
vt-dictionary-3.0.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/ output.base.jar = build/
source.base.jar = src/ source.base.jar = src/
src.includes = schema/ src.includes = schema/

View File

@ -42,6 +42,15 @@
priority="0"> priority="0">
</exporter> </exporter>
</extension> </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 <extension
id="org.adempiere.base.ModelGeneratorApplication" id="org.adempiere.base.ModelGeneratorApplication"
name="Model Generator" name="Model Generator"

View File

@ -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;
}
}

View File

@ -537,7 +537,7 @@ public class GridTable extends AbstractTableModel
} // getColumn } // getColumn
/** /**
* Return Columns with Indentifier (ColumnName) * Return Columns with Identifier (ColumnName)
* @param identifier column name * @param identifier column name
* @return MField * @return MField
*/ */

View File

@ -603,4 +603,24 @@ public class MColumn extends X_AD_Column
else else
return false; 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 } // MColumn

View File

@ -130,6 +130,10 @@ public final class DisplayType
/** Default Amount Precision */ /** Default Amount Precision */
private static final int AMOUNT_FRACTION = 2; 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 */ /** Logger */
private static CLogger s_log = CLogger.getCLogger (DisplayType.class); private static CLogger s_log = CLogger.getCLogger (DisplayType.class);
@ -398,7 +402,7 @@ public final class DisplayType
*/ */
static public SimpleDateFormat getDateFormat_JDBC() static public SimpleDateFormat getDateFormat_JDBC()
{ {
return new SimpleDateFormat ("yyyy-MM-dd"); return new SimpleDateFormat (DEFAULT_DATE_FORMAT);
} // getDateFormat_JDBC } // getDateFormat_JDBC
/** /**
@ -407,9 +411,14 @@ public final class DisplayType
*/ */
static public SimpleDateFormat getTimestampFormat_Default() static public SimpleDateFormat getTimestampFormat_Default()
{ {
return new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); return new SimpleDateFormat (DEFAULT_TIMESTAMP_FORMAT);
} // getTimestampFormat_JDBC } // getTimestampFormat_JDBC
static public SimpleDateFormat getTimeFormat_Default()
{
return new SimpleDateFormat (DEFAULT_TIME_FORMAT);
} // getTimeFormat_Default
/** /**
* Return Storage Class. * Return Storage Class.
* (used for MiniTable) * (used for MiniTable)

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <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 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.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="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/> <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"/> <classpathentry kind="output" path="build"/>
</classpath> </classpath>

View File

@ -4,12 +4,13 @@ Bundle-Name: org.adempiere.extend
Bundle-SymbolicName: org.adempiere.extend;singleton:=true Bundle-SymbolicName: org.adempiere.extend;singleton:=true
Bundle-Version: 1.0.0.qualifier Bundle-Version: 1.0.0.qualifier
Bundle-ClassPath: extend.jar, Bundle-ClassPath: extend.jar,
spiffy-with_source-all-0.05.jar, spiffy-with_source-all-0.05.jar
SuperCSV-with_src-1.52.jar
Eclipse-RegisterBuddy: org.adempiere.tools Eclipse-RegisterBuddy: org.adempiere.tools
Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: compiere.model, Export-Package: compiere.model,
test test
Fragment-Host: org.adempiere.base;bundle-version="0.0.0" Fragment-Host: org.adempiere.base;bundle-version="0.0.0"
Eclipse-PatchFragment: true 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

View File

@ -1,8 +1,7 @@
output.extend.jar = build/ output.extend.jar = build/
bin.includes = META-INF/,\ bin.includes = META-INF/,\
extend.jar,\ extend.jar,\
spiffy-with_source-all-0.05.jar,\ spiffy-with_source-all-0.05.jar
SuperCSV-with_src-1.52.jar
jars.compile.order = extend.jar jars.compile.order = extend.jar
source.extend.jar = src/ source.extend.jar = src/

View File

@ -80,7 +80,7 @@ public class CSVFactory
private String[] getCSVHeader() throws IOException private String[] getCSVHeader() throws IOException
{ {
String[] header = reader.getCSVHeader(true); String[] header = reader.getHeader(true);
for (int i = 0; i < header.length; i++) for (int i = 0; i < header.length; i++)
{ {
header[i] = header[i].trim().replaceAll("\\s", ""); header[i] = header[i].trim().replaceAll("\\s", "");