/** * 执行操作 * * @param context * @param modelName * @param executeName * @param currentPath * @return */ public static ResponseObject execute( Context context, String modelName, String executeName, String currentPath) { LogUtil.info("-----------------------execute begin------------------------------------"); try { if (executeName == null || "".equals(executeName)) executeName = "_default"; Model model = context.getModel(modelName); Execute execute = model.getExecute(executeName); if (execute == null) { throw new ExecuteNotFoundException( "can not found Execute:" + executeName + ", on Model:" + modelName); } execute.setCurrentPath(currentPath); execute(context, model, execute); // 向客服端传送成功消息 ResponseObject ro = new ResponseObject(); ro.setSuccess(true); ro.setMessage("success"); ro.setOutParam(getOutParams(context, execute.getParams())); return ro; } finally { context.closeConns(); LogUtil.info("-----------------------execute end--------------------------------------"); } }
// 注册数据源 static void registSource(ConnectionProvide provide) { String provideName = provide.getProvideName(); synchronized (provides) { ConnectionProvide s = provides.get(provideName); if (s != null) { LogUtil.info("close provide (" + provides + "),release resource"); provides.remove(provideName); } provides.put(provideName, provide); } }
/** * 执行execute * * @param context * @param model * @param execute */ private static void execute(Context context, Model model, Execute execute) { String modelName = model.getModelName(); String currentPath = execute.getCurrentPath(); LogUtil.info( "Execute info (modelName:" + modelName + ", executeName:" + execute.getName() + ")"); // 把model、currentPath对象放入到 当前线程里 context.setCurrentPath(currentPath); context.setCurrentModel(modelName); // 设想sql查询参数 Collection<Param> params = execute.getParams().values(); for (Param nfParam : params) { setParam(nfParam, context, currentPath); } execute.execute(context, model.getConnectionProvide(context)); // 执行 }
/** * 查询 可以指定当前路径、是否自动分页、返回对象的查询 * * @param context * @param modelName * @param queryName * @param currentPath * @param autoPaging * @param obect * @return */ public static <T> QueryResponseObject<T> query( Context context, String modelName, String queryName, String currentPath, boolean autoPaging, Class<T> obect) { LogUtil.info("-----------------------query begin--------------------------------------"); try { if (queryName == null || "".equals(queryName)) queryName = "_default"; LogUtil.info("Query info (modelName:" + modelName + ", queryName:" + queryName + ")"); Model model = context.getModel(modelName); Query query = model.getQuery(queryName); if (query == null) { throw new QueryNotFoundException( "can not found Query:" + queryName + ", on Model:" + modelName); } if (context.isExport) { context.queryLimitSize = context.reportQueryLimitSize; } query.setCurrentPath(currentPath); // 初始化查询参数param Collection<Param> params = query.getParams().values(); for (Param nfParam : params) { setParam(nfParam, context, query.getCurrentPath()); } // 初始化查询过滤参数filter Collection<Filter> filters = query.getFilters().values(); for (Filter filter : filters) { setParam(filter, context, query.getCurrentPath()); Object value = filter.getValue(); if (value != null) { if (value instanceof String && !"".equals(value)) { filter.setActive(true); query.getParams().put(filter.getName(), filter); } else if (value instanceof Integer && (Integer) value != 0) { filter.setActive(true); query.getParams().put(filter.getName(), filter); } else if (value instanceof Long && (Long) value != 0) { filter.setActive(true); query.getParams().put(filter.getName(), filter); } else { filter.setActive(true); query.getParams().put(filter.getName(), filter); } } else if (filter.isActive()) { query.getParams().put(filter.getName(), filter); } } // 设想分页参数 long start = 0; if (autoPaging) { String startMessage = context.getString("param.start"); if (startMessage != null && startMessage != "") { start = Long.parseLong(startMessage); query.setStartWith(start); } String sizeMessage = context.getString("param.limit"); if (sizeMessage != null && sizeMessage != "") { int size = Integer.parseInt(sizeMessage); query.setPagerSize(size); } } // 查询数据,返回结果 String provideName = model.getConnectionProvide(context); List<T> datas = query.query(context, provideName, obect); QueryResponseObject<T> ro = new QueryResponseObject<T>(); ro.setDatas(datas); int dataSize = datas.size(); int pSize = query.getPagerSize(); if (autoPaging == false || pSize == 0 || (pSize > dataSize && start == 0)) { ro.setTotalCounts(datas.size()); } else { Connection conn = context.getConn(provideName); long totalCounts = query.countItems(conn); ro.setTotalCounts(totalCounts); } ro.setSuccess(true); ro.setMessage("success"); ro.setOutParam(getOutParams(context, query.getParams())); return ro; } finally { context.closeConns(); LogUtil.info("-----------------------query end----------------------------------------"); } }
/** * 批量执行操作 * * @param context * @param modelName * @param executeName * @param sourcePath * @return */ public static ResponseObject batchExecute( Context context, String modelName, String executeName, String sourcePath) { LogUtil.info("-----------------------batch execute begin------------------------------"); try { if (executeName == null || "".equals(executeName)) executeName = "addOrUpdate"; // 批量执行查找客户端数据的路径 String batchExecutePath = defaultBatchPath; Execute execute = null; Model model = context.getModel(modelName); // 查询数据,返回结果 if (sourcePath != null && !"".equals(sourcePath)) { batchExecutePath = sourcePath; } Object object = context.getData(batchExecutePath); int size = 0; if (object != null) { if (object instanceof Object[]) { Object[] objects = (Object[]) object; size = objects.length; } else if (object instanceof List) { List list = (List) object; size = list.size(); } else if (object instanceof Set) { Set set = (Set) object; size = set.size(); } } // 向客服端传送成功消息 ResponseObject ro = new ResponseObject(); ro.setSuccess(true); ro.setMessage("success"); if (size > 0) { for (int j = 0; j < size; j++) { String en = null; String currentPath = batchExecutePath + "[" + j + "]"; if ("addOrUpdate".equals(executeName)) { String status = context.getString(currentPath + "._status"); if (status != null) status = status.toUpperCase(); if ("NEW".equals(status)) { en = "add"; } else if ("OLD".equals(status)) { en = "update"; } else { throw new ExecuteNotFoundException( "cant not found (_status) field,can not found Execute"); } } else { en = executeName; } execute = model.getExecute(en); if (execute == null) { throw new ExecuteNotFoundException( "can not found Execute:" + executeName + ", on Model:" + modelName); } execute.setCurrentPath(currentPath); execute(context, model, execute); } ro.setOutParam(getOutParams(context, execute.getParams())); } return ro; } finally { context.closeConns(); LogUtil.info("-----------------------batch execute end--------------------------------"); } }