@@ -10,5 +10,6 @@ public interface SwConsts { | |||||
String KEY_PARENT_ID = "pr"; | String KEY_PARENT_ID = "pr"; | ||||
//级次码、字符串连接符 | //级次码、字符串连接符 | ||||
String SPLIT_CHAR = "-"; | String SPLIT_CHAR = "-"; | ||||
//默认根节点 | |||||
String DEF_ROOT_ID = "-1"; | |||||
} | } |
@@ -12,31 +12,46 @@ import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||||
import cc.smtweb.framework.core.db.vo.ModelField; | import cc.smtweb.framework.core.db.vo.ModelField; | ||||
import cc.smtweb.framework.core.db.vo.ModelTable; | import cc.smtweb.framework.core.db.vo.ModelTable; | ||||
import java.util.ArrayList; | |||||
import java.util.Collection; | |||||
import java.util.Comparator; | |||||
import java.util.List; | |||||
import java.util.*; | |||||
import java.util.concurrent.ConcurrentHashMap; | |||||
/** | /** | ||||
* Created by Akmm at 2022/3/17 19:56 | * Created by Akmm at 2022/3/17 19:56 | ||||
*/ | */ | ||||
public class TreeHelper<T extends DefaultEntity> { | public class TreeHelper<T extends DefaultEntity> { | ||||
private static Map<String, TreeHelper> mapInstance; | |||||
static { | |||||
mapInstance = new ConcurrentHashMap<>(); | |||||
} | |||||
private String tableName; | private String tableName; | ||||
private ModelTable table; | private ModelTable table; | ||||
private AbstractCache<T> cache; | |||||
protected AbstractCache<T> cache; | |||||
private EntityDao<T> dao; | private EntityDao<T> dao; | ||||
private String fieldParent; | private String fieldParent; | ||||
private String fieldLevelCode; | private String fieldLevelCode; | ||||
//todo 待扩展,进缓存 | |||||
public static TreeHelper getTreeHelper(String tableName) { | public static TreeHelper getTreeHelper(String tableName) { | ||||
return new TreeHelper(tableName); | |||||
return mapInstance.computeIfAbsent(tableName, k-> new TreeHelper(tableName)); | |||||
} | |||||
public static TreeHelper getTreeHelper(String tableName, Class<? extends TreeHelper> clazz) { | |||||
TreeHelper helper = mapInstance.get(tableName); | |||||
if (helper == null) { | |||||
try { | |||||
helper = clazz.newInstance(); | |||||
mapInstance.put(tableName, helper); | |||||
} catch (Exception e) { | |||||
throw new SwException(e); | |||||
} | |||||
} | |||||
return helper; | |||||
} | } | ||||
//先禁止外部创建 | //先禁止外部创建 | ||||
private TreeHelper(String tableName) { | |||||
protected TreeHelper(String tableName) { | |||||
this.tableName = tableName; | this.tableName = tableName; | ||||
table = ModelTableCache.getInstance().getByName(tableName); | table = ModelTableCache.getInstance().getByName(tableName); | ||||
cache = CacheManager.getIntance().getCache(tableName); | cache = CacheManager.getIntance().getCache(tableName); | ||||
@@ -103,8 +103,8 @@ | |||||
}, | }, | ||||
{ | { | ||||
"name": "prj", | "name": "prj", | ||||
"title": "按目录", | |||||
"fields": "mc_prj_id", | |||||
"title": "按项目", | |||||
"fields": "mc_prj_id,mc_parent_id", | |||||
"type": "L" | "type": "L" | ||||
} | } | ||||
] | ] |
@@ -103,8 +103,8 @@ | |||||
}, | }, | ||||
{ | { | ||||
"name": "prj", | "name": "prj", | ||||
"title": "按目录", | |||||
"fields": "mc_prj_id", | |||||
"title": "按项目", | |||||
"fields": "mc_prj_id,mc_parent_id", | |||||
"type": "L" | "type": "L" | ||||
} | } | ||||
] | ] |
@@ -26,14 +26,14 @@ public class ModelCatalogTreeHandler extends AbstractTreeHandler<Object> { | |||||
private long prj_id;//所属项目 | private long prj_id;//所属项目 | ||||
private int type;//查询类型:0-目录;1-表定义;2-页面定义 | private int type;//查询类型:0-目录;1-表定义;2-页面定义 | ||||
private TreeHelper<ModelCatalog> mcTreeHelper = null; | |||||
private ModelCatalogTreeHelper mcTreeHelper = null; | |||||
@Override | @Override | ||||
public void init(SwMap params, UserSession us) { | public void init(SwMap params, UserSession us) { | ||||
super.init(params, us); | super.init(params, us); | ||||
type = params.readInt("type"); | type = params.readInt("type"); | ||||
prj_id = params.readLong("prj_id"); | prj_id = params.readLong("prj_id"); | ||||
mcTreeHelper = (TreeHelper<ModelCatalog>) TreeHelper.getTreeHelper(ModelCatalog.ENTITY_NAME); | |||||
mcTreeHelper = (ModelCatalogTreeHelper) TreeHelper.getTreeHelper(ModelCatalog.ENTITY_NAME, ModelCatalogTreeHelper.class); | |||||
} | } | ||||
@Override | @Override | ||||
@@ -69,11 +69,7 @@ public class ModelCatalogTreeHandler extends AbstractTreeHandler<Object> { | |||||
protected List<Object> getChildren(long id) { | protected List<Object> getChildren(long id) { | ||||
List<Object> listRet = new ArrayList<>(); | List<Object> listRet = new ArrayList<>(); | ||||
List<ModelCatalog> list; | List<ModelCatalog> list; | ||||
if (id > 0) { | |||||
list = mcTreeHelper.getChildren(id, (o1, o2) -> CommUtil.chineseCompare(o1.getName(), o2.getName())); | |||||
} else { | |||||
list = mcTreeHelper.getChildren(-1, (o1, o2) -> CommUtil.chineseCompare(o1.getName(), o2.getName())); | |||||
} | |||||
list = mcTreeHelper.getChildren(id, prj_id, (o1, o2) -> CommUtil.chineseCompare(o1.getName(), o2.getName())); | |||||
listRet.addAll(list); | listRet.addAll(list); | ||||
@@ -0,0 +1,34 @@ | |||||
package cc.smtweb.system.bpm.web.design.table; | |||||
import cc.smtweb.framework.core.common.SwConsts; | |||||
import cc.smtweb.framework.core.db.vo.ModelCatalog; | |||||
import cc.smtweb.framework.core.mvc.service.TreeHelper; | |||||
import java.util.ArrayList; | |||||
import java.util.Collection; | |||||
import java.util.Comparator; | |||||
import java.util.List; | |||||
/** | |||||
* Created by Akmm at 2022/3/25 10:47 | |||||
*/ | |||||
public class ModelCatalogTreeHelper extends TreeHelper<ModelCatalog> { | |||||
//按项目的缓存key,确保此表缓存配置:prj:prjid_parentid | |||||
private final static String CACHE_KEY = "prj"; | |||||
public ModelCatalogTreeHelper() { | |||||
super(ModelCatalog.ENTITY_NAME); | |||||
} | |||||
public List<ModelCatalog> getChildren(long id, long prj_id, Comparator<ModelCatalog> comparator) { | |||||
if (id > 0) { | |||||
return getChildren(id, comparator); | |||||
} | |||||
Collection<ModelCatalog> set = cache.getListByKey(CACHE_KEY, prj_id + SwConsts.SPLIT_CHAR + SwConsts.DEF_ROOT_ID); | |||||
List<ModelCatalog> list = set == null ? new ArrayList<>() : new ArrayList<>(set); | |||||
if (comparator != null) | |||||
list.sort(comparator); | |||||
return list; | |||||
} | |||||
} |