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.digglike.business;
35  
36  import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.portal.service.regularexpression.RegularExpressionService;
39  import fr.paris.lutece.portal.service.spring.SpringContextService;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  
45  /**
46   * This class provides instances management methods (create, find, ...) for
47   * Entry objects
48   */
49  public final class EntryHome
50  {
51      // Static variable pointed at the DAO instance
52      private static IEntryDAO _dao = SpringContextService.getBean( "digglike.entryDAO" );
53      private static IEntryAdditionalAttributeDAO _daoAA = SpringContextService.getBean( 
54              "digglike.entryAdditionalAttributeDAO" );
55  
56      /**
57       * Private constructor - this class need not be instantiated
58       */
59      private EntryHome(  )
60      {
61      }
62  
63      /**
64       * Creation of an instance of Entry
65       *
66       * @param entry The instance of the Entry which contains the informations to
67       *            store
68       * @param plugin the Plugin
69       * @return The primary key of the new entry.
70       */
71      public static int create( IEntry entry, Plugin plugin )
72      {
73          int nPK;
74          nPK = _dao.insert( entry, plugin );
75  
76          if ( entry.getEntryAdditionalAttributeList(  ) != null )
77          {
78              for ( EntryAdditionalAttribute entryAdditionalAttribute : entry.getEntryAdditionalAttributeList(  ) )
79              {
80                  entryAdditionalAttribute.setIdEntry( nPK );
81                  _daoAA.insert( entryAdditionalAttribute, plugin );
82              }
83          }
84  
85          return nPK;
86      }
87  
88      /**
89       * Copy of an instance of Entry
90       *
91       * @param entry The instance of the Entry who must copy
92       * @param plugin the Plugin
93       *
94       */
95      public static void copy( IEntry entry, Plugin plugin )
96      {
97          IEntry entryCopy = entry;
98          entryCopy.setIdEntry( create( entry, plugin ) );
99  
100         for ( RegularExpression regularExpression : entry.getRegularExpressionList(  ) )
101         {
102             insertVerifyBy( entryCopy.getIdEntry(  ), regularExpression.getIdExpression(  ), plugin );
103         }
104     }
105 
106     /**
107      * Update of the entry which is specified in parameter
108      *
109      * @param entry The instance of the Entry which contains the informations to
110      *            update
111      * @param plugin the Plugin
112      *
113      */
114     public static void update( IEntry entry, Plugin plugin )
115     {
116         _dao.store( entry, plugin );
117 
118         if ( entry.getEntryAdditionalAttributeList(  ) != null )
119         {
120             for ( EntryAdditionalAttribute entryAdditionalAttribute : entry.getEntryAdditionalAttributeList(  ) )
121             {
122                 entryAdditionalAttribute.setIdEntry( entry.getIdEntry(  ) );
123                 _daoAA.store( entryAdditionalAttribute, plugin );
124             }
125         }
126     }
127 
128     /**
129      * Remove the entry whose identifier is specified in parameter
130      *
131      * @param nIdEntry The entry Id
132      * @param plugin the Plugin
133      */
134     public static void remove( int nIdEntry, Plugin plugin )
135     {
136         _dao.delete( nIdEntry, plugin );
137         _daoAA.delete( nIdEntry, plugin );
138     }
139 
140     ///////////////////////////////////////////////////////////////////////////
141     // Finders
142 
143     /**
144      * Returns an instance of a Entry whose identifier is specified in parameter
145      *
146      * @param nKey The entry primary key
147      * @param plugin the Plugin
148      * @return an instance of Entry
149      */
150     public static IEntry findByPrimaryKey( int nKey, Plugin plugin )
151     {
152         IEntry entry = _dao.load( nKey, plugin );
153         List<RegularExpression> listRegularExpression = new ArrayList<RegularExpression>(  );
154 
155         if ( RegularExpressionService.getInstance(  ).isAvailable(  ) )
156         {
157             List<Integer> listRegularExpressionKeyEntry = getListRegularExpressionKeyByIdEntry( nKey, plugin );
158 
159             if ( ( listRegularExpressionKeyEntry != null ) && ( listRegularExpressionKeyEntry.size(  ) != 0 ) )
160             {
161                 RegularExpression regularExpression = null;
162 
163                 for ( Integer regularExpressionKey : listRegularExpressionKeyEntry )
164                 {
165                     regularExpression = RegularExpressionService.getInstance(  )
166                                                                 .getRegularExpressionByKey( regularExpressionKey );
167 
168                     if ( regularExpression != null )
169                     {
170                         listRegularExpression.add( regularExpression );
171                     }
172                 }
173             }
174         }
175 
176         entry.setRegularExpressionList( listRegularExpression );
177 
178         List<EntryAdditionalAttribute> additionalAttribute = _daoAA.selectEntryAdditionalAttributeList( nKey, plugin );
179         entry.setEntryAdditionalAttributeList( additionalAttribute );
180 
181         return entry;
182     }
183 
184     /**
185      * Load the data of all the entry who verify the filter and returns them in
186      * a list
187      * @param filter the filter
188      * @param plugin the plugin
189      * @return the list of entry
190      */
191     public static List<IEntry> getEntryList( EntryFilter filter, Plugin plugin )
192     {
193         List<IEntry> entries = _dao.selectEntryListByFilter( filter, plugin );
194 
195         for ( IEntry e : entries )
196         {
197             e.setEntryAdditionalAttributeList( _daoAA.selectEntryAdditionalAttributeList( e.getIdEntry(  ), plugin ) );
198         }
199 
200         return entries;
201     }
202 
203     /**
204      * Return the number of entry who verify the filter
205      * @param filter the filter
206      * @param plugin the plugin
207      * @return the number of entry who verify the filter
208      */
209     public static int getNumberEntryByFilter( EntryFilter filter, Plugin plugin )
210     {
211         return _dao.selectNumberEntryByFilter( filter, plugin );
212     }
213 
214     /**
215      * remove a regular expression in the entry
216      *
217      * @param nIdEntry The identifier of the entry
218      * @param nIdExpression The identifier of the regular expression
219      * @param plugin the plugin
220      */
221     public static void deleteVerifyBy( int nIdEntry, int nIdExpression, Plugin plugin )
222     {
223         _dao.deleteVerifyBy( nIdEntry, nIdExpression, plugin );
224     }
225 
226     /**
227      * insert a regular expression in the entry
228      *
229      * @param nIdEntry The identifier of the entry
230      * @param nIdExpression The identifier of the regular expression
231      * @param plugin the plugin
232      */
233     public static void insertVerifyBy( int nIdEntry, int nIdExpression, Plugin plugin )
234     {
235         _dao.insertVerifyBy( nIdEntry, nIdExpression, plugin );
236     }
237 
238     /**
239      * verify if the regular expresssion is use
240      *
241      * @param nIdExpression The identifier of the regular expression
242      * @param plugin the plugin
243      * @return true if the regular expression is use
244      */
245     public static boolean isRegularExpressionIsUse( int nIdExpression, Plugin plugin )
246     {
247         return _dao.isRegularExpressionIsUse( nIdExpression, plugin );
248     }
249 
250     /**
251      * Load the key of all the regularExpression associate to the entry and
252      * returns them in a list
253      * @param nIdEntry the id of entry
254      * @param plugin the plugin
255      * @return the list of regular expression key
256      */
257     public static List<Integer> getListRegularExpressionKeyByIdEntry( int nIdEntry, Plugin plugin )
258     {
259         return _dao.selectListRegularExpressionKeyByIdEntry( nIdEntry, plugin );
260     }
261 }