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;
}
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;
}
Aggregations