Browse Source

公共参数接口

sunyj 8 năm trước cách đây
mục cha
commit
cf5b5d7bce

+ 19 - 0
kanban-console/src/main/java/com/uas/kanban/controller/GlobalParameterController.java

@@ -0,0 +1,19 @@
+package com.uas.kanban.controller;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import com.uas.kanban.base.BaseController;
+import com.uas.kanban.model.GlobalParameter;
+
+/**
+ * 公共参数
+ * 
+ * @author sunyj
+ * @since 2017年9月1日 下午4:42:10
+ */
+@Controller
+@RequestMapping("/globalParameters")
+public class GlobalParameterController extends BaseController<GlobalParameter> {
+
+}

+ 17 - 0
kanban-console/src/main/java/com/uas/kanban/dao/GlobalParameterDao.java

@@ -0,0 +1,17 @@
+package com.uas.kanban.dao;
+
+import org.springframework.stereotype.Component;
+
+import com.uas.kanban.base.BaseDao;
+import com.uas.kanban.model.GlobalParameter;
+
+/**
+ * 公共参数
+ * 
+ * @author sunyj
+ * @since 2017年8月29日 上午10:08:50
+ */
+@Component
+public class GlobalParameterDao extends BaseDao<GlobalParameter> {
+
+}

+ 187 - 0
kanban-console/src/main/java/com/uas/kanban/model/GlobalParameter.java

@@ -0,0 +1,187 @@
+package com.uas.kanban.model;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.mongodb.morphia.annotations.Embedded;
+import org.mongodb.morphia.annotations.Entity;
+
+import com.uas.kanban.annotation.FieldProperty;
+import com.uas.kanban.base.BaseEntity;
+import com.uas.kanban.util.CollectionUtils;
+import com.uas.kanban.util.ObjectUtils;
+
+/**
+ * 公共参数
+ * 
+ * @author sunyj
+ * @since 2017年9月7日 下午4:04:04
+ */
+@Entity
+public class GlobalParameter extends BaseEntity {
+
+	private static final long serialVersionUID = 1L;
+
+	/**
+	 * 参数名称,需要和模板中保持一致
+	 */
+	@FieldProperty(nullable = false)
+	private String name;
+
+	/**
+	 * 参数类型
+	 */
+	@FieldProperty(nullable = false)
+	private Type type;
+
+	/**
+	 * 输入方式
+	 */
+	@FieldProperty(nullable = false)
+	private InputMode inputMode;
+
+	/**
+	 * 输入方式为 {@link InputMode.Radio} 时,可选择的值,此时不可为空
+	 */
+	@Embedded
+	private List<Value> radioValues;
+
+	/**
+	 * 输入方式为 {@link InputMode.Radio} 时,默认值的序号
+	 */
+	private Integer defaultRadioValueIndex;
+
+	/**
+	 * 应用模版时,选择或者输入的值
+	 */
+	@Embedded
+	private Value value;
+
+	@Override
+	public void init() {
+		if (!ObjectUtils.isEmpty(getValue())) {
+			throw new IllegalArgumentException("不能指定value:" + this.toString());
+		}
+		super.init();
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public Type getType() {
+		return type;
+	}
+
+	public void setType(Type type) {
+		this.type = type;
+	}
+
+	public InputMode getInputMode() {
+		return inputMode;
+	}
+
+	public void setInputMode(InputMode inputMode) {
+		this.inputMode = inputMode;
+	}
+
+	public Integer getDefaultRadioValueIndex() {
+		return defaultRadioValueIndex;
+	}
+
+	public void setDefaultRadioValueIndex(Integer defaultRadioValueIndex) {
+		this.defaultRadioValueIndex = defaultRadioValueIndex;
+	}
+
+	@Override
+	public String toString() {
+		return "GlobalParameter [name=" + name + ", type=" + type + ", inputMode=" + inputMode + ", radioValues="
+				+ radioValues + ", defaultRadioValueIndex=" + defaultRadioValueIndex + ", value=" + value + ", id=" + id
+				+ ", createTime=" + createTime + ", lastModified=" + lastModified + ", version=" + version + ", code="
+				+ code + "]";
+	}
+
+	public List<Object> getRadioValues() {
+		checkType();
+		if (CollectionUtils.isEmpty(radioValues)) {
+			return null;
+		}
+		List<Object> values = new ArrayList<>();
+		for (Value v : radioValues) {
+			values.add(v == null ? null : v.getValue(type));
+		}
+		return values;
+	}
+
+	public void setRadioValues(List<Object> radioValues) {
+		checkType();
+		if (!CollectionUtils.isEmpty(radioValues)) {
+			if (this.radioValues == null) {
+				this.radioValues = new ArrayList<>();
+			}
+			for (Object object : radioValues) {
+				Value v = new Value();
+				v.setValue(type, object);
+				this.radioValues.add(v);
+			}
+		}
+
+	}
+
+	public Object getValue() {
+		checkType();
+		return value == null ? null : value.getValue(type);
+	}
+
+	public void setValue(Object value) {
+		checkType();
+		if (this.value == null) {
+			this.value = new Value();
+		}
+		this.value.setValue(type, value);
+	}
+
+	private void checkType() {
+		if (type == null) {
+			throw new IllegalStateException("type 为空:" + this);
+		}
+	}
+
+	/**
+	 * 检查参数
+	 * 
+	 * @throws IllegalArgumentException
+	 *             必填参数没有填写时,报错
+	 */
+	public void checkValue() throws IllegalArgumentException {
+		// 检查参数值是否已填写
+		if (ObjectUtils.isEmpty(getValue())) {
+			throw new IllegalArgumentException("需填写参数:code=" + code + ", name=" + name);
+		}
+	}
+
+	/**
+	 * 输入方式
+	 * 
+	 * @author sunyj
+	 * @since 2017年9月1日 下午8:01:53
+	 */
+	public enum InputMode {
+
+		/**
+		 * 单选
+		 */
+		Radio,
+
+		/**
+		 * 手输
+		 */
+		Manual;
+
+	}
+
+}

+ 11 - 0
kanban-console/src/main/java/com/uas/kanban/model/TemplateParameter.java

@@ -1,5 +1,7 @@
 package com.uas.kanban.model;
 
+import org.mongodb.morphia.annotations.Embedded;
+
 import com.uas.kanban.annotation.FieldProperty;
 import com.uas.kanban.base.Coded;
 import com.uas.kanban.util.ObjectUtils;
@@ -29,8 +31,17 @@ public class TemplateParameter extends Coded {
 	/**
 	 * 应用模版时,参数的值
 	 */
+	@Embedded
 	private Value value;
 
+	@Override
+	public void init() throws IllegalArgumentException {
+		if (!ObjectUtils.isEmpty(getValue())) {
+			throw new IllegalArgumentException("不能指定value:" + this.toString());
+		}
+		super.init();
+	}
+
 	// TODO 分组,参数分组主要考虑部分参数可能名称相同
 
 	public String getName() {

+ 75 - 0
kanban-console/src/main/java/com/uas/kanban/service/impl/GlobalParameterServiceImpl.java

@@ -0,0 +1,75 @@
+package com.uas.kanban.service.impl;
+
+import java.util.List;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import com.uas.kanban.annotation.NotEmpty;
+import com.uas.kanban.base.BaseService;
+import com.uas.kanban.dao.GlobalParameterDao;
+import com.uas.kanban.exception.OperationException;
+import com.uas.kanban.model.GlobalParameter;
+import com.uas.kanban.model.GlobalParameter.InputMode;
+import com.uas.kanban.util.CollectionUtils;
+
+/**
+ * 公共参数
+ * 
+ * @author sunyj
+ * @since 2017年9月1日 下午4:46:01
+ */
+@Service
+public class GlobalParameterServiceImpl extends BaseService<GlobalParameter> {
+
+	@Autowired
+	private GlobalParameterDao globalParameterDao;
+
+	@Override
+	public GlobalParameter save(@NotEmpty("json") String json) {
+		GlobalParameter globalParameter = globalParameterDao.parse(json);
+		checkInputMode(globalParameter);
+		return globalParameterDao.save(globalParameter);
+	}
+
+	@Override
+	public int update(@NotEmpty("json") String json) throws IllegalArgumentException, OperationException {
+		GlobalParameter globalParameter = globalParameterDao.parse(json);
+		checkInputMode(globalParameter);
+		return globalParameterDao.update(globalParameter);
+	}
+
+	/**
+	 * 检查输入方式
+	 * 
+	 * @param globalParameter
+	 *            公共参数
+	 * @throws IllegalArgumentException
+	 *             输入方式为 Radio 时,未提供可选值或者默认值的序号不合法
+	 */
+	private void checkInputMode(@NotEmpty("globalParameter") GlobalParameter globalParameter) {
+		InputMode inputMode = globalParameter.getInputMode();
+		// 输入方式为 {@link InputMode.Radio} 时,需提供可选值
+		if (inputMode != null) {
+			List<Object> radioValues = globalParameter.getRadioValues();
+			Integer defaultRadioValueIndex = globalParameter.getDefaultRadioValueIndex();
+			switch (inputMode) {
+			case Radio:
+				if (CollectionUtils.isEmpty(radioValues)) {
+					throw new IllegalArgumentException("输入方式为 Radio 时,需提供可选值");
+				}
+				if (defaultRadioValueIndex != null && defaultRadioValueIndex < 1
+						|| defaultRadioValueIndex > radioValues.size()) {
+					throw new IllegalArgumentException("默认值的序号不合法:" + defaultRadioValueIndex);
+				}
+				break;
+			case Manual:
+				if (!CollectionUtils.isEmpty(radioValues) || defaultRadioValueIndex != null) {
+					throw new IllegalArgumentException("输入方式为 Manual 时,不可指定radioValues 和 defaultRadioValueIndex");
+				}
+				break;
+			}
+		}
+	}
+
+}

+ 10 - 0
kanban-console/src/main/webapp/WEB-INF/views/console.html

@@ -30,6 +30,16 @@
 				<br/>
 				<li><a target="_blank">datasource/get/un/username_123</a></li>
 			</ol>
+			<strong><li class="title">公共参数</li></strong>
+			<ol>
+				<li><a target="_blank">globalParameters/save?json={"name": "公共参数1","type": "String", "inputMode": "Radio", "radioValues":["值1","值2"], "defaultRadioValueIndex": 1}</a></li>
+				<li><a target="_blank">globalParameters/update?json={"code":"5060ED02612", "name": "公共参数1","type": "String", "inputMode": "Radio", "radioValues":["值1","值2"], "defaultRadioValueIndex": 1}</a></li>
+				<li><a target="_blank">globalParameters/delete/all</a></li>
+				<li><a target="_blank">globalParameters/delete/4EC2735D343</a></li>
+				<li><a target="_blank">globalParameters/get/all</a></li>
+				<li><a target="_blank">globalParameters/get/4EC2735D343</a></li>
+				<li><a target="_blank">globalParameters/get?page=1&size=10</a></li>
+			</ol>
 			<strong><li class="title">模版</li></strong>
 			<ol>
 				<li><a target="_blank">template/save?json={"name": "name","content": "&lt;content&gt;&lt;/content&gt;","dataSourceCode":"4EC3C69D011","parameters":[{"name":"PU_NAME","type":"String","mandatory":true},{"name":"PU_DATE","type":"Date","mandatory":true}]}</a></li>