Explorar o código

完善部署功能

周浩 %!s(int64=9) %!d(string=hai) anos
pai
achega
a4df962b82

+ 23 - 7
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/form/DynamicFormServiceImpl.java

@@ -4,8 +4,13 @@ import org.hsweb.web.bean.common.PagerResult;
 import org.hsweb.web.bean.common.QueryParam;
 import org.hsweb.web.bean.po.form.Form;
 import org.hsweb.web.service.form.DynamicFormService;
+import org.hsweb.web.service.form.FormService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.webbuilder.sql.DataBase;
+import org.webbuilder.sql.TableMetaData;
 
+import javax.annotation.Resource;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.locks.Lock;
@@ -17,16 +22,30 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
  */
 @Service("dynamicFormService")
 public class DynamicFormServiceImpl implements DynamicFormService {
-    private ReadWriteLock lock = new ReentrantReadWriteLock();
+    protected ReadWriteLock lock = new ReentrantReadWriteLock();
+
+    @Autowired(required = false)
+    protected FormParser formParser = new CommonFormParser();
+
+    @Autowired
+    protected DataBase dataBase;
+
+    @Resource
+    protected FormService formService;
 
     @Override
     public void deploy(Form form) throws Exception {
         Lock writeLock = lock.writeLock();
         try {
             writeLock.lock();
-            System.out.println("初始化");
-            Thread.sleep(3000);
-            System.out.println("初始化完成");
+            TableMetaData metaData = formParser.parse(form);
+            List<Form> oldList = formService.select(new QueryParam().where("name", form.getName()).where("version", form.getVersion() - 1));
+            Form old = null;
+            if (oldList.size() > 0) old = oldList.get(0);
+            if (old != null)
+                dataBase.updateTable(metaData);
+            else
+                dataBase.createTable(metaData);
         } finally {
             writeLock.unlock();
         }
@@ -37,9 +56,6 @@ public class DynamicFormServiceImpl implements DynamicFormService {
         Lock writeLock = lock.writeLock();
         try {
             writeLock.lock();
-            System.out.println("初始化");
-            Thread.sleep(3000);
-            System.out.println("初始化完成");
         } finally {
             writeLock.unlock();
         }

+ 14 - 4
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/form/FormServiceImpl.java

@@ -9,6 +9,7 @@ import org.hsweb.web.exception.BusinessException;
 import org.hsweb.web.service.form.DynamicFormService;
 import org.hsweb.web.service.form.FormService;
 import org.hsweb.web.service.impl.AbstractServiceImpl;
+import org.hsweb.web.utils.RandomUtil;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
@@ -47,13 +48,19 @@ public class FormServiceImpl extends AbstractServiceImpl<Form, String> implement
 
     @Override
     public String createNewVersion(String oldVersionId) throws Exception {
-        return null;
+        Form old = this.selectByPk(oldVersionId);
+        Assert.isNull(old, "表单不存在!");
+        old.setU_id(RandomUtil.randomChar());
+        old.setVersion(old.getVersion() + 1);
+        old.setCreate_date(new Date());
+        old.setUpdate_date(null);
+        return old.getU_id();
     }
 
     @Override
     public String insert(Form data) throws Exception {
         List<Form> old = this.select(new QueryParam().where("name", data.getName()));
-        Assert.notEmpty(old, "表单 [" + data.getName() + "] 已存在!");
+        Assert.notNull(old, "表单 [" + data.getName() + "] 已存在!");
         data.setCreate_date(new Date());
         data.setVersion(1);
         super.insert(data);
@@ -91,9 +98,10 @@ public class FormServiceImpl extends AbstractServiceImpl<Form, String> implement
     }
 
     @Override
+    @Transactional(rollbackFor = Throwable.class)
     public void deploy(String formId) throws Exception {
         Form old = this.selectByPk(formId);
-        Assert.isNull(old, "表单不存在");
+        Assert.notNull(old, "表单不存在");
         //先卸载正在使用的表单
         Form using = getMapper().selectUsing(old.getName());
         if (using != null) {
@@ -102,12 +110,14 @@ public class FormServiceImpl extends AbstractServiceImpl<Form, String> implement
         //开始发布
         old.setUsing(true);
         dynamicFormService.deploy(old);
+        getMapper().update(new UpdateParam<>(old).includes("using").where("u_id", old.getU_id()));
     }
 
     @Override
+    @Transactional(rollbackFor = Throwable.class)
     public void unDeploy(String formId) throws Exception {
         Form old = this.selectByPk(formId);
-        Assert.isNull(old, "表单不存在");
+        Assert.notNull(old, "表单不存在");
         dynamicFormService.unDeploy(old);
         old.setUsing(false);
         UpdateParam param = new UpdateParam<>(old);