Example usage for javax.xml.bind DatatypeConverter parseDate

List of usage examples for javax.xml.bind DatatypeConverter parseDate

Introduction

In this page you can find the example usage for javax.xml.bind DatatypeConverter parseDate.

Prototype

public static java.util.Calendar parseDate(String lexicalXSDDate) 

Source Link

Document

Converts the string argument into a Calendar value.

Usage

From source file:org.ojbc.adapters.analyticaldatastore.processor.IncidentReportProcessor.java

@Transactional
public void processReport(Document incidentReport) throws Exception {
    Incident incident = new Incident();

    String rootElemntName = incidentReport.getDocumentElement().getLocalName();
    if (INCIDENT_REPORT_ROOT_ELEMENT_NAME.equals(rootElemntName)) {
        incident.setRecordType('N');
    } else if (INCIDENT_REPORT_UPDATE_ROOT_ELEMENT_NAME.equals(rootElemntName)) {
        incident.setRecordType('U');
    }//from   w w w .j av  a2  s .c  o  m

    String reportingAgencyName = XmlUtils.xPathStringSearch(incidentReport, PATH_TO_LEXS_DIGEST
            + "/lexsdigest:EntityOrganization/nc:Organization[@s:id= " + PATH_TO_LEXS_DIGEST
            + " /lexsdigest:Associations/nc:ActivityReportingOrganizationAssociation[nc:ActivityReference/@s:ref="
            + PATH_TO_LEXS_DIGEST
            + "/lexsdigest:EntityActivity/nc:Activity[nc:ActivityCategoryText='Incident']/@s:id]/nc:OrganizationReference/@s:ref]/nc:OrganizationName");
    log.debug("Agency Name: " + reportingAgencyName);

    String reportingAgencyORI = XmlUtils.xPathStringSearch(incidentReport, PATH_TO_LEXS_DATA_ITEM_PACKAGE
            + "/lexs:PackageMetadata/lexs:DataOwnerMetadata/lexs:DataOwnerIdentifier/lexs:ORI");
    log.debug("Agency ORI: " + reportingAgencyORI);

    Integer reportingAgencyId = null;

    if (StringUtils.isNotBlank(reportingAgencyORI)) {
        reportingAgencyId = analyticalDatastoreDAO.searchForAgenyIDbyAgencyORI(reportingAgencyORI);

        if (reportingAgencyId == null) {
            throw new Exception("Valid Agency ORI required for incident.  Agency Name is: "
                    + reportingAgencyName + ", Agency ORI is: " + reportingAgencyORI);
        }

        incident.setReportingAgencyID(reportingAgencyId);
    } else {
        throw new Exception("Valid Agency ORI required for incident.  Agency Name is: "
                + StringUtils.trimToEmpty(reportingAgencyName) + ", Agency ORI is: "
                + StringUtils.trimToEmpty(reportingAgencyORI));
    }

    String incidentCaseNumber = XmlUtils.xPathStringSearch(incidentReport,
            PATH_TO_LEXS_DATA_ITEM_PACKAGE + "/lexs:PackageMetadata/lexs:DataItemID");
    log.debug("Incident Case Number: " + incidentCaseNumber);

    if (StringUtils.isNotBlank(incidentCaseNumber)) {
        incident.setIncidentCaseNumber(incidentCaseNumber);
    }

    //Check to see if incident(s) already exists
    List<Incident> incidents = analyticalDatastoreDAO
            .searchForIncidentsByIncidentNumberAndReportingAgencyID(incidentCaseNumber, reportingAgencyId);

    //if incidents exist, delete them prior to inserting a new one
    if (incidents.size() > 1) {
        throw new IllegalStateException(
                "Error condition. Duplicate records with same incident number and agency ID exists in database");
    }

    Integer incidentIDToReplace = null;

    if (incidents.size() == 1) {
        incidentIDToReplace = incidents.get(0).getIncidentID();
        incident.setIncidentID(incidentIDToReplace);
        log.debug("Incident ID to replace: " + incidentIDToReplace);

        analyticalDatastoreDAO.deleteIncident(incidents.get(0).getIncidentID());
    }

    String reportingSystem = XmlUtils.xPathStringSearch(incidentReport, PATH_TO_LEXS_DATA_ITEM_PACKAGE
            + "/lexs:PackageMetadata/lexs:DataOwnerMetadata/lexs:DataOwnerIdentifier/lexs:SystemID");
    incident.setReportingSystem(reportingSystem);

    //Look for either incident date/time or incident date field
    String incidentDateTimeAsString = XmlUtils.xPathStringSearch(incidentReport, PATH_TO_LEXS_DIGEST
            + "/lexsdigest:EntityActivity/nc:Activity/nc:ActivityDateRange/nc:StartDate/nc:DateTime");
    log.debug("Incident Date/Time: " + incidentDateTimeAsString);

    if (StringUtils.isNotEmpty(incidentDateTimeAsString)) {
        Calendar incidentDateTimeCal = DatatypeConverter.parseDateTime(incidentDateTimeAsString);
        Date incidentDateTime = incidentDateTimeCal.getTime();

        if (incidentDateTime != null) {
            incident.setIncidentDate(incidentDateTime);
        }

        Time incidentTime = new Time(incidentDateTime.getTime());
        log.debug("Incident Time: " + incidentTime.toString());

        if (incidentTime != null) {
            incident.setIncidentTime(incidentTime);
        }
    } else {
        String incidentDateAsString = XmlUtils.xPathStringSearch(incidentReport,
                PATH_TO_LEXS_DIGEST + "/lexsdigest:EntityActivity/nc:Activity/nc:ActivityDate/nc:Date");
        log.debug("Incident Date: " + incidentDateAsString);

        Calendar incidentDateCal = DatatypeConverter.parseDate(incidentDateAsString);
        Date incidentDate = incidentDateCal.getTime();

        if (incidentDate != null) {
            incident.setIncidentDate(incidentDate);
        }

    }

    String mapHorizontalCoordinateText = XmlUtils.xPathStringSearch(incidentReport,
            PATH_TO_LEXS_DATA_ITEM_PACKAGE
                    + "/lexs:StructuredPayload/inc-ext:IncidentReport/inc-ext:Location/nc:LocationMapLocation/nc:MapHorizontalCoordinateText");

    if (StringUtils.isNotBlank(mapHorizontalCoordinateText)) {
        //TODO: put this into a strategy
        try {
            mapHorizontalCoordinateText = updatedCoordinate(mapHorizontalCoordinateText);
            log.debug("Map horizontal coordinate text: " + mapHorizontalCoordinateText);

            BigDecimal longitude = new BigDecimal(mapHorizontalCoordinateText);
            incident.setIncidentLocationLongitude(longitude);
        } catch (Exception ex) {
            log.warn("Unable to set map horizontal text coordinate");
        }
    }

    String mapVerticalCoordinateText = XmlUtils.xPathStringSearch(incidentReport, PATH_TO_LEXS_DATA_ITEM_PACKAGE
            + "/lexs:StructuredPayload/inc-ext:IncidentReport/inc-ext:Location/nc:LocationMapLocation/nc:MapVerticalCoordinateText");

    if (StringUtils.isNotBlank(mapVerticalCoordinateText)) {
        //TODO: put this into a strategy
        try {
            mapVerticalCoordinateText = updatedCoordinate(mapVerticalCoordinateText);
            log.debug("Map vertical coordinate text: " + mapVerticalCoordinateText);

            BigDecimal latitude = new BigDecimal(mapVerticalCoordinateText);
            incident.setIncidentLocationLatitude(latitude);
        } catch (Exception ex) {
            log.warn("Unable to set map vertical text coordinate");
        }
    }

    String incidentLocationReference = XmlUtils.xPathStringSearch(incidentReport, PATH_TO_LEXS_DIGEST
            + "/lexsdigest:Associations/lexsdigest:IncidentLocationAssociation/nc:LocationReference/@s:ref");

    if (StringUtils.isNotEmpty(incidentLocationReference)) {
        Node locationNode = XmlUtils.xPathNodeSearch(incidentReport, PATH_TO_LEXS_DIGEST
                + "/lexsdigest:EntityLocation/nc:Location[@s:id='" + incidentLocationReference + "']");

        String streetFullText = XmlUtils.xPathStringSearch(locationNode,
                "nc:LocationAddress/nc:StructuredAddress/nc:LocationStreet/nc:StreetFullText");
        log.debug("Street Full Text: " + streetFullText);

        if (StringUtils.isNotBlank(streetFullText)) {
            incident.setIncidentLocationStreetAddress(streetFullText);
        } else {
            String streetNumberText = XmlUtils.xPathStringSearch(locationNode,
                    "nc:LocationAddress/nc:StructuredAddress/nc:LocationStreet/nc:StreetNumberText");
            log.debug("Street Number Text: " + streetNumberText);

            String streetName = XmlUtils.xPathStringSearch(locationNode,
                    "nc:LocationAddress/nc:StructuredAddress/nc:LocationStreet/nc:StreetName");
            log.debug("Street Name Text: " + streetName);

            String streetCategoryText = XmlUtils.xPathStringSearch(locationNode,
                    "nc:LocationAddress/nc:StructuredAddress/nc:LocationStreet/nc:StreetCategoryText");
            log.debug("Street Category Text: " + streetCategoryText);

            streetFullText = streetNumberText + streetName + streetFullText;
            log.debug("Street Full Text built from number, name and category: " + streetFullText);

            if (StringUtils.isNotBlank(streetFullText)) {
                incident.setIncidentLocationStreetAddress(streetFullText);
            }

        }

        String cityTown = XmlUtils.xPathStringSearch(locationNode,
                "nc:LocationAddress/nc:StructuredAddress/nc:LocationCityName");
        log.debug("City/Town: " + cityTown);

        if (StringUtils.isNotBlank(cityTown)) {
            incident.setIncidentLocationTown(cityTown);
        }

    }

    Integer incidentPk = analyticalDatastoreDAO.saveIncident(incident);

    //Add Incident Description Text
    processIncidentType(incidentReport, incidentPk);

    //Save circumstance codes
    processCircumstanceCodes(incidentReport, incidentPk);

    processArrests(incidentReport, incidentPk, reportingSystem);

}

From source file:org.openestate.io.core.XmlUtils.java

private static Calendar parseDate(String value, boolean tryDateTimeOnError) {
    value = StringUtils.trimToNull(value);
    if (value == null)
        return null;
    try {//ww  w . j a va  2s  .co  m
        return DatatypeConverter.parseDate(value);
    } catch (IllegalArgumentException ex) {
        //LOGGER.warn( "Can't parse value '" + value + "' as date!" );
        //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex );
    }
    try {
        Date date = DateUtils.parseDateStrictly(value,
                new String[] { "dd.MM.yyyy", "dd.MM.yy", "dd/MM/yyyy", "dd/MM/yy", "dd-MM-yyyy", "dd-MMM-yyyy",
                        "yyyy-MM-dd", "yyyy/MM/dd", "yyyy-D", "MM/yyyy", "MMM yyyy", "MMMMM yyyy", "yyyy" });
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    } catch (ParseException ex) {
        //LOGGER.warn( "Can't parse value '" + value + "' as date!" );
        //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex );
    }

    // try to parse the value as xsd:dateTime instead
    if (tryDateTimeOnError) {
        try {
            return XmlUtils.parseDateTime(value, false);
        } catch (IllegalArgumentException ex) {
            //LOGGER.warn( "Can't parse value '" + value + "' as datetime!" );
            //LOGGER.warn( "> " + ex.getLocalizedMessage(), ex );
        }
    }

    throw new IllegalArgumentException("Can't parse date value '" + value + "'!");
}

From source file:org.openestate.is24.restapi.utils.XmlUtils.java

/**
 * Read a {@link Calendar} value from XML.
 *
 * @param value/*  w w  w . ja  va  2  s  .  c o m*/
 * XML string
 *
 * @return
 * parsed value
 */
public static Calendar parseDate(String value) {
    value = StringUtils.trimToNull(value);
    if (value == null)
        return null;
    try {
        return DatatypeConverter.parseDate(value);
    } catch (Exception ex) {
        //LOGGER.debug( "Invalid xsd:date value '" + value + "'!" );
        try {
            Date date = DateUtils.parseDateStrictly(value,
                    new String[] { "dd.MM.yyyy", "dd.MM.yy", "dd/MM/yyyy", "dd/MM/yy", "dd-MM-yyyy",
                            "dd-MMM-yyyy", "yyyy-MM-dd", "yyyy/MM/dd", "yyyy-D", "MM/yyyy", "MMM yyyy",
                            "MMMMM yyyy", "yyyy" });
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            return cal;
        } catch (Exception ex2) {
            throw new IllegalArgumentException("Can't parse xsd:date value '" + value + "'!", ex2);
        }
    }
}

From source file:org.paxml.bean.DateTag.java

private Date parseDate() throws ParseException {
    final Object value = getValue();

    Date date;/*w  w  w.j a  v  a  2s .  co m*/
    if (value instanceof Date) {
        date = (Date) value;
    } else if (value instanceof Calendar) {
        date = ((Calendar) value).getTime();
    } else if (value instanceof java.sql.Date) {
        date = new Date(((java.sql.Date) value).getTime());
    } else if (value instanceof java.sql.Timestamp) {
        date = new Date(((java.sql.Timestamp) value).getTime());
    } else if (value == null || StringUtils.isBlank(value.toString())) {
        date = new Date();
    } else if (value instanceof Number) {
        date = new Date(((Number) value).longValue());
    } else {
        if (StringUtils.isBlank(fromFormat)) {
            date = DatatypeConverter.parseDate(value.toString()).getTime();
        } else {
            date = new SimpleDateFormat(fromFormat).parse(value.toString());
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Parsing from: " + value + " to: " + date + " where fromFormat=" + fromFormat);
    }
    return date;
}