|
|
@@ -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;
|
|
|
+ }
|
|
|
|
|
|
}
|