View Javadoc
1   /*
2    * Copyright (c) 2002-2016, 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   
35  package fr.paris.lutece.plugins.parisconnect.business;
36  
37  
38  import fr.paris.lutece.plugins.parisconnect.service.NewsletterCacheService;
39  import fr.paris.lutece.portal.business.file.FileHome;
40  import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
41  import fr.paris.lutece.portal.service.plugin.Plugin;
42  import fr.paris.lutece.portal.service.plugin.PluginService;
43  import fr.paris.lutece.portal.service.spring.SpringContextService;
44  import fr.paris.lutece.util.ReferenceList;
45  import fr.paris.lutece.util.sql.DAOUtil;
46  
47  import java.util.List;
48  
49  /**
50   * This class provides instances management methods (create, find, ...) for Newsletter objects
51   */
52  
53  public final class NewsletterHome
54  {
55      // Static variable pointed at the DAO instance
56      private static INewsletterDAO _dao = SpringContextService.getBean( "parisconnect.newsletterDAO" );
57      private static Plugin _plugin = PluginService.getPlugin( "parisconnect" );
58      private static AbstractCacheableService _cache = new NewsletterCacheService(  );
59  
60      /**
61       * Private constructor - this class need not be instantiated
62       */
63      private NewsletterHome(  )
64      {
65      }
66  
67      /**
68       * Create an instance of the newsletter class
69       * @param newsletter The instance of the Newsletter which contains the informations to store
70       * @return The  instance of newsletter which has been created with its primary key.
71       */
72      public static Newsletter create( Newsletter newsletter )
73      {
74      	
75      	if(newsletter.getPicture()!= null)
76      	{
77      		FileHome.create(newsletter.getPicture());
78      	}
79      	
80      	_dao.insert( newsletter, _plugin );
81      	
82      	
83  
84          return newsletter;
85      }
86  
87      /**
88       * Update of the newsletter which is specified in parameter
89       * @param newsletter The instance of the Newsletter which contains the data to store
90       * @return The instance of the  newsletter which has been updated
91       */
92      public static Newsletter update( Newsletter newsletter )
93      {
94      	
95      	if(newsletter.getPicture()!= null && newsletter.getPicture().getPhysicalFile() != null && newsletter.getPicture().getPhysicalFile().getValue()!=null)
96      	{	
97      		Newsletter newsletterStore=findByPrimaryKey(newsletter.getId());
98      		if(newsletterStore.getPicture()!=null && newsletterStore.getPicture().getPhysicalFile() != null)
99      		{
100     			newsletter.getPicture().setIdFile(newsletterStore.getPicture().getIdFile());
101     			newsletter.getPicture().getPhysicalFile().setIdPhysicalFile(newsletterStore.getPicture().getPhysicalFile().getIdPhysicalFile());
102     			FileHome.update(newsletter.getPicture());
103     		}
104     		else
105     		{
106     			FileHome.create(newsletter.getPicture());
107     			
108     		}
109     			
110     	}
111     	
112     	if(newsletter != null)
113     	{
114     		_cache.removeKey( newsletter.getCode());
115     	}
116     	
117         _dao.store( newsletter, _plugin );
118 
119         return newsletter;
120     }
121 
122     /**
123      * Remove the newsletter whose identifier is specified in parameter
124      * @param nKey The newsletter Id
125      */
126     public static void remove( int nKey )
127     {
128     	Newsletter newsletter=findByPrimaryKey(nKey);
129     	if(newsletter!=null)
130     	{
131     		if(newsletter.getPicture()!=null)
132     		{
133     			FileHome.remove(newsletter.getPicture().getIdFile());
134     		}
135     		_cache.removeKey( newsletter.getCode());
136         	
137     		
138     		_dao.delete( nKey, _plugin );
139     	}
140     }
141 
142     ///////////////////////////////////////////////////////////////////////////
143     // Finders
144 
145     /**
146      * Returns an instance of a newsletter whose identifier is specified in parameter
147      * @param nKey The newsletter primary key
148      * @return an instance of Newsletter
149      */
150     public static Newsletter findByPrimaryKey( int nKey )
151     {
152     	Newsletter newsletter= _dao.load( nKey, _plugin);
153     	if(newsletter!=null && newsletter.getPicture()!= null)
154     	{	
155         	newsletter.setPicture(FileHome.findByPrimaryKey(newsletter.getPicture().getIdFile()));
156     	}
157     	
158     	
159     	 return newsletter;
160     }
161     
162     
163     /**
164      * Returns an instance of a newsletter whose identifier is specified in parameter
165      * @param nKey The newsletter primary key
166      * @return an instance of Newsletter
167      */
168     public static Newsletter findByCode( String strCode )
169     {
170     	Newsletter newsletter=(Newsletter) _cache.getFromCache(strCode );
171     	if(newsletter == null)
172     	{
173 	    	newsletter= _dao.loadByCode( strCode, _plugin);
174 	    	if(newsletter!=null &&newsletter.getPicture()!= null)
175 	    	{	
176 	        	newsletter.setPicture(FileHome.findByPrimaryKey(newsletter.getPicture().getIdFile()));
177 	    	}
178 	    	
179     	}
180     	 if(newsletter!=null)
181 		 {
182 			  _cache.putInCache( strCode, newsletter );
183 			  
184 			  
185 		 }
186 	   return newsletter;
187     }
188     
189     
190     
191 
192     /**
193      * Load the data of all the newsletter objects and returns them as a list
194      * @return the list which contains the data of all the newsletter objects
195      */
196     public static List<Newsletter> getNewslettersList( )
197     {
198         return _dao.selectNewslettersList( _plugin );
199     }
200     
201     /**
202      * Load the id of all the newsletter objects and returns them as a list
203      * @return the list which contains the id of all the newsletter objects
204      */
205     public static List<Integer> getIdNewslettersList( )
206     {
207         return _dao.selectIdNewslettersList( _plugin );
208     }
209     
210     /**
211      * Load the data of all the newsletter objects and returns them as a referenceList
212      * @return the referenceList which contains the data of all the newsletter objects
213      */
214     public static ReferenceList getNewslettersReferenceList( )
215     {
216         return _dao.selectNewslettersReferenceList(_plugin );
217     }
218     
219     /**
220      * check if the icon id exist
221      * @param nIdIcon the icon Id
222      * @return true if the icon id exist
223      */
224     
225     public static boolean isIconExist(int nIdIcon)
226     {
227     	
228     	return _dao.isIconExist(nIdIcon,_plugin);
229     	
230     }
231 }
232