Search in sources :

Example 1 with Model

use of datawave.webservice.model.Model in project datawave by NationalSecurityAgency.

the class LoadModelFromXml method loadModelFromXml.

public static QueryModel loadModelFromXml(InputStream stream) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
    spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    spf.setNamespaceAware(true);
    Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(stream));
    JAXBContext ctx = JAXBContext.newInstance(Model.class);
    Unmarshaller um = ctx.createUnmarshaller();
    Model xmlModel = (Model) um.unmarshal(xmlSource);
    if (log.isDebugEnabled()) {
        log.debug(xmlModel.getName());
        for (FieldMapping fieldMapping : xmlModel.getFields()) {
            log.debug(fieldMapping.toString());
        }
    }
    QueryModel model = new QueryModel();
    for (FieldMapping mapping : xmlModel.getFields()) {
        switch(mapping.getDirection()) {
            case FORWARD:
                model.addTermToModel(mapping.getModelFieldName(), mapping.getFieldName());
                break;
            case REVERSE:
                model.addTermToReverseModel(mapping.getFieldName(), mapping.getModelFieldName());
                break;
            default:
                log.error("Unknown direction: " + mapping.getDirection());
        }
    }
    if (model.getForwardQueryMapping().isEmpty() && model.getReverseQueryMapping().isEmpty()) {
        throw new IllegalArgumentException("The resulting, loaded query model was empty.");
    }
    return model;
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) FieldMapping(datawave.webservice.model.FieldMapping) Model(datawave.webservice.model.Model) QueryModel(datawave.query.model.QueryModel) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) QueryModel(datawave.query.model.QueryModel) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Source(javax.xml.transform.Source) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 2 with Model

use of datawave.webservice.model.Model in project datawave by NationalSecurityAgency.

the class TestCardinalityConfiguration method init.

@Before
public void init() throws Exception {
    URL mUrl = TestCardinalityConfiguration.class.getResource("/models/CardinalityModel.xml");
    JAXBContext ctx = JAXBContext.newInstance(datawave.webservice.model.Model.class);
    Unmarshaller u = ctx.createUnmarshaller();
    Model MODEL = (datawave.webservice.model.Model) u.unmarshal(mUrl);
    QUERY_MODEL = new QueryModel();
    for (FieldMapping mapping : MODEL.getFields()) {
        if (mapping.getDirection().equals(Direction.FORWARD)) {
            QUERY_MODEL.addTermToModel(mapping.getModelFieldName(), mapping.getFieldName());
        } else {
            QUERY_MODEL.addTermToReverseModel(mapping.getFieldName(), mapping.getModelFieldName());
        }
    }
    reverseMap = new HashMap<>();
    reverseMap.put("FIELD_1", "UUID");
    reverseMap.put("FIELD_3", "R_LABEL");
    reverseMap.put("FIELD_4", "FIELD_4B");
    reverseMap.put("FIELD_2", "PROTOCOL");
}
Also used : FieldMapping(datawave.webservice.model.FieldMapping) Model(datawave.webservice.model.Model) QueryModel(datawave.query.model.QueryModel) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) QueryModel(datawave.query.model.QueryModel) URL(java.net.URL) Before(org.junit.Before)

Aggregations

QueryModel (datawave.query.model.QueryModel)2 FieldMapping (datawave.webservice.model.FieldMapping)2 Model (datawave.webservice.model.Model)2 JAXBContext (javax.xml.bind.JAXBContext)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 URL (java.net.URL)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1 Source (javax.xml.transform.Source)1 SAXSource (javax.xml.transform.sax.SAXSource)1 Before (org.junit.Before)1 InputSource (org.xml.sax.InputSource)1