Просмотр исходного кода

增加资源点 /login和 /exist接口

sunyj 8 лет назад
Родитель
Сommit
58feab04a0

+ 39 - 0
kanban-auth/src/main/java/com/uas/kanban/controller/ResourcePointController.java

@@ -1,10 +1,16 @@
 package com.uas.kanban.controller;
 
+import javax.servlet.http.HttpServletRequest;
+
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
 
+import com.uas.kanban.annotation.NotEmpty;
 import com.uas.kanban.base.BaseController;
 import com.uas.kanban.model.ResourcePoint;
+import com.uas.kanban.service.ResourcePointService;
 
 /**
  * 用户
@@ -16,4 +22,37 @@ import com.uas.kanban.model.ResourcePoint;
 @RequestMapping("/resourcePoint")
 public class ResourcePointController extends BaseController<ResourcePoint> {
 
+	@Autowired
+	private ResourcePointService resourcePointService;
+
+	/**
+	 * 登陆
+	 * 
+	 * @param name
+	 *            名称
+	 * @param password
+	 *            密码
+	 * @param request
+	 * @return 是否成功登陆
+	 */
+	@RequestMapping("/login")
+	@ResponseBody
+	public boolean login(@NotEmpty("name") String name, @NotEmpty("password") String password,
+			HttpServletRequest request) {
+		return resourcePointService.login(name, password);
+	}
+
+	/**
+	 * 资源点已存在
+	 * 
+	 * @param name
+	 *            名称
+	 * @param request
+	 * @return 是否存在
+	 */
+	@RequestMapping("/exist")
+	@ResponseBody
+	public boolean exist(@NotEmpty("name") String name, HttpServletRequest request) {
+		return resourcePointService.exist(name);
+	}
 }

+ 14 - 0
kanban-auth/src/main/java/com/uas/kanban/model/ResourcePoint.java

@@ -1,5 +1,7 @@
 package com.uas.kanban.model;
 
+import java.util.Objects;
+
 import com.uas.kanban.annotation.FieldProperty;
 import com.uas.kanban.base.BaseEntity;
 
@@ -47,4 +49,16 @@ public class ResourcePoint extends BaseEntity {
 				+ ", lastModified=" + lastModified + ", version=" + version + ", code=" + code + "]";
 	}
 
+	@Override
+	public boolean equals(Object obj) {
+		if (this == obj) {
+			return true;
+		}
+		if (obj == null || getClass() != obj.getClass() || !(obj instanceof ResourcePoint)) {
+			return false;
+		}
+		ResourcePoint other = (ResourcePoint) obj;
+		return Objects.equals(code, other.getCode()) && Objects.equals(name, other.getName())
+				&& Objects.equals(password, other.getPassword());
+	}
 }

+ 32 - 0
kanban-auth/src/main/java/com/uas/kanban/service/ResourcePointService.java

@@ -0,0 +1,32 @@
+package com.uas.kanban.service;
+
+import com.uas.kanban.annotation.NotEmpty;
+
+/**
+ * 资源点
+ * 
+ * @author sunyj
+ * @since 2017年9月1日 下午4:45:50
+ */
+public interface ResourcePointService {
+
+	/**
+	 * 登陆
+	 * 
+	 * @param name
+	 *            名称
+	 * @param password
+	 *            密码
+	 * @return 是否成功登陆
+	 */
+	boolean login(@NotEmpty("name") String name, @NotEmpty("password") String password);
+
+	/**
+	 * 资源点已存在
+	 * 
+	 * @param name
+	 *            名称
+	 * @return 是否存在
+	 */
+	boolean exist(@NotEmpty("name") String name);
+}

+ 78 - 1
kanban-auth/src/main/java/com/uas/kanban/service/impl/ResourcePointServiceImpl.java

@@ -1,9 +1,17 @@
 package com.uas.kanban.service.impl;
 
+import java.util.Objects;
+
+import org.mongodb.morphia.query.Query;
+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.ResourcePointDao;
+import com.uas.kanban.exception.OperationException;
 import com.uas.kanban.model.ResourcePoint;
+import com.uas.kanban.service.ResourcePointService;
 
 /**
  * 资源点
@@ -12,6 +20,75 @@ import com.uas.kanban.model.ResourcePoint;
  * @since 2017年9月2日 下午8:47:20
  */
 @Service
-public class ResourcePointServiceImpl extends BaseService<ResourcePoint> {
+public class ResourcePointServiceImpl extends BaseService<ResourcePoint> implements ResourcePointService {
+
+	@Autowired
+	private ResourcePointDao resourcePointDao;
+
+	@Override
+	public ResourcePoint save(@NotEmpty("json") String json) {
+		ResourcePoint resourcePoint = resourcePointDao.parse(json);
+		if (exist(resourcePoint.getName())) {
+			throw new IllegalStateException("资源点已存在");
+		}
+		checkValid(resourcePoint);
+		return resourcePointDao.save(resourcePoint);
+	}
+
+	@Override
+	public int update(@NotEmpty("json") String json) throws IllegalArgumentException, OperationException {
+		ResourcePoint resourcePoint = resourcePointDao.parse(json);
+		ResourcePoint rPoint = resourcePointDao.findOne(resourcePoint.codeNotEmpty());
+		if (rPoint == null) {
+			throw new IllegalStateException("资源点不存在");
+		}
+		if (Objects.equals(resourcePoint, rPoint)) {
+			throw new IllegalStateException("未发现任何变更");
+		}
+		String name = resourcePoint.getName();
+		if (!Objects.equals(name, rPoint.getName()) && exist(name)) {
+			throw new IllegalStateException("资源点已存在");
+		}
+		checkValid(resourcePoint);
+		return resourcePointDao.update(resourcePoint);
+	}
+
+	/**
+	 * 对名称和密码长度进行校验
+	 * 
+	 * @param resourcePoint
+	 */
+	private void checkValid(@NotEmpty("resourcePoint") ResourcePoint resourcePoint) {
+		String name = resourcePoint.getName();
+		String password = resourcePoint.getPassword();
+		if (name != null && name.trim().length() < 3) {
+			throw new IllegalArgumentException("名称过短");
+		}
+		if (password != null && password.trim().length() < 3) {
+			throw new IllegalArgumentException("密码过短");
+		}
+	}
+
+	@Override
+	public boolean login(@NotEmpty("name") String name, @NotEmpty("password") String password) {
+		Query<ResourcePoint> query = resourcePointDao.createQuery();
+		query.field("name").equal(name);
+		query.field("password").equal(password);
+		long count = query.count();
+		if (count == 0) {
+			throw new IllegalStateException("名称不存在或密码错误");
+		}
+		if (count > 1) {
+			throw new IllegalStateException("资源点重复");
+		}
+		return true;
+	}
+
+	@Override
+	public boolean exist(@NotEmpty("name") String name) {
+		Query<ResourcePoint> query = resourcePointDao.createQuery();
+		query.field("name").equal(name);
+		return query.count() > 0;
+	}
 
 }

+ 3 - 0
kanban-common/src/main/java/com/uas/kanban/aop/ArgumentsCheckAspect.java

@@ -32,6 +32,9 @@ public class ArgumentsCheckAspect {
 	@Before("checkArguments()")
 	public void before(JoinPoint joinPoint) {
 		Method method = getMethod(joinPoint);
+		if (method == null) {
+			return;
+		}
 
 		// 参数值
 		Object[] args = joinPoint.getArgs();

+ 50 - 10
kanban-common/src/main/java/com/uas/kanban/base/BaseDao.java

@@ -5,6 +5,7 @@ import java.lang.reflect.Modifier;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Date;
 import java.util.List;
@@ -316,10 +317,7 @@ public class BaseDao<T extends BaseEntity> {
 	 */
 	public int update(@NotEmpty("t") T t, Set<String> ignoreFields)
 			throws IllegalArgumentException, OperationException {
-		String code = t.getCode();
-		if (StringUtils.isEmpty(code)) {
-			throw new IllegalArgumentException("没有指定 code:" + t);
-		}
+		String code = t.codeNotEmpty();
 		checkFields(t);
 		UpdateOperations<T> operations = createUpdateOperations(t, ignoreFields);
 		return update(code, operations);
@@ -375,11 +373,7 @@ public class BaseDao<T extends BaseEntity> {
 	 *             数据超过1条
 	 */
 	public T findOne(@NotEmpty("code") String code) throws IllegalStateException {
-		Query<T> query = createQuery(code);
-		if (query.count() > 1) {
-			throw new IllegalStateException("数据超过1条");
-		}
-		return query.get();
+		return findBy("code", code);
 	}
 
 	/**
@@ -390,7 +384,53 @@ public class BaseDao<T extends BaseEntity> {
 	 * @return the matched entities. may be null.
 	 */
 	public List<T> findIn(@NotEmpty("codes") Iterable<String> codes) {
-		return createQuery().field("code").in(codes).asList();
+		return findListBy("code", codes);
+	}
+
+	/**
+	 * 根据指定的 field 和值获取数据
+	 * 
+	 * @param field
+	 *            指定的field
+	 * @param value
+	 *            指定 field 的值
+	 * @return 查询结果
+	 */
+	public T findBy(@NotEmpty("field") String field, @NotEmpty("value") Object value) {
+		Query<T> query = createQuery().field(field).equal(value);
+		if (query.count() > 1) {
+			throw new IllegalStateException("数据超过1条");
+		}
+		return query.get();
+	}
+
+	/**
+	 * 根据指定的 field 和值获取数据
+	 * 
+	 * @param field
+	 *            指定的field
+	 * @param values
+	 *            指定 field 的值
+	 * @return 查询结果
+	 */
+	public List<T> findListBy(@NotEmpty("field") String field, @NotEmpty("values") Object... values) {
+		return findListBy(field, Arrays.asList(values));
+	}
+
+	/**
+	 * 根据指定的 field 和值获取数据
+	 * 
+	 * @param <K>
+	 *            值的类型
+	 * 
+	 * @param field
+	 *            指定的field
+	 * @param values
+	 *            指定 field 的值
+	 * @return 查询结果
+	 */
+	public <K> List<T> findListBy(@NotEmpty("field") String field, @NotEmpty("values") Iterable<K> values) {
+		return createQuery().field(field).in(values).asList();
 	}
 
 	/**

+ 15 - 2
kanban-common/src/main/java/com/uas/kanban/base/Coded.java

@@ -2,10 +2,9 @@ package com.uas.kanban.base;
 
 import java.io.Serializable;
 
-import org.springframework.util.StringUtils;
-
 import com.uas.kanban.annotation.FieldProperty;
 import com.uas.kanban.util.NumberGenerator;
+import com.uas.kanban.util.StringUtils;
 
 /**
  * 抽象类,表示含有 code 字段
@@ -44,4 +43,18 @@ public abstract class Coded implements Serializable {
 		return code;
 	}
 
+	/**
+	 * code 是否不为空
+	 * 
+	 * @return code
+	 * @throws IllegalArgumentException
+	 *             code为空
+	 */
+	public String codeNotEmpty() throws IllegalArgumentException {
+		if (StringUtils.isEmpty(code)) {
+			throw new IllegalArgumentException("未指定 code");
+		}
+		return code;
+	}
+
 }

+ 1 - 5
kanban-console/src/main/java/com/uas/kanban/service/impl/TemplateServiceImpl.java

@@ -21,7 +21,6 @@ import com.uas.kanban.model.Template;
 import com.uas.kanban.model.TemplateParameter;
 import com.uas.kanban.service.TemplateService;
 import com.uas.kanban.util.CollectionUtils;
-import com.uas.kanban.util.StringUtils;
 
 /**
  * 模版
@@ -59,10 +58,7 @@ public class TemplateServiceImpl extends BaseService<Template> implements Templa
 	@Override
 	public int update(@NotEmpty("json") String json) throws IllegalArgumentException, OperationException {
 		Template template = templateDao.parse(json);
-		String code = template.getCode();
-		if (StringUtils.isEmpty(code)) {
-			throw new IllegalArgumentException("没有指定code:" + template);
-		}
+		String code = template.codeNotEmpty();
 		checkDataSource(template.getDataSourceCode());
 		checkGlobalParameters(template.getGlobalParameterCodes());
 		List<TemplateParameter> parameters = template.getParameters();

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

@@ -27,6 +27,9 @@
 				<li><a target="_blank">resourcePoint/get/all</a></li>
 				<li><a target="_blank">resourcePoint/get/4EC2735D343</a></li>
 				<li><a target="_blank">resourcePoint/get?page=1&size=10</a></li>
+				<br/>
+				<li><a target="_blank">resourcePoint/login?name=name&password=123</a></li>
+				<li><a target="_blank">resourcePoint/exist?name=name</a></li>
 			</ol>
 			<strong><li class="title">数据源</li></strong>
 			<ol>