Skip to content

Commit ef8a5e4

Browse files
committed
Relaxed isReadOnly() constraint on save/post requests on plural-forms of a model (e.g. post /users). Also added experimental method lookup for anonymous classes.
1 parent e33c3ea commit ef8a5e4

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

src/javaxt/express/WebService.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,34 @@ public ServiceResponse getServiceResponse(ServiceRequest request, Database datab
169169
}
170170

171171

172+
173+
//Experimental special case for anonymous classes
174+
if (this.getClass().isAnonymousClass()){
175+
for (Method m : this.getClass().getMethods()){
176+
if (Modifier.isPrivate(m.getModifiers())) continue;
177+
178+
if (m.getReturnType().equals(ServiceResponse.class)){
179+
180+
Class<?>[] params = m.getParameterTypes();
181+
if (params.length>0){
182+
if (ServiceRequest.class.isAssignableFrom(params[0])){
183+
String key = m.getName();
184+
if (!strictLookup) key = key.toLowerCase();
185+
ArrayList<Method> methods = serviceMethods.get(key);
186+
if (methods==null){
187+
methods = new ArrayList<>();
188+
serviceMethods.put(key, methods);
189+
methods.add(m);
190+
}
191+
//methods.add(m);
192+
}
193+
}
194+
}
195+
}
196+
}
197+
198+
199+
172200
//Find service methods that implement the requested method
173201
ArrayList<Method> methods = null;
174202
if (serviceMethods.containsKey(methodName)){
@@ -275,7 +303,7 @@ else if (method.startsWith("save")){
275303
}
276304

277305

278-
//Special case for plural-form of a model
306+
//Special case for plural-form of a model. Return list of models.
279307
if (className.endsWith("ies")){ //Categories == Category
280308
c = getClass(className.substring(0, className.length()-3) + "y");
281309
}
@@ -285,14 +313,7 @@ else if (className.endsWith("ses")){ //Classes == Class
285313
else if (className.endsWith("s")){ //Sources == Source
286314
c = getClass(className.substring(0, className.length()-1));
287315
}
288-
if (c!=null){
289-
if (c.isReadOnly()){
290-
return list(c.c, request, database);
291-
}
292-
else{
293-
return new ServiceResponse(501, "Not Implemented.");
294-
}
295-
}
316+
if (c!=null) return list(c.c, request, database);
296317

297318
}
298319
else if (method.startsWith("delete")){
@@ -767,6 +788,7 @@ protected static HashMap<String, Object> getTableAndFields(Class c) throws Excep
767788
String tableName;
768789
HashMap<String, String> fieldMap = new HashMap<>();
769790
HashSet<String> stringFields = new HashSet<>();
791+
HashSet<String> arrayFields = new HashSet<>();
770792
HashSet<String> spatialFields = new HashSet<>();
771793

772794
Object obj = c.newInstance(); //maybe clone instead?
@@ -802,15 +824,21 @@ protected static HashMap<String, Object> getTableAndFields(Class c) throws Excep
802824
spatialFields.add(f.getName());
803825
}
804826

805-
if (fieldType.equals(String.class)){
827+
if (fieldType.equals(String.class) || fieldType.equals(String[].class)){
806828
stringFields.add(f.getName());
807829
}
830+
831+
if (fieldType.isArray()){
832+
arrayFields.add(f.getName());
833+
}
834+
808835
}
809836

810837
HashMap<String, Object> p = new HashMap<>();
811838
p.put("tableName", tableName);
812839
p.put("fieldMap", fieldMap);
813840
p.put("stringFields", stringFields);
841+
p.put("arrayFields", arrayFields);
814842
p.put("spatialFields", spatialFields);
815843
return p;
816844
}

0 commit comments

Comments
 (0)