1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
68
69 private MyAppsDatabaseProvider( )
70 {
71 }
72
73
74
75
76
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
90
91 public void init( )
92 {
93 getInstance( ).register( );
94 }
95
96
97
98
99 public void register( )
100 {
101 MyAppsManager.getInstance( ).registerProvider( this );
102 }
103
104
105
106
107 public String getPluginName( )
108 {
109 return MyAppsDatabasePlugin.PLUGIN_NAME;
110 }
111
112
113
114
115 public String getProviderName( )
116 {
117 return AppPropertiesService.getProperty( PROPERTY_DEFAULT_PROVIDER_NAME );
118 }
119
120
121
122
123 public String getProviderName( Locale locale )
124 {
125 return I18nService.getLocalizedString( PROPERTY_PROVIDER_NAME, locale );
126 }
127
128
129
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
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
151
152 public List<MyApps> getMyAppsListByCategory( String strCodeCategory, boolean bIsAscSort )
153 {
154
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
177
178 public List<MyApps> getMyAppsListByUserNameAndCategory( String strUserName, String strCodeCategory, boolean bIsAscSort )
179 {
180
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
190
191 public String getLabelManageMyApps( Locale locale )
192 {
193 return I18nService.getLocalizedString( MyAppsDatabaseConstants.PROPERTY_LABEL_MANAGE_MYAPPS, locale );
194 }
195
196
197
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
209
210
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
239 if ( myAppsUser != null )
240 {
241
242
243 String strLoginFieldName = myApp.getCode( );
244 String strUserLogin = myAppsUser.getStoredUserName( );
245
246
247 String strPasswordField = myApp.getPassword( );
248 String strUserPassword = myAppsUser.getStoredUserPassword( );
249
250
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
264
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 }