details: https://code.openbravo.com/erp/devel/pi/rev/d70218caa062 changeset: 16234:d70218caa062 user: Asier Galdos <asier.galdos <at> almis.com> date: Thu Apr 26 17:05:23 2012 +0200 summary: Fixes issue 20357: Support for property filtering in EntityXMLConverter
details: https://code.openbravo.com/erp/devel/pi/rev/054ca3ab4a63 changeset: 16235:054ca3ab4a63 user: Iván Perdomo <ivan.perdomo <at> openbravo.com> date: Fri Apr 27 09:39:49 2012 +0200 summary: Related to issue 20357: Added simple test, filtering product xml by id and name diffstat: src-test/org/openbravo/test/xml/EntityXMLIssues.java | 35 ++++++++++++++++++++ src/org/openbravo/dal/xml/EntityXMLConverter.java | 31 +++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletions(-) diffs (135 lines): diff -r 400415a4f8ae -r 054ca3ab4a63 src-test/org/openbravo/test/xml/EntityXMLIssues.java --- a/src-test/org/openbravo/test/xml/EntityXMLIssues.java Thu Apr 26 17:10:09 2012 +0200 +++ b/src-test/org/openbravo/test/xml/EntityXMLIssues.java Fri Apr 27 09:39:49 2012 +0200 @@ -19,14 +19,18 @@ package org.openbravo.test.xml; +import java.io.StringWriter; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import org.openbravo.base.provider.OBProvider; import org.openbravo.base.structure.BaseOBObject; import org.openbravo.dal.core.DalUtil; +import org.openbravo.dal.service.OBCriteria; import org.openbravo.dal.service.OBDal; +import org.openbravo.dal.xml.EntityXMLConverter; import org.openbravo.model.ad.datamodel.Column; import org.openbravo.model.ad.system.Client; import org.openbravo.model.common.businesspartner.Greeting; @@ -163,4 +167,35 @@ assertTrue(column2.getLength() == 60); OBDal.getInstance().rollbackAndClose(); } + + /** + * 20357: There should be a way to define with properties i want to export in a EntityXMLConverter + * call + */ + public void testIssue20357() { + + final OBCriteria<BaseOBObject> obc = OBDal.getInstance().createCriteria("Product"); + final EntityXMLConverter exc = EntityXMLConverter.newInstance(); + + exc.setOptionIncludeChildren(false); + exc.setOptionEmbedChildren(false); + exc.setOptionIncludeReferenced(false); + exc.setOptionExportClientOrganizationReferences(false); + + StringWriter sw = new StringWriter(); + exc.setOutput(sw); + + exc.process(obc.list()); + + // We only need the id, name of the products + exc.setIncludedProperties(Arrays.asList("id", "name")); + + StringWriter sw2 = new StringWriter(); + exc.setOutput(sw2); + + exc.process(obc.list()); + + // dummy assert + assertNotSame("Exported XML is the same", exc.toString(), sw2.toString()); + } } \ No newline at end of file diff -r 400415a4f8ae -r 054ca3ab4a63 src/org/openbravo/dal/xml/EntityXMLConverter.java --- a/src/org/openbravo/dal/xml/EntityXMLConverter.java Thu Apr 26 17:10:09 2012 +0200 +++ b/src/org/openbravo/dal/xml/EntityXMLConverter.java Fri Apr 27 09:39:49 2012 +0200 @@ -129,6 +129,9 @@ private DataSet dataSet; private Map<Entity, DataSetTable> dataSetTablesByEntity; + // properties that are to be exported + private List<String> includedProperties = new ArrayList<String>(); + /** * Clear internal data structures, after this call this converter can be used for a new set of * objects which need to be exported to a xml representation. @@ -336,6 +339,9 @@ if (excludeAuditInfo != null && excludeAuditInfo) { DataSetService.getInstance().removeAuditInfo(exportableProperties); } + if (!includedProperties.isEmpty()) { + filterProperties(exportableProperties); + } } // export each property @@ -384,6 +390,7 @@ final Object value = obObject.get(p.getName()); // will result in an empty tag if null + if (value == null) { propertyAttrs.addAttribute("", "", "xsi:nil", "CDATA", "true"); xmlHandler.startElement("", "", p.getName(), propertyAttrs); @@ -466,7 +473,6 @@ addToExportList((BaseOBObject) o); } } - xmlHandler.endElement("", "", p.getName()); } else if (!p.isOneToMany()) { @@ -487,6 +493,20 @@ xmlHandler.endElement("", "", entityName); } + /** + * Filters the list of properties to export using the includedProperties list + * + */ + private void filterProperties(List<Property> properties) { + final List<Property> toRemove = new ArrayList<Property>(); + for (final Property p : properties) { + if (!p.isChild() && !includedProperties.contains(p.getName())) { + toRemove.add(p); + } + } + properties.removeAll(toRemove); + } + private void addReferenceAttributes(AttributesImpl attrs, BaseOBObject referedObject) { if (referedObject == null) { return; @@ -663,6 +683,15 @@ this.optionExportTransientInfo = optionExportTransientInfo; } + /** + * + * @param includedProperties + * set to the Names of the properties to be exported + */ + public void setIncludedProperties(List<String> includedProperties) { + this.includedProperties = includedProperties; + } + public Client getClient() { return client; } ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Openbravo-commits mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openbravo-commits
