1
2
3
4
5 package gboat2.base.core.service;
6
7 import gboat2.base.core.dao.IBaseDAO;
8 import gboat2.base.core.model.Parameter;
9
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 import net.sf.json.JSONObject;
15
16 import org.apache.commons.lang3.StringUtils;
17 import org.springframework.beans.factory.annotation.Autowired;
18 import org.springframework.transaction.annotation.Transactional;
19 import org.springframework.util.Assert;
20
21 @Transactional
22 public class ParameterServiceImpl implements IParameterService {
23
24 @Autowired
25 private IBaseDAO baseDAO;
26
27 @Override
28 public Parameter getParameter(String jsonOfParameter) {
29
30 Assert.hasLength(jsonOfParameter);
31
32 JSONObject json = new JSONObject();
33 try {
34 json = JSONObject.fromObject(jsonOfParameter);
35 } catch (Exception e) {
36 throw new IllegalArgumentException("非法的json字符串",e.getCause());
37 }
38
39
40 Assert.state(json.containsKey("code"));
41 Assert.state(json.containsKey("name"));
42 Assert.state(json.containsKey("value"));
43
44 Parameter bean = (Parameter)JSONObject.toBean(json,Parameter.class);
45 if(StringUtils.isEmpty(bean.getGroupName())){
46 bean.setGroupName(bean.getName());
47 }
48 Map<String,Object> params = new HashMap<String,Object>();
49 params.put("code", bean.getCode());
50 List<?> queryListByHql = this.baseDAO.queryListByHql(" select parameter from Parameter parameter where parameter.code = :code", params);
51
52
53 if(queryListByHql.size() == 0){
54 this.baseDAO.save(bean);
55 return bean;
56 }
57
58
59 return (Parameter)queryListByHql.get(0);
60
61 }
62
63 @Override
64 public Parameter getParameterByCode(String code){
65 Map<String,Object> params = new HashMap<String,Object>();
66 params.put("code", code);
67 List<?> queryListByHql = this.baseDAO.queryListByHql(" select parameter from Parameter parameter where parameter.code = :code", params);
68
69 if(queryListByHql.size() > 0){
70 return (Parameter)queryListByHql.get(0);
71 }
72
73 throw new RuntimeException("当前的code不存在,请调用【IPlatformParameterService.getParameter】完成此参数的初始化!");
74 }
75
76 @Override
77 public String getValueByCode(String code){
78 Parameter parameter = getParameterByCode(code);
79 return parameter.getValue();
80 }
81
82 @Override
83 public boolean hasCodeByCode(String code){
84 Map<String,Object> params = new HashMap<String,Object>();
85 params.put("code", code);
86 List<?> queryListByHql = this.baseDAO.queryListByHql(" select parameter from Parameter parameter where parameter.code = :code", params);
87
88 return queryListByHql.size() > 0 ;
89 }
90 }