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.myapps.modules.database.service;
35  
36  import fr.paris.lutece.plugins.myapps.business.MyApps;
37  import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabase;
38  import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabaseFilter;
39  import fr.paris.lutece.plugins.myapps.modules.database.business.MyAppsDatabaseUser;
40  import fr.paris.lutece.plugins.myapps.modules.database.utils.constants.MyAppsDatabaseConstants;
41  import fr.paris.lutece.plugins.myapps.service.MyAppsManager;
42  import fr.paris.lutece.plugins.myapps.service.MyAppsProvider;
43  import fr.paris.lutece.portal.service.i18n.I18nService;
44  import fr.paris.lutece.portal.service.plugin.Plugin;
45  import fr.paris.lutece.portal.service.plugin.PluginService;
46  import fr.paris.lutece.portal.service.security.LuteceUser;
47  import fr.paris.lutece.portal.service.util.AppPropertiesService;
48  import fr.paris.lutece.util.url.UrlItem;
49  
50  import org.apache.commons.lang.StringUtils;
51  
52  import java.util.List;
53  import java.util.Locale;
54  
55  /**
56   * 
57   * MyAppsDatabaseProvider
58   * 
59   */
60  public final class MyAppsDatabaseProvider implements MyAppsProvider
61  {
62      private static final String PROPERTY_PROVIDER_NAME = "module.myapps.database.page_myapps.provider.name";
63      private static final String PROPERTY_DEFAULT_PROVIDER_NAME = "myapps-database.provider.defaultName";
64      private static MyAppsDatabaseProvider _singleton;
65  
66      /**
67       * Constructor
68       */
69      private MyAppsDatabaseProvider( )
70      {
71      }
72  
73      /**
74       * Get the instance of {@link MyAppsDatabaseProvider}
75       * 
76       * @return an instance of {@link MyAppsDatabaseProvider}
77       */
78      public static synchronized MyAppsDatabaseProvider getInstance( )
79      {
80          if ( _singleton == null )
81          {
82              _singleton = new MyAppsDatabaseProvider( );
83          }
84  
85          return _singleton;
86      }
87  
88      /**
89       * Init the provider
90       */
91      public void init( )
92      {
93          getInstance( ).register( );
94      }
95  
96      /**
97       * Register the provider to the manager
98       */
99      public void register( )
100     {
101         MyAppsManager.getInstance( ).registerProvider( this );
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     public String getPluginName( )
108     {
109         return MyAppsDatabasePlugin.PLUGIN_NAME;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     public String getProviderName( )
116     {
117         return AppPropertiesService.getProperty( PROPERTY_DEFAULT_PROVIDER_NAME );
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     public String getProviderName( Locale locale )
124     {
125         return I18nService.getLocalizedString( PROPERTY_PROVIDER_NAME, locale );
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     public String getResourceImage( String strMyAppId )
132     {
133         Plugin plugin = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
134 
135         return MyAppsDatabaseService.getInstance( ).getResourceImageUrl( strMyAppId, plugin );
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
141     public List<MyApps> getMyAppsListByUserName( String strUserName, boolean isAscSort )
142     {
143         Plugin plugin = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
144         MyAppsDatabaseFilter filter = new MyAppsDatabaseFilter( );
145         filter.setUserName( strUserName );
146         return MyAppsDatabaseService.getInstance( ).getMyAppsListByFilter( filter, isAscSort, plugin );
147     }
148 
149     /**
150      * {@inheritDoc}
151      */
152     public List<MyApps> getMyAppsListByCategory( String strCodeCategory, boolean bIsAscSort )
153     {
154         // TODO Auto-generated method stub
155         List<MyApps> myApps = null;
156         if ( !MyAppsDatabaseCacheService.getInstance( ).isCacheEnable( ) || MyAppsDatabaseCacheService.getInstance( ).getFromCache( strCodeCategory ) == null )
157         {
158             Plugin plugin = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
159             MyAppsDatabaseFilter filter = new MyAppsDatabaseFilter( );
160             filter.setCategory( strCodeCategory );
161             myApps = MyAppsDatabaseService.getInstance( ).getMyAppsListByFilter( filter, bIsAscSort, plugin );
162             if ( MyAppsDatabaseCacheService.getInstance( ).isCacheEnable( ) && myApps != null && myApps.size( ) > 0 )
163             {
164                 MyAppsDatabaseCacheService.getInstance( ).putInCache( strCodeCategory, myApps );
165             }
166         }
167         else
168         {
169             myApps = (List<MyApps>) MyAppsDatabaseCacheService.getInstance( ).getFromCache( strCodeCategory );
170         }
171         return myApps;
172 
173     }
174 
175     /**
176      * {@inheritDoc}
177      */
178     public List<MyApps> getMyAppsListByUserNameAndCategory( String strUserName, String strCodeCategory, boolean bIsAscSort )
179     {
180         // TODO Auto-generated method stub
181         Plugin plugin = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
182         MyAppsDatabaseFilter filter = new MyAppsDatabaseFilter( );
183         filter.setUserName( strUserName );
184         filter.setCategory( strCodeCategory );
185         return MyAppsDatabaseService.getInstance( ).getMyAppsListByFilter( filter, bIsAscSort, plugin );
186     }
187 
188     /**
189      * {@inheritDoc}
190      */
191     public String getLabelManageMyApps( Locale locale )
192     {
193         return I18nService.getLocalizedString( MyAppsDatabaseConstants.PROPERTY_LABEL_MANAGE_MYAPPS, locale );
194     }
195 
196     /**
197      * {@inheritDoc}
198      */
199     public String getUrlOpenMyApps( int nMyAppId, LuteceUser user )
200     {
201         Plugin plugin = PluginService.getPlugin( MyAppsDatabasePlugin.PLUGIN_NAME );
202 
203         MyAppsDatabase myApp = (MyAppsDatabase) MyAppsDatabaseService.getInstance( ).findByPrimaryKey( nMyAppId, plugin );
204         String strUrl = StringUtils.EMPTY;
205 
206         if ( myApp != null )
207         {
208             // If the label of data is blank, then the value of data has to
209             // concatenated to the url
210             // This way, the AdminUser can pass hidden parameters
211             if ( StringUtils.isBlank( myApp.getDataHeading( ) ) && StringUtils.isNotBlank( myApp.getData( ) ) )
212             {
213                 StringBuilder sbUrl = new StringBuilder( myApp.getUrl( ) );
214 
215                 if ( myApp.getUrl( ).indexOf( MyAppsDatabaseConstants.QUESTION_MARK ) == -1 )
216                 {
217                     sbUrl.append( MyAppsDatabaseConstants.QUESTION_MARK );
218                 }
219                 else
220                 {
221                     sbUrl.append( MyAppsDatabaseConstants.AMPERSAND );
222                 }
223 
224                 sbUrl.append( myApp.getData( ) );
225                 strUrl = sbUrl.toString( );
226             }
227             else
228             {
229                 strUrl = myApp.getUrl( );
230             }
231             UrlItem url = new UrlItem( strUrl );
232             if ( user != null )
233             {
234 
235                 String strUserName = user.getName( );
236                 MyAppsDatabaseUser myAppsUser = (MyAppsDatabaseUser) MyAppsDatabaseService.getInstance( ).getCredential( nMyAppId, strUserName, plugin );
237 
238                 // an myApp can be not associated to a user
239                 if ( myAppsUser != null )
240                 {
241 
242                     // The login
243                     String strLoginFieldName = myApp.getCode( );
244                     String strUserLogin = myAppsUser.getStoredUserName( );
245 
246                     // Password
247                     String strPasswordField = myApp.getPassword( );
248                     String strUserPassword = myAppsUser.getStoredUserPassword( );
249 
250                     // Extra Field
251                     String strExtraField = myApp.getData( );
252                     String strExtraFieldValue = myAppsUser.getStoredUserData( );
253 
254                     if ( StringUtils.isNotBlank( myApp.getCode( ) ) )
255                     {
256                         url.addParameter( strLoginFieldName, strUserLogin );
257                     }
258                     if ( StringUtils.isNotBlank( myApp.getPassword( ) ) )
259                     {
260                         url.addParameter( strPasswordField, strUserPassword );
261                     }
262 
263                     // If the label of data is not blank, then it is an extra field the
264                     // LuteceUser has to fill
265                     if ( StringUtils.isNotBlank( strExtraField ) && StringUtils.isNotBlank( myApp.getDataHeading( ) ) )
266                     {
267                         url.addParameter( strExtraField, strExtraFieldValue );
268                     }
269                 }
270             }
271 
272             strUrl = url.getUrl( );
273         }
274 
275         return strUrl;
276     }
277 
278 }