PhraseanetService.java

  1. /*
  2.  * Copyright (c) 2002-2014, Mairie de Paris
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  *  1. Redistributions of source code must retain the above copyright notice
  10.  *     and the following disclaimer.
  11.  *
  12.  *  2. Redistributions in binary form must reproduce the above copyright notice
  13.  *     and the following disclaimer in the documentation and/or other materials
  14.  *     provided with the distribution.
  15.  *
  16.  *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
  17.  *     contributors may be used to endorse or promote products derived from
  18.  *     this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
  24.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * License 1.0
  33.  */
  34. package fr.paris.lutece.plugins.phraseanet.service;


  35. import fr.paris.lutece.plugins.phraseanet.business.databox.Collection;
  36. import fr.paris.lutece.plugins.phraseanet.business.databox.Databox;
  37. import fr.paris.lutece.plugins.phraseanet.business.record.Record;
  38. import fr.paris.lutece.plugins.phraseanet.service.api.PhraseanetApiCallService;
  39. import fr.paris.lutece.plugins.phraseanet.service.parsers.CollectionsJsonParser;
  40. import fr.paris.lutece.plugins.phraseanet.service.parsers.DataboxesJsonParser;
  41. import fr.paris.lutece.plugins.phraseanet.service.parsers.EmbedJsonParser;
  42. import fr.paris.lutece.plugins.phraseanet.service.parsers.MetadatasJsonParser;
  43. import fr.paris.lutece.plugins.phraseanet.service.parsers.RecordJsonParser;
  44. import fr.paris.lutece.plugins.phraseanet.service.parsers.SearchResultsJsonParser;
  45. import java.text.MessageFormat;
  46. import java.util.ArrayList;
  47. import java.util.StringTokenizer;
  48. import net.sf.json.JSONObject;
  49. import net.sf.json.JSONArray;
  50. import net.sf.json.JSONException;
  51. import fr.paris.lutece.plugins.phraseanet.business.account.Account;
  52. import fr.paris.lutece.plugins.phraseanet.business.embed.Embed;
  53. import fr.paris.lutece.plugins.phraseanet.business.record.Metadata;
  54. import fr.paris.lutece.plugins.phraseanet.business.search.SearchResults;
  55. import fr.paris.lutece.plugins.phraseanet.service.api.PhraseanetApiCallException;
  56. import fr.paris.lutece.portal.service.util.AppPropertiesService;
  57. import java.util.HashMap;
  58. import java.util.List;
  59. import java.util.Map;

  60. import org.apache.log4j.Logger;



  61. /**
  62.  * PhraseanetService
  63.  */
  64. public final class PhraseanetService
  65. {
  66.     private static final String PROPERTY_ITEMS_PER_PAGE_VALUES = "phraseanet.itemsPerPageValues";
  67.     private static final String PROPERTY_MEDIA_TYPE_VALUES = "phraseanet.mediaTypeValues";
  68.     private static final String PATH_GET_RECORD = "/api/v1/records/{0}/{1}/";
  69.     private static final String PATH_GET_DATABOXE_METADATAS = "/api/v1/databoxes/{0}/metadatas/";
  70.     private static final String PATH_GET_RECORD_METADATAS = "/api/v1/records/{0}/{1}/metadatas/";
  71.     private static final String PATH_SEARCH = "/api/v1/records/search/?";
  72.     private static final String PATH_DATABOXES = "/api/v1/databoxes/list/";
  73.     private static final String PATH_COLLECTIONS = "/api/v1/databoxes/{0}/collections/";
  74.     private static final String PATH_EMBED = "/api/v1/records/{0}/{1}/embed/";
  75.     private static final String PARAMETER_QUERY = "query";
  76.     private static final String PARAMETER_PAGE = "page";
  77.     private static final String PARAMETER_PER_PAGE = "per_page";
  78.     private static final String PARAMETER_RECORD_TYPE = "record_type";
  79.     private static final String PARAMETER_BASES = "bases[]";
  80.     private static final String DELIMITER = ",";
  81.     private static final String SEARCH_ALL = "< All >";
  82.     private static List<String> _listItemsPerPageValues;
  83.     private static List<String> _listMediaTypeValues;
  84.     private static Logger _logger = Logger.getLogger( Constants.LOGGER );
  85.    
  86.     /** private constructor */
  87.     private PhraseanetService()
  88.     {
  89.        
  90.     }
  91.    

  92.     /**
  93.      * Gets a record
  94.      * @param nDataboxId The databox id
  95.      * @param nRecordId The record id
  96.      * @param account the user phraseanet account
  97.      * @return The record
  98.      * @throws PhraseanetApiCallException if an error occurs
  99.      */
  100.     public static Record getRecord( int nDataboxId, int nRecordId, Account account )
  101.         throws PhraseanetApiCallException
  102.     {
  103.         _logger.debug( "getRecord" );
  104.         Object[] arguments = { Integer.toString( nDataboxId ), Integer.toString( nRecordId ) };
  105.         String url = account.getAccessURL(  ) + MessageFormat.format( PATH_GET_RECORD, arguments );
  106.         JSONObject jsonResponse = PhraseanetApiCallService.getResponse( url, account );
  107.         JSONObject jsonRecord = jsonResponse.getJSONObject( "record" );

  108.         return RecordJsonParser.parse( jsonRecord );
  109.     }
  110.    
  111.     /**
  112.      * Get metadatas for a given databoxe
  113.      * @param nDataboxId The databox id
  114.      * @param account the user phraseanet account
  115.      * @return Metadatas
  116.      * @throws PhraseanetApiCallException if an error occurs
  117.      */
  118.     public static List<Metadata> getDataboxeMetadatas( int nDataboxId, Account account )
  119.         throws PhraseanetApiCallException
  120.     {
  121.         Object[] arguments = { Integer.toString( nDataboxId ) };
  122.         String url = account.getAccessURL(  ) + MessageFormat.format( PATH_GET_DATABOXE_METADATAS, arguments );
  123.         _logger.debug("getDataboxeMetadatas : " + url ) ;
  124.         JSONObject jsonResponse = PhraseanetApiCallService.getResponse( url, account );
  125.         _logger.debug("getDataboxeMetadatas (response) : " + jsonResponse ) ;

  126.         return MetadatasJsonParser.parseByDataboxe( jsonResponse );
  127.     }

  128.     /**
  129.      * Get metadatas for a given record
  130.      * @param nDataboxId The databox id
  131.      * @param nRecordId The record id
  132.      * @param account the user phraseanet account
  133.      * @return Metadatas
  134.      * @throws PhraseanetApiCallException if an error occurs
  135.      */
  136.     public static List<Metadata> getRecordMetadatas( int nDataboxId, int nRecordId, Account account )
  137.         throws PhraseanetApiCallException
  138.     {
  139.         Object[] arguments = { Integer.toString( nDataboxId ), Integer.toString( nRecordId ) };
  140.         String url = account.getAccessURL(  ) + MessageFormat.format( PATH_GET_RECORD_METADATAS, arguments );
  141.         JSONObject jsonResponse = PhraseanetApiCallService.getResponse( url, account );
  142.        
  143.         return MetadatasJsonParser.parse( jsonResponse );
  144.     }
  145.      
  146.     /**
  147.      * Searc results
  148.      * @param strQuery Query terms
  149.      * @param nPage Page number
  150.      * @param nPerPage Number of items per page
  151.      * @param criterias Criterias
  152.      * @param account the user phraseanet account
  153.      * @return search results
  154.      * @throws PhraseanetApiCallException if an error occurs
  155.      */
  156.     public static SearchResults search( String strQuery, int nPage, int nPerPage, SearchCriterias criterias, Account account )
  157.         throws PhraseanetApiCallException
  158.     {
  159.         String strUrl = account.getAccessURL(  ) + PATH_SEARCH ;
  160.         _logger.debug("URL de la reqette API : " + strUrl);
  161.         Map<String, List<String>> mapParameters = new HashMap<String, List<String>>(  );
  162.         putParameter( mapParameters, PARAMETER_QUERY, strQuery );
  163.         putParameter( mapParameters, PARAMETER_PAGE, String.valueOf( nPage ) );
  164.         putParameter( mapParameters, PARAMETER_PER_PAGE, String.valueOf( nPerPage ) );
  165.         _logger.debug("Parametres de la requette : " + mapParameters);

  166.         if ( ( criterias.getRecordType(  ) != null ) || !criterias.getRecordType(  ).equals( SEARCH_ALL ) )
  167.         {
  168.             putParameter( mapParameters, PARAMETER_RECORD_TYPE, criterias.getRecordType(  ) );
  169.         }

  170.         mapParameters.put( PARAMETER_BASES, criterias.getBases(  ) );

  171.         // TODO add other criterias
  172.         JSONObject jsonResponse = PhraseanetApiCallService.getPostResponse( strUrl, mapParameters, account );

  173.         return SearchResultsJsonParser.parse( jsonResponse );
  174.     }

  175.     /**
  176.      * Get databoxes
  177.      * @param account the user phraseanet account
  178.      * @return The lis of databoxes
  179.      * @throws PhraseanetApiCallException if an error occurs
  180.      */
  181.     public static List<Databox> getDataboxes( Account account ) throws PhraseanetApiCallException
  182.     {
  183.         String strUrl = account.getAccessURL(  ) + PATH_DATABOXES;
  184.         JSONObject jsonResponse = PhraseanetApiCallService.getResponse( strUrl, account );
  185.         JSONObject jsonDataboxes;
  186.        
  187.         try
  188.         {
  189.             jsonDataboxes = jsonResponse.getJSONObject( "databoxes" );
  190.         }
  191.         catch ( JSONException e )
  192.         {
  193.             // maybe api_version 2
  194.             JSONArray jsonDataboxesList = jsonResponse.getJSONArray( "databoxes" );
  195.             jsonDataboxes = jsonDataboxesList.toJSONObject( jsonDataboxesList ) ;
  196.         }  

  197.         return DataboxesJsonParser.parse( jsonDataboxes );
  198.     }

  199.     /**
  200.      * Get all collections of databox
  201.      * @param nDataboxId The databox id
  202.      * @param account the user phraseanet account
  203.      * @return a collection list
  204.      * @throws PhraseanetApiCallException if an error occurs
  205.      */
  206.     public static List<Collection> getColletions( int nDataboxId, Account account )
  207.         throws PhraseanetApiCallException
  208.     {
  209.         Object[] arguments = { nDataboxId };
  210.         String strUrl = account.getAccessURL(  ) + MessageFormat.format( PATH_COLLECTIONS, arguments );
  211.         JSONObject jsonResponse = PhraseanetApiCallService.getResponse( strUrl, account );
  212.         _logger.debug("Collections : " + jsonResponse);

  213.         return CollectionsJsonParser.parse( jsonResponse );
  214.     }

  215.     /**
  216.      * Get embed data of a record
  217.      * @param nDataboxId The databox id
  218.      * @param nRecordId The record id
  219.      * @param account the user phraseanet account
  220.      * @return embed data
  221.      * @throws PhraseanetApiCallException if an error occurs
  222.      */
  223.     public static Embed getEmbed( int nDataboxId, int nRecordId, Account account )
  224.         throws PhraseanetApiCallException
  225.     {
  226.         Object[] arguments = { Integer.toString( nDataboxId ), Integer.toString( nRecordId ) };
  227.         String url = account.getAccessURL(  ) + MessageFormat.format( PATH_EMBED, arguments );
  228.         JSONObject jsonResponse = PhraseanetApiCallService.getResponse( url, account );
  229.         JSONArray jsonEmbedList = jsonResponse.getJSONArray( "embed" );
  230.         JSONObject jsonEmbed = jsonEmbedList.toJSONObject( jsonEmbedList ) ;

  231.         return EmbedJsonParser.parse( jsonEmbed );
  232.     }

  233.     /**
  234.      * Gets items per page values
  235.      * @return items per page values
  236.      */
  237.     public static synchronized List<String> getItemsPerPageValues(  )
  238.     {
  239.         if ( _listItemsPerPageValues == null )
  240.         {
  241.             _listItemsPerPageValues = new ArrayList<String>(  );

  242.             String strItemsPerPageValues = AppPropertiesService.getProperty( PROPERTY_ITEMS_PER_PAGE_VALUES );
  243.             StringTokenizer st = new StringTokenizer( strItemsPerPageValues, DELIMITER );

  244.             while ( st.hasMoreTokens(  ) )
  245.             {
  246.                 String strValue = st.nextToken(  );
  247.                 _listItemsPerPageValues.add( strValue.trim(  ) );
  248.             }
  249.         }

  250.         return _listItemsPerPageValues;
  251.     }

  252.     /**
  253.      * Get media types values
  254.      * @return media types values
  255.      */
  256.     public static synchronized List<String> getMediaTypeValues(  )
  257.     {
  258.         if ( _listMediaTypeValues == null )
  259.         {
  260.             _listMediaTypeValues = new ArrayList<String>(  );

  261.             String strMediaTypeValues = AppPropertiesService.getProperty( PROPERTY_MEDIA_TYPE_VALUES );
  262.             StringTokenizer st = new StringTokenizer( strMediaTypeValues, DELIMITER );

  263.             while ( st.hasMoreTokens(  ) )
  264.             {
  265.                 String strValue = st.nextToken(  );
  266.                 _listMediaTypeValues.add( strValue.trim(  ) );
  267.             }
  268.         }

  269.         return _listMediaTypeValues;
  270.     }

  271.     /**
  272.      * Add parameters to a map
  273.      * @param map The map
  274.      * @param strKey The parameter key
  275.      * @param strValue The parameter value
  276.      */
  277.     private static void putParameter( Map<String, List<String>> map, String strKey, String strValue )
  278.     {
  279.         List<String> listValue = new ArrayList<String>(  );
  280.         listValue.add( strValue );
  281.         map.put( strKey, listValue );
  282.     }
  283.    
  284.     /**
  285.      * Get embed data of a record
  286.      * @param nDataboxId The databox id
  287.      * @param nRecordId The record id
  288.      * @return embed data
  289.      * @throws PhraseanetApiCallException if an error occurs
  290.      */
  291.     /**
  292.     public static Record getBasesRecords( List<Integer> listIdBases, Account account )
  293.         throws PhraseanetApiCallException
  294.     {
  295.         Object[] arguments = { Integer.toString( nDataboxId ), Integer.toString( nRecordId ) };
  296.         String url = account.getAccessURL(  ) + PATH_SEARCH ;
  297.         Map<String, List<String>> mapParameters = new HashMap<String, List<String>>(  );
  298.         putParameter( mapParameters, PARAMETER_QUERY, strQuery );
  299.         JSONObject jsonResponse = PhraseanetApiCallService.getPostResponse( strUrl, mapParameters, account );
  300.        
  301.         return EmbedJsonParser.parse( jsonEmbed );
  302.     }
  303.     */
  304. }