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.xmlpage.service;
35  
36  import fr.paris.lutece.portal.service.resource.ResourceService;
37  import fr.paris.lutece.util.ReferenceItem;
38  import fr.paris.lutece.util.ReferenceList;
39  
40  import java.util.Collection;
41  import java.util.Collections;
42  import java.util.Comparator;
43  import java.util.Iterator;
44  
45  
46  /**
47   * The XmlPage implementation of ResourceService
48   */
49  public final class XmlPageService extends ResourceService
50  {
51      private static final String PROPERTY_LOADER = "xmlpage.service.loaders";
52      private static final String PROPERTY_NAME = "xmlpage.service.name";
53      private static final String PROPERTY_CACHE = "xmlpage.service.cache";
54      private static final String PARAMETER_VALUE_SEPARATOR = "-";
55      private static XmlPageService _singleton = new XmlPageService(  );
56      private static final Comparator<ReferenceItem> COMPARATOR_TRI_ASC = new Comparator<ReferenceItem>(  )
57          {
58              public int compare( ReferenceItem item1, ReferenceItem item2 )
59              {
60                  int nOrder = item1.getName(  ).compareTo( item2.getName(  ) );
61  
62                  return nOrder;
63              }
64          };
65  
66      /**
67       * Creates a new instance of XmlPageService
68       */
69      private XmlPageService(  )
70      {
71          setNameKey( PROPERTY_NAME );
72          setCacheKey( PROPERTY_CACHE );
73      }
74  
75      /**
76       * Returns the instance of the singleton
77       * @return The instance of the singleton
78       */
79      public static XmlPageService getInstance(  )
80      {
81          return _singleton;
82      }
83  
84      /**
85       * Returns the property key that contains the loaders list
86       * @return A property key
87       */
88      protected String getLoadersProperty(  )
89      {
90          return PROPERTY_LOADER;
91      }
92  
93      /**
94       * Returns the XmlPageElement
95       * @param strPageName The XmlPageElement name
96       * @return the XmlPageElement
97       */
98      public XmlPageElement getXmlPageResource( String strPageName )
99      {
100         XmlPageElement xmlPageElement = (XmlPageElement) getResource( strPageName );
101 
102         return xmlPageElement;
103     }
104 
105     /**
106      * Returns a list of different XmlPage.
107      * The result list is a reference list of all the (xmlpage, style) available.
108      * @return The list of all Xmlpage
109      */
110     public ReferenceList getXmlPageList(  )
111     {
112         ReferenceList list = new ReferenceList(  );
113         Collection colXmlPages = getResources(  );
114         Iterator iColXmlPages = colXmlPages.iterator(  );
115 
116         while ( iColXmlPages.hasNext(  ) )
117         {
118             XmlPageElement xmlPageElement = (XmlPageElement) iColXmlPages.next(  );
119 
120             if ( xmlPageElement.getListXslContent(  ) != null )
121             {
122                 Iterator iXslElement = xmlPageElement.getListXslContent(  ).keySet(  ).iterator(  );
123 
124                 while ( iXslElement.hasNext(  ) )
125                 {
126                     String strStyle = (String) iXslElement.next(  );
127                     list.addItem( xmlPageElement.getName(  ).concat( PARAMETER_VALUE_SEPARATOR ).concat( strStyle ),
128                         xmlPageElement.getTitle(  ).concat( " (" ).concat( strStyle ).concat( ")" ) );
129                 }
130             }
131         }
132 
133         // Ordering the list
134         Collections.sort( list, COMPARATOR_TRI_ASC );
135 
136         return list;
137     }
138 }