import java.io.IOException;\r
import java.lang.reflect.InvocationTargetException;\r
import java.lang.reflect.Method;\r
+import java.lang.reflect.ParameterizedType;\r
+import java.lang.reflect.Type;\r
import java.util.ArrayList;\r
import java.util.Arrays;\r
import java.util.Collection;\r
import org.apache.commons.io.FilenameUtils;\r
import org.apache.commons.lang.StringUtils;\r
import org.hibernate.mapping.Map;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
import org.springframework.web.bind.WebDataBinder;\r
import org.springframework.web.bind.annotation.InitBinder;\r
import org.springframework.web.bind.annotation.PathVariable;\r
import eu.etaxonomy.cdm.api.service.IService;\r
import eu.etaxonomy.cdm.api.service.pager.Pager;\r
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;\r
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;\r
import eu.etaxonomy.cdm.model.common.CdmBase;\r
+import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;\r
import eu.etaxonomy.cdm.remote.editor.UUIDPropertyEditor;\r
\r
/**\r
protected Class<T> baseClass;\r
\r
public abstract void setService(SERVICE service);\r
+ \r
+ public BaseController (){\r
+ \r
+ Type superClass = this.getClass().getGenericSuperclass();\r
+ if(superClass instanceof ParameterizedType){\r
+ ParameterizedType parametrizedSuperClass = (ParameterizedType) superClass;\r
+ Type[] typeArguments = parametrizedSuperClass.getActualTypeArguments();\r
+ \r
+ if(typeArguments.length > 1 && typeArguments[0] instanceof Class<?>){\r
+ baseClass = (Class<T>) typeArguments[0];\r
+ } else {\r
+ logger.error("unable to find baseClass");\r
+ }\r
+ }\r
+ }\r
\r
@InitBinder\r
public void initBinder(WebDataBinder binder) {\r
public T doGet(@PathVariable("uuid") UUID uuid,\r
HttpServletRequest request, \r
HttpServletResponse response) throws IOException {\r
- logger.info("doGet() " + request.getServletPath());\r
+ if(request != null)\r
+ logger.info("doGet() " + request.getServletPath());\r
T obj = (T) getCdmBaseInstance(uuid, response, initializationStrategy);\r
return obj;\r
}\r
String servletPath = request.getServletPath();\r
String baseName = FilenameUtils.getBaseName(servletPath);\r
\r
- logger.info("doGetMethod()[doGet" + StringUtils.capitalize(baseName) + "] " + request.getServletPath());\r
+ if(request != null)\r
+ logger.info("doGetMethod()[doGet" + StringUtils.capitalize(baseName) + "] " + request.getServletPath());\r
\r
- T instance = getCdmBaseInstance(uuid, response, (List<String>)null);\r
+ // <CUT\r
+// T instance = getCdmBaseInstance(uuid, response, (List<String>)null);\r
\r
//Class<?> propertyClass = propertyClass(instance, baseName);\r
\r
- Object objectFromProperty = invokeProperty(instance, baseName, response);\r
+ Object objectFromProperty = getCdmBaseProperty(uuid, baseName, response);// invokeProperty(instance, baseName, response);\r
+ \r
+ // CUT>\r
\r
if(objectFromProperty != null){\r
\r
if( Collection.class.isAssignableFrom(objectFromProperty.getClass())){\r
- // Map types cannot be returend as list or in a pager!\r
+ // Map types cannot be returned as list or in a pager!\r
\r
Collection c = (Collection)objectFromProperty;\r
if(start != null){\r
modelAndView.addObject(sub_c);\r
\r
} else {\r
+ //FIXME use real paging mechanism of according service class instead of subCollection()\r
+ //FIXME use BaseListController.normalizeAndValidatePagerParameters(pageNumber, pageSize, response);\r
pageSize = (pageSize == null ? DEFAULT_PAGE_SIZE : pageSize);\r
pageNumber = (pageNumber == null ? 0 : pageNumber);\r
start = pageNumber * pageSize;\r
}\r
}\r
\r
+ public Object getCdmBaseProperty(UUID uuid, String property, HttpServletResponse response) throws IOException{\r
+ \r
+ T instance = (T) HibernateProxyHelper.deproxy(getCdmBaseInstance(uuid, response, property));\r
+ \r
+ Object objectFromProperty = invokeProperty(instance, property, response);\r
+ \r
+ return objectFromProperty;\r
+ }\r
+ \r
private Class<?> propertyClass(T instance, String baseName) {\r
PropertyDescriptor propertyDescriptor = null;\r
Class<?> c = null;\r
Object result = null; \r
try {\r
PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(instance, baseName);\r
+ if(propertyDescriptor == null){\r
+ throw new NoSuchMethodException("No such method: " + instance.getClass().getSimpleName() + ".get" + baseName);\r
+ }\r
Method method = propertyDescriptor.getReadMethod();\r
\r
Class<?> returnType = method.getReturnType();\r
\r
if(CdmBase.class.isAssignableFrom(returnType) \r
|| Collection.class.isAssignableFrom(returnType) \r
- || Map.class.isAssignableFrom(returnType)){\r
+ || Map.class.isAssignableFrom(returnType)\r
+ || INomenclaturalReference.class.isAssignableFrom(returnType)){\r
+ \r
result = method.invoke(instance, (Object[])null);\r
+ \r
+ result = HibernateProxyHelper.deproxy(result);\r
+ \r
}else{\r
HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);\r
}\r