Commit be67e60514d16d4e1c55978658c0f02d60f036ae

Authored by baohanddd
1 parent 7dc386392a

add mongo sync

Showing 7 changed files with 334 additions and 2 deletions

platform-biz-patient-service/src/main/java/com/lyms/platform/biz/service/MongoSyncService.java View file @ be67e60
  1 +package com.lyms.platform.biz.service;
  2 +
  3 +import com.lyms.platform.common.dao.operator.MongoCondition;
  4 +import com.lyms.platform.common.dao.operator.MongoOper;
  5 +import com.lyms.platform.common.dao.operator.MongoQuery;
  6 +import com.lyms.platform.common.utils.*;
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.data.mongodb.core.MongoTemplate;
  9 +import org.springframework.data.mongodb.core.query.Update;
  10 +import org.springframework.stereotype.Service;
  11 +import org.springframework.util.Assert;
  12 +
  13 +/**
  14 + * Created by Administrator on 2016/9/13 0013.
  15 + */
  16 +@Service("mongoSyncService")
  17 +public class MongoSyncService {
  18 +
  19 + /**
  20 + * spring mongodb 集成操作类
  21 + */
  22 + @Autowired
  23 + protected MongoTemplate mongoTemplate;
  24 +
  25 + public static String mongo_crypto_key = Config.getItem("mongo_crypto_key", "0");
  26 +
  27 + public boolean syncData(String action, String id, String className, String json) {
  28 + try {
  29 + if ("ADD".equals(action)) {
  30 + Object entity = JsonUtil.str2Obj(LymsEncodeUtil.aesDecrypt(json, mongo_crypto_key), Class.forName(LymsEncodeUtil.aesDecrypt(className, mongo_crypto_key)));
  31 + Assert.notNull(entity, "execute insert method must not null.");
  32 + mongoTemplate.save(entity);
  33 + return true;
  34 + } else if ("UPDATE".equals(action)) {
  35 + Class cla = Class.forName(LymsEncodeUtil.aesDecrypt(className, mongo_crypto_key));
  36 + Object obj = JsonUtil.str2Obj(LymsEncodeUtil.aesDecrypt(json, mongo_crypto_key), cla);
  37 + Update update = MongoConvertHelper.convertToNativeUpdate(ReflectionUtils.getUpdateField(obj));
  38 + Assert.notNull(update, "execute update method must not null.");
  39 + mongoTemplate.updateMulti(new MongoQuery(new MongoCondition("id", LymsEncodeUtil.aesDecrypt(id, mongo_crypto_key), MongoOper.IS)).convertToMongoQuery(), update, cla);
  40 + return true;
  41 + } else if ("DELETE".equals(action)) {
  42 + Class cla = Class.forName(LymsEncodeUtil.aesDecrypt(className, mongo_crypto_key));
  43 + mongoTemplate.findAllAndRemove(new MongoQuery(new MongoCondition("id", LymsEncodeUtil.aesDecrypt(id, mongo_crypto_key), MongoOper.IS)).convertToMongoQuery(), cla);
  44 + return true;
  45 + }
  46 + return false;
  47 + } catch (Exception e) {
  48 + e.printStackTrace();
  49 + return false;
  50 + }
  51 + }
  52 +
  53 +
  54 +}
platform-common/src/main/java/com/lyms/platform/common/dao/BaseMongoDAOImpl.java View file @ be67e60
... ... @@ -2,6 +2,7 @@
2 2  
3 3 import com.lyms.platform.common.dao.operator.Page;
4 4 import com.lyms.platform.common.utils.MongoConvertHelper;
  5 +import com.lyms.platform.common.utils.MongoSyncUtil;
5 6 import com.lyms.platform.common.utils.ReflectionUtils;
6 7 import org.apache.commons.collections.CollectionUtils;
7 8 import org.springframework.beans.factory.annotation.Autowired;
8 9  
9 10  
... ... @@ -47,13 +48,19 @@
47 48 Update update = MongoConvertHelper
48 49 .convertToNativeUpdate(ReflectionUtils.getUpdateField(obj));
49 50 Assert.notNull(update, "execute update method must not null.");
  51 +// query.getQueryObject().get()
50 52 mongoTemplate.updateMulti(query, update, this.getEntityClass());
  53 + Object id = query.getQueryObject().get("id");
  54 + if (id != null) {
  55 + MongoSyncUtil.sync("UPDATE", obj, id.toString());
  56 + }
51 57 }
52 58  
53 59 @Override
54 60 public T save(T entity) {
55 61 Assert.notNull(entity, "execute insert method must not null.");
56 62 mongoTemplate.insert(entity);
  63 + MongoSyncUtil.sync("ADD", entity, "");
57 64 return entity;
58 65 }
59 66  
... ... @@ -97,6 +104,10 @@
97 104 }
98 105  
99 106 public void delete(Query query) {
  107 + Object id = query.getQueryObject().get("id");
  108 + if (id != null) {
  109 + MongoSyncUtil.sync("DELETE", "", id.toString());
  110 + }
100 111 mongoTemplate.findAllAndRemove(query, this.getEntityClass());
101 112 }
102 113 public void delete(Query query,Class clazz) {
platform-common/src/main/java/com/lyms/platform/common/utils/LymsEncodeUtil.java View file @ be67e60
  1 +package com.lyms.platform.common.utils;
  2 +
  3 +import org.apache.commons.lang.*;
  4 +import sun.misc.BASE64Decoder;
  5 +import sun.misc.BASE64Encoder;
  6 +
  7 +import javax.crypto.Cipher;
  8 +import javax.crypto.KeyGenerator;
  9 +import javax.crypto.spec.SecretKeySpec;
  10 +import java.math.BigInteger;
  11 +import java.security.MessageDigest;
  12 +import java.security.SecureRandom;
  13 +
  14 +/**
  15 + * Created by Administrator on 2016/9/13 0013.
  16 + */
  17 +public class LymsEncodeUtil {
  18 +
  19 + public static void main(String[] args) throws Exception {
  20 + String content = "我爱你";
  21 + System.out.println("加密前:" + content);
  22 +
  23 + String key = "123456";
  24 + System.out.println("加密密钥和解密密钥:" + key);
  25 +
  26 + String encrypt = aesEncrypt(content, key);
  27 + System.out.println("加密后:" + encrypt);
  28 +
  29 + String decrypt = aesDecrypt(encrypt, key);
  30 + System.out.println("解密后:" + decrypt);
  31 + }
  32 +
  33 + /**
  34 + * 将byte[]转为各种进制的字符串
  35 + * @param bytes byte[]
  36 + * @param radix 可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制
  37 + * @return 转换后的字符串
  38 + */
  39 + public static String binary(byte[] bytes, int radix){
  40 + return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
  41 + }
  42 +
  43 + /**
  44 + * base 64 encode
  45 + * @param bytes 待编码的byte[]
  46 + * @return 编码后的base 64 code
  47 + */
  48 + public static String base64Encode(byte[] bytes){
  49 + return new BASE64Encoder().encode(bytes);
  50 + }
  51 +
  52 + /**
  53 + * base 64 decode
  54 + * @param base64Code 待解码的base 64 code
  55 + * @return 解码后的byte[]
  56 + * @throws Exception
  57 + */
  58 + public static byte[] base64Decode(String base64Code) throws Exception{
  59 + return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
  60 + }
  61 +
  62 + /**
  63 + * 获取byte[]的md5值
  64 + * @param bytes byte[]
  65 + * @return md5
  66 + * @throws Exception
  67 + */
  68 + public static byte[] md5(byte[] bytes) throws Exception {
  69 + MessageDigest md = MessageDigest.getInstance("MD5");
  70 + md.update(bytes);
  71 +
  72 + return md.digest();
  73 + }
  74 +
  75 + /**
  76 + * 获取字符串md5值
  77 + * @param msg
  78 + * @return md5
  79 + * @throws Exception
  80 + */
  81 + public static byte[] md5(String msg) throws Exception {
  82 + return StringUtils.isEmpty(msg) ? null : md5(msg.getBytes());
  83 + }
  84 +
  85 + /**
  86 + * 结合base64实现md5加密
  87 + * @param msg 待加密字符串
  88 + * @return 获取md5后转为base64
  89 + * @throws Exception
  90 + */
  91 + public static String md5Encrypt(String msg) throws Exception{
  92 + return StringUtils.isEmpty(msg) ? null : base64Encode(md5(msg));
  93 + }
  94 +
  95 + /**
  96 + * AES加密
  97 + * @param content 待加密的内容
  98 + * @param encryptKey 加密密钥
  99 + * @return 加密后的byte[]
  100 + * @throws Exception
  101 + */
  102 + public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
  103 + KeyGenerator kgen = KeyGenerator.getInstance("AES");
  104 + kgen.init(128, new SecureRandom(encryptKey.getBytes()));
  105 +
  106 + Cipher cipher = Cipher.getInstance("AES");
  107 + cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
  108 +
  109 + return cipher.doFinal(content.getBytes("utf-8"));
  110 + }
  111 +
  112 + /**
  113 + * AES加密为base 64 code
  114 + * @param content 待加密的内容
  115 + * @param encryptKey 加密密钥
  116 + * @return 加密后的base 64 code
  117 + * @throws Exception
  118 + */
  119 + public static String aesEncrypt(String content, String encryptKey) throws Exception {
  120 + return base64Encode(aesEncryptToBytes(content, encryptKey));
  121 + }
  122 +
  123 + /**
  124 + * AES解密
  125 + * @param encryptBytes 待解密的byte[]
  126 + * @param decryptKey 解密密钥
  127 + * @return 解密后的String
  128 + * @throws Exception
  129 + */
  130 + public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
  131 + KeyGenerator kgen = KeyGenerator.getInstance("AES");
  132 + kgen.init(128, new SecureRandom(decryptKey.getBytes()));
  133 +
  134 + Cipher cipher = Cipher.getInstance("AES");
  135 + cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));
  136 + byte[] decryptBytes = cipher.doFinal(encryptBytes);
  137 +
  138 + return new String(decryptBytes);
  139 + }
  140 +
  141 + /**
  142 + * 将base 64 code AES解密
  143 + * @param encryptStr 待解密的base 64 code
  144 + * @param decryptKey 解密密钥
  145 + * @return 解密后的string
  146 + * @throws Exception
  147 + */
  148 + public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
  149 + return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
  150 + }
  151 +
  152 +}
platform-common/src/main/java/com/lyms/platform/common/utils/MongoSyncUtil.java View file @ be67e60
  1 +package com.lyms.platform.common.utils;
  2 +
  3 +import com.lyms.platform.common.base.LoginContext;
  4 +import com.lyms.platform.common.base.PageInfo;
  5 +import com.lyms.platform.common.dao.operator.MongoCondition;
  6 +import com.lyms.platform.common.dao.operator.MongoOper;
  7 +import com.lyms.platform.common.dao.operator.MongoQuery;
  8 +import com.mchange.lang.LongUtils;
  9 +import org.apache.commons.httpclient.HttpClient;
  10 +import org.apache.commons.httpclient.NameValuePair;
  11 +import org.apache.commons.httpclient.methods.PostMethod;
  12 +
  13 +import java.util.HashMap;
  14 +import java.util.Map;
  15 +
  16 +/**
  17 + * Created by Administrator on 2016/9/13 0013.
  18 + */
  19 +public class MongoSyncUtil {
  20 +
  21 + public static String mongo_sync = Config.getItem("mongo_sync", "0");
  22 + public static String mongo_sync_url = Config.getItem("mongo_sync_url", "0");
  23 + public static String mongo_sync_token = Config.getItem("mongo_sync_token", "0");
  24 + public static String mongo_crypto_key = Config.getItem("mongo_crypto_key", "0");
  25 +
  26 + public static boolean sync(String action, Object data, String id) {
  27 + if ("1".equals(mongo_sync)) {
  28 + boolean boo = false;
  29 + try {
  30 + HttpClient client = new HttpClient();
  31 + client.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
  32 + client.getHttpConnectionManager().getParams().setSoTimeout(3000);
  33 + PostMethod post = new MessageUtil.UTF8PostMethod(mongo_sync_url);
  34 + NameValuePair[] pairs = {
  35 + new NameValuePair("action", action),
  36 + new NameValuePair("token", mongo_sync_token),
  37 + new NameValuePair("className", LymsEncodeUtil.aesEncrypt(data.getClass().getName(), mongo_crypto_key)),
  38 + new NameValuePair("jsonData", LymsEncodeUtil.aesEncrypt(JsonUtil.obj2JsonString(data), mongo_crypto_key)),
  39 + new NameValuePair("id", LymsEncodeUtil.aesEncrypt(id, mongo_crypto_key)),
  40 + };
  41 + post.setRequestBody(pairs);
  42 + client.executeMethod(post);
  43 + int statusCode = post.getStatusCode();
  44 + post.releaseConnection();
  45 + if (200 == statusCode) {
  46 + boo = true;
  47 + }
  48 + return boo;
  49 + } catch (Exception e) {
  50 + e.printStackTrace();
  51 + return boo;
  52 + }
  53 + }
  54 + return true;
  55 + }
  56 +
  57 + public static void main(String[] a) throws Exception {
  58 + System.out.println(MongoSyncUtil.mongo_sync);
  59 + MongoSyncUtil util = new MongoSyncUtil();
  60 + System.out.println(util.getClass().getName());
  61 + PageInfo info = new PageInfo();
  62 + info.setCount(123);
  63 + info.setLimit(456);
  64 + Object aaa = JsonUtil.str2Obj(JsonUtil.obj2JsonString(info), Class.forName(info.getClass().getName()));
  65 + System.out.println(MD5Utils.md5(aaa.getClass().getName()));
  66 +
  67 + }
  68 +
  69 +}
platform-operate-api/src/main/java/com/lyms/platform/operate/web/controller/MongoSyncController.java View file @ be67e60
  1 +package com.lyms.platform.operate.web.controller;
  2 +
  3 +import com.lyms.platform.biz.service.MongoSyncService;
  4 +import com.lyms.platform.common.base.BaseController;
  5 +import com.lyms.platform.common.utils.Config;
  6 +import org.apache.commons.lang.StringUtils;
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.stereotype.Controller;
  9 +import org.springframework.web.bind.annotation.RequestMapping;
  10 +import org.springframework.web.bind.annotation.RequestMethod;
  11 +import org.springframework.web.bind.annotation.ResponseBody;
  12 +
  13 +/**
  14 + * Created by Administrator on 2016/9/13 0013.
  15 + */
  16 +@Controller
  17 +public class MongoSyncController extends BaseController {
  18 +
  19 + public static String mongo_sync_token = Config.getItem("mongo_sync_token", "0");
  20 +
  21 + @Autowired
  22 + private MongoSyncService mongoSyncService;
  23 +
  24 + @ResponseBody
  25 + @RequestMapping(method = RequestMethod.POST,value = "/syncmongo")
  26 + public String syncmongo(String action, String token, String className, String jsonData, String id){
  27 + if (StringUtils.isNotBlank(token) && mongo_sync_token.equals(token)) {
  28 + boolean boo = mongoSyncService.syncData(action,id, className, jsonData);
  29 + if (boo) {
  30 + return "success";
  31 + }
  32 + return "fail";
  33 + }
  34 + return "token vaild";
  35 + }
  36 +
  37 +}
platform-operate-api/src/main/resources/database.properties View file @ be67e60
... ... @@ -59,4 +59,11 @@
59 59  
60 60 #添加管理员默认所属机构
61 61 defaultAdminOrgId=203
  62 +
  63 +
  64 +#mongo数据是否上传 1:上传,0:不上传
  65 +mongo_sync=0
  66 +mongo_sync_url=http://mms.api.stage.platform.healthbaby.com.cn/syncmongo
  67 +mongo_sync_token=68884AD1832AB397E2F85F87FE371C74
  68 +mongo_crypto_key=Lymsh@2016
... ... @@ -529,6 +529,7 @@
529 529 <!--<artifactId>sqljdbc4</artifactId>-->
530 530 <!--<version>4.0</version>-->
531 531 <!--</dependency>-->
  532 +
532 533 </dependencies>
533 534 </project>