com.glaf.dts.web.springmvc.MxDtsQueryController.java Source code

Java tutorial

Introduction

Here is the source code for com.glaf.dts.web.springmvc.MxDtsQueryController.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.glaf.dts.web.springmvc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.glaf.core.config.DatabaseConnectionConfig;
import com.glaf.core.config.ViewProperties;
import com.glaf.core.domain.ColumnDefinition;
import com.glaf.core.domain.Database;
import com.glaf.core.domain.QueryDefinition;
import com.glaf.core.query.DatabaseQuery;
import com.glaf.core.query.QueryDefinitionQuery;
import com.glaf.core.security.LoginContext;
import com.glaf.core.service.IDatabaseService;
import com.glaf.core.service.IQueryDefinitionService;
import com.glaf.core.service.ITableDefinitionService;
import com.glaf.core.util.LogUtils;
import com.glaf.core.util.Paging;
import com.glaf.core.util.ParamUtils;
import com.glaf.core.util.RequestUtils;
import com.glaf.core.util.Tools;

@Controller("/dts/query")
@RequestMapping("/dts/query")
public class MxDtsQueryController {
    protected static final Log logger = LogFactory.getLog(MxDtsQueryController.class);

    protected IDatabaseService databaseService;

    protected IQueryDefinitionService queryDefinitionService;

    protected ITableDefinitionService tableDefinitionService;

    @RequestMapping("/chooseQuery")
    public ModelAndView chooseQuery(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        String elementValue = request.getParameter("elementValue");
        QueryDefinitionQuery query = new QueryDefinitionQuery();
        query.setOrderBy(" E.TARGETTABLENAME_ asc, E.TITLE_ asc ");
        List<QueryDefinition> list = queryDefinitionService.list(query);
        request.setAttribute("unselecteds", list);

        if (StringUtils.isNotEmpty(elementValue)) {
            StringBuffer sb01 = new StringBuffer();
            StringBuffer sb02 = new StringBuffer();
            List<String> selecteds = new java.util.ArrayList<String>();
            for (QueryDefinition q : list) {
                if (StringUtils.contains(elementValue, q.getId())) {
                    selecteds.add(q.getId());
                    sb01.append(q.getId()).append(",");
                    sb02.append(q.getName()).append(",");
                }
            }
            if (sb01.toString().endsWith(",")) {
                sb01.delete(sb01.length() - 1, sb01.length());
            }
            if (sb02.toString().endsWith(",")) {
                sb02.delete(sb02.length() - 1, sb02.length());
            }
            request.setAttribute("selecteds", selecteds);
            request.setAttribute("queryIds", sb01.toString());
            request.setAttribute("queryNames", sb02.toString());
        }

        String x_view = ViewProperties.getString("dts.chooseQuery");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/bi/dts/query/chooseQuery", modelMap);
    }

    @RequestMapping("/edit")
    public ModelAndView edit(HttpServletRequest request, ModelMap modelMap) {
        LoginContext loginContext = RequestUtils.getLoginContext(request);
        Map<String, Object> params = RequestUtils.getParameterMap(request);
        RequestUtils.setRequestParameterToAttribute(request);
        logger.debug(params);

        DatabaseQuery query = new DatabaseQuery();
        query.active("1");
        List<Database> activeDatabases = new ArrayList<Database>();

        List<Database> databases = null;
        if (loginContext.isSystemAdministrator()) {
            databases = databaseService.list(query);
        } else {
            databases = databaseService.getDatabases(loginContext.getActorId());
        }

        if (databases != null && !databases.isEmpty()) {
            for (Database database : databases) {
                if ("1".equals(database.getActive())) {
                    DatabaseConnectionConfig config = new DatabaseConnectionConfig();
                    if (config.checkConfig(database)) {
                        activeDatabases.add(database);
                        logger.debug(database.getName() + " check connection ok.");
                    }
                }
            }
        }

        if (!activeDatabases.isEmpty()) {
            request.setAttribute("databases", databases);
        }

        String queryId = request.getParameter("queryId");

        QueryDefinition queryDefinition = null;
        if (queryId != null) {
            queryDefinition = queryDefinitionService.getQueryDefinition(queryId);
        }

        if (queryDefinition != null) {
            request.setAttribute("nodeId", queryDefinition.getNodeId());
            if (StringUtils.isNotEmpty(queryDefinition.getTargetTableName())) {
                List<ColumnDefinition> columns = tableDefinitionService
                        .getColumnDefinitionsByTableName(queryDefinition.getTargetTableName());
                request.setAttribute("columns", columns);
            }
        }

        if (queryDefinition == null) {
            queryDefinition = new QueryDefinition();
        }

        Tools.populate(queryDefinition, params);

        request.setAttribute("query", queryDefinition);

        QueryDefinitionQuery q = new QueryDefinitionQuery();
        List<QueryDefinition> queries = queryDefinitionService.list(q);
        request.setAttribute("queries", queries);

        String actionType = request.getParameter("actionType");
        if ("query".equals(actionType)) {
            Long databaseId = RequestUtils.getLong(request, "databaseId");
            Database db = databaseService.getDatabaseById(databaseId);
            if (db != null) {
                request.setAttribute("dbName", db.getName());
                request.setAttribute("database", db);
            }
        }

        String jx_view = request.getParameter("jx_view");

        if (StringUtils.isNotEmpty(jx_view)) {
            return new ModelAndView(jx_view, modelMap);
        }

        String x_view = ViewProperties.getString("dts_query.edit");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }
        return new ModelAndView("/bi/dts/query/edit", modelMap);
    }

    @RequestMapping("/json")
    @ResponseBody
    public byte[] json(HttpServletRequest request, ModelMap modelMap) throws IOException {
        LoginContext loginContext = RequestUtils.getLoginContext(request);
        Map<String, Object> params = RequestUtils.getParameterMap(request);

        QueryDefinitionQuery query = new QueryDefinitionQuery();
        Tools.populate(query, params);
        query.type(com.glaf.dts.util.Constants.DTS_TASK_TYPE);

        Long nodeId = RequestUtils.getLong(request, "nodeId");
        if (nodeId != null && nodeId > 0) {
            query.nodeId(nodeId);
        }

        query.setActorId(loginContext.getActorId());
        query.setLoginContext(loginContext);
        /**
         * 
         */
        if (!loginContext.isSystemAdministrator()) {
            // String actorId = loginContext.getActorId();
            // query.createBy(actorId);
        }

        String gridType = ParamUtils.getString(params, "gridType");
        if (gridType == null) {
            gridType = "easyui";
        }
        int start = 0;
        int limit = 10;
        String orderName = null;
        String order = null;

        int pageNo = ParamUtils.getInt(params, "page");
        limit = ParamUtils.getInt(params, "rows");
        start = (pageNo - 1) * limit;
        orderName = ParamUtils.getString(params, "sortName");
        order = ParamUtils.getString(params, "sortOrder");

        if (start < 0) {
            start = 0;
        }

        if (limit <= 0) {
            limit = Paging.DEFAULT_PAGE_SIZE;
        }

        JSONObject result = new JSONObject();
        int total = queryDefinitionService.getQueryDefinitionCountByQueryCriteria(query);
        if (total > 0) {
            result.put("total", total);
            result.put("totalCount", total);
            result.put("totalRecords", total);
            result.put("start", start);
            result.put("startIndex", start);
            result.put("limit", limit);
            result.put("pageSize", limit);

            if (StringUtils.isNotEmpty(orderName)) {
                query.setSortOrder(orderName);
                if (StringUtils.equals(order, "desc")) {
                    query.setSortOrder(" desc ");
                }
            }

            List<QueryDefinition> list = queryDefinitionService.getQueryDefinitionsByQueryCriteria(start, limit,
                    query);

            if (list != null && !list.isEmpty()) {
                JSONArray rowsJSON = new JSONArray();
                result.put("rows", rowsJSON);
                for (QueryDefinition queryDefinition : list) {
                    JSONObject rowJSON = queryDefinition.toJsonObject();
                    rowJSON.put("id", queryDefinition.getId());
                    rowJSON.put("queryId", queryDefinition.getId());
                    rowJSON.put("startIndex", ++start);
                    rowsJSON.add(rowJSON);
                }

            }
        } else {
            JSONArray rowsJSON = new JSONArray();
            result.put("rows", rowsJSON);
            result.put("total", total);
        }
        LogUtils.debug(result.toJSONString());
        return result.toJSONString().getBytes("UTF-8");
    }

    @RequestMapping
    public ModelAndView list(HttpServletRequest request, ModelMap modelMap) {
        String jx_view = request.getParameter("jx_view");
        RequestUtils.setRequestParameterToAttribute(request);
        if (StringUtils.isNotEmpty(jx_view)) {
            return new ModelAndView(jx_view, modelMap);
        }

        String x_view = ViewProperties.getString("dts_query.list");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }
        return new ModelAndView("/bi/dts/query/list", modelMap);
    }

    @RequestMapping("/queryTree")
    public ModelAndView queryTree(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);

        String x_view = ViewProperties.getString("dts.queryTree");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/bi/dts/query/query_tree", modelMap);
    }

    @javax.annotation.Resource
    public void setDatabaseService(IDatabaseService databaseService) {
        this.databaseService = databaseService;
    }

    @javax.annotation.Resource
    public void setQueryDefinitionService(IQueryDefinitionService queryDefinitionService) {
        this.queryDefinitionService = queryDefinitionService;
    }

    @javax.annotation.Resource
    public void setTableDefinitionService(ITableDefinitionService tableDefinitionService) {
        this.tableDefinitionService = tableDefinitionService;
    }

    @RequestMapping("/sqleditor")
    public ModelAndView sqleditor(HttpServletRequest request, ModelMap modelMap) {
        String jx_view = request.getParameter("jx_view");
        RequestUtils.setRequestParameterToAttribute(request);
        if (StringUtils.isNotEmpty(jx_view)) {
            return new ModelAndView(jx_view, modelMap);
        }

        String x_view = ViewProperties.getString("dts_query.sqleditor");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }
        return new ModelAndView("/bi/dts/query/sqleditor", modelMap);
    }

}