ref #9327 , ref #9211 adapt some classpaths for webservice and BeanInitializer for...
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / ClassificationPortalListController.java
index 412834eba08d4fe4b2ab6fefc45d3dca5cff48ba..f8b28232d05729215e7b92c87f71c49b7623f1f3 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$\r
 /**\r
  * Copyright (C) 2009 EDIT European Distributed Institute of Taxonomy\r
  * http://www.e-taxonomy.eu\r
@@ -25,44 +24,54 @@ import org.springframework.web.bind.annotation.InitBinder;
 import org.springframework.web.bind.annotation.PathVariable;\r
 import org.springframework.web.bind.annotation.RequestMapping;\r
 import org.springframework.web.bind.annotation.RequestMethod;\r
+import org.springframework.web.bind.annotation.RequestParam;\r
 \r
 import eu.etaxonomy.cdm.api.service.IClassificationService;\r
+import eu.etaxonomy.cdm.api.service.ITaxonNodeService;\r
 import eu.etaxonomy.cdm.api.service.ITaxonService;\r
 import eu.etaxonomy.cdm.api.service.ITermService;\r
-import eu.etaxonomy.cdm.model.common.DefinedTermBase;\r
+import eu.etaxonomy.cdm.api.service.TaxonNodeDtoSortMode;\r
+import eu.etaxonomy.cdm.exception.FilterException;\r
+import eu.etaxonomy.cdm.exception.UnpublishedException;\r
 import eu.etaxonomy.cdm.model.name.Rank;\r
 import eu.etaxonomy.cdm.model.taxon.Classification;\r
 import eu.etaxonomy.cdm.model.taxon.Taxon;\r
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;\r
+import eu.etaxonomy.cdm.model.term.DefinedTermBase;\r
+import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;\r
 import eu.etaxonomy.cdm.remote.editor.RankPropertyEditor;\r
+import io.swagger.annotations.Api;\r
 \r
 /**\r
  * The ClassificationController class is a Spring MVC Controller.\r
  * @author a.kohlbecker\r
- * @date 20.03.2009\r
+ * @since 20.03.2009\r
  */\r
 @Controller\r
-public class ClassificationPortalListController extends IdentifiableListController<Classification,IClassificationService> {\r
+@Api("portal_classification")\r
+@RequestMapping(value="/portal/classification")\r
+public class ClassificationPortalListController extends AbstractIdentifiableListController<Classification,IClassificationService> {\r
 \r
+    public static final Logger logger = Logger.getLogger(ClassificationPortalListController.class);\r
 \r
     private static final List<String> CLASSIFICATION_INIT_STRATEGY = Arrays.asList(new String[]{\r
-            "reference.authorTeam"\r
+            "source.citation.authorship"\r
     });\r
 \r
     private static final List<String> NODE_INIT_STRATEGY = Arrays.asList(new String[]{\r
-            "taxon.sec",\r
-            "taxon.name"\r
-            });\r
-\r
-\r
-    public static final Logger logger = Logger.getLogger(ClassificationPortalListController.class);\r
+            "taxon.name.rank",\r
+            "taxon.secSource.citation"\r
+    });\r
 \r
     private ITaxonService taxonService;\r
-\r
-    private IClassificationService service;\r
+    private ITaxonNodeService taxonNodeService;\r
 \r
     private ITermService termService;\r
 \r
+    public ClassificationPortalListController() {\r
+        setInitializationStrategy(CLASSIFICATION_INIT_STRATEGY);\r
+    }\r
+\r
     @Override\r
     @Autowired\r
     public void setService(IClassificationService service) {\r
@@ -79,6 +88,10 @@ public class ClassificationPortalListController extends IdentifiableListControll
         this.taxonService = taxonService;\r
     }\r
 \r
+    @Autowired\r
+    public void setTaxonNodeService(ITaxonNodeService taxonNodeService) {\r
+        this.taxonNodeService = taxonNodeService;\r
+    }\r
 \r
     @InitBinder\r
     @Override\r
@@ -87,75 +100,6 @@ public class ClassificationPortalListController extends IdentifiableListControll
         binder.registerCustomEditor(Rank.class, new RankPropertyEditor());\r
     }\r
 \r
-\r
-    /**\r
-     * Lists all available {@link Classification}s.\r
-     * <p>\r
-     * URI: <b>&#x002F;{datasource-name}&#x002F;portal&#x002F;classification</b>\r
-     *\r
-     * @param request\r
-     * @param response\r
-     * @return a list of {@link Classification}s initialized by\r
-     *         the {@link #CLASSIFICATION_INIT_STRATEGY}\r
-     * @throws IOException\r
-     */\r
-    @RequestMapping(value = { "/portal/classification" }, method = RequestMethod.GET)\r
-    public List<Classification> getClassifications(HttpServletRequest request, HttpServletResponse response)\r
-            throws IOException {\r
-        logger.info("getClassification() " + requestPathAndQuery(request));\r
-        return service.list(null, null, null,null, CLASSIFICATION_INIT_STRATEGY);\r
-    }\r
-\r
-\r
-    /**\r
-     * @param treeUuid\r
-     * @param response\r
-     * @return\r
-     * @throws IOException\r
-     */\r
-    @RequestMapping(\r
-            value = {"/portal/classification/{treeUuid}/childNodes"},\r
-            method = RequestMethod.GET)\r
-    public List<TaxonNode> getChildNodes(\r
-            @PathVariable("treeUuid") UUID treeUuid,\r
-            HttpServletRequest request,\r
-            HttpServletResponse response\r
-            ) throws IOException {\r
-\r
-        return getChildNodesAtRank(treeUuid, null, request, response);\r
-    }\r
-\r
-\r
-    @RequestMapping(\r
-            value = {"/portal/classification/{treeUuid}/childNodesAt/{rankUuid}"},\r
-            method = RequestMethod.GET)\r
-    public List<TaxonNode> getChildNodesAtRank(\r
-            @PathVariable("treeUuid") UUID treeUuid,\r
-            @PathVariable("rankUuid") UUID rankUuid,\r
-            HttpServletRequest request,\r
-            HttpServletResponse response\r
-            ) throws IOException {\r
-\r
-        logger.info("getChildNodesAtRank() " + request.getRequestURI());\r
-        Classification tree = null;\r
-        Rank rank = null;\r
-        if(treeUuid != null){\r
-            // get view and rank\r
-            tree = service.find(treeUuid);\r
-\r
-            if(tree == null) {\r
-                response.sendError(404 , "Classification not found using " + treeUuid );\r
-                return null;\r
-            }\r
-        }\r
-        rank = findRank(rankUuid);\r
-\r
-        return service.listRankSpecificRootNodes(tree, rank, null, null, NODE_INIT_STRATEGY);\r
-    }\r
-\r
-\r
-\r
-\r
     /**\r
      * Lists all child-{@link TaxonNode}s of the specified {@link Taxon} in the {@link Classification}. The\r
      * a given {@link Rank} is ignored in this method but for consistency reasons it has been allowed to included it into the URI.\r
@@ -174,20 +118,45 @@ public class ClassificationPortalListController extends IdentifiableListControll
      *         the {@link #NODE_INIT_STRATEGY}\r
      */\r
     @RequestMapping(\r
-            value = {"/portal/classification/{treeUuid}/childNodesOf/{taxonUuid}"},\r
+            value = {"{treeUuid}/childNodesOf/{taxonUuid}"},\r
             method = RequestMethod.GET)\r
-    public List<TaxonNode> getChildNodesOfTaxon(\r
+    public List<TaxonNodeDto> getChildNodesOfTaxon(\r
             @PathVariable("treeUuid") UUID treeUuid,\r
             @PathVariable("taxonUuid") UUID taxonUuid,\r
+            @RequestParam(value = "subtree", required = false) UUID subtreeUuid,\r
+            @RequestParam(value = "sortMode", required = false, defaultValue = ClassificationController.DEFAULT_TAXONNODEDTO_SORT_MODE) TaxonNodeDtoSortMode sortMode,\r
             HttpServletRequest request,\r
             HttpServletResponse response) throws IOException {\r
         logger.info("getChildNodesOfTaxon() " + request.getRequestURI());\r
 \r
-        Classification tree = service.find(treeUuid);\r
-        Taxon taxon = (Taxon) taxonService.load(taxonUuid);\r
-        List<TaxonNode> childs = service.loadChildNodesOfTaxon(taxon, tree, NODE_INIT_STRATEGY);\r
-        return childs;\r
+        boolean includeUnpublished = NO_UNPUBLISHED;  //for now we do not allow any remote service to publish unpublished data\r
+\r
+        List<TaxonNodeDto> children;\r
+        try {\r
+            children = service.listChildNodeDtosOfTaxon(taxonUuid, treeUuid, subtreeUuid,\r
+                    includeUnpublished, null, null, sortMode, NODE_INIT_STRATEGY);\r
+        } catch (FilterException e) {\r
+            HttpStatusMessage.SUBTREE_FILTER_INVALID.send(response);\r
+            return null;\r
+        }\r
+        return children;\r
+\r
+    }\r
 \r
+    @RequestMapping(\r
+            value = {"{treeUuid}/siblingsOf/{taxonUuid}"},\r
+            method = RequestMethod.GET)\r
+    public List<TaxonNode> getSiblingsOfTaxon(\r
+            @PathVariable("treeUuid") UUID classificationUuid,\r
+            @PathVariable("taxonUuid") UUID taxonUuid,\r
+            HttpServletRequest request,\r
+            @SuppressWarnings("unused") HttpServletResponse response) {\r
+        logger.info("getSiblingsOfTaxon() " + request.getRequestURI());\r
+\r
+        boolean includeUnpublished = NO_UNPUBLISHED;\r
+        //FIXME return pager\r
+        List<TaxonNode> childs = service.listSiblingsOfTaxon(taxonUuid, classificationUuid, includeUnpublished, null, null, NODE_INIT_STRATEGY);\r
+        return childs;\r
     }\r
 \r
     /**\r
@@ -206,23 +175,37 @@ public class ClassificationPortalListController extends IdentifiableListControll
      * @param request\r
      * @return a List of {@link TaxonNode} entities initialized by\r
      *         the {@link #NODE_INIT_STRATEGY}\r
+     * @throws IOException\r
      */\r
     @RequestMapping(\r
-            value = {"/portal/classification/{treeUuid}/pathFrom/{taxonUuid}/toRank/{rankUuid}"},\r
+            value = {"{treeUuid}/pathFrom/{taxonUuid}/toRank/{rankUuid}"},\r
             method = RequestMethod.GET)\r
-    public List<TaxonNode> getPathFromTaxonToRank(\r
-            @PathVariable("treeUuid") UUID treeUuid,\r
+    public List<TaxonNodeDto> getPathFromTaxonToRank(\r
+            @PathVariable("treeUuid") UUID classificationUuid,\r
             @PathVariable("taxonUuid") UUID taxonUuid,\r
             @PathVariable("rankUuid") UUID rankUuid,\r
+            @RequestParam(value = "subtree", required = false) UUID subtreeUuid,\r
             HttpServletRequest request,\r
             HttpServletResponse response) throws IOException {\r
         logger.info("getPathFromTaxonToRank() " + request.getRequestURI());\r
 \r
-        Classification tree = service.find(treeUuid);\r
+        boolean includeUnpublished = NO_UNPUBLISHED;\r
+\r
+        Classification classification = service.find(classificationUuid);\r
+        TaxonNode subtree = getSubtreeOrError(subtreeUuid, taxonNodeService, response);\r
         Rank rank = findRank(rankUuid);\r
         Taxon taxon = (Taxon) taxonService.load(taxonUuid);\r
-\r
-        return service.loadTreeBranchToTaxon(taxon, tree, rank, NODE_INIT_STRATEGY);\r
+        if(classification == null){\r
+            HttpStatusMessage.UUID_INVALID.send(response, "Classification uuid does not exist.");\r
+            return null;\r
+        }\r
+        try {\r
+            List<TaxonNodeDto> result = service.loadTreeBranchDTOsToTaxon(taxon, classification, subtree, rank, includeUnpublished, NODE_INIT_STRATEGY);\r
+            return result;\r
+        } catch (UnpublishedException e) {\r
+            HttpStatusMessage.ACCESS_DENIED.send(response);\r
+            return null;\r
+        }\r
     }\r
 \r
     /**\r
@@ -240,32 +223,31 @@ public class ClassificationPortalListController extends IdentifiableListControll
      * @param request\r
      * @return a List of {@link TaxonNode} entities initialized by\r
      *         the {@link #NODE_INIT_STRATEGY}\r
+     * @throws IOException\r
      */\r
     @RequestMapping(\r
-            value = {"/portal/classification/{treeUuid}/pathFrom/{taxonUuid}"},\r
+            value = {"{treeUuid}/pathFrom/{taxonUuid}"},\r
             method = RequestMethod.GET)\r
-    public List<TaxonNode> getPathFromTaxon(\r
-            @PathVariable("treeUuid") UUID treeUuid,\r
+    public List<TaxonNodeDto> getPathFromTaxon(\r
+            @PathVariable("treeUuid") UUID classificationUuid,\r
             @PathVariable("taxonUuid") UUID taxonUuid,\r
+            @RequestParam(value = "subtree", required = false) UUID subtreeUuid,\r
             HttpServletRequest request,\r
             HttpServletResponse response) throws IOException {\r
 \r
-        return getPathFromTaxonToRank(treeUuid, taxonUuid, null, request, response);\r
+        return getPathFromTaxonToRank(classificationUuid, taxonUuid, null, subtreeUuid, request, response);\r
     }\r
 \r
-\r
     private Rank findRank(UUID rankUuid) {\r
         Rank rank = null;\r
         if(rankUuid != null){\r
-            DefinedTermBase dt =  termService.find(rankUuid);\r
+            DefinedTermBase<?> dt =  termService.find(rankUuid);\r
             if(dt instanceof Rank){\r
                 rank = (Rank)dt;\r
             } else {\r
-               new IllegalArgumentException("DefinedTermBase is not a Rank");\r
+               throw new IllegalArgumentException("DefinedTermBase is not a Rank");\r
             }\r
         }\r
         return rank;\r
     }\r
-\r
-\r
 }
\ No newline at end of file