Coverage Report - fr.paris.lutece.plugins.phraseanet.service.parsers.MetadatasJsonParser
 
Classes in this File Line Coverage Branch Coverage Complexity
MetadatasJsonParser
0 %
0/39
0 %
0/4
3,333
 
 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.parsers;
 35  
 
 36  
 import fr.paris.lutece.plugins.phraseanet.business.record.Metadata;
 37  
 import fr.paris.lutece.plugins.phraseanet.business.record.MetadataValue;
 38  
 import fr.paris.lutece.plugins.phraseanet.service.Constants;
 39  
 import fr.paris.lutece.plugins.phraseanet.service.api.PhraseanetApiCallException;
 40  
 import fr.paris.lutece.portal.service.util.AppLogService;
 41  
 
 42  
 import net.sf.json.JSONException;
 43  
 import net.sf.json.JSONObject;
 44  
 import net.sf.json.JSONArray;
 45  
 
 46  
 import java.util.ArrayList;
 47  
 import java.util.Iterator;
 48  
 import java.util.List;
 49  
 
 50  
 import org.apache.log4j.Logger;
 51  
 
 52  
 
 53  
 /**
 54  
  * Metadata Json Parser
 55  
  */
 56  
 public final class MetadatasJsonParser
 57  
 {  
 58  0
     private static Logger _logger = Logger.getLogger( Constants.LOGGER );
 59  
     
 60  
     /** private constructor */
 61  
     private MetadatasJsonParser(  )
 62  0
     {
 63  0
         _logger.debug( "MetadatasJsonParser" );
 64  0
     }
 65  
     
 66  
 
 67  
     /**
 68  
      * Parse a list of metadatas
 69  
      * @param jsonResponse The response as JSONObject
 70  
      * @return The list
 71  
      * @throws PhraseanetApiCallException if an error occurs
 72  
      */
 73  
     public static List<Metadata> parse( JSONObject jsonResponse )
 74  
     throws PhraseanetApiCallException
 75  
     {   
 76  
         try
 77  
         {
 78  0
             List<Metadata> listMetadatas = new ArrayList<Metadata>(  );
 79  
             //JSONObject jsonMetadatas = jsonResponse.getJSONObject( "metadatas" );
 80  0
             JSONArray jsonMetadatasList = jsonResponse.getJSONArray( "record_metadatas" );
 81  0
             JSONObject jsonMetadatas = jsonMetadatasList.toJSONObject( jsonMetadatasList ) ;
 82  0
             Iterator i = jsonMetadatas.keys(  );
 83  0
             while ( i.hasNext(  ) )
 84  
             {
 85  0
                 String strKey = (String) i.next(  );
 86  0
                 JSONObject jsonMetadata = jsonMetadatas.getJSONObject( strKey );
 87  0
                 Metadata metadata = new Metadata(  );
 88  0
                 metadata.setMetaId( jsonMetadata.getInt( "meta_id" ) );
 89  0
                 metadata.setMetaStructureId( jsonMetadata.getInt( "meta_structure_id" ) );
 90  0
                 metadata.setName( jsonMetadata.getString( "name" ) );
 91  0
                 metadata.setValue( jsonMetadata.getString( "value" ) );
 92  0
                 listMetadatas.add( metadata );
 93  0
             }
 94  
 
 95  0
             return listMetadatas;
 96  
         }
 97  0
         catch ( JSONException e )
 98  
         {//throw new PhraseanetApiCallException( "Error parsing metadatas : " + e.getMessage(  ) + " - JSON : " + jsonResponse.toString( 4 ) );
 99  0
             AppLogService.error( "Error parsing metadatas " + e.getMessage()+ " - JSON : " + jsonResponse.toString( 4 ) );
 100  0
             return null;
 101  
         }     
 102  
     }
 103  
 
 104  
     /**
 105  
      * Parse a list of metadatas
 106  
      * @param jsonResponse The response as JSONObject
 107  
      * @return The list
 108  
      * @throws PhraseanetApiCallException if an error occurs
 109  
      */
 110  
     public static List<Metadata> parseByDataboxe( JSONObject jsonResponse )
 111  
         throws PhraseanetApiCallException
 112  
     {
 113  
         try
 114  
         {
 115  0
             List<Metadata> listMetadatas = new ArrayList<Metadata>(  );
 116  0
             JSONArray jsonMetadatasList = jsonResponse.getJSONArray( "document_metadatas" );
 117  0
             _logger.debug("Liste des metadatas : " + jsonMetadatasList ) ;
 118  0
             JSONObject jsonMetadatas = jsonMetadatasList.toJSONObject(jsonMetadatasList);
 119  0
             _logger.debug("JSONObject : " + jsonMetadatas ) ;
 120  0
             Iterator i = jsonMetadatas.keys(  );
 121  
 
 122  0
             while ( i.hasNext(  ) )
 123  
             {
 124  0
                 String strKey = (String) i.next(  );
 125  0
                 JSONObject jsonMetadata = jsonMetadatas.getJSONObject( strKey );
 126  0
                 Metadata metadata = new Metadata(  );
 127  0
                 metadata.setName( jsonMetadata.getString( "name" ) );
 128  0
                 listMetadatas.add( metadata );
 129  0
             }
 130  
 
 131  
 
 132  0
             return listMetadatas;
 133  
         }
 134  0
         catch ( JSONException e )
 135  
         {
 136  0
             throw new PhraseanetApiCallException( "Error parsing metadatas : " + e.getMessage(  ) + " - JSON : " +
 137  0
                 jsonResponse.toString( 4 ) );
 138  
         }
 139  
     }
 140  
 }