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.directory.business;
35  
36  import fr.paris.lutece.portal.service.html.XmlTransformerService;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.portal.service.spring.SpringContextService;
39  import fr.paris.lutece.util.ReferenceItem;
40  import fr.paris.lutece.util.ReferenceList;
41  
42  import java.util.List;
43  
44  /**
45   * This class provides instances management methods (create, find, ...) for ExportFormat objects
46   */
47  public final class DirectoryXslHome
48  {
49      // Static variable pointed at the DAO instance
50      private static IDirectoryXslDAO _dao = SpringContextService.getBean( "directoryXslDAO" );
51      private static final String CSV = "csv";
52  
53      /**
54       * Private constructor - this class need not be instantiated
55       */
56      private DirectoryXslHome( )
57      {
58      }
59  
60      /**
61       * Creation of an instance of Directory Xsl
62       *
63       * @param directoryXsl
64       *            The instance of the directoryXsl which contains the informations to store
65       * @param plugin
66       *            the Plugin
67       *
68       */
69      public static void create( DirectoryXsl directoryXsl, Plugin plugin )
70      {
71          _dao.insert( directoryXsl, plugin );
72      }
73  
74      /**
75       * Update of the DirectoryXsl which is specified in parameter
76       *
77       * @param directoryXsl
78       *            The instance of the directoryXsl which contains the informations to update
79       * @param plugin
80       *            the Plugin
81       *
82       */
83      public static void update( DirectoryXsl directoryXsl, Plugin plugin )
84      {
85          _dao.store( directoryXsl, plugin );
86          XmlTransformerService.clearXslCache( );
87      }
88  
89      /**
90       * Remove the DirectoryXsl whose identifier is specified in parameter
91       *
92       * @param nIdDirectoryXsl
93       *            The DirectoryXsl Id
94       * @param plugin
95       *            the Plugin
96       */
97      public static void remove( int nIdDirectoryXsl, Plugin plugin )
98      {
99          _dao.delete( nIdDirectoryXsl, plugin );
100         XmlTransformerService.clearXslCache( );
101     }
102 
103     // /////////////////////////////////////////////////////////////////////////
104     // Finders
105 
106     /**
107      * Returns an instance of a DirectoryXsl whose identifier is specified in parameter
108      *
109      * @param nKey
110      *            The directoryXsl primary key
111      * @param plugin
112      *            the Plugin
113      * @return an instance of DirectoryXsl
114      */
115     public static DirectoryXsl findByPrimaryKey( int nKey, Plugin plugin )
116     {
117         DirectoryXsl directoryXsl = _dao.load( nKey, plugin );
118 
119         if ( ( directoryXsl != null ) && ( directoryXsl.getFile( ) != null ) )
120         {
121             directoryXsl.setFile( FileHome.findByPrimaryKey( directoryXsl.getFile( ).getIdFile( ), plugin ) );
122         }
123 
124         return directoryXsl;
125     }
126 
127     /**
128      * Returns an instance of a DirectoryXsl whose file is specified in parameter
129      *
130      * @param nIdFile
131      *            The file id
132      * @param plugin
133      *            the Plugin
134      * @return an instance of DirectoryXsl
135      */
136     public static DirectoryXsl findByFile( int nIdFile, Plugin plugin )
137     {
138         DirectoryXsl directoryXsl = _dao.loadByFile( nIdFile, plugin );
139 
140         if ( ( directoryXsl != null ) && ( directoryXsl.getFile( ) != null ) )
141         {
142             directoryXsl.setFile( FileHome.findByPrimaryKey( directoryXsl.getFile( ).getIdFile( ), plugin ) );
143         }
144 
145         return directoryXsl;
146     }
147 
148     /**
149      * Loads the data of all the DirectoryXsl who verify the filter and returns them in a list
150      * 
151      * @param filter
152      *            the filter
153      * @param plugin
154      *            the Plugin
155      * @return the list which contains the data of all the Directory Xsl
156      */
157     public static List<DirectoryXsl> getList( DirectoryXslFilter filter, Plugin plugin )
158     {
159         return _dao.selectList( filter, plugin );
160     }
161 
162     /**
163      * Loads in the reference list the data of all the DirectoryXsl who verify the filter and returns them in a list
164      * 
165      * @param filter
166      *            the filter
167      * @param plugin
168      *            the Plugin
169      * @return the list which contains the data of all the Directory Xsl
170      */
171     public static ReferenceList getRefList( DirectoryXslFilter filter, Plugin plugin )
172     {
173         ReferenceList refList = new ReferenceList( );
174 
175         List<DirectoryXsl> xslList = getList( filter, plugin );
176         int index = -1;
177 
178         for ( DirectoryXsl directoryXsl : xslList )
179         {
180             if ( directoryXsl.getExtension( ).equals( CSV ) )
181             {
182                 index = xslList.indexOf( directoryXsl );
183             }
184             else
185             {
186                 refList.addItem( directoryXsl.getIdDirectoryXsl( ), directoryXsl.getTitle( ) );
187             }
188         }
189 
190         if ( index != -1 )
191         {
192             ReferenceItem referenceItem = new ReferenceItem( );
193             referenceItem.setCode( String.valueOf( xslList.get( index ).getIdDirectoryXsl( ) ) );
194             referenceItem.setName( xslList.get( index ).getTitle( ) );
195             refList.add( 0, referenceItem );
196         }
197 
198         return refList;
199     }
200 }