瀏覽代碼

优化逻辑。

周浩 9 年之前
父節點
當前提交
a65ce65993

+ 14 - 0
hsweb-web-service-impl-common/pom.xml

@@ -13,6 +13,7 @@
     <dependencies>
     <dependencies>
 
 
         <!--test-->
         <!--test-->
+
         <dependency>
         <dependency>
             <groupId>org.hsweb</groupId>
             <groupId>org.hsweb</groupId>
             <artifactId>hsweb-web-dao-impl-mybatis</artifactId>
             <artifactId>hsweb-web-dao-impl-mybatis</artifactId>
@@ -23,6 +24,7 @@
             <artifactId>spring-boot-starter-web</artifactId>
             <artifactId>spring-boot-starter-web</artifactId>
             <scope>test</scope>
             <scope>test</scope>
         </dependency>
         </dependency>
+
         <dependency>
         <dependency>
             <groupId>com.oracle</groupId>
             <groupId>com.oracle</groupId>
             <artifactId>ojdbc14</artifactId>
             <artifactId>ojdbc14</artifactId>
@@ -33,12 +35,24 @@
             <artifactId>h2</artifactId>
             <artifactId>h2</artifactId>
             <scope>test</scope>
             <scope>test</scope>
         </dependency>
         </dependency>
+
+        <dependency>
+            <groupId>org.hsweb</groupId>
+            <artifactId>hsweb-web-concurrent-cache</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.hsweb</groupId>
+            <artifactId>hsweb-web-concurrent-lock</artifactId>
+        </dependency>
+
         <dependency>
         <dependency>
             <groupId>javax.el</groupId>
             <groupId>javax.el</groupId>
             <artifactId>el-api</artifactId>
             <artifactId>el-api</artifactId>
             <version>2.2</version>
             <version>2.2</version>
             <scope>provided</scope>
             <scope>provided</scope>
         </dependency>
         </dependency>
+
         <dependency>
         <dependency>
             <groupId>org.webbuilder</groupId>
             <groupId>org.webbuilder</groupId>
             <artifactId>wb-sql-util</artifactId>
             <artifactId>wb-sql-util</artifactId>

+ 0 - 2
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/form/CommonFormParser.java

@@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONObject;
 import org.hsweb.web.bean.po.form.Form;
 import org.hsweb.web.bean.po.form.Form;
 import org.jsoup.Jsoup;
 import org.jsoup.Jsoup;
-import org.jsoup.nodes.Document;
 import org.jsoup.nodes.Element;
 import org.jsoup.nodes.Element;
 import org.jsoup.select.Elements;
 import org.jsoup.select.Elements;
 import org.springframework.util.Assert;
 import org.springframework.util.Assert;
@@ -69,7 +68,6 @@ public class CommonFormParser implements FormParser {
         return metaData;
         return metaData;
     }
     }
 
 
-
     public void validField(FieldMetaData field) {
     public void validField(FieldMetaData field) {
         Assert.notNull(field.getName(), "字段名称不能为空!");
         Assert.notNull(field.getName(), "字段名称不能为空!");
         Assert.notNull(field.getJavaType(), "java类型未找到!");
         Assert.notNull(field.getJavaType(), "java类型未找到!");

+ 25 - 13
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/form/DynamicFormServiceImpl.java

@@ -1,8 +1,10 @@
 package org.hsweb.web.service.impl.form;
 package org.hsweb.web.service.impl.form;
 
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSON;
+import org.hsweb.concurrent.lock.LockFactory;
 import org.hsweb.web.Install;
 import org.hsweb.web.Install;
 import org.hsweb.web.bean.common.*;
 import org.hsweb.web.bean.common.*;
+import org.hsweb.web.bean.common.QueryParam;
 import org.hsweb.web.bean.po.GenericPo;
 import org.hsweb.web.bean.po.GenericPo;
 import org.hsweb.web.bean.po.form.Form;
 import org.hsweb.web.bean.po.form.Form;
 import org.hsweb.web.bean.po.history.History;
 import org.hsweb.web.bean.po.history.History;
@@ -13,10 +15,13 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 import org.springframework.util.Assert;
 import org.springframework.util.Assert;
 import org.webbuilder.sql.*;
 import org.webbuilder.sql.*;
+import org.webbuilder.sql.param.query.*;
 
 
+import javax.annotation.PostConstruct;
 import javax.annotation.Resource;
 import javax.annotation.Resource;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -26,7 +31,11 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
  */
  */
 @Service("dynamicFormService")
 @Service("dynamicFormService")
 public class DynamicFormServiceImpl implements DynamicFormService {
 public class DynamicFormServiceImpl implements DynamicFormService {
-    protected ReadWriteLock lock = new ReentrantReadWriteLock();
+
+    @Autowired
+    protected LockFactory lockFactory;
+
+    protected Lock writeLock, readLock;
 
 
     @Autowired(required = false)
     @Autowired(required = false)
     protected FormParser formParser = new CommonFormParser();
     protected FormParser formParser = new CommonFormParser();
@@ -61,9 +70,16 @@ public class DynamicFormServiceImpl implements DynamicFormService {
 
 
     }
     }
 
 
+    @PostConstruct
+    public void init() {
+        ReadWriteLock readWriteLock = lockFactory.createReadWriteLock("dynamicForm.lock");
+        writeLock = readWriteLock.writeLock();
+        readLock = readWriteLock.readLock();
+    }
+
     @Override
     @Override
     public void deploy(Form form) throws Exception {
     public void deploy(Form form) throws Exception {
-        Lock writeLock = lock.writeLock();
+
         try {
         try {
             writeLock.lock();
             writeLock.lock();
             TableMetaData metaData = formParser.parse(form);
             TableMetaData metaData = formParser.parse(form);
@@ -88,7 +104,6 @@ public class DynamicFormServiceImpl implements DynamicFormService {
 
 
     @Override
     @Override
     public void unDeploy(Form form) throws Exception {
     public void unDeploy(Form form) throws Exception {
-        Lock writeLock = lock.writeLock();
         try {
         try {
             writeLock.lock();
             writeLock.lock();
             dataBase.removeTable(form.getName());
             dataBase.removeTable(form.getName());
@@ -107,7 +122,6 @@ public class DynamicFormServiceImpl implements DynamicFormService {
 
 
     @Override
     @Override
     public <T> PagerResult<T> selectPager(String name, QueryParam param) throws Exception {
     public <T> PagerResult<T> selectPager(String name, QueryParam param) throws Exception {
-        Lock readLock = lock.readLock();
         PagerResult<T> result = new PagerResult<>();
         PagerResult<T> result = new PagerResult<>();
         try {
         try {
             readLock.lock();
             readLock.lock();
@@ -127,7 +141,6 @@ public class DynamicFormServiceImpl implements DynamicFormService {
 
 
     @Override
     @Override
     public <T> List<T> select(String name, QueryParam param) throws Exception {
     public <T> List<T> select(String name, QueryParam param) throws Exception {
-        Lock readLock = lock.readLock();
         try {
         try {
             readLock.lock();
             readLock.lock();
             Table table = getTableByName(name);
             Table table = getTableByName(name);
@@ -142,7 +155,6 @@ public class DynamicFormServiceImpl implements DynamicFormService {
 
 
     @Override
     @Override
     public int total(String name, QueryParam param) throws Exception {
     public int total(String name, QueryParam param) throws Exception {
-        Lock readLock = lock.readLock();
         try {
         try {
             readLock.lock();
             readLock.lock();
             Table table = getTableByName(name);
             Table table = getTableByName(name);
@@ -157,7 +169,6 @@ public class DynamicFormServiceImpl implements DynamicFormService {
 
 
     @Override
     @Override
     public String insert(String name, InsertParam<Map<String, Object>> param) throws Exception {
     public String insert(String name, InsertParam<Map<String, Object>> param) throws Exception {
-        Lock readLock = lock.readLock();
         try {
         try {
             readLock.lock();
             readLock.lock();
             Table table = getTableByName(name);
             Table table = getTableByName(name);
@@ -175,7 +186,6 @@ public class DynamicFormServiceImpl implements DynamicFormService {
 
 
     @Override
     @Override
     public boolean deleteByPk(String name, String pk) throws Exception {
     public boolean deleteByPk(String name, String pk) throws Exception {
-        Lock readLock = lock.readLock();
         try {
         try {
             readLock.lock();
             readLock.lock();
             String primaryKeyName = getPrimaryKeyName(name);
             String primaryKeyName = getPrimaryKeyName(name);
@@ -189,7 +199,6 @@ public class DynamicFormServiceImpl implements DynamicFormService {
 
 
     @Override
     @Override
     public int delete(String name, DeleteParam where) throws Exception {
     public int delete(String name, DeleteParam where) throws Exception {
-        Lock readLock = lock.readLock();
         try {
         try {
             readLock.lock();
             readLock.lock();
             Table table = getTableByName(name);
             Table table = getTableByName(name);
@@ -202,7 +211,6 @@ public class DynamicFormServiceImpl implements DynamicFormService {
 
 
     @Override
     @Override
     public int updateByPk(String name, String pk, UpdateParam<Map<String, Object>> param) throws Exception {
     public int updateByPk(String name, String pk, UpdateParam<Map<String, Object>> param) throws Exception {
-        Lock readLock = lock.readLock();
         try {
         try {
             readLock.lock();
             readLock.lock();
             Table table = getTableByName(name);
             Table table = getTableByName(name);
@@ -217,7 +225,6 @@ public class DynamicFormServiceImpl implements DynamicFormService {
 
 
     @Override
     @Override
     public int update(String name, UpdateParam<Map<String, Object>> param) throws Exception {
     public int update(String name, UpdateParam<Map<String, Object>> param) throws Exception {
-        Lock readLock = lock.readLock();
         try {
         try {
             readLock.lock();
             readLock.lock();
             Table table = getTableByName(name);
             Table table = getTableByName(name);
@@ -236,7 +243,6 @@ public class DynamicFormServiceImpl implements DynamicFormService {
 
 
     @Override
     @Override
     public <T> T selectByPk(String name, Object pk) throws Exception {
     public <T> T selectByPk(String name, Object pk) throws Exception {
-        Lock readLock = lock.readLock();
         try {
         try {
             readLock.lock();
             readLock.lock();
             Table table = getTableByName(name);
             Table table = getTableByName(name);
@@ -250,12 +256,18 @@ public class DynamicFormServiceImpl implements DynamicFormService {
     }
     }
 
 
     public static class QueryParamProxy extends org.webbuilder.sql.param.query.QueryParam {
     public static class QueryParamProxy extends org.webbuilder.sql.param.query.QueryParam {
+        public QueryParamProxy orderBy(String mode, Set<String> fields) {
+            addProperty("order_by", fields);
+            addProperty("order_by_mod", mode);
+            return this;
+        }
+
         public static QueryParamProxy build(QueryParam param) {
         public static QueryParamProxy build(QueryParam param) {
             QueryParamProxy proxy = new QueryParamProxy();
             QueryParamProxy proxy = new QueryParamProxy();
             proxy.where(param.getTerm());
             proxy.where(param.getTerm());
             proxy.exclude(param.getExcludes());
             proxy.exclude(param.getExcludes());
             proxy.include(param.getIncludes());
             proxy.include(param.getIncludes());
-            proxy.orderBy("desc".equals(param.getSortOrder()), param.getSortField());
+            proxy.orderBy(param.getSortOrder(), param.getSortField());
             proxy.doPaging(param.getPageIndex(), param.getPageSize());
             proxy.doPaging(param.getPageIndex(), param.getPageSize());
             proxy.setPaging(param.isPaging());
             proxy.setPaging(param.isPaging());
             return proxy;
             return proxy;

+ 35 - 6
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/form/FormServiceImpl.java

@@ -1,6 +1,7 @@
 package org.hsweb.web.service.impl.form;
 package org.hsweb.web.service.impl.form;
 
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSON;
+import org.hsweb.web.authorize.annotation.Authorize;
 import org.hsweb.web.bean.common.QueryParam;
 import org.hsweb.web.bean.common.QueryParam;
 import org.hsweb.web.bean.common.UpdateParam;
 import org.hsweb.web.bean.common.UpdateParam;
 import org.hsweb.web.bean.po.form.Form;
 import org.hsweb.web.bean.po.form.Form;
@@ -51,7 +52,7 @@ public class FormServiceImpl extends AbstractServiceImpl<Form, String> implement
     protected DynamicFormService dynamicFormService;
     protected DynamicFormService dynamicFormService;
 
 
     @Override
     @Override
-    @Cacheable(value = CACHE_KEY, key = "#id")
+    @Cacheable(value = CACHE_KEY, key = "'form.'+#id")
     public Form selectByPk(String id) throws Exception {
     public Form selectByPk(String id) throws Exception {
         return super.selectByPk(id);
         return super.selectByPk(id);
     }
     }
@@ -80,7 +81,7 @@ public class FormServiceImpl extends AbstractServiceImpl<Form, String> implement
     }
     }
 
 
     @Override
     @Override
-    @CacheEvict(value = {CACHE_KEY, CACHE_KEY + "_html_view"}, key = "#data.u_id")
+    @CacheEvict(value = {CACHE_KEY}, key = "'form.'+#data.u_id")
     public int update(Form data) throws Exception {
     public int update(Form data) throws Exception {
         Form old = this.selectByPk(data.getU_id());
         Form old = this.selectByPk(data.getU_id());
         Assert.notNull(old, "表单不存在!");
         Assert.notNull(old, "表单不存在!");
@@ -89,7 +90,7 @@ public class FormServiceImpl extends AbstractServiceImpl<Form, String> implement
     }
     }
 
 
     @Override
     @Override
-    @CacheEvict(value = CACHE_KEY, key = "#id")
+    @CacheEvict(value = CACHE_KEY, key = "'form.'+#id")
     public int delete(String id) throws Exception {
     public int delete(String id) throws Exception {
         Form old = this.selectByPk(id);
         Form old = this.selectByPk(id);
         Assert.notNull(old, "表单不存在!");
         Assert.notNull(old, "表单不存在!");
@@ -109,9 +110,30 @@ public class FormServiceImpl extends AbstractServiceImpl<Form, String> implement
         return formMapper.countLatestList(param);
         return formMapper.countLatestList(param);
     }
     }
 
 
+    @Override
+    public Form selectByVersion(String name, int version) throws Exception {
+        QueryParam param = QueryParam.newInstance()
+                .where("name", name).where("version", version);
+        List<Form> formList = formMapper.selectLatestList(param);
+        return formList.size() > 0 ? formList.get(0) : null;
+    }
+
+    @Override
+    public Form selectLatest(String name) throws Exception {
+        QueryParam param = QueryParam.newInstance()
+                .where("name", name).orderBy("version").desc().doPaging(0, 1);
+        List<Form> formList = formMapper.selectLatestList(param);
+        return formList.size() > 0 ? formList.get(0) : null;
+    }
+
     @Override
     @Override
     @Transactional(rollbackFor = Throwable.class)
     @Transactional(rollbackFor = Throwable.class)
-    @CacheEvict(value = {CACHE_KEY + "_html"}, allEntries = true)
+    @Caching(evict = {
+            @CacheEvict(value = {CACHE_KEY + ".deploy"},
+                    key = "'form.deploy.'+target.selectByPk(#formId).getName()+'.html'"),
+            @CacheEvict(value = {CACHE_KEY},
+                    key = "'form.using.'+target.selectByPk(#formId).getName()")
+    })
     public void deploy(String formId) throws Exception {
     public void deploy(String formId) throws Exception {
         Form old = this.selectByPk(formId);
         Form old = this.selectByPk(formId);
         Assert.notNull(old, "表单不存在");
         Assert.notNull(old, "表单不存在");
@@ -146,7 +168,7 @@ public class FormServiceImpl extends AbstractServiceImpl<Form, String> implement
     }
     }
 
 
     @Override
     @Override
-    @Cacheable(value = CACHE_KEY + "_html", key = "#name")
+    @Cacheable(value = CACHE_KEY + ".deploy", key = "'form.deploy.'+#name+'.html'")
     public String createDeployHtml(String name) throws Exception {
     public String createDeployHtml(String name) throws Exception {
         History history = historyService.selectLastHistoryByType("form.deploy." + name);
         History history = historyService.selectLastHistoryByType("form.deploy." + name);
         Assert.notNull(history, "表单不存在");
         Assert.notNull(history, "表单不存在");
@@ -154,10 +176,17 @@ public class FormServiceImpl extends AbstractServiceImpl<Form, String> implement
     }
     }
 
 
     @Override
     @Override
-    @Cacheable(value = CACHE_KEY + "_html_view", key = "#name")
     public String createViewHtml(String formId) throws Exception {
     public String createViewHtml(String formId) throws Exception {
         Form form = this.selectByPk(formId);
         Form form = this.selectByPk(formId);
         Assert.notNull(form, "表单不存在");
         Assert.notNull(form, "表单不存在");
         return formParser.parseHtml(form);
         return formParser.parseHtml(form);
     }
     }
+
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "'form.using.'+#name")
+    public Form selectUsing(String name) throws Exception {
+        return formMapper.selectUsing(name);
+    }
+
+
 }
 }

+ 1 - 1
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/history/HistoryServiceImpl.java

@@ -28,7 +28,7 @@ public class HistoryServiceImpl extends AbstractServiceImpl<History, String> imp
         QueryParam queryParam = new QueryParam()
         QueryParam queryParam = new QueryParam()
                 .where("type", type)
                 .where("type", type)
                 .doPaging(0, 1)
                 .doPaging(0, 1)
-                .orderBy("create_date", false);
+                .orderBy("create_date").desc();
         List<History> history = historyMapper.select(queryParam);
         List<History> history = historyMapper.select(queryParam);
         if (history.size() == 1) return history.get(0);
         if (history.size() == 1) return history.get(0);
         return null;
         return null;

+ 29 - 1
hsweb-web-service-interface/src/main/java/org/hsweb/web/service/form/FormService.java

@@ -71,12 +71,40 @@ public interface FormService extends GenericService<Form, String> {
     String createDeployHtml(String name) throws Exception;
     String createDeployHtml(String name) throws Exception;
 
 
     /**
     /**
-     *  创建表单的html预览。
+     * 根据表单名称,获取最新版本的表单
+     *
+     * @param name 表单名称
+     * @return 表单对象,表单不存在将返回null
+     * @throws Exception
+     */
+    Form selectLatest(String name) throws Exception;
+
+    /**
+     * 根据表单名称和版本,获取表单
+     *
+     * @param name    表单名称
+     * @param version 表单版本
+     * @return 表单对象,表单不存在将返回null
+     * @throws Exception
+     */
+    Form selectByVersion(String name, int version) throws Exception;
+
+    /**
+     * 创建表单的html预览。
+     *
      * @param id 表单ID
      * @param id 表单ID
      * @return html 字符串
      * @return html 字符串
      * @throws Exception
      * @throws Exception
      */
      */
     String createViewHtml(String id) throws Exception;
     String createViewHtml(String id) throws Exception;
 
 
+    /**
+     * 查询当前正在使用的表单
+     * @param name 正在使用的表单名称
+     * @return 表单对象。没有则返回null
+     * @throws Exception
+     */
+    Form selectUsing(String name) throws Exception;
+
 
 
 }
 }