View Javadoc
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.document.modules.ckan.service;
35  
36  import fr.paris.lutece.plugins.document.business.Document;
37  import fr.paris.lutece.plugins.document.business.DocumentHome;
38  import fr.paris.lutece.plugins.document.business.portlet.DocumentPortletHome;
39  import fr.paris.lutece.plugins.document.modules.ckan.business.PackageShowResult;
40  import fr.paris.lutece.plugins.document.modules.ckan.business.WSBase;
41  import fr.paris.lutece.plugins.document.modules.ckan.business.WSError;
42  import fr.paris.lutece.plugins.document.modules.ckan.business.WSPackageList;
43  import fr.paris.lutece.plugins.document.modules.ckan.business.WSPackageShow;
44  import fr.paris.lutece.plugins.document.modules.ckan.business.WSResult;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  
47  import org.xml.sax.SAXException;
48  
49  import java.util.ArrayList;
50  import java.util.List;
51  
52  
53  /**
54   * PackageServiceV3
55   */
56  public final class PackageServiceV3
57  {
58      private static final String PROPERTY_HELP_PACKAGE_LIST = "document-ckan.help.package_list";
59      private static final String PROPERTY_HELP_PACKAGE_SHOW = "document-ckan.help.package_show";
60      private static final String PROPERTY_DATASET_SPACE_ID = "document-ckan.datasetSpaceId";
61  
62      /** Private constructor */
63      private PackageServiceV3(  )
64      {
65          
66      }
67      
68      /**
69       * Get the package list
70       * @return The package list
71       */
72      public static WSBase getPackageList(  )
73      {
74          WSPackageList pl = new WSPackageList(  );
75          pl.setHelp( AppPropertiesService.getProperty( PROPERTY_HELP_PACKAGE_LIST ) );
76          pl.setSuccess( true );
77  
78          List<String> listResults = new ArrayList<String>(  );
79          int nSpaceId = Integer.parseInt( AppPropertiesService.getProperty( PROPERTY_DATASET_SPACE_ID ) );
80  
81          for ( Document doc : DocumentHome.findBySpaceKey( nSpaceId ) )
82          {
83              listResults.add( CkanService.getNameId( doc ) );
84          }
85  
86          pl.setResult( listResults );
87  
88          return pl;
89      }
90  
91      /**
92       * Returns the Package Show response
93       * @param strId The dataset ID
94       * @return The Package Show response
95       * @throws SAXException If a parsing error occurs
96       */
97      public static WSBase getPackageShow( String strId )
98          throws SAXException
99      {
100         String strHelp = AppPropertiesService.getProperty( PROPERTY_HELP_PACKAGE_SHOW );
101         Document doc;
102 
103         try
104         {
105             doc = DocumentHome.findByPrimaryKey( Integer.parseInt( strId ) );
106         }
107         catch ( NumberFormatException e )
108         {
109             return new WSError( WSError.MESSAGE_INVALID, WSError.TYPE_INVALID, strHelp );
110         }
111 
112         if ( doc == null )
113         {
114             return new WSError( WSError.MESSAGE_NOT_FOUND, WSError.TYPE_NOT_FOUND, strHelp );
115         }
116 
117         PackageShowResult psr = new PackageShowResult(  );
118         List<Integer> listPublications = DocumentPortletHome.findPortletForDocument( doc.getId(  ) );
119 
120         if ( listPublications.isEmpty(  ) )
121         {
122             // dataset is not published
123             return new WSError( WSError.MESSAGE_NOT_FOUND, WSError.TYPE_NOT_FOUND, strHelp );
124         }
125 
126         int nPortletId = listPublications.get( 0 );
127         WSResult ps = new WSPackageShow(  );
128         ps.setHelp( strHelp );
129         psr = DocumentParser.parse( psr, doc.getXmlValidatedContent(  ), nPortletId );
130         ps.setResult( psr );
131 
132         return ps;
133     }
134 }