View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of 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.directories.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.portal.service.plugin.PluginService;
38  import fr.paris.lutece.portal.service.spring.SpringContextService;
39  import fr.paris.lutece.util.ReferenceList;
40  import java.util.ArrayList;
41  import java.util.List;
42  import java.util.stream.Collectors;
43  
44  /**
45   * This class provides instances management methods (create, find, ...) for DirectoryEntity objects
46   */
47  public final class DirectoryEntityHome
48  {
49      // Static variable pointed at the DAO instance
50      private static IDirectoryEntityDAO _dao = SpringContextService.getBean( "directories.directoryEntityDAO" );
51      private static Plugin _plugin = PluginService.getPlugin( "directories" );
52  
53      /**
54       * Private constructor - this class need not be instantiated
55       */
56      private DirectoryEntityHome( )
57      {
58      }
59  
60      /**
61       * Create an instance of the directoryEntity class
62       * 
63       * @param directoryEntity
64       *            The instance of the DirectoryEntity which contains the informations to store
65       * @return The instance of directoryEntity which has been created with its primary key.
66       */
67      public static DirectoryEntity./../../../fr/paris/lutece/plugins/directories/business/DirectoryEntity.html#DirectoryEntity">DirectoryEntity create( DirectoryEntity directoryEntity )
68      {
69          _dao.insert( directoryEntity, _plugin );
70          return directoryEntity;
71      }
72  
73      /**
74       * Update of the directoryEntity which is specified in parameter
75       * 
76       * @param directoryEntity
77       *            The instance of the DirectoryEntity which contains the data to store
78       * @return The instance of the directoryEntity which has been updated
79       */
80      public static DirectoryEntity./../../../fr/paris/lutece/plugins/directories/business/DirectoryEntity.html#DirectoryEntity">DirectoryEntity update( DirectoryEntity directoryEntity )
81      {
82          _dao.store( directoryEntity, _plugin );
83          return directoryEntity;
84      }
85  
86      /**
87       * Remove the directoryEntity whose identifier is specified in parameter
88       * 
89       * @param nKey
90       *            The directoryEntity Id
91       */
92      public static void remove( int nKey )
93      {
94          _dao.delete( nKey, _plugin );
95      }
96  
97      /**
98       * Returns an instance of a directoryEntity whose identifier is specified in parameter
99       * 
100      * @param nKey
101      *            The directoryEntity primary key
102      * @return an instance of DirectoryEntity
103      */
104     public static DirectoryEntity findByPrimaryKey( int nKey )
105     {
106         return _dao.load( nKey, _plugin );
107     }
108 
109     /**
110      * Load the data of all the directoryEntity objects and returns them as a list
111      * 
112      * @return the list which contains the data of all the directoryEntity objects
113      */
114     public static List<DirectoryEntity> getDirectoryEntityListByIdDirectory( int nKey )
115     {
116         return _dao.selectDirectoryEntitiesListByIdDirectory( nKey, _plugin );
117     }
118 
119     /**
120      * Load the data of all the directoryEntity objects and returns them as a list
121      * 
122      * @return the list which contains the data of all the directoryEntity objects
123      */
124     public static List<DirectoryEntity> getDirectoryEntitiesList( )
125     {
126         return _dao.selectDirectoryEntitiesList( _plugin );
127     }
128 
129     /**
130      * Load the id of all the directoryEntity objects and returns them as a list
131      * 
132      * @return the list which contains the id of all the directoryEntity objects
133      */
134     public static List<Integer> getIdDirectoryEntitiesList( )
135     {
136         return _dao.selectIdDirectoryEntitiesList( _plugin );
137     }
138 
139     /**
140      * Load the data of all the directoryEntity objects and returns them as a referenceList
141      * 
142      * @return the referenceList which contains the data of all the directoryEntity objects
143      */
144     public static ReferenceList getDirectoryEntitiesReferenceList( )
145     {
146         return _dao.selectDirectoryEntitiesReferenceList( _plugin );
147     }
148 
149     /**
150      * Fill name for creator and modificator
151      * 
152      */
153     public static void fillAdminUserName( List<DirectoryEntity> listEntity )
154     {
155         for ( DirectoryEntity entity : listEntity )
156         {
157             int nIdCreator = entity.getIdCreator( );
158             int nIdModificator = entity.getIdModificator( );
159             if ( nIdCreator != 0 )
160             {
161                 entity.setCreator( nIdCreator );
162             }
163             if ( nIdModificator != 0 )
164             {
165                 entity.setModificator( nIdModificator );
166             }
167         }
168     }
169 
170     /**
171      * Search in entity list ( title, creator , modificator )
172      * 
173      * @return a filtered list
174      */
175     public static List<DirectoryEntity> filter( String searchValue, List<DirectoryEntity> listEntity )
176     {
177         String [ ] terms = searchValue.split( " " );
178         List<DirectoryEntity> listEntityFilter = new ArrayList<>( listEntity );
179         for ( String term : terms )
180         {
181             List<DirectoryEntity> list = listEntityFilter.parallelStream( ).filter( x -> x.getCreator( ).matches( "(?i).*" + term + ".*" )
182                     || x.getModificator( ).matches( "(?i).*" + term + ".*" ) || x.getTitle( ).matches( "(?i).*" + term + ".*" ) )
183                     .collect( Collectors.toList( ) );
184             listEntityFilter = new ArrayList<>( list );
185         }
186         return listEntityFilter;
187     }
188 }