com.bstek.ureport.console.designer.DatasourceServletAction Maven / Gradle / Ivy
/*******************************************************************************
* Copyright 2017 Bstek
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
******************************************************************************/
package com.bstek.ureport.console.designer;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.PreparedStatementCreatorFactory;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterUtils;
import org.springframework.jdbc.core.namedparam.ParsedSql;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.jdbc.support.JdbcUtils;
import com.bstek.ureport.Utils;
import com.bstek.ureport.build.Context;
import com.bstek.ureport.console.RenderPageServletAction;
import com.bstek.ureport.console.exception.ReportDesignException;
import com.bstek.ureport.definition.dataset.Field;
import com.bstek.ureport.definition.datasource.BuildinDatasource;
import com.bstek.ureport.definition.datasource.DataType;
import com.bstek.ureport.expression.ExpressionUtils;
import com.bstek.ureport.expression.model.Expression;
import com.bstek.ureport.expression.model.data.ExpressionData;
import com.bstek.ureport.expression.model.data.ObjectExpressionData;
/**
* @author Jacky.gao
* @since 2017年2月6日
*/
public class DatasourceServletAction extends RenderPageServletAction {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method=retriveMethod(req);
if(method!=null){
invokeMethod(method, req, resp);
}else{
/*VelocityContext context = new VelocityContext();
context.put("contextPath", req.getContextPath());
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
Template template=ve.getTemplate("html/designer.html","utf-8");
PrintWriter writer=resp.getWriter();
template.merge(context, writer);
writer.close();*/
}
}
public void loadBuildinDatasources(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List datasources=new ArrayList();
for(BuildinDatasource datasource:Utils.getBuildinDatasources()){
datasources.add(datasource.name());
}
writeObjectToJson(resp, datasources);
}
public void loadMethods(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String beanId=req.getParameter("beanId");
Object obj=applicationContext.getBean(beanId);
Class> clazz=obj.getClass();
Method[] methods=clazz.getMethods();
List result=new ArrayList();
for(Method method:methods){
Class>[] types=method.getParameterTypes();
if(types.length!=3){
continue;
}
Class> typeClass1=types[0];
Class> typeClass2=types[1];
Class> typeClass3=types[2];
if(!String.class.isAssignableFrom(typeClass1)){
continue;
}
if(!String.class.isAssignableFrom(typeClass2)){
continue;
}
if(!Map.class.isAssignableFrom(typeClass3)){
continue;
}
result.add(method.getName());
}
writeObjectToJson(resp, result);
}
public void buildClass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String clazz=req.getParameter("clazz");
List result=new ArrayList();
try{
Class> targetClass=Class.forName(clazz);
PropertyDescriptor[] propertyDescriptors=PropertyUtils.getPropertyDescriptors(targetClass);
for(PropertyDescriptor pd:propertyDescriptors){
String name=pd.getName();
if("class".equals(name)){
continue;
}
result.add(new Field(name));
}
writeObjectToJson(resp, result);
}catch(Exception ex){
throw new ReportDesignException(ex);
}
}
public void buildDatabaseTables(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Connection conn=null;
ResultSet rs = null;
try{
conn=buildConnection(req);
DatabaseMetaData metaData = conn.getMetaData();
String url = metaData.getURL();
String schema = null;
if (url.toLowerCase().contains("oracle")) {
schema = metaData.getUserName();
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy