GlobalParameter.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.uas.kanban.model;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.mongodb.morphia.annotations.Embedded;
  5. import org.mongodb.morphia.annotations.Entity;
  6. import com.uas.kanban.annotation.FieldProperty;
  7. import com.uas.kanban.base.BaseEntity;
  8. import com.uas.kanban.util.CollectionUtils;
  9. import com.uas.kanban.util.ObjectUtils;
  10. /**
  11. * 公共参数
  12. *
  13. * @author sunyj
  14. * @since 2017年9月7日 下午4:04:04
  15. */
  16. @Entity
  17. public class GlobalParameter extends BaseEntity {
  18. private static final long serialVersionUID = 1L;
  19. /**
  20. * 参数名称,需要和模板中保持一致
  21. */
  22. @FieldProperty(nullable = false)
  23. private String name;
  24. /**
  25. * 参数类型
  26. */
  27. @FieldProperty(nullable = false)
  28. private Type type;
  29. /**
  30. * 输入方式
  31. */
  32. @FieldProperty(nullable = false)
  33. private InputMode inputMode;
  34. /**
  35. * 输入方式为 {@link InputMode.Radio} 时,可选择的值,此时不可为空
  36. */
  37. @Embedded
  38. private List<Value> radioValues;
  39. /**
  40. * 输入方式为 {@link InputMode.Radio} 时,默认值的序号
  41. */
  42. private Integer defaultRadioValueIndex;
  43. /**
  44. * 应用模版时,选择或者输入的值
  45. */
  46. @Embedded
  47. private Value value;
  48. @Override
  49. public void init() {
  50. if (!ObjectUtils.isEmpty(getValue())) {
  51. throw new IllegalArgumentException("不能指定value:" + this.toString());
  52. }
  53. super.init();
  54. }
  55. public String getName() {
  56. return name;
  57. }
  58. public void setName(String name) {
  59. this.name = name;
  60. }
  61. public Type getType() {
  62. return type;
  63. }
  64. public void setType(Type type) {
  65. this.type = type;
  66. }
  67. public InputMode getInputMode() {
  68. return inputMode;
  69. }
  70. public void setInputMode(InputMode inputMode) {
  71. this.inputMode = inputMode;
  72. }
  73. public Integer getDefaultRadioValueIndex() {
  74. return defaultRadioValueIndex;
  75. }
  76. public void setDefaultRadioValueIndex(Integer defaultRadioValueIndex) {
  77. this.defaultRadioValueIndex = defaultRadioValueIndex;
  78. }
  79. @Override
  80. public String toString() {
  81. return "GlobalParameter [name=" + name + ", type=" + type + ", inputMode=" + inputMode + ", radioValues="
  82. + radioValues + ", defaultRadioValueIndex=" + defaultRadioValueIndex + ", value=" + value + ", id=" + id
  83. + ", createTime=" + createTime + ", lastModified=" + lastModified + ", version=" + version + ", code="
  84. + code + "]";
  85. }
  86. public List<Object> getRadioValues() {
  87. checkType();
  88. if (CollectionUtils.isEmpty(radioValues)) {
  89. return null;
  90. }
  91. List<Object> values = new ArrayList<>();
  92. for (Value v : radioValues) {
  93. values.add(v == null ? null : v.getValue(type));
  94. }
  95. return values;
  96. }
  97. public void setRadioValues(List<Object> radioValues) {
  98. checkType();
  99. if (!CollectionUtils.isEmpty(radioValues)) {
  100. if (this.radioValues == null) {
  101. this.radioValues = new ArrayList<>();
  102. }
  103. for (Object object : radioValues) {
  104. Value v = new Value();
  105. v.setValue(type, object);
  106. this.radioValues.add(v);
  107. }
  108. }
  109. }
  110. public Object getValue() {
  111. checkType();
  112. return value == null ? null : value.getValue(type);
  113. }
  114. public void setValue(Object value) {
  115. checkType();
  116. if (this.value == null) {
  117. this.value = new Value();
  118. }
  119. this.value.setValue(type, value);
  120. }
  121. private void checkType() {
  122. if (type == null) {
  123. throw new IllegalStateException("type 为空:" + this);
  124. }
  125. }
  126. /**
  127. * 检查参数
  128. *
  129. * @throws IllegalArgumentException
  130. * 必填参数没有填写时,报错
  131. */
  132. public void checkValue() throws IllegalArgumentException {
  133. // 检查参数值是否已填写
  134. if (ObjectUtils.isEmpty(getValue())) {
  135. throw new IllegalArgumentException("需填写参数:code=" + code + ", name=" + name);
  136. }
  137. }
  138. /**
  139. * 输入方式
  140. *
  141. * @author sunyj
  142. * @since 2017年9月1日 下午8:01:53
  143. */
  144. public enum InputMode {
  145. /**
  146. * 单选
  147. */
  148. Radio,
  149. /**
  150. * 手输
  151. */
  152. Manual;
  153. }
  154. }