Commit 539d4a6f authored by 雍欢's avatar 雍欢

grid中也可以使用基于非SQL的easysearch

parent 45d2eb47
package com.huigou.uasp.bmp.common.easysearch;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* easysearch执行器。
*
* @author yonghuan
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface EasySearch {
String configType() default "system";
String queryName();
}
package com.huigou.uasp.bmp.common.easysearch.controller;
import com.huigou.uasp.bmp.common.easysearch.EasySearch;
import com.huigou.uasp.client.CommonController;
import com.huigou.util.SDO;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author yonghuan
*/
@Component
@Aspect
public class EasySearchDispatcher {
private final Map<String, Object[]> easySearchCache = new ConcurrentHashMap<>(16);
private final static Object[] EMPTY_EASY_SEARCH = new Object[0];
private static Method getSDOMethod;
static {
try {
getSDOMethod = CommonController.class.getDeclaredMethod("getSDO");
getSDOMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
}
}
@Around("execution(* com.huigou.uasp.bmp.common.easysearch.controller.EasySearchController.execute(..))")
public String intercept(ProceedingJoinPoint pjp) throws Throwable {
SDO sdo = (SDO) getSDOMethod.invoke(pjp.getTarget());
String configType = sdo.getProperty("configType", String.class, "system");
String queryName = sdo.getString("queryName");
Object[] config = getConfig(configType, queryName);
if (config != EMPTY_EASY_SEARCH) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
Object bean = config[0];
Method method = (Method) config[1];
Object model = method.invoke(bean, sdo);
HttpServletResponse response = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getResponse();
request.setAttribute("EASY_SEARCH_DATA", model);
request.getRequestDispatcher("/common/easySearch.jsp").forward(request, response);
return null;
}
return (String) pjp.proceed();
}
public Object[] getConfig(String configType, String queryName) {
String cacheKey = String.format("%s.%s", configType, queryName);
Object[] config = easySearchCache.get(cacheKey);
if (config != null) {
return config;
}
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// 获取到Servlet WebApplicationContext(不是Root WebApplicationContext)
WebApplicationContext wac = ((WebApplicationContext) request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
for (String beanName : wac.getBeanDefinitionNames()) {
Object bean = wac.getBean(beanName);
for (Method method : ReflectionUtils.getAllDeclaredMethods(bean.getClass())) {
EasySearch easySearch = method.getAnnotation(EasySearch.class);
if (easySearch != null && easySearch.configType().equals(configType) && easySearch.queryName().equals(queryName)) {
config = new Object[]{bean, method};
break;
}
}
if (config != null) {
break;
}
}
if (config == null) {
config = EMPTY_EASY_SEARCH;
}
easySearchCache.put(cacheKey, config);
return config;
}
}
package com.huigou.uasp.bmp.opm.controller;
import com.huigou.uasp.annotation.ControllerMapping;
import com.huigou.uasp.bmp.common.easysearch.EasySearch;
import com.huigou.uasp.bmp.common.easysearch.domain.model.EasySearchParse;
import com.huigou.uasp.bmp.common.easysearch.domain.model.QuerySchemeField;
import com.huigou.uasp.bmp.opm.application.CssIconParser;
......@@ -45,7 +46,6 @@ public class IconController extends CommonController implements ApplicationListe
private MemEasySearcher<Icon> memEasySearcher;
private List<String> iconCssFiles = Collections.emptyList();
private Collection<Icon> icons = Collections.emptyList();
private WebApplicationContext webApplicationContext;
private ServletContext servletContext;
@Value("${icon.cssFile}")
......@@ -60,8 +60,7 @@ public class IconController extends CommonController implements ApplicationListe
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
webApplicationContext = (WebApplicationContext) applicationContext;
servletContext = webApplicationContext.getServletContext();
servletContext = ((WebApplicationContext) applicationContext).getServletContext();
}
/**
......@@ -75,8 +74,16 @@ public class IconController extends CommonController implements ApplicationListe
/**
* 搜索图标。
*/
@Deprecated
public String easySearch() {
SDO sdo = getSDO();
Object model = executeEasySearch(sdo);
putAttribute(EASY_SEARCH_DATA, model);
return forward("/common/easySearch.jsp");
}
@EasySearch(queryName = "icon")
public Object executeEasySearch(SDO sdo) {
Integer intPage = sdo.getInteger("intPage", 1);
Integer pageSize = sdo.getInteger("pageSize");
PageRequest pageRequest = new PageRequest(intPage - 1, pageSize);
......@@ -86,9 +93,7 @@ public class IconController extends CommonController implements ApplicationListe
EasySearchParse easySearchParse = new EasySearchParse();
easySearchParse.setFields(fields);
easySearchParse.setWidth(200L);
Map<String, Object> model = memEasySearcher.search(icons, easySearchParse, pageRequest, (icon -> StringUtils.isBlank(paramValue) || icon.getCode().contains(paramValue)));
putAttribute(EASY_SEARCH_DATA, model);
return forward("/common/easySearch.jsp");
return memEasySearcher.search(icons, easySearchParse, pageRequest, (icon -> StringUtils.isBlank(paramValue) || icon.getCode().contains(paramValue)));
}
private void syncIconsInternal() {
......
package com.huigou.uasp.bmp.opm.controller;
import com.huigou.uasp.annotation.ControllerMapping;
import com.huigou.uasp.bmp.common.easysearch.EasySearch;
import com.huigou.uasp.bmp.common.easysearch.domain.model.EasySearchParse;
import com.huigou.uasp.bmp.common.easysearch.domain.model.QuerySchemeField;
import com.huigou.uasp.bmp.opm.application.MemEasySearcher;
......@@ -20,7 +21,6 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.huigou.util.Constants.EASY_SEARCH_DATA;
......@@ -55,8 +55,15 @@ public class LinkController extends CommonController implements ApplicationListe
/**
* 搜索链接
*/
@Deprecated
public String easySearch() {
SDO sdo = getSDO();
Object model = executeEasySearch(getSDO());
putAttribute(EASY_SEARCH_DATA, model);
return forward("/common/easySearch.jsp");
}
@EasySearch(queryName = "link")
public Object executeEasySearch(SDO sdo) {
Integer intPage = sdo.getInteger("intPage", 1);
Integer pageSize = sdo.getInteger("pageSize");
PageRequest pageRequest = new PageRequest(intPage - 1, pageSize);
......@@ -65,10 +72,7 @@ public class LinkController extends CommonController implements ApplicationListe
List<QuerySchemeField> fields = Collections.singletonList(new QuerySchemeField("地址", "url", "string", 400L));
easySearchParse.setFields(fields);
easySearchParse.setWidth(400L);
Map<String, Object> model = memSearcher.search(links, easySearchParse, pageRequest, (link -> StringUtils.isBlank(paramValue) || StringUtils.containsIgnoreCase(link.getUrl(), paramValue)));
putAttribute(EASY_SEARCH_DATA, model);
return forward("/common/easySearch.jsp");
return memSearcher.search(links, easySearchParse, pageRequest, (link -> StringUtils.isBlank(paramValue) || StringUtils.containsIgnoreCase(link.getUrl(), paramValue)));
}
private void syncLinksInternal() {
......
......@@ -42,4 +42,6 @@ email.host=https://mail.topsunit.com/EWS/Exchange.asmx
email.username=portal@topsunit.com
email.password=20180101
icon.cssFile=themes/fontawesome/css/font-awesome.min.css
\ No newline at end of file
icon.cssFile=themes/fontawesome/css/font-awesome.min.css
org.forceGenerateIdentifier=false
......@@ -188,7 +188,7 @@ function initDetail(div) {
_showIcon($(this).val());
});
_icon.searchbox({
url: '/icon/easySearch.load',
name: 'icon',
getParam: function () {
return {};
},
......@@ -219,7 +219,7 @@ function initDetail(div) {
$('#url-selector-container').remove();
$('<div id="url-selector-container"><input id="urlSelector" name="urlSelector" /></div>').insertBefore('#url_click');
$('#urlSelector').searchbox({
url: '/link/easySearch.load',
name: 'link',
getParam: function () {
return {};
},
......@@ -382,4 +382,4 @@ function syncLinks() {
null,
function (data) {
});
}
\ No newline at end of file
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment