/
RobotTriggerControll.java
119 lines (104 loc) · 4.64 KB
/
RobotTriggerControll.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Copyright (c) REBUILD <https://getrebuild.com/> and its owners. All rights reserved.
rebuild is dual-licensed under commercial and open source licenses (GPLv3).
See LICENSE and COMMERCIAL in the project root for license information.
*/
package com.rebuild.web.admin.robot;
import cn.devezhao.persist4j.Entity;
import cn.devezhao.persist4j.engine.ID;
import com.rebuild.server.Application;
import com.rebuild.server.business.trigger.ActionFactory;
import com.rebuild.server.business.trigger.ActionType;
import com.rebuild.server.business.trigger.TriggerAction;
import com.rebuild.server.metadata.MetadataHelper;
import com.rebuild.server.metadata.MetadataSorter;
import com.rebuild.server.metadata.entity.EasyMeta;
import com.rebuild.utils.JSONUtils;
import com.rebuild.web.BasePageControll;
import com.rebuild.web.admin.entityhub.DataReportControll;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author devezhao zhaofang123@gmail.com
* @since 2019/05/23
*/
@Controller
@RequestMapping("/admin/robot/")
public class RobotTriggerControll extends BasePageControll {
@RequestMapping("triggers")
public ModelAndView pageList() throws IOException {
return createModelAndView("/admin/robot/trigger-list.jsp");
}
@RequestMapping("trigger/{id}")
public ModelAndView pageEditor(@PathVariable String id, HttpServletResponse response) throws IOException {
ID configId = ID.valueOf(id);
Object[] config = Application.createQuery(
"select belongEntity,actionType,when,whenFilter,actionContent,priority,name,whenTimer from RobotTriggerConfig where configId = ?")
.setParameter(1, configId)
.unique();
if (config == null) {
response.sendError(404, "触发器不存在");
return null;
}
Entity sourceEntity = MetadataHelper.getEntity((String) config[0]);
ActionType actionType = ActionType.valueOf((String) config[1]);
ModelAndView mv = createModelAndView("/admin/robot/trigger-design.jsp");
mv.getModel().put("configId", configId);
mv.getModel().put("sourceEntity", sourceEntity.getName());
mv.getModel().put("sourceEntityLabel", EasyMeta.getLabel(sourceEntity));
mv.getModel().put("actionType", actionType.name());
mv.getModel().put("actionTypeLabel", actionType.getDisplayName());
mv.getModel().put("when", config[2]);
mv.getModel().put("whenTimer", config[7] == null ? StringUtils.EMPTY : config[7]);
mv.getModel().put("whenFilter", StringUtils.defaultIfBlank((String) config[3], JSONUtils.EMPTY_OBJECT_STR));
mv.getModel().put("actionContent", StringUtils.defaultIfBlank((String) config[4], JSONUtils.EMPTY_OBJECT_STR));
mv.getModel().put("priority", config[5]);
mv.getModel().put("name", config[6]);
return mv;
}
@RequestMapping("trigger/available-actions")
public void getAvailableActions(HttpServletResponse response) throws IOException {
ActionType[] ts = ActionFactory.getAvailableActions();
List<String[]> list = new ArrayList<>();
for (ActionType t : ts) {
list.add(new String[] { t.name(), t.getDisplayName() });
}
writeSuccess(response, list);
}
@RequestMapping("trigger/available-entities")
public void getAvailableEntities(HttpServletRequest request, HttpServletResponse response) throws IOException {
String actionType = getParameterNotNull(request, "action");
TriggerAction action = ActionFactory.createAction(actionType);
List<String[]> list = new ArrayList<>();
for (Entity e : MetadataSorter.sortEntities()) {
if (MetadataHelper.isBizzEntity(e.getEntityCode())) {
continue;
}
if (action.isUsableSourceEntity(e.getEntityCode())) {
list.add(new String[] { e.getName(), EasyMeta.getLabel(e) });
}
}
writeSuccess(response, list);
}
@RequestMapping("trigger/list")
public void triggerList(HttpServletRequest request, HttpServletResponse response) throws IOException {
String belongEntity = getParameter(request, "entity");
String q = getParameter(request, "q");
String sql = "select configId,belongEntity,belongEntity,name,isDisabled,modifiedOn,when,actionType from RobotTriggerConfig" +
" where (1=1) and (2=2)" +
" order by name, modifiedOn desc";
Object[][] array = DataReportControll.queryListOfConfig(sql, belongEntity, q);
for (Object[] o : array) {
o[7] = ActionType.valueOf((String) o[7]).getDisplayName();
}
writeSuccess(response, array);
}
}