Jelajahi Sumber

新增草稿服务类

周浩 9 tahun lalu
induk
melakukan
177cbe8684

+ 50 - 0
hsweb-web-bean/src/main/java/org/hsweb/web/bean/po/draft/Draft.java

@@ -0,0 +1,50 @@
+package org.hsweb.web.bean.po.draft;
+
+import org.hsweb.web.bean.po.GenericPo;
+
+import java.util.Date;
+
+/**
+ * Created by zhouhao on 16-6-2.
+ */
+public class Draft extends GenericPo<String> {
+    private String name;
+
+    private Object value;
+
+    private Date createDate;
+
+    private String creatorId;
+
+    public String getCreatorId() {
+        return creatorId;
+    }
+
+    public void setCreatorId(String creatorId) {
+        this.creatorId = creatorId;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Object getValue() {
+        return value;
+    }
+
+    public void setValue(Object value) {
+        this.value = value;
+    }
+
+    public Date getCreateDate() {
+        return createDate;
+    }
+
+    public void setCreateDate(Date createDate) {
+        this.createDate = createDate;
+    }
+}

+ 2 - 2
hsweb-web-bean/src/main/java/org/hsweb/web/bean/po/script/DynamicScript.java

@@ -18,11 +18,11 @@ public class DynamicScript extends GenericPo<String> {
     //名称
     @Length(min = 4, message = "名称长度不能少于4")
     @NotNull
-    @Pattern(regexp = "[a-zA-Z]+", message = "名称只能为大小写字母组成")
+    @Pattern(regexp = "^[a-zA-Z0-9_-]+$", message = "名称只能为大小写字母和下划线组成")
     private String name;
 
     //类型
-    @Pattern(regexp = "(js)|(groovy)|(spel)|(ognl)", message = "类型仅支持js,groovy,spel,ognl")
+    @Pattern(regexp = "(js)|(groovy)|(spel)|(ognl)|(java)", message = "类型仅支持js,groovy,spel,ognl,java")
     private String type;
 
     //内容

+ 78 - 0
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/draft/CacheDraftService.java

@@ -0,0 +1,78 @@
+package org.hsweb.web.service.impl.draft;
+
+import org.hsweb.web.bean.po.draft.Draft;
+import org.hsweb.web.service.draft.DraftService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.Cache;
+import org.springframework.cache.CacheManager;
+import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.PostConstruct;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Created by zhouhao on 16-6-3.
+ */
+@Service
+public class CacheDraftService implements DraftService {
+    @Autowired(required = false)
+    private CacheManager cacheManager;
+
+    private String cacheKey = "draft:";
+
+    @PostConstruct
+    public void init() {
+        if (cacheManager == null) {
+            cacheManager = new ConcurrentMapCacheManager();
+        }
+    }
+
+    @Override
+    public String createDraft(String key, Draft draft) {
+        Cache cache = cacheManager.getCache(cacheKey + draft.getCreatorId());
+        Cache.ValueWrapper wrapper = cache.get(key);
+        Map<String, Draft> drafts;
+        if (wrapper == null) {
+            drafts = new HashMap<>();
+        } else {
+            drafts = ((Map) wrapper.get());
+        }
+        drafts.put(draft.getId(), draft);
+        cache.put(key, drafts);
+        return draft.getId();
+    }
+
+    @Override
+    public List<Draft> getAllDraftByKey(String key, String userId) {
+        Cache cache = cacheManager.getCache(cacheKey + userId);
+        Cache.ValueWrapper wrapper = cache.get(key);
+        if (wrapper != null) {
+            return (List) ((Map) wrapper.get()).values();
+        }
+        return new ArrayList<>();
+    }
+
+    @Override
+    public boolean removeDraft(String key, String id, String userId) {
+        Cache cache = cacheManager.getCache(cacheKey + userId);
+        Cache.ValueWrapper wrapper = cache.get(key);
+        if (wrapper != null) {
+            Map<String, Draft> drafts = ((Map) wrapper.get());
+            drafts.remove(id);
+            cache.put(key, drafts);
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public boolean removeDraft(String key, String userId) {
+        Cache cache = cacheManager.getCache(cacheKey + userId);
+        cache.evict(key);
+        return true;
+    }
+}