Search in sources :

Example 1 with RecordIterator

use of org.apache.ofbiz.datafile.RecordIterator in project ofbiz-framework by apache.

the class ZipSalesServices method importFlatTable.

// import table service
public static Map<String, Object> importFlatTable(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    Security security = dctx.getSecurity();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    String taxFileLocation = (String) context.get("taxFileLocation");
    String ruleFileLocation = (String) context.get("ruleFileLocation");
    Locale locale = (Locale) context.get("locale");
    // do security check
    if (!security.hasPermission("SERVICE_INVOKE_ANY", userLogin)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderYouDoNotHavePermissionToLoadTaxTables", locale));
    }
    // get a now stamp (we'll use 2000-01-01)
    Timestamp now = parseDate("20000101", null);
    // load the data file
    DataFile tdf = null;
    try {
        tdf = DataFile.makeDataFile(UtilURL.fromResource(dataFile), flatTable);
    } catch (DataFileException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderUnableToReadZipSalesDataFile", locale));
    }
    // locate the file to be imported
    URL tUrl = UtilURL.fromResource(taxFileLocation);
    if (tUrl == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderUnableToLocateTaxFileAtLocation", UtilMisc.toMap("taxFileLocation", taxFileLocation), locale));
    }
    RecordIterator tri = null;
    try {
        tri = tdf.makeRecordIterator(tUrl);
    } catch (DataFileException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderProblemGettingTheRecordIterator", locale));
    }
    if (tri != null) {
        while (tri.hasNext()) {
            Record entry = null;
            try {
                entry = tri.next();
            } catch (DataFileException e) {
                Debug.logError(e, module);
            }
            GenericValue newValue = delegator.makeValue("ZipSalesTaxLookup");
            // PK fields
            newValue.set("zipCode", entry.getString("zipCode").trim());
            newValue.set("stateCode", entry.get("stateCode") != null ? entry.getString("stateCode").trim() : "_NA_");
            newValue.set("city", entry.get("city") != null ? entry.getString("city").trim() : "_NA_");
            newValue.set("county", entry.get("county") != null ? entry.getString("county").trim() : "_NA_");
            newValue.set("fromDate", parseDate(entry.getString("effectiveDate"), now));
            // non-PK fields
            newValue.set("countyFips", entry.get("countyFips"));
            newValue.set("countyDefault", entry.get("countyDefault"));
            newValue.set("generalDefault", entry.get("generalDefault"));
            newValue.set("insideCity", entry.get("insideCity"));
            newValue.set("geoCode", entry.get("geoCode"));
            newValue.set("stateSalesTax", entry.get("stateSalesTax"));
            newValue.set("citySalesTax", entry.get("citySalesTax"));
            newValue.set("cityLocalSalesTax", entry.get("cityLocalSalesTax"));
            newValue.set("countySalesTax", entry.get("countySalesTax"));
            newValue.set("countyLocalSalesTax", entry.get("countyLocalSalesTax"));
            newValue.set("comboSalesTax", entry.get("comboSalesTax"));
            newValue.set("stateUseTax", entry.get("stateUseTax"));
            newValue.set("cityUseTax", entry.get("cityUseTax"));
            newValue.set("cityLocalUseTax", entry.get("cityLocalUseTax"));
            newValue.set("countyUseTax", entry.get("countyUseTax"));
            newValue.set("countyLocalUseTax", entry.get("countyLocalUseTax"));
            newValue.set("comboUseTax", entry.get("comboUseTax"));
            try {
                delegator.createOrStore(newValue);
            } catch (GenericEntityException e) {
                Debug.logError(e, module);
                return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorWritingRecordsToTheDatabase", locale));
            }
            // console log
            Debug.logInfo(newValue.get("zipCode") + "/" + newValue.get("stateCode") + "/" + newValue.get("city") + "/" + newValue.get("county") + "/" + newValue.get("fromDate"), module);
        }
    }
    // load the data file
    DataFile rdf = null;
    try {
        rdf = DataFile.makeDataFile(UtilURL.fromResource(dataFile), ruleTable);
    } catch (DataFileException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderUnableToReadZipSalesDataFile", locale));
    }
    // locate the file to be imported
    URL rUrl = UtilURL.fromResource(ruleFileLocation);
    if (rUrl == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderUnableToLocateRuleFileFromLocation", UtilMisc.toMap("ruleFileLocation", ruleFileLocation), locale));
    }
    RecordIterator rri = null;
    try {
        rri = rdf.makeRecordIterator(rUrl);
    } catch (DataFileException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderProblemGettingTheRecordIterator", locale));
    }
    if (rri != null) {
        while (rri.hasNext()) {
            Record entry = null;
            try {
                entry = rri.next();
            } catch (DataFileException e) {
                Debug.logError(e, module);
            }
            if (UtilValidate.isNotEmpty(entry.getString("stateCode"))) {
                GenericValue newValue = delegator.makeValue("ZipSalesRuleLookup");
                // PK fields
                newValue.set("stateCode", entry.get("stateCode") != null ? entry.getString("stateCode").trim() : "_NA_");
                newValue.set("city", entry.get("city") != null ? entry.getString("city").trim() : "_NA_");
                newValue.set("county", entry.get("county") != null ? entry.getString("county").trim() : "_NA_");
                newValue.set("fromDate", parseDate(entry.getString("effectiveDate"), now));
                // non-PK fields
                newValue.set("idCode", entry.get("idCode") != null ? entry.getString("idCode").trim() : null);
                newValue.set("taxable", entry.get("taxable") != null ? entry.getString("taxable").trim() : null);
                newValue.set("shipCond", entry.get("shipCond") != null ? entry.getString("shipCond").trim() : null);
                try {
                    // using storeAll as an easy way to create/update
                    delegator.storeAll(UtilMisc.toList(newValue));
                } catch (GenericEntityException e) {
                    Debug.logError(e, module);
                    return ServiceUtil.returnError(UtilProperties.getMessage(resource_error, "OrderErrorWritingRecordsToTheDatabase", locale));
                }
                // console log
                Debug.logInfo(newValue.get("stateCode") + "/" + newValue.get("city") + "/" + newValue.get("county") + "/" + newValue.get("fromDate"), module);
            }
        }
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) DataFile(org.apache.ofbiz.datafile.DataFile) GenericValue(org.apache.ofbiz.entity.GenericValue) RecordIterator(org.apache.ofbiz.datafile.RecordIterator) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) DataFileException(org.apache.ofbiz.datafile.DataFileException) Record(org.apache.ofbiz.datafile.Record) Security(org.apache.ofbiz.security.Security) Timestamp(java.sql.Timestamp) URL(java.net.URL) UtilURL(org.apache.ofbiz.base.util.UtilURL)

Aggregations

URL (java.net.URL)1 Timestamp (java.sql.Timestamp)1 Locale (java.util.Locale)1 UtilURL (org.apache.ofbiz.base.util.UtilURL)1 DataFile (org.apache.ofbiz.datafile.DataFile)1 DataFileException (org.apache.ofbiz.datafile.DataFileException)1 Record (org.apache.ofbiz.datafile.Record)1 RecordIterator (org.apache.ofbiz.datafile.RecordIterator)1 Delegator (org.apache.ofbiz.entity.Delegator)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericValue (org.apache.ofbiz.entity.GenericValue)1 Security (org.apache.ofbiz.security.Security)1