1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.logic;
17
18 import java.util.Collection;
19 import java.util.Map;
20
21 import org.seasar.tuigwaa.logic.interceptor.DataSaveInterceptor;
22 import org.seasar.tuigwaa.logic.interceptor.PageViewInterceptor;
23 import org.seasar.tuigwaa.util.TgwPathResolver;
24
25 import com.isenshi.util.ResourceUtils;
26 import com.isenshi.util.TableMap;
27 import com.isenshi.util.extlib.DiconResource;
28
29 public class LogicServiceImpl implements LogicService {
30
31 private TableMap logicMap = new TableMap();
32
33
34 private PageViewInterceptor pageViewInterceptor;
35
36 private DataSaveInterceptor dataSaveInterceptor;
37
38 private ExternalLogicContainer extLogicContainer;
39
40 public LogicServiceImpl() {
41 }
42
43
44 public void setDataSaveInterceptor(DataSaveInterceptor dataSaveInterceptor) {
45 this.dataSaveInterceptor = dataSaveInterceptor;
46 }
47
48
49 public void setPageViewInterceptor(PageViewInterceptor pageViewInterceptor) {
50 this.pageViewInterceptor = pageViewInterceptor;
51 }
52
53
54 public void setExtLogicContainer(ExternalLogicContainer extLogicContainer) {
55 this.extLogicContainer = extLogicContainer;
56 }
57
58 public TgwLogic getLogic(String siteName, String logicName) {
59 return (TgwLogic) logicMap.get(siteName, logicName);
60 }
61
62 public void editLogic(String siteName, TgwLogic logic) {
63 deleteLogic(siteName, logic.getName());
64 addLogic(siteName, logic);
65 }
66
67 public void deleteLogic(String siteName, String logicName) {
68 TgwLogic tlogic = (TgwLogic) logicMap.get(siteName, logicName);
69
70 if (tlogic == null) {
71 return;
72 }
73
74 TgwEvent tevent = tlogic.getEvent();
75 String eventOption = tevent.getOption();
76
77 if (TgwEvent.PAGE_VIEW.equals(tevent.getType())) {
78 String pageName = eventOption;
79 pageViewInterceptor.deregisterAction(siteName, pageName, logicName);
80 } else if (TgwEvent.BEFORE_DATA_SAVE.equals(tevent.getType())) {
81 String entityName = eventOption;
82 dataSaveInterceptor.deregisterBeforeAction(siteName, entityName);
83 } else if (TgwEvent.BEFORE_CUSTOMFORM_SAVE.equals(tevent.getType())) {
84 String customFormName = eventOption;
85 dataSaveInterceptor.deregisterBeforeCustomFormAction(siteName,
86 customFormName);
87 } else if (TgwEvent.AFTER_DATA_SAVE.equals(tevent.getType())) {
88 String entityName = eventOption;
89 dataSaveInterceptor.deregisterAfterAction(siteName, entityName);
90 } else if (TgwEvent.AFTER_CUSTOMFORM_SAVE.equals(tevent.getType())) {
91 String customFormName = eventOption;
92 dataSaveInterceptor.deregisterAfterCustomFormAction(siteName,
93 customFormName);
94 }
95 deregisterLogic(siteName, logicName);
96 }
97
98 public void addLogic(String siteName, TgwLogic logic) {
99 TgwEvent event = logic.getEvent();
100 String eventOption = event.getOption();
101 TgwFunction function = logic.getFunction();
102
103 if (TgwEvent.PAGE_VIEW.equals(event.getType())) {
104 String pageName = eventOption;
105 pageViewInterceptor.registerAction(siteName, pageName, logic
106 .getName(), function);
107 registerLogic(siteName, logic);
108 } else if (TgwEvent.BEFORE_DATA_SAVE.equals(event.getType())) {
109 String entityName = eventOption;
110 dataSaveInterceptor.registerBeforeAction(siteName, entityName,
111 function);
112 registerLogic(siteName, logic);
113 } else if (TgwEvent.BEFORE_CUSTOMFORM_SAVE.equals(event.getType())) {
114 String customFormName = eventOption;
115 dataSaveInterceptor.registerBeforeCustomFormAction(siteName,
116 customFormName, function);
117 registerLogic(siteName, logic);
118 } else if (TgwEvent.AFTER_DATA_SAVE.equals(event.getType())) {
119 String entityName = eventOption;
120 dataSaveInterceptor.registerAfterAction(siteName, entityName,
121 function);
122 registerLogic(siteName, logic);
123 } else if (TgwEvent.AFTER_CUSTOMFORM_SAVE.equals(event.getType())) {
124 String customFormName = eventOption;
125 dataSaveInterceptor.registerAfterCustomFormAction(siteName,
126 customFormName, function);
127 registerLogic(siteName, logic);
128 } else if (TgwEvent.NOW.equals(event.getType())) {
129 function.run(null);
130 }
131 }
132
133 private void registerLogic(String siteName, TgwLogic logic) {
134 String logicName = logic.getName();
135 logicMap.put(logic, siteName, logicName);
136 }
137
138 private void deregisterLogic(String siteName, String logicName) {
139 logicMap.remove(siteName, logicName);
140 }
141
142 public void saveLogics(String siteName) {
143 DiconResource resource = new DiconResource();
144 resource.addComponentAll(getLogicList(siteName));
145 resource.setPath(TgwPathResolver.getLogicFilePath(siteName));
146 resource.save();
147 }
148
149 public void loadLogics(String siteName) {
150 String path = TgwPathResolver.getLogicFilePath(siteName);
151
152 String extLogic = TgwPathResolver.getExternalLogicDicon(siteName);
153 if (ResourceUtils.isExist(extLogic)) {
154 extLogicContainer.create(siteName, new String[] { extLogic });
155 }
156
157 DiconResource resource = new DiconResource();
158 resource.setPath(path);
159 resource.load();
160 if (!resource.hasContainer()) {
161 return;
162 }
163 Object[] logics = resource.findComponents(TgwLogic.class);
164 for (int i = 0; i < logics.length; i++) {
165 addLogic(siteName, (TgwLogic) logics[i]);
166 }
167 }
168
169 public Collection getLogicList(String siteName) {
170 Map map = logicMap.getSubMap(siteName);
171
172 if (map == null) {
173 return null;
174 } else {
175 return map.values();
176 }
177 }
178
179 public void addExternalLogics() {
180
181 }
182
183 }