merge-update from trunk
[cdmlib.git] / cdmlib-ext / src / main / java / eu / etaxonomy / cdm / ext / ipni / IpniService.java
index 89e7c713ce29e9e8c46c5eb07972db351d36aa9b..37d00782356ec19a0577892cb57f3293e25bf7ea 100644 (file)
@@ -54,7 +54,6 @@ import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;\r
 import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;\r
 \r
-\r
 /**\r
 * @author a.mueller\r
 * @created Aug 16, 2010\r
@@ -167,6 +166,7 @@ public class IpniService  implements IIpniService{
                 AUTHOR,\r
                 NAME,\r
                 PUBLICATION,\r
+                ID\r
        }\r
        \r
        public enum IpniRank{\r
@@ -277,7 +277,7 @@ public class IpniService  implements IIpniService{
                result = buildAuthorList(content, services, config);\r
             }else if (serviceType.equals(ServiceType.NAME)){\r
                result = buildNameList(content, services, config);\r
-            }else{\r
+            }else {\r
                result = buildPublicationList(content, services, config);\r
             }\r
             if(responseCode == HttpURLConnection.HTTP_OK){\r
@@ -296,6 +296,43 @@ public class IpniService  implements IIpniService{
         // error\r
         return null;\r
     }\r
+       \r
+       public InputStream queryServiceForID (String request, URL serviceUrl){\r
+               \r
+               try {\r
+                       \r
+            // create the request url\r
+            URL newUrl = new URL(serviceUrl.getProtocol(),\r
+                                                     serviceUrl.getHost(),\r
+                                                     serviceUrl.getPort(),\r
+                                                     serviceUrl.getPath() \r
+                                                     + "?" + request);\r
+            \r
+            \r
+            URI newUri = newUrl.toURI();\r
+                       \r
+            logger.info("Firing request for URI: " + newUri);\r
+                \r
+            HttpResponse response = UriUtils.getResponse(newUri, null);\r
+            \r
+            int responseCode = response.getStatusLine().getStatusCode();\r
+            \r
+            // get the content at the resource\r
+            InputStream content = response.getEntity().getContent();\r
+            return content;\r
+           \r
+                  } catch (IOException e) {\r
+                   logger.error("No content for request: " + request);\r
+               } catch (URISyntaxException e) {\r
+                               logger.error("Given URL could not be transformed into URI", e);\r
+                       }\r
+         \r
+               return null;\r
+       \r
+       }\r
+\r
+       \r
+\r
 \r
        private List<Reference> buildPublicationList( InputStream content, ICdmApplicationConfiguration services, IIpniServiceConfigurator iConfig) throws IOException {\r
                IpniServicePublicationConfigurator config = (IpniServicePublicationConfigurator)iConfig;\r
@@ -352,7 +389,7 @@ public class IpniService  implements IIpniService{
                String author = valueMap.get(PUBLICATION_AUTHOR_TEAM);\r
                if (StringUtils.isNotBlank(author)){\r
                        Team team = Team.NewTitledInstance(author, author);\r
-                       ref.setAuthorTeam(team);\r
+                       ref.setAuthorship(team);\r
                }\r
                \r
                //remarks\r
@@ -362,12 +399,12 @@ public class IpniService  implements IIpniService{
 \r
 \r
                String tl2AuthorString = valueMap.get(TL2_AUTHOR);\r
-               if (ref.getAuthorTeam() == null){\r
+               if (ref.getAuthorship() == null){\r
                        Team tl2Author = Team.NewTitledInstance(tl2AuthorString, null);\r
-                       ref.setAuthorTeam(tl2Author);\r
+                       ref.setAuthorship(tl2Author);\r
                }else{\r
                        //TODO parse name, \r
-                       ref.getAuthorTeam().setTitleCache(tl2AuthorString, true);\r
+                       ref.getAuthorship().setTitleCache(tl2AuthorString, true);\r
                        ref.addAnnotation(Annotation.NewInstance(tl2AuthorString, AnnotationType.EDITORIAL(), Language.ENGLISH()));\r
                }\r
 \r
@@ -407,9 +444,11 @@ public class IpniService  implements IIpniService{
                \r
                String line = reader.readLine();\r
                while (StringUtils.isNotBlank(line)){\r
+                       \r
                        BotanicalName name = getNameFromLine(line,parameterMap, appConfig);\r
                        result.add(name);\r
                        line = reader.readLine();\r
+                       \r
                }\r
 \r
                \r
@@ -451,8 +490,8 @@ public class IpniService  implements IIpniService{
                name.setCombinationAuthorTeam(Team.NewTitledInstance(valueMap.get(PUBLISHING_AUTHOR), valueMap.get(PUBLISHING_AUTHOR)));\r
                \r
                //publication\r
-               Reference ref = ReferenceFactory.newGeneric();\r
-               ref.setTitleCache(valueMap.get(PUBLICATION));\r
+               Reference<?> ref = ReferenceFactory.newGeneric();\r
+               ref.setTitleCache(valueMap.get(PUBLICATION), true);\r
                TimePeriod datePublished = TimePeriodParser.parseString(valueMap.get(PUBLICATION_YEAR_FULL));\r
                name.setNomenclaturalReference(ref);\r
                \r
@@ -461,7 +500,7 @@ public class IpniService  implements IIpniService{
                String statusString = valueMap.get(NAME_STATUS);\r
                if (StringUtils.isNotBlank(statusString)){\r
                        try {\r
-                               statusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(statusString);\r
+                               statusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(statusString, name);\r
                                NomenclaturalStatus nomStatus = NomenclaturalStatus.NewInstance(statusType);\r
                                name.addStatus(nomStatus);\r
                        } catch (UnknownCdmTypeException e) {\r
@@ -884,6 +923,25 @@ public class IpniService  implements IIpniService{
                return serviceUrl;\r
        }\r
 \r
+\r
+       @Override\r
+       public InputStream getNamesById(String id) {\r
+               \r
+       \r
+               String request = "id="+id + "&output_format=lsid-metadata";\r
+               return queryServiceForID(request, getServiceUrl(IIpniService.ID_NAMESEARCH_SERVICE_URL));\r
+               \r
+       }\r
+       \r
+       @Override\r
+       public InputStream getPublicationsById(String id) {\r
+               \r
+       \r
+               String request = "id="+id ;\r
+               return queryServiceForID(request, getServiceUrl(IIpniService.ID_PUBLICATION_SERVICE_URL));\r
+               \r
+       }\r
+\r
                \r
        \r
 }\r