1 /*
2 * Copyright (c) 2002-2025, 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.pluginwizard.service;
35
36 import java.util.List;
37
38 import org.apache.commons.lang3.StringUtils;
39
40 import fr.paris.lutece.plugins.pluginwizard.business.model.Application;
41 import fr.paris.lutece.plugins.pluginwizard.business.model.Attribute;
42 import fr.paris.lutece.plugins.pluginwizard.business.model.BusinessClass;
43 import fr.paris.lutece.plugins.pluginwizard.business.model.Feature;
44 import fr.paris.lutece.plugins.pluginwizard.business.model.Portlet;
45 import fr.paris.lutece.plugins.pluginwizard.web.formbean.BusinessClassFormBean;
46
47 /**
48 * Model Service provides all plugin'model manipulations QualityService checks if the values entered in the pluginwizard forms are already used
49 */
50 public final class QualityService
51 {
52 // Validation for business class
53 public static boolean bValidUniqueBusinessClassName;
54 public static boolean bValidUniquePluralBusinessClassName;
55 public static boolean bValidUniqueTableName;
56
57 // Validation for attribute
58 public static boolean bValidUniqueAttributeName;
59
60 // Validation for admin feature
61 public static boolean bValideUniqueFeatureRight;
62 public static boolean bValideUniqueFeatureTitle;
63 public static boolean bValideUniqueFeatureTechName;
64
65 // Validation for XPage
66 public static boolean bValideUniqueAdministrationName;
67 public static boolean bValideUniqueAdministrationClass;
68
69 // Validation for portlet
70 public static boolean bValideUniquePortletClassName;
71 public static boolean bValideUniquePortletType;
72 public static boolean bValideUniquePortletJspName;
73
74 //////////////////////////////////// Validation for admin feature///////////////////////////////////////
75
76 /**
77 * Check if the feature title already used
78 *
79 * @param feature
80 * Feature to check.
81 * @param features
82 * The list of features in the plugin model
83 * @return true if it already used, false otherwise.
84 */
85 public static boolean existsDuplicateFeatureTitle( Feature feature, List<Feature> features )
86 {
87
88 for ( Feature item : features )
89 {
90 if ( item.getFeatureTitle( ).equals( feature.getFeatureTitle( ) ) && item.getId( ) != feature.getId( ) )
91 {
92 return true;
93 }
94 }
95 return false;
96 }
97
98 /**
99 * Check if the feature tech name already used
100 *
101 * @param feature
102 * Feature to check.
103 * @param features
104 * The list of features in the plugin model
105 * @return true if it already used, false otherwise.
106 */
107 public static boolean existsDuplicateFeatureTechName( Feature feature, List<Feature> features )
108 {
109 for ( Feature item : features )
110 {
111 if ( item.getFeatureName( ).equals( feature.getFeatureName( ) ) && item.getId( ) != feature.getId( ) )
112 {
113 return true;
114 }
115 }
116 return false;
117 }
118
119 /**
120 * Check if the feature right already used
121 *
122 * @param feature
123 * Feature to check.
124 * @param features
125 * The list of features in the plugin model
126 * @return true if it already used, false otherwise.
127 */
128 public static boolean existsDuplicateFeatureRight( Feature feature, List<Feature> features )
129 {
130 for ( Feature item : features )
131 {
132 if ( item.getFeatureRight( ).equals( feature.getFeatureRight( ) ) && item.getId( ) != feature.getId( ) )
133 {
134 return true;
135 }
136 }
137 return false;
138 }
139
140 /**
141 * Check if the form feature entries already used
142 *
143 * @param feature
144 * Feature to check.
145 * @param features
146 * The list of features in the plugin model
147 * @return true if it already used, false otherwise.
148 */
149 public static boolean existsDuplicateFeatureFields( Feature feature, List<Feature> features )
150 {
151 bValideUniqueFeatureRight = !existsDuplicateFeatureRight( feature, features );
152 bValideUniqueFeatureTitle = !existsDuplicateFeatureTitle( feature, features );
153 bValideUniqueFeatureTechName = !existsDuplicateFeatureTechName( feature, features );
154
155 return !( bValideUniqueFeatureRight && bValideUniqueFeatureTitle && bValideUniqueFeatureTechName );
156 }
157
158 //////////////////////////////////// Validation for Business Class ///////////////////////////////////////
159
160 /**
161 * Check if Business Class Name is already used
162 *
163 * @param businessClass
164 * business class to check.
165 * @param businessClasses
166 * The list of business class in the plugin model
167 * @param bCreate
168 * Boolean to know if it's a creation or a modification of business class
169 * @return true if it already used, false otherwise.
170 */
171 public static boolean existsDuplicateBusinessClassName( BusinessClassFormBean businessClass, List<BusinessClass> businessClasses, boolean bCreate )
172 {
173
174 for ( BusinessClass item : businessClasses )
175 {
176 if ( StringUtils.equals( StringUtils.lowerCase( item.getBusinessClass( ) ), StringUtils.lowerCase( businessClass.getBusinessClass( ) ) )
177 && ( item.getId( ) != businessClass.getId( ) || bCreate ) )
178 {
179 return true;
180 }
181 }
182 return false;
183 }
184
185 /**
186 * Check if Business Class Plural Name is already used
187 *
188 * @param businessClass
189 * business class to check.
190 * @param businessClasses
191 * The list of business class in the plugin model
192 * @param bCreate
193 * Boolean to know if it's a creation or a modification of business class
194 * @return true if it already used, false otherwise.
195 */
196 public static boolean existsDuplicatePluralBusinessClassName( BusinessClassFormBean businessClass, List<BusinessClass> businessClasses, boolean bCreate )
197 {
198
199 for ( BusinessClass item : businessClasses )
200 {
201 if ( StringUtils.equals( StringUtils.lowerCase( item.getPluralBusinessClass( ) ), StringUtils.lowerCase( businessClass.getPluralBusinessClass( ) ) )
202 && ( item.getId( ) != businessClass.getId( ) || bCreate ) )
203 {
204 return true;
205 }
206 }
207 return false;
208 }
209
210 /**
211 * Check if Business Class Table Name is already used
212 *
213 * @param businessClass
214 * business class to check.
215 * @param businessClasses
216 * The list of business class in the plugin model
217 * @param bCreate
218 * Boolean to know if it's a creation or a modification of business class
219 * @return true if it already used, false otherwise.
220 */
221 public static boolean existsDuplicateTableName( BusinessClassFormBean businessClass, List<BusinessClass> businessClasses, boolean bCreate )
222 {
223
224 for ( BusinessClass item : businessClasses )
225 {
226 if ( StringUtils.equals( StringUtils.lowerCase( item.getBusinessTableName( ) ), StringUtils.lowerCase( businessClass.getBusinessTableName( ) ) )
227 && ( item.getId( ) != businessClass.getId( ) || bCreate ) )
228 {
229 return true;
230 }
231 }
232 return false;
233 }
234
235 /**
236 * Check if the form business Class entries already used
237 *
238 * @param businessClass
239 * business class to check.
240 * @param businessClasses
241 * The list of business class in the plugin model
242 * @param bCreate
243 * Boolean to know if it's a creation or a modification of business class
244 * @return true if it already used, false otherwise.
245 */
246 public static boolean existsDuplicateBusinessClassFields( BusinessClassFormBean businessClass, List<BusinessClass> businessClasses, boolean bCreate )
247 {
248
249 bValidUniqueBusinessClassName = !existsDuplicateBusinessClassName( businessClass, businessClasses, bCreate );
250 bValidUniquePluralBusinessClassName = !existsDuplicatePluralBusinessClassName( businessClass, businessClasses, bCreate );
251 bValidUniqueTableName = !existsDuplicateTableName( businessClass, businessClasses, bCreate );
252
253 return !( bValidUniqueBusinessClassName && bValidUniquePluralBusinessClassName && bValidUniqueTableName );
254 }
255
256 //////////////////////////////////// Validation for Business Class ///////////////////////////////////////
257
258 /**
259 * Check if attribute Name entry is already used in a specific business class
260 *
261 * @param attribute
262 * attribute to check.
263 * @param attributes
264 * The list of attributes in a specific business class
265 * @return true if it already used, false otherwise.
266 */
267 public static boolean existsDuplicateAttributeName( Attribute attribute, List<Attribute> attributes )
268 {
269
270 for ( Attribute item : attributes )
271 {
272 if ( StringUtils.equals( StringUtils.lowerCase( item.getAttributeName( ) ), StringUtils.lowerCase( attribute.getAttributeName( ) ) )
273 && item.getId( ) != attribute.getId( ) )
274 {
275 return true;
276 }
277 }
278 return false;
279 }
280
281 /**
282 * Check if the form attribute Name entries already used
283 *
284 * @param attribute
285 * attribute to check.
286 * @param attributes
287 * The list of attributes in a specific business class
288 * @return true if it already used, false otherwise.
289 */
290 public static boolean existsDuplicateAttributeFields( Attribute attribute, List<Attribute> attributes )
291 {
292
293 bValidUniqueAttributeName = !existsDuplicateAttributeName( attribute, attributes );
294
295 return !bValidUniqueAttributeName;
296 }
297
298 //////////////////////////////////// Validation for Application ///////////////////////////////////////
299
300 /**
301 * Check if application Name entry is already used
302 *
303 * @param application
304 * application to check.
305 * @param applications
306 * The list of applications
307 * @return true if it already used, false otherwise.
308 */
309 public static boolean existsDuplicateAttributeName( Application application, List<Application> applications )
310 {
311
312 for ( Application item : applications )
313 {
314 if ( StringUtils.equals( StringUtils.lowerCase( item.getApplicationName( ) ), StringUtils.lowerCase( application.getApplicationName( ) ) )
315 && item.getId( ) != application.getId( ) )
316 {
317 return true;
318 }
319 }
320 return false;
321 }
322
323 /**
324 * Check if application Class entry is already used
325 *
326 * @param application
327 * application to check.
328 * @param applications
329 * The list of applications
330 * @return true if it already used, false otherwise.
331 */
332 public static boolean existsDuplicateAttributeClass( Application application, List<Application> applications )
333 {
334
335 for ( Application item : applications )
336 {
337 if ( StringUtils.equals( StringUtils.lowerCase( item.getApplicationClass( ) ), StringUtils.lowerCase( application.getApplicationClass( ) ) )
338 && item.getId( ) != application.getId( ) )
339 {
340 return true;
341 }
342 }
343 return false;
344 }
345
346 /**
347 * Check if the application form entries already used
348 *
349 * @param application
350 * application to check.
351 * @param applications
352 * The list of applications in a specific business class
353 * @return true if it already used, false otherwise.
354 */
355 public static boolean existsDuplicateApplicationFields( Application application, List<Application> applications )
356 {
357
358 bValideUniqueAdministrationName = !existsDuplicateAttributeName( application, applications );
359 bValideUniqueAdministrationClass = !existsDuplicateAttributeClass( application, applications );
360
361 return !( bValideUniqueAdministrationName && bValideUniqueAdministrationClass );
362 }
363
364 //////////////////////////////////// Validation for portlet///////////////////////////////////////
365
366 /**
367 * Check if the portlet class name is already used
368 *
369 * @param portlet
370 * Portlet to check.
371 * @param portlets
372 * The list of portlet in the plugin model
373 * @return true if it already used, false otherwise.
374 */
375 public static boolean existsDuplicatePortletClassName( Portlet portlet, List<Portlet> portlets )
376 {
377
378 for ( Portlet item : portlets )
379 {
380 if ( item.getPortletClass( ).equals( portlet.getPortletClass( ) ) && item.getId( ) != portlet.getId( ) )
381 {
382 return true;
383 }
384 }
385 return false;
386 }
387
388 /**
389 * Check if the portlet type already used
390 *
391 * @param portlet
392 * Portlet to check.
393 * @param portlets
394 * The list of portlet in the plugin model
395 * @return true if it already used, false otherwise.
396 */
397 public static boolean existsDuplicatePortletType( Portlet portlet, List<Portlet> portlets )
398 {
399 for ( Portlet item : portlets )
400 {
401 if ( item.getPortletTypeName( ).equals( portlet.getPortletTypeName( ) ) && item.getId( ) != portlet.getId( ) )
402 {
403 return true;
404 }
405 }
406 return false;
407 }
408
409 /**
410 * Check if the portlet jsp name already used
411 *
412 * @param portlet
413 * Portlet to check.
414 * @param portlets
415 * The list of portlet in the plugin model
416 * @return true if it already used, false otherwise.
417 */
418 public static boolean existsDuplicatePortletJspName( Portlet portlet, List<Portlet> portlets )
419 {
420 for ( Portlet item : portlets )
421 {
422 if ( item.getJspBaseName( ).equals( portlet.getJspBaseName( ) ) && item.getId( ) != portlet.getId( ) )
423 {
424 return true;
425 }
426 }
427 return false;
428 }
429
430 /**
431 * Check if the form feature entries already used
432 *
433 * @param portlet
434 * Portlet to check.
435 * @param portlets
436 * The list of portlet in the plugin model
437 * @return true if it already used, false otherwise.
438 */
439 public static boolean existsDuplicatePortletFields( Portlet portlet, List<Portlet> portlets )
440 {
441 bValideUniquePortletClassName = !existsDuplicatePortletClassName( portlet, portlets );
442 bValideUniquePortletType = !existsDuplicatePortletType( portlet, portlets );
443 bValideUniquePortletJspName = !existsDuplicatePortletJspName( portlet, portlets );
444
445 return !( bValideUniquePortletClassName && bValideUniquePortletType && bValideUniquePortletJspName );
446 }
447
448 }