Authored by henry

remove indexController

package com.yoho.search.restapi;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yoho.search.dal.model.ProductIndex;
import com.yoho.search.dal.service.ProductIndexService;
import com.yoho.search.index.service.IndexService;
import com.yoho.search.utils.ISearchConstans;
/**
* 索引管理相关请求
* @author YOHO
*
*/
@Controller
public class IndexController {
private static final Logger logger = LoggerFactory.getLogger(IndexController.class);
@Autowired
private IndexService indexService;
@Autowired
private ProductIndexService productIndexService;
@Autowired
private AmqpTemplate amqpTemplate;
public static final String DEFAULT_CHARSET = "UTF-8";
@RequestMapping(value = "/index/create/{indexName}")
@ResponseBody
public Map create(@PathVariable String indexName, HttpServletRequest request) {
try{
indexService.createIndex(indexName, true);
}catch(Exception e){
return getResultMap(400, "create " + indexName + " error: " + e.getMessage());
}
return getResultMap(200, "create " + indexName + " success");
}
@RequestMapping(value = "/test")
@ResponseBody
public Map test(HttpServletRequest request) {
return getResultMap(200, "create " + "test" + " success");
}
@RequestMapping(value = "/index/exist/{indexName}")
@ResponseBody
public Map exist(@PathVariable String indexName, HttpServletRequest request) {
try{
Boolean bool = indexService.indexExists(indexName);
if (bool) return getResultMap(200, indexName + "exist ");
}catch(Exception e){
return getResultMap(400, indexName + "not exist " + "error: " + e.getMessage());
}
return getResultMap(200, indexName + " not exist ");
}
@RequestMapping(value = "/index/rebuild/{indexName}")
@ResponseBody
public Map rebuild(@PathVariable String indexName, HttpServletRequest request) {
try{
boolean isExist = indexService.indexExists(indexName);
if (isExist) {
indexService.rebuild(indexName);
} else {
indexService.createIndex(indexName, true);
}
}catch(Exception e){
return getResultMap(400, "rebuild " + indexName +" error: " + e.getMessage());
}
return getResultMap(200, "rebuild " + indexName + " success");
}
@RequestMapping(value = "/index/update/{indexName}/{id}")
@ResponseBody
public Map updateIndex(@PathVariable String indexName, @PathVariable Integer id, HttpServletRequest request) {
try{
if (indexName.equals(ISearchConstans.INDEX_NAME_PRODUCT_INDEX)) {
ProductIndex productIndex = productIndexService.getById(id);
if (productIndex != null) {
indexService.updateIndexData(ISearchConstans.INDEX_NAME_PRODUCT_INDEX, id + "", productIndexService.beanToMap(productIndex));
return getResultMap(200, "update " + indexName + " id=" + id + " success");
}
}
}catch(Exception e){
return getResultMap(400, "update " + indexName + " id=" + id +" error: " + e.getMessage());
}
return getResultMap(200, "update " + indexName + " id=" + id + " error: No indexName");
}
@RequestMapping(method = RequestMethod.POST, value = "/index/update/{indexName}")
@ResponseBody
public Map update(@PathVariable String indexName, @RequestBody String json, HttpServletRequest request) {
//通过indexName得到消息地址,然后发消息
//使用这个接口需要了解消息的格式
String channel = ISearchConstans.REDIS_CHANNEL_PRIFIX + indexName;
amqpTemplate.convertAndSend(channel, createMessage(json));
logger.info("[func=sendMessage][step=success][indexName={}][msg={}]", indexName, json);
return getResultMap(200, "update " + indexName + " success");
}
private Message createMessage(String json) {
byte[] bytes = null;
try {
bytes = json.getBytes(DEFAULT_CHARSET);
} catch (UnsupportedEncodingException e) {
throw new MessageConversionException("Failed to convert Message content", e);
}
MessageProperties messageProperties = new MessageProperties();
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
messageProperties.setContentEncoding(DEFAULT_CHARSET);
if (bytes != null) {
messageProperties.setContentLength(bytes.length);
}
return new Message(bytes, messageProperties);
}
private Map<String, Object> getResultMap(final int code, final String message) {
Map<String, Object> rtnMap = new HashMap<String, Object>();
rtnMap.put("code", code);
rtnMap.put("msg", message);
return rtnMap;
}
}