Skip to content

Commit

Permalink
1、添加了SolrDataProvider和其依赖包的maven配置
Browse files Browse the repository at this point in the history
2、优化了输入框模板,并更新了query.vm和datasource.vm模板
3、修改了三级地图返回箭头图片路径的BUG
  • Loading branch information
junjiem committed Jul 8, 2017
1 parent 12d7b77 commit 167946c
Show file tree
Hide file tree
Showing 15 changed files with 394 additions and 20 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@
<artifactId>postgresql</artifactId>
<version>9.3-1104-jdbc41</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.3</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/cboard/controller/DashboardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public Set<String> getProviderList() {
}

@RequestMapping(value = "/getConfigView")
public String getConfigView(@RequestParam(name = "type") String type) {
return DataProviderViewManager.getQueryView(type);
public String getConfigView(@RequestParam(name = "type") String type, @RequestParam(name = "page") String page) {
return DataProviderViewManager.getQueryView(type, page);
}

@RequestMapping(value = "/getDatasourceView")
Expand Down
33 changes: 27 additions & 6 deletions src/main/java/org/cboard/dataprovider/DataProviderViewManager.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.cboard.dataprovider;

import org.apache.velocity.app.Velocity;
import org.apache.velocity.runtime.RuntimeConstants;
import org.cboard.dataprovider.annotation.DatasourceParameter;
import org.cboard.dataprovider.annotation.QueryParameter;
import com.google.common.collect.Ordering;
import org.apache.commons.io.output.StringBuilderWriter;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.cboard.dataprovider.annotation.DatasourceParameter;
import org.cboard.dataprovider.annotation.QueryParameter;
import org.reflections.ReflectionUtils;

import java.lang.reflect.Field;
Expand All @@ -34,7 +35,7 @@ public class DataProviderViewManager {

private static Map<String, String> rendered = new HashMap<>();

public static String getQueryView(String type) {
public static String getQueryView(String type, String page) {
Class clz = DataProviderManager.getDataProviderClass(type);

Set<Field> fieldSet = ReflectionUtils.getAllFields(clz, ReflectionUtils.withAnnotation(QueryParameter.class));
Expand All @@ -49,7 +50,26 @@ public static String getQueryView(String type) {
param.put("label", queryParameter.label());
param.put("type", queryParameter.type().toString());
param.put("name", (String) field.get(o));
lists.add(param);
param.put("value", queryParameter.value());
param.put("placeholder", queryParameter.placeholder());
param.put("required", String.valueOf(queryParameter.required()));

/*
不同页面显示不同输入框
*/
String pageType = queryParameter.pageType();
if (pageType.contains("all") || StringUtils.isBlank(page)) {
lists.add(param);
} else if ("test.html".equals(page) && pageType.contains("test")) {
lists.add(param);
} else if ("dataset.html".equals(page) && pageType.contains("dataset")) {
lists.add(param);
} else if ("widget.html".equals(page) && pageType.contains("widget")) {
lists.add(param);
} else {
continue;
}

}
VelocityContext context = new VelocityContext();
context.put("params", lists);
Expand Down Expand Up @@ -77,6 +97,7 @@ public static String getDatasourceView(String type) {
param.put("label", datasourceParameter.label());
param.put("type", datasourceParameter.type().toString());
param.put("name", (String) field.get(o));
param.put("placeholder", datasourceParameter.placeholder());
param.put("options", datasourceParameter.options());
lists.add(param);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.cboard.dataprovider.config.DimensionConfig;
import org.cboard.dataprovider.result.AggregateResult;
import org.cboard.dataprovider.result.ColumnIndex;
import org.cboard.exception.CBoardException;
import org.cboard.util.NaturalOrderComparator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -82,7 +83,11 @@ private Map<String, Integer> getColumnIndex(String[][] data) {
@Override
public String[] getColumn() throws Exception {
String[][] data = rawDataCache.get(getCacheKey());
return data[0];
try {
return data[0];
} catch (Exception e) {
throw new CBoardException("data set is null");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

int order() default 0;

// Init value
String value() default "";

String placeholder() default "";

String[] options() default "N/A";

enum Type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@

int order() default 0;

// Init value
String value() default "";

String placeholder() default "";

boolean required() default false;

// 页面类型,all、test、dataset、widget,多个可用逗号隔开,默认:all
String pageType() default "all";

enum Type {
Input("input"), TextArea("textarea");
Input("input"), TextArea("textarea"), TextArea2("textarea2"), Number("number");

private String name;

Type(String name) {
Expand Down
214 changes: 214 additions & 0 deletions src/main/java/org/cboard/solr/SolrDataProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package org.cboard.solr;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.LBHttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.cboard.dataprovider.DataProvider;
import org.cboard.dataprovider.annotation.DatasourceParameter;
import org.cboard.dataprovider.annotation.ProviderName;
import org.cboard.dataprovider.annotation.QueryParameter;
import org.cboard.exception.CBoardException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* Created by JunjieM on 2017-7-7.
*/
@ProviderName(name = "Solr")
public class SolrDataProvider extends DataProvider {

private static final Logger LOG = LoggerFactory.getLogger(SolrDataProvider.class);

@Value("${dataprovider.resultLimit:300000}")
private int resultLimit;

@DatasourceParameter(label = "Solr Servers", placeholder = "<ip>:<port>,[<ip>:<port>]...", type = DatasourceParameter.Type.Input, order = 1)
private String solrServers = "solrServers";

@QueryParameter(label = "Collection", pageType = "test,dataset,widget", required = true, type = QueryParameter.Type.Input, order = 1)
private String collection = "collection";

@QueryParameter(label = "q", pageType = "dataset,widget", value = "*:*", placeholder = "*:*|<fieldName>:<fieldValue>[ <AND|OR> <fieldName>:<fieldValue>]...", required = true, type = QueryParameter.Type.TextArea2, order = 2)
private String q = "q";

@QueryParameter(label = "fq", pageType = "dataset,widget", placeholder = "<fieldName>:<fieldValue>[,<fieldName>:<fieldValue>]...", type = QueryParameter.Type.Input, order = 3)
private String fq = "fq";

@QueryParameter(label = "sort", pageType = "dataset,widget", placeholder = "<fieldName> <ASC|DESC>[,<fieldName> <ASC|DESC>]...", type = QueryParameter.Type.Input, order = 4)
private String sort = "sort";

@QueryParameter(label = "start", pageType = "dataset,widget", value = "0", required = true, type = QueryParameter.Type.Number, order = 5)
private String start = "start";

@QueryParameter(label = "rows", pageType = "dataset,widget", value = "10", required = true, type = QueryParameter.Type.Number, order = 6)
private String rows = "rows";

@QueryParameter(label = "fl", pageType = "dataset,widget", placeholder = "*|<fieldName>[,<fieldName>]...", type = QueryParameter.Type.Input, order = 7)
private String fl = "fl";

@DatasourceParameter(label = "{{'DATAPROVIDER.JDBC.POOLEDCONNECTION'|translate}}", type = DatasourceParameter.Type.Checkbox, order = 5)
private String pooled = "pooled";

private static Map<String, SolrServerPoolFactory> poolMap;

@Override
public boolean doAggregationInDataSource() {
return false;
}

private synchronized SolrServerPoolFactory getSolrServerPoolFactory(String solrServers, String collectionName) {
String poolKey = getPoolKey(solrServers, collectionName);
if (poolMap == null) {
poolMap = new HashMap();
}
SolrServerPoolFactory factory = poolMap.get(poolKey);
if (factory == null) {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(10);
config.setLifo(true);
config.setMinIdle(1);
config.setMaxTotal(10);
config.setMaxWaitMillis(3000);
config.setTimeBetweenEvictionRunsMillis(30000);
config.setTestWhileIdle(true);
config.setTestOnBorrow(false);
config.setTestOnReturn(false);
factory = new SolrServerPoolFactory(config, solrServers, collectionName);
}
poolMap.put(poolKey, factory);
return factory;
}

private SolrServer getConnection(String solrServers, String collectionName) {
String usePool = dataSource.get(pooled);
SolrServer solrServer = null;
if (usePool != null && "true".equals(usePool)) {
solrServer = getSolrServerPoolFactory(solrServers, collectionName).getConnection();
} else {
solrServer = getSolrServer(solrServers, collectionName);
}
return solrServer;
}

private void releaseConnection(String solrServers, String collectionName, SolrServer solrServer) {
getSolrServerPoolFactory(solrServers, collectionName).releaseConnection(solrServer);
}

private SolrQuery getSolrQuery() {
SolrQuery solrQuery = new SolrQuery();

String q = StringUtils.isBlank(query.get("q")) ? "*:*" : query.get("q");
String fqs = StringUtils.isBlank(query.get("fq")) ? "" : query.get("fq");
String fl = StringUtils.isBlank(query.get("fl")) ? "" : query.get("fl");
String sort = StringUtils.isBlank(query.get("sort")) ? "" : query.get("sort");
int start = StringUtils.isBlank(query.get("start")) ? 0 : Integer.parseInt(query.get("start"));
int rows = StringUtils.isBlank(query.get("rows")) ? 10 : Integer.parseInt(query.get("rows"));

solrQuery.set("q", q);
String[] fqArr = fqs.split(",");
for (String fq : fqArr)
solrQuery.set("fq", fq.trim());
solrQuery.set("fl", fl);
solrQuery.set("sort", sort);
solrQuery.setStart(start);
solrQuery.setRows(rows);

LOG.debug("solrQuery=" + solrQuery.toString());
return solrQuery;
}

private QueryResponse getQueryResponse(String solrServers, String collectionName) {
SolrServer solrServer = null;
QueryResponse res = null;
try {
solrServer = getConnection(solrServers, collectionName);
res = solrServer.query(getSolrQuery());
} catch (SolrServerException e) {
e.printStackTrace();
} finally {
if (solrServer != null) {
releaseConnection(solrServers, collectionName, solrServer);
}
}
return res;
}

private String getPoolKey(String solrServers, String collectionName) {
return Hashing.md5().newHasher().putString(solrServers + "_" + collectionName, Charsets.UTF_8).hash().toString();
}

private String getCacheKey() {
return Hashing.md5().newHasher().putString(JSONObject.toJSON(dataSource).toString() + JSONObject.toJSON(query).toString(), Charsets.UTF_8).hash().toString();
}

private SolrServer getSolrServer(String solrServers, String collectionName) {
String[] tempServers = solrServers.split(",");
String[] servers = new String[tempServers.length];
for (int i = 0; i < tempServers.length; i++) {
servers[i] = "http://" + tempServers[i] + "/solr/" + collectionName;
}
SolrServer solrServer = null;
try {
solrServer = new LBHttpSolrServer(servers);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return solrServer;
}

@Override
public String[][] getData() throws Exception {
String solrServers = dataSource.get("solrServers");
if (StringUtils.isBlank(solrServers))
throw new CBoardException("Datasource config Solr Servers can not be empty.");
String collectionName = query.get("collection");
if (StringUtils.isBlank(collectionName))
throw new CBoardException("Collection can not be empty.");

QueryResponse qs = getQueryResponse(solrServers, collectionName);

if (qs == null || qs.getResults().size() == 0) {
return new String[0][0];
}

SolrDocumentList results = qs.getResults();
Set<Map.Entry<String, Object>> entrySet = results.get(0).entrySet();
String[][] strings = new String[results.size() + 1][entrySet.size()];

// 字段行
int col = 0;
for (Map.Entry<String, Object> entry : entrySet) {
strings[0][col] = entry.getKey();
col++;
}

if (results.size() > resultLimit) {
throw new CBoardException("Cube result count is greater than limit " + resultLimit);
}

// 数据集
for (int i = 1; i <= results.size(); i++) {
int j = 0;
for (Map.Entry<String, Object> e : entrySet) {
strings[i][j] = String.valueOf(results.get(i - 1).get(e.getKey()));
j++;
}
}

return strings;
}
}
Loading

0 comments on commit 167946c

Please sign in to comment.