Search in sources :

Example 1 with EntitySaxReader

use of org.apache.ofbiz.entity.util.EntitySaxReader in project ofbiz-framework by apache.

the class WebToolsServices method parseEntityXmlFile.

public static Map<String, Object> parseEntityXmlFile(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    Locale locale = (Locale) context.get("locale");
    URL url = (URL) context.get("url");
    String xmltext = (String) context.get("xmltext");
    if (url == null && xmltext == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityImportNoXmlFileOrTextSpecified", locale));
    }
    boolean onlyInserts = (String) context.get("onlyInserts") != null;
    boolean maintainTimeStamps = (String) context.get("maintainTimeStamps") != null;
    boolean createDummyFks = (String) context.get("createDummyFks") != null;
    boolean checkDataOnly = (String) context.get("checkDataOnly") != null;
    Integer txTimeout = (Integer) context.get("txTimeout");
    Map<String, Object> placeholderValues = UtilGenerics.checkMap(context.get("placeholderValues"));
    if (txTimeout == null) {
        txTimeout = Integer.valueOf(7200);
    }
    long rowProcessed = 0;
    try {
        EntitySaxReader reader = new EntitySaxReader(delegator);
        reader.setUseTryInsertMethod(onlyInserts);
        reader.setMaintainTxStamps(maintainTimeStamps);
        reader.setTransactionTimeout(txTimeout.intValue());
        reader.setCreateDummyFks(createDummyFks);
        reader.setCheckDataOnly(checkDataOnly);
        reader.setPlaceholderValues(placeholderValues);
        long numberRead = (url != null ? reader.parse(url) : reader.parse(xmltext));
        rowProcessed = numberRead;
    } catch (Exception ex) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityImportParsingError", UtilMisc.toMap("errorString", ex.toString()), locale));
    }
    // send the notification
    Map<String, Object> resp = UtilMisc.<String, Object>toMap("rowProcessed", rowProcessed);
    return resp;
}
Also used : Locale(java.util.Locale) EntitySaxReader(org.apache.ofbiz.entity.util.EntitySaxReader) Delegator(org.apache.ofbiz.entity.Delegator) URL(java.net.URL) UtilURL(org.apache.ofbiz.base.util.UtilURL) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) FileNotFoundException(java.io.FileNotFoundException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) SAXException(org.xml.sax.SAXException) GeneralException(org.apache.ofbiz.base.util.GeneralException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with EntitySaxReader

use of org.apache.ofbiz.entity.util.EntitySaxReader in project ofbiz-framework by apache.

the class EntityTestSuite method testEntitySaxReaderUpdate.

public void testEntitySaxReaderUpdate() throws Exception {
    String xmlContentLoad = "<entity-engine-xml>" + "<TestingType testingTypeId=\"create-update\" description=\"create update\"/>" + "<TestingType testingTypeId=\"create-updated\" description=\"create update updated\"/>" + "<Testing testingId=\"create-update-T3\" testingTypeId=\"create-update\" testingName=\"Test 3\" testingSize=\"10\" testingDate=\"2010-01-01 00:00:00\"/>" + "<create-update>" + "    <Testing testingId=\"create-update-T1\" testingTypeId=\"create-update\" testingName=\"First test update\" testingSize=\"20\" testingDate=\"2010-01-01 00:00:00\"/>" + "    <Testing testingId=\"create-update-T3\" testingTypeId=\"create-updated\" testingName=\"Third test\" testingSize=\"30\" testingDate=\"2010-03-01 00:00:00\"/>" + "</create-update>" + "</entity-engine-xml>";
    EntitySaxReader reader = new EntitySaxReader(delegator);
    long numberLoaded = reader.parse(xmlContentLoad);
    assertEquals("Update Entity loaded ", 5, numberLoaded);
    GenericValue t1 = EntityQuery.use(delegator).from("Testing").where("testingId", "create-update-T1").queryOne();
    GenericValue t3 = EntityQuery.use(delegator).from("Testing").where("testingId", "create-update-T3").queryOne();
    assertNotNull("Update Testing(T1)", t1);
    assertEquals("Update Testing(T1).testingTypeId", "create-update", t1.getString("testingTypeId"));
    assertEquals("Update Testing(T1).testingName", "First test update", t1.getString("testingName"));
    assertEquals("Update Testing(T1).testingSize", Long.valueOf(20), t1.getLong("testingSize"));
    assertEquals("Update Testing(T1).testingDate", UtilDateTime.toTimestamp("01/01/2010 00:00:00"), t1.getTimestamp("testingDate"));
    assertNotNull("Update Testing(T3)", t3);
    assertEquals("Update Testing(T3).testingTypeId", "create-updated", t3.getString("testingTypeId"));
    assertEquals("Update Testing(T3).testingName", "Third test", t3.getString("testingName"));
    assertEquals("Update Testing(T3).testingSize", Long.valueOf(30), t3.getLong("testingSize"));
    assertEquals("Update Testing(T3).testingDate", UtilDateTime.toTimestamp("03/01/2010 00:00:00"), t3.getTimestamp("testingDate"));
}
Also used : EntitySaxReader(org.apache.ofbiz.entity.util.EntitySaxReader) GenericValue(org.apache.ofbiz.entity.GenericValue)

Example 3 with EntitySaxReader

use of org.apache.ofbiz.entity.util.EntitySaxReader in project ofbiz-framework by apache.

the class EntityTestSuite method testEntitySaxReaderCreateSkip.

public void testEntitySaxReaderCreateSkip() throws Exception {
    String xmlContentLoad = "<entity-engine-xml>" + "<TestingType testingTypeId=\"reader-create-skip\" description=\"reader create skip\"/>" + "<Testing testingId=\"reader-create-skip\" testingTypeId=\"reader-create-skip\" testingName=\"reader create skip\" testingSize=\"10\" testingDate=\"2010-01-01 00:00:00\"/>" + "</entity-engine-xml>";
    EntitySaxReader reader = new EntitySaxReader(delegator);
    long numberLoaded = reader.parse(xmlContentLoad);
    xmlContentLoad = "<create>" + "    <Testing testingId=\"reader-create-skip\" testingName=\"reader create skip updated\" testingSize=\"20\" testingDate=\"2012-02-02 02:02:02\"/>" + "</create>";
    reader = new EntitySaxReader(delegator);
    numberLoaded += reader.parse(xmlContentLoad);
    assertEquals("Create Skip Entity loaded ", 3, numberLoaded);
    GenericValue t1 = EntityQuery.use(delegator).from("Testing").where("testingId", "reader-create-skip").queryOne();
    assertNotNull("Create Skip Testing(T1)", t1);
    assertEquals("Create Skip Testing(T1).testingTypeId", "reader-create-skip", t1.getString("testingTypeId"));
    assertEquals("Create Skip Testing(T1).testingName", "reader create skip", t1.getString("testingName"));
    assertEquals("Create Skip Testing(T1).testingSize", Long.valueOf(10), t1.getLong("testingSize"));
    assertEquals("Create Skip Testing(T1).testingDate", UtilDateTime.toTimestamp("01/01/2010 00:00:00"), t1.getTimestamp("testingDate"));
}
Also used : EntitySaxReader(org.apache.ofbiz.entity.util.EntitySaxReader) GenericValue(org.apache.ofbiz.entity.GenericValue)

Example 4 with EntitySaxReader

use of org.apache.ofbiz.entity.util.EntitySaxReader in project ofbiz-framework by apache.

the class EntityTestSuite method testEntitySaxReaderDelete.

public void testEntitySaxReaderDelete() throws Exception {
    String xmlContentLoad = "<delete>" + "    <Testing testingId=\"T1\"/>" + "    <Testing testingId=\"T2\"/>" + "    <Testing testingId=\"T3\"/>" + "    <TestingType testingTypeId=\"JUNIT-TEST\"/>" + "    <TestingType testingTypeId=\"JUNIT-TEST2\"/>" + "</delete>";
    EntitySaxReader reader = new EntitySaxReader(delegator);
    long numberLoaded = reader.parse(xmlContentLoad);
    assertEquals("Delete Entity loaded ", 5, numberLoaded);
    GenericValue t1 = EntityQuery.use(delegator).from("Testing").where("testingId", "T1").queryOne();
    GenericValue t2 = EntityQuery.use(delegator).from("Testing").where("testingId", "T2").queryOne();
    GenericValue t3 = EntityQuery.use(delegator).from("Testing").where("testingId", "T3").queryOne();
    assertNull("Delete Testing(T1)", t1);
    assertNull("Delete Testing(T2)", t2);
    assertNull("Delete Testing(T3)", t3);
    GenericValue testType = EntityQuery.use(delegator).from("TestingType").where("testingTypeId", "JUNIT-TEST").queryOne();
    assertNull("Delete TestingType 1", testType);
    testType = EntityQuery.use(delegator).from("TestingType").where("testingTypeId", "JUNIT-TEST2").queryOne();
    assertNull("Delete TestingType 2", testType);
}
Also used : EntitySaxReader(org.apache.ofbiz.entity.util.EntitySaxReader) GenericValue(org.apache.ofbiz.entity.GenericValue)

Example 5 with EntitySaxReader

use of org.apache.ofbiz.entity.util.EntitySaxReader in project ofbiz-framework by apache.

the class EntityTestSuite method testEntitySaxReaderReplace.

public void testEntitySaxReaderReplace() throws Exception {
    String xmlContentLoad = "<entity-engine-xml>" + "<TestingType testingTypeId=\"create-replace\" description=\"reader create skip\"/>" + "<Testing testingTypeId=\"create-replace\" testingId=\"create-replace-T1\" testingName=\"First test\" testingSize=\"10\" testingDate=\"2010-01-01 00:00:00\"/>" + "<create-replace>" + "    <Testing testingTypeId=\"create-replace\" testingId=\"create-replace-T1\" testingName=\"First test replace\" />" + "</create-replace>" + "<Testing testingTypeId=\"create-replace\" testingId=\"create-replace-T2\" testingName=\"Second test update\" testingSize=\"20\" testingDate=\"2010-02-01 00:00:00\"/>" + "</entity-engine-xml>";
    EntitySaxReader reader = new EntitySaxReader(delegator);
    long numberLoaded = reader.parse(xmlContentLoad);
    assertEquals("Replace Entity loaded ", 4, numberLoaded);
    GenericValue t1 = EntityQuery.use(delegator).from("Testing").where("testingId", "create-replace-T1").queryOne();
    GenericValue t2 = EntityQuery.use(delegator).from("Testing").where("testingId", "create-replace-T2").queryOne();
    assertNotNull("Replace Testing(T1)", t1);
    assertEquals("Replace Testing(T1).testingTypeId", "create-replace", t1.getString("testingTypeId"));
    assertEquals("Replace Testing(T1).testingName", "First test replace", t1.getString("testingName"));
    assertNull("Replace Testing(T1).testingSize", t1.getLong("testingSize"));
    assertNull("Replace Testing(T1).testingDate", t1.getTimestamp("testingDate"));
    assertNotNull("Replace Testing(T2)", t2);
    assertEquals("Replace Testing(T2).testingTypeId", "create-replace", t2.getString("testingTypeId"));
    assertEquals("Replace Testing(T2).testingName", "Second test update", t2.getString("testingName"));
    assertEquals("Replace Testing(T2).testingSize", Long.valueOf(20), t2.getLong("testingSize"));
    assertEquals("Replace Testing(T2).testingDate", UtilDateTime.toTimestamp("02/01/2010 00:00:00"), t2.getTimestamp("testingDate"));
}
Also used : EntitySaxReader(org.apache.ofbiz.entity.util.EntitySaxReader) GenericValue(org.apache.ofbiz.entity.GenericValue)

Aggregations

EntitySaxReader (org.apache.ofbiz.entity.util.EntitySaxReader)9 GenericValue (org.apache.ofbiz.entity.GenericValue)5 URL (java.net.URL)4 MalformedURLException (java.net.MalformedURLException)2 Delegator (org.apache.ofbiz.entity.Delegator)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 LinkedList (java.util.LinkedList)1 Locale (java.util.Locale)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 AssertionFailedError (junit.framework.AssertionFailedError)1 GeneralException (org.apache.ofbiz.base.util.GeneralException)1 UtilURL (org.apache.ofbiz.base.util.UtilURL)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)1 MiniLangException (org.apache.ofbiz.minilang.MiniLangException)1 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)1 SAXException (org.xml.sax.SAXException)1