1
2
3
4
5 package gboat2.base.core.util;
6
7 import gboat2.base.core.annotation.FormPage;
8 import gboat2.base.core.annotation.ListPage;
9 import gboat2.base.core.web.BaseActionSupport;
10
11 import java.lang.reflect.Method;
12 import java.util.Map;
13 import java.util.Set;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16
17 import org.apache.commons.lang3.text.WordUtils;
18 import org.apache.struts2.convention.annotation.ResultPath;
19 import org.springframework.util.ReflectionUtils;
20
21 import com.opensymphony.xwork2.ActionContext;
22 import com.opensymphony.xwork2.config.Configuration;
23 import com.opensymphony.xwork2.config.entities.ActionConfig;
24
25
26
27
28
29
30
31
32
33 public abstract class ActionUtil {
34
35 public static final String ACTION_EXTENSION = ".do";
36
37
38
39
40
41
42 public static String getUriPrefix(String actionClassName) {
43 StringBuilder prefix = new StringBuilder();
44 if (actionClassName.indexOf(".") != -1) {
45 actionClassName = actionClassName.substring(actionClassName.lastIndexOf(".") + 1);
46 }
47 String pathName = actionClassName.replaceAll("Action$", "");
48 Pattern regrex = Pattern.compile("[A-Z][a-z]*");
49 Matcher matcher = regrex.matcher(pathName);
50 while (matcher.find()) {
51 if (prefix.length() != 0) {
52 prefix.append("-");
53 }
54 prefix.append(matcher.group().toLowerCase());
55 }
56 return prefix.toString();
57 }
58
59
60
61
62
63
64 public static String getNamespace(String actionClassName) {
65 Configuration configuration = ActionContext.getContext().getContainer().getInstance(Configuration.class);
66 Map<String, Map<String, ActionConfig>> allActionConfigs = configuration.getRuntimeConfiguration().getActionConfigs();
67 if (allActionConfigs != null) {
68 Set<String> namespaces = allActionConfigs.keySet();
69 if (null != namespaces) {
70 for (String namespace : namespaces) {
71 Map<String, ActionConfig> actions = allActionConfigs.get(namespace);
72 if (null != actions) {
73 Set<Map.Entry<String, ActionConfig>> actionConfigs = actions.entrySet();
74 if (null != actionConfigs) {
75 for (Map.Entry<String, ActionConfig> actionConfig : actionConfigs) {
76
77 String actionClass = actionConfig.getValue().getClassName();
78 if (actionClassName.equals(actionClass)) {
79 return (null == namespace) ? "" : namespace;
80 }
81 }
82 }
83 }
84 }
85 }
86 }
87 return "";
88 }
89
90
91
92
93
94
95 public static String getUri(String actionClassName) {
96 String namespace = getNamespace(actionClassName);
97 if (!"".equals(namespace) && !namespace.endsWith("/")) {
98 namespace += "/";
99 }
100 return namespace + getUriPrefix(actionClassName) + ACTION_EXTENSION;
101 }
102
103
104
105
106
107
108
109 public static String getUri(String actionClassName, String methodName) {
110 String namespace = getNamespace(actionClassName);
111 if (!"".equals(namespace) && !namespace.endsWith("/")) {
112 namespace += "/";
113 }
114 return namespace + getUriPrefix(actionClassName) + "!" + methodName + ACTION_EXTENSION;
115 }
116
117
118
119
120
121
122 public static String getDefaultResultPath(String className){
123 if(className.indexOf(".")!=-1){
124 className = className.substring(className.lastIndexOf(".")+1);
125 }
126 className = className.replaceAll("Action$", "");
127 return "/content/"+String.valueOf(className.charAt(0)).toLowerCase()+className.substring(1);
128 }
129
130
131
132
133
134 public static String getPathPrefix(Class<?> actionClass) {
135 StringBuffer pathPrefix = new StringBuffer();
136 if(actionClass.isAnnotationPresent(ResultPath.class)){
137 ResultPath path = actionClass.getAnnotation(ResultPath.class);
138 pathPrefix.append(path.value());
139 }else{
140 pathPrefix.append("/content/");
141 String className = actionClass.getName().replace(actionClass.getPackage().getName() + ".", "");
142 className = className.replaceAll("Action$", "");
143 pathPrefix.append(String.valueOf(className.charAt(0)).toLowerCase());
144 pathPrefix.append(className.substring(1));
145 }
146
147 return pathPrefix.toString();
148 }
149
150
151
152
153
154 public static String getPathPrefix(String className) {
155 StringBuffer pathPrefix = new StringBuffer();
156
157 pathPrefix.append("/content/");
158 className = className.replaceAll("Action$", "");
159 pathPrefix.append(String.valueOf(className.charAt(0)).toLowerCase());
160 pathPrefix.append(className.substring(1));
161
162 return pathPrefix.toString();
163 }
164
165 public static boolean isListPage(String invokeMethod,BaseActionSupport action){
166 if ("list".equals(invokeMethod))
167 return true;
168
169 Method method = ReflectionUtils.findMethod(action.getClass(), invokeMethod);
170 if (method != null)
171 return method.isAnnotationPresent(ListPage.class);
172
173
174 method = ReflectionUtils.findMethod(action.getClass(), getInitMethodName(invokeMethod), new Class[] { Map.class });
175 if (method != null)
176 return method.isAnnotationPresent(ListPage.class);
177
178 return false;
179 }
180
181 public static String getInitMethodName(String invokeMethod) {
182 return "init" + WordUtils.capitalize(invokeMethod);
183 }
184
185 public static String getPostMethodName(String invokeMethod){
186 return "post" + WordUtils.capitalize(invokeMethod);
187 }
188
189 public static boolean isFormPage(String invokeMethod,BaseActionSupport action){
190 if ("edit".equals(invokeMethod) || "view".equals(invokeMethod)) {
191 return true;
192 } else {
193 Method method = ReflectionUtils.findMethod(action.getClass(), invokeMethod);
194 if (method != null) {
195 return method.isAnnotationPresent(FormPage.class);
196 } else {
197
198 method = ReflectionUtils.findMethod(action.getClass(), ActionUtil.getInitMethodName(invokeMethod));
199 if (method != null) {
200 return method.isAnnotationPresent(FormPage.class);
201 }
202 }
203 }
204
205 return false;
206 }
207 }