View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.updatercatalog.service;
35  
36  import fr.paris.lutece.plugins.updatercatalog.business.Catalog;
37  import fr.paris.lutece.plugins.updatercatalog.business.CatalogHome;
38  import fr.paris.lutece.plugins.updatercatalog.business.CatalogPluginEntry;
39  import fr.paris.lutece.plugins.updatercatalog.business.ReleaseUpgrade;
40  import fr.paris.lutece.portal.service.util.AppLogService;
41  import fr.paris.lutece.portal.service.util.AppPathService;
42  import fr.paris.lutece.portal.service.util.AppPropertiesService;
43  import fr.paris.lutece.util.xml.XmlUtil;
44  
45  import org.apache.commons.io.FileUtils;
46  
47  import java.io.File;
48  import java.io.IOException;
49  
50  import java.util.List;
51  
52  
53  /**
54   * Catalog Generation Service
55   */
56  public final class CatalogGenerationService
57  {
58      private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
59      private static final String TAG_CATALOG = "catalog";
60      private static final String TAG_PLUGINS = "plugins";
61      private static final String TAG_PLUGIN = "plugin";
62      private static final String TAG_NAME = "name";
63      private static final String TAG_DESCRIPTION = "description";
64      private static final String TAG_VERSION = "version";
65      private static final String TAG_MIN_CORE_VERSION = "min-core-version";
66      private static final String TAG_MAX_CORE_VERSION = "max-core-version";
67      private static final String TAG_AUTHOR = "author";
68      private static final String TAG_URL_HOMEPAGE = "url-homepage";
69      private static final String TAG_URL_DOWNLOAD = "url-download";
70      private static final String TAG_UPGRADES = "upgrades";
71      private static final String TAG_UPGRADE = "upgrade";
72      private static final String TAG_VERSION_FROM = "version-from";
73      private static final String TAG_CRITICAL = "critical";
74      private static final String TAG_URL_UPGRADE_DOWNLOAD = "url-upgrade-download";
75      private static final String PROPERTY_GENERATED_CATALOGS_DIRECTORY_PATH = "updatercatalog.generated_catalogs.directory.path";
76  
77      /**
78       * Private constructor
79       */
80      private CatalogGenerationService(  )
81      {
82      }
83  
84      /**
85       * Generate a given catalog
86       * @param nCatalogId The catalog ID
87       */
88      public static void generateCatalog( int nCatalogId )
89      {
90          try
91          {
92              Catalog catalog = CatalogHome.findByPrimaryKey( nCatalogId );
93              File file = new File( AppPathService.getWebAppPath(  ) + AppPropertiesService.getProperty( 
94              		PROPERTY_GENERATED_CATALOGS_DIRECTORY_PATH ) + catalog.getOutputFilename(  ) );
95              FileUtils.writeStringToFile( file, generateCatalogXmlDocument( catalog ) );
96          }
97          catch ( IOException e )
98          {
99              AppLogService.error( "Error writing catalog : " + e.getMessage(  ), e );
100         }
101     }
102 
103     /**
104      * Generate the XML document of a given catalog
105      * @param catalog The catalog
106      * @return The catalog as XML
107      */
108     private static String generateCatalogXmlDocument( Catalog catalog )
109     {
110         List<CatalogPluginEntry> listEntries = CatalogHome.getPluginsEntries( catalog.getId(  ) );
111 
112         StringBuffer sbXml = new StringBuffer( XML_HEADER );
113         XmlUtil.beginElement( sbXml, TAG_CATALOG );
114         XmlUtil.beginElement( sbXml, TAG_PLUGINS );
115 
116         for ( CatalogPluginEntry entry : listEntries )
117         {
118             String strRepository = catalog.getUrlRepository(  ) + "/plugin-" + entry.getPluginName(  ) + "/";
119             XmlUtil.beginElement( sbXml, TAG_PLUGIN );
120             XmlUtil.addElement( sbXml, TAG_NAME, entry.getPluginName(  ) );
121             XmlUtil.addElementHtml( sbXml, TAG_DESCRIPTION, entry.getPluginDescription(  ) );
122             XmlUtil.addElement( sbXml, TAG_VERSION, entry.getPluginVersion(  ) );
123             XmlUtil.addElement( sbXml, TAG_MIN_CORE_VERSION, entry.getCoreVersionMin(  ) );
124 
125             if ( ( entry.getCoreVersionMax(  ) != null ) && !entry.getCoreVersionMax(  ).equals( "" ) )
126             {
127                 XmlUtil.addElement( sbXml, TAG_MAX_CORE_VERSION, entry.getCoreVersionMax(  ) );
128             }
129 
130             XmlUtil.addElementHtml( sbXml, TAG_AUTHOR, entry.getPluginAuthor(  ) );
131             XmlUtil.addElement( sbXml, TAG_URL_HOMEPAGE, entry.getUrlHomepage(  ) );
132             XmlUtil.addElement( sbXml, TAG_URL_DOWNLOAD, strRepository + entry.getUrlDownload(  ) );
133             XmlUtil.beginElement( sbXml, TAG_UPGRADES );
134 
135             for ( ReleaseUpgrade upgrade : entry.getUpgrades(  ) )
136             {
137                 XmlUtil.beginElement( sbXml, TAG_UPGRADE );
138                 XmlUtil.addElement( sbXml, TAG_VERSION_FROM, upgrade.getVersionFrom(  ) );
139                 XmlUtil.addElement( sbXml, TAG_CRITICAL, upgrade.getCritical(  ) );
140                 XmlUtil.addElement( sbXml, TAG_URL_UPGRADE_DOWNLOAD, strRepository + upgrade.getUrlDownload(  ) );
141                 XmlUtil.endElement( sbXml, TAG_UPGRADE );
142             }
143 
144             XmlUtil.endElement( sbXml, TAG_UPGRADES );
145             XmlUtil.endElement( sbXml, TAG_PLUGIN );
146         }
147 
148         XmlUtil.endElement( sbXml, TAG_PLUGINS );
149         XmlUtil.endElement( sbXml, TAG_CATALOG );
150 
151         return sbXml.toString(  );
152     }
153 }