|
package com.ekexiu.project.msconsole.system.service;
import com.ekexiu.project.msconsole.system.dao.DictDao;
import com.ekexiu.project.msconsole.system.po.DictItem;
import org.jfw.apt.annotation.Autowrie;
import org.jfw.apt.annotation.Nullable;
import org.jfw.apt.web.annotation.Path;
import org.jfw.apt.web.annotation.operate.Get;
import org.jfw.apt.web.annotation.operate.Post;
import org.jfw.apt.web.annotation.param.AfterCommit;
import org.jfw.apt.web.annotation.param.JdbcConn;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@Path("/dict")
public class DictService {
private static final HashMap<String, List<DictItem>> dictCache = new HashMap<String, List<DictItem>>();
private static final List<DictItem> Empty_DICT = Collections.emptyList();
@Autowrie
private DictDao dictDao;
public DictDao getDictDao() {
return dictDao;
}
public void setDictDao(DictDao dictDao) {
this.dictDao = dictDao;
}
@Path("/load")
@Get
public void load(@JdbcConn Connection con) throws SQLException {
HashMap<String, List<DictItem>> map = new HashMap<String, List<DictItem>>();
List<DictItem> items = this.dictDao.query(con);
for (DictItem item : items) {
List<DictItem> list = map.get(item.getDictCode());
if (list == null) {
list = new LinkedList<DictItem>();
map.put(item.getDictCode(), list);
}
list.add(item);
}
synchronized(dictCache){
dictCache.clear();
dictCache.putAll(map);
}
}
@Path("/items")
@Get
public List<DictItem> query(String dict) {
List<DictItem> ret = null;
synchronized (dictCache) {
ret = dictCache.get(dict);
}
return ret == null ? Empty_DICT : ret;
}
@Path("/delete")
@Post
public int delete(@JdbcConn(true) Connection con, final String dict, final String code, @AfterCommit List<Runnable> runs) throws SQLException {
int num = this.dictDao.disable(con, dict, code);
if (num > 0) {
runs.add(new Runnable() {
@Override
public void run() {
synchronized (dictCache) {
List<DictItem> list = dictCache.get(dict);
if (list != null) {
List<DictItem> newlist = new LinkedList<DictItem>();
for (DictItem item : list) {
if (!item.getCode().equals(code)) {
newlist.add(item);
}
}
dictCache.put(dict, newlist);
}
}
}
});
}
return num;
}
@Post
@Path("/add")
public void insert(@JdbcConn(true) Connection con, String code, String dict, @Nullable String bcode, String caption, @Nullable String fullCaption,
@Nullable String shortCut, @Nullable String remark, boolean readonly, @AfterCommit List<Runnable> runs) throws SQLException {
final DictItem item = new DictItem();
item.setActive(true);
item.setBcode(bcode);
item.setCaption(caption);
item.setCode(code);
item.setDictCode(dict);
item.setFullCaption(fullCaption);
item.setReadonly(readonly);
item.setRemark(remark);
item.setShortCut(shortCut);
this.dictDao.insert(con, item);
runs.add(new Runnable() {
@Override
public void run() {
synchronized (dictCache) {
List<DictItem> newlist = new LinkedList<DictItem>();
List<DictItem> list = dictCache.get(item.getDictCode());
if (list != null) {
newlist.addAll(list);
}
newlist.add(item);
dictCache.put(item.getDictCode(), newlist);
}
}
});
}
@Post
@Path("/modify")
public int update(@JdbcConn(true) Connection con, String code, String dict, @Nullable String bcode, String caption, @Nullable String fullCaption,
@Nullable String shortCut, @Nullable String remark, @AfterCommit List<Runnable> runs) throws SQLException {
int num = this.dictDao.update(con, bcode, caption, fullCaption, shortCut, remark, code, dict);
if (num > 0) {
final DictItem item = new DictItem();
item.setActive(true);
item.setBcode(bcode);
item.setCaption(caption);
item.setCode(code);
item.setDictCode(dict);
item.setFullCaption(fullCaption);
item.setReadonly(false);
item.setRemark(remark);
item.setShortCut(shortCut);
runs.add(new Runnable() {
@Override
public void run() {
synchronized (dictCache) {
List<DictItem> newlist = new LinkedList<DictItem>();
List<DictItem> list = dictCache.get(item.getDictCode());
if (list != null) {
for (DictItem nitem : list) {
if (!nitem.getCode().equals(item.getCode())) {
newlist.add(nitem);
}
}
}
newlist.add(item);
dictCache.put(item.getDictCode(), newlist);
}
}
});
}
return num;
}
}
|