Search in sources :

Example 1 with DynamicHql

use of com.netsteadfast.greenstep.base.model.dynamichql.DynamicHql in project bamboobsc by billchen198318.

the class DynamicHqlUtils method loadResource.

public static DynamicHql loadResource(String resource) throws Exception {
    DynamicHql dynamicHql = resourceDataMap.get(resource);
    if (dynamicHql == null) {
        InputStream in = null;
        try {
            in = DynamicHqlUtils.class.getResourceAsStream("/dynamichql/" + resource);
            byte[] xmlBytes = IOUtils.toByteArray(in);
            JAXBContext jaxbContext = JAXBContext.newInstance(DynamicHql.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            dynamicHql = (DynamicHql) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(xmlBytes));
            resourceDataMap.put(resource, dynamicHql);
        } catch (IOException e) {
            logger.error(e.getMessage().toString());
            throw e;
        } finally {
            if (in != null) {
                IOUtils.closeQuietly(in);
            }
            in = null;
        }
    }
    return dynamicHql;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DynamicHql(com.netsteadfast.greenstep.base.model.dynamichql.DynamicHql) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 2 with DynamicHql

use of com.netsteadfast.greenstep.base.model.dynamichql.DynamicHql in project bamboobsc by billchen198318.

the class DynamicHqlUtils method process.

public static String process(String resource, String queryName, Map<String, Object> paramMap) throws Exception {
    DynamicHql dynamicHql = loadResource(resource);
    if (null == dynamicHql) {
        logger.error("no dynamic hql resource.");
        throw new Exception("no dynamic hql resource.");
    }
    String hql = "";
    for (int i = 0; i < dynamicHql.getQuery().size() && hql.length() < 1; i++) {
        Query queryObj = dynamicHql.getQuery().get(i);
        if (!queryObj.getName().equals(queryName)) {
            continue;
        }
        StringTemplateLoader templateLoader = new StringTemplateLoader();
        templateLoader.putTemplate(TEMPLATE_ID, queryObj.getContent());
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_21);
        cfg.setTemplateLoader(templateLoader);
        Template template = cfg.getTemplate(TEMPLATE_ID, Constants.BASE_ENCODING);
        Writer out = new StringWriter();
        template.process(paramMap, out);
        hql = out.toString();
    }
    if (StringUtils.isBlank(hql)) {
        logger.warn("hql is blank.");
    }
    return hql;
}
Also used : StringTemplateLoader(freemarker.cache.StringTemplateLoader) Query(com.netsteadfast.greenstep.base.model.dynamichql.Query) Configuration(freemarker.template.Configuration) StringWriter(java.io.StringWriter) DynamicHql(com.netsteadfast.greenstep.base.model.dynamichql.DynamicHql) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Template(freemarker.template.Template)

Aggregations

DynamicHql (com.netsteadfast.greenstep.base.model.dynamichql.DynamicHql)2 IOException (java.io.IOException)2 Query (com.netsteadfast.greenstep.base.model.dynamichql.Query)1 StringTemplateLoader (freemarker.cache.StringTemplateLoader)1 Configuration (freemarker.template.Configuration)1 Template (freemarker.template.Template)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 JAXBContext (javax.xml.bind.JAXBContext)1 Unmarshaller (javax.xml.bind.Unmarshaller)1