Skip to content

Commit a4b6db4

Browse files
atos-mohamed-bseboo
authored andcommitted
LUT-29765: Ensure that mandatory fields are properly checked when validating a Form
1 parent 05d21f0 commit a4b6db4

File tree

1 file changed

+143
-98
lines changed

1 file changed

+143
-98
lines changed

src/java/fr/paris/lutece/plugins/genericattributes/service/entrytype/AbstractEntryTypeGeolocation.java

Lines changed: 143 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@
3636
*/
3737
package fr.paris.lutece.plugins.genericattributes.service.entrytype;
3838

39+
import java.util.ArrayList;
3940
import java.util.List;
4041
import java.util.Locale;
4142
import java.util.Objects;
4243

4344
import javax.servlet.http.HttpServletRequest;
4445

46+
import org.apache.commons.collections4.CollectionUtils;
4547
import org.apache.commons.lang3.StringUtils;
4648

4749
import fr.paris.lutece.plugins.genericattributes.business.Entry;
@@ -187,121 +189,54 @@ private void createOrUpdateProviderField( Entry entry, String fieldValue )
187189
* {@inheritDoc}
188190
*/
189191
@Override
190-
public GenericAttributeError getResponseData(Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale) {
191-
Integer maxIterationGeolocation = 0;
192+
public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
193+
{
192194
Integer lastIterationGeolocation = 0;
193195
Integer iterationNumberToSave = 0;
194196

195-
if (request.getSession().getAttribute(ATTRIBUTE_LAST_ITERATION_GEOLOCATION) != null) {
196-
lastIterationGeolocation = (Integer) request.getSession().getAttribute(ATTRIBUTE_LAST_ITERATION_GEOLOCATION);
197-
request.getSession().setAttribute(ATTRIBUTE_LAST_ITERATION_GEOLOCATION, lastIterationGeolocation + 1);
198-
lastIterationGeolocation = lastIterationGeolocation + 1;
199-
} else {
200-
request.getSession().setAttribute(ATTRIBUTE_LAST_ITERATION_GEOLOCATION, 0);
197+
if ( request.getAttribute( ATTRIBUTE_LAST_ITERATION_GEOLOCATION ) != null )
198+
{
199+
lastIterationGeolocation = (Integer) request.getAttribute( ATTRIBUTE_LAST_ITERATION_GEOLOCATION );
200+
lastIterationGeolocation += 1;
201+
request.setAttribute( ATTRIBUTE_LAST_ITERATION_GEOLOCATION, lastIterationGeolocation );
202+
}
203+
else
204+
{
205+
request.setAttribute( ATTRIBUTE_LAST_ITERATION_GEOLOCATION, 0 );
201206
}
202-
if (request.getParameter(PARAMETER_NUMBER_ITERATION) != null) {
203-
maxIterationGeolocation = Integer.parseInt(request.getParameter(PARAMETER_NUMBER_ITERATION));
207+
if ( request.getParameter( PARAMETER_NUMBER_ITERATION ) != null )
208+
{
204209
iterationNumberToSave = lastIterationGeolocation;
205-
} else {
210+
}
211+
else
212+
{
206213
iterationNumberToSave = -1;
207214
}
215+
216+
// Get the value of the fields containing the user's input
208217
String prefixIteration = String.valueOf(entry.getIdEntry( ));
209218
String prefixWithIteration = PARAMETER_PREFIX_ITERATION + lastIterationGeolocation + "_" + IEntryTypeService.PREFIX_ATTRIBUTE + entry.getIdEntry();
210219
if(request.getParameter(prefixWithIteration+PARAMETER_SUFFIX_ADDRESS) != null)
211220
{
212221
prefixIteration = prefixWithIteration;
213222
}
214-
String strIdAddressValue = request.getParameter(prefixIteration + PARAMETER_SUFFIX_ID_ADDRESS);
215223

224+
// Retrieve the values from the user's input
225+
String strIdAddressValue = request.getParameter(prefixIteration + PARAMETER_SUFFIX_ID_ADDRESS);
216226
String strAddressValue = request.getParameter(prefixIteration + PARAMETER_SUFFIX_ADDRESS);
217227
String strAdditionalAddressValue = request.getParameter(prefixIteration + entry.getIdEntry() + PARAMETER_SUFFIX_ADDITIONAL_ADDRESS);
218228
String strXValue = request.getParameter(prefixIteration + PARAMETER_SUFFIX_X);
219229
String strYValue = request.getParameter(prefixIteration + PARAMETER_SUFFIX_Y);
220230
String strGeometryValue = request.getParameter(prefixIteration + PARAMETER_SUFFIX_GEOMETRY);
221231

222-
Field fieldIdAddress = entry.getFieldByCode(FIELD_ID_ADDRESS);
223-
Field fieldAddress = entry.getFieldByCode(FIELD_ADDRESS);
224-
Field fieldAdditionalAddress = entry.getFieldByCode(FIELD_ADDITIONAL_ADDRESS);
225-
Field fieldX = entry.getFieldByCode(FIELD_X);
226-
Field fieldY = entry.getFieldByCode(FIELD_Y);
227-
Field fieldGeometry = entry.getFieldByCode(FIELD_GEOMETRY);
228-
229-
/**
230-
* Create the field "idAddress" in case the field does not exist in the database.
231-
*/
232-
if (fieldIdAddress == null) {
233-
fieldIdAddress = GenericAttributesUtils.createOrUpdateField(entry, FIELD_ID_ADDRESS, null, FIELD_ID_ADDRESS);
234-
FieldHome.create(fieldIdAddress);
235-
}
236-
237-
// 1 : Response Id Address
238-
if (strIdAddressValue != null) {
239-
Response responseIdAddress = new Response();
240-
responseIdAddress.setEntry(entry);
241-
responseIdAddress.setResponseValue(strIdAddressValue);
242-
responseIdAddress.setField(fieldIdAddress);
243-
responseIdAddress.setToStringValueResponse(strIdAddressValue);
244-
responseIdAddress.setIterationNumber(iterationNumberToSave);
245-
listResponse.add(responseIdAddress);
246-
// take this value of the request
247-
248-
}
249-
250-
// 2 : Response Address
251-
if (strAddressValue != null) {
252-
Response responseAddress = new Response();
253-
responseAddress.setEntry(entry);
254-
responseAddress.setResponseValue(strAddressValue);
255-
responseAddress.setField(fieldAddress);
256-
responseAddress.setToStringValueResponse(strAddressValue);
257-
responseAddress.setIterationNumber(iterationNumberToSave);
258-
listResponse.add(responseAddress);
259-
}
260-
261-
// 3 : Response Additional Address
262-
if (strAdditionalAddressValue != null) {
263-
Response responseAdditionalAddress = new Response();
264-
responseAdditionalAddress.setEntry(entry);
265-
responseAdditionalAddress.setResponseValue(strAdditionalAddressValue);
266-
responseAdditionalAddress.setField(fieldAdditionalAddress);
267-
responseAdditionalAddress.setToStringValueResponse(strAdditionalAddressValue);
268-
responseAdditionalAddress.setIterationNumber(iterationNumberToSave);
269-
listResponse.add(responseAdditionalAddress);
270-
}
271-
// 4 : Response X
272-
if (strXValue != null) {
273-
Response responseX = new Response();
274-
responseX.setEntry(entry);
275-
responseX.setResponseValue(strXValue);
276-
responseX.setField(fieldX);
277-
responseX.setToStringValueResponse(strXValue);
278-
responseX.setIterationNumber(iterationNumberToSave);
279-
listResponse.add(responseX);
280-
}
281-
282-
// 5: Response Y
283-
if (strXValue != null) {
284-
Response responseY = new Response();
285-
responseY.setEntry(entry);
286-
responseY.setResponseValue(strYValue);
287-
responseY.setField(fieldY);
288-
responseY.setToStringValueResponse(strYValue);
289-
responseY.setIterationNumber(iterationNumberToSave);
290-
listResponse.add(responseY);
291-
}
292-
293-
// 6 : Response Desc Geo
294-
if (strGeometryValue != null) {
295-
Response responseGeomerty = new Response();
296-
responseGeomerty.setEntry(entry);
297-
responseGeomerty.setResponseValue(strGeometryValue);
298-
responseGeomerty.setField(fieldGeometry);
299-
responseGeomerty.setToStringValueResponse(strGeometryValue);
300-
responseGeomerty.setIterationNumber(iterationNumberToSave);
301-
listResponse.add(responseGeomerty);
232+
// Get the List of responses for this Entry
233+
List<Response> listEntryResponse = getGeolocationResponseList( entry, iterationNumberToSave, strIdAddressValue, strAddressValue, strAdditionalAddressValue,
234+
strXValue, strYValue, strGeometryValue );
235+
if ( CollectionUtils.isNotEmpty( listEntryResponse ) )
236+
{
237+
listResponse.addAll( listEntryResponse );
302238
}
303239

304-
305240
if (entry.isMandatory() && StringUtils.isBlank(strAddressValue)) {
306241
return new MandatoryError(entry, locale);
307242
}
@@ -316,11 +251,6 @@ public GenericAttributeError getResponseData(Entry entry, HttpServletRequest req
316251

317252
return error;
318253
}
319-
if (request.getSession().getAttribute(ATTRIBUTE_LAST_ITERATION_GEOLOCATION) != null) {
320-
if (lastIterationGeolocation >= maxIterationGeolocation) {
321-
request.getSession().removeAttribute(ATTRIBUTE_LAST_ITERATION_GEOLOCATION);
322-
}
323-
}
324254
return super.getResponseData(entry, request, listResponse, locale);
325255
}
326256

@@ -445,4 +375,119 @@ private Field buildFieldMapProvider( Entry entry, String strMapProvider )
445375

446376
return fieldMapProvider;
447377
}
378+
379+
/**
380+
* Get the List of Responses from a Geolocation Entry, containing the address values for the user's input
381+
*
382+
* @param entry
383+
* The Entry to process
384+
* @param iterationNumberToSave
385+
* Current iteration of this Entry
386+
* @param strIdAddressValue
387+
* Specific ID of the address
388+
* @param strAddressValue
389+
* Actual value of the address
390+
* @param strAdditionalAddressValue
391+
* Optional extra address value
392+
* @param strXValue
393+
* X coordinates of the address
394+
* @param strYValue
395+
* Y coordinates of the address
396+
* @param strGeometryValue
397+
*
398+
* @return a List of Reponses
399+
*/
400+
private List<Response> getGeolocationResponseList( Entry entry, int iterationNumberToSave, String strIdAddressValue, String strAddressValue,
401+
String strAdditionalAddressValue, String strXValue, String strYValue, String strGeometryValue )
402+
{
403+
// List of all the responses retrieved from the Geolocation Entry's input
404+
List<Response> listGeolocationResponses = new ArrayList<>( );
405+
406+
// Fields part of Geolocation entries
407+
Field fieldIdAddress = entry.getFieldByCode( FIELD_ID_ADDRESS );
408+
Field fieldAddress = entry.getFieldByCode( FIELD_ADDRESS );
409+
Field fieldAdditionalAddress = entry.getFieldByCode( FIELD_ADDITIONAL_ADDRESS );
410+
Field fieldX = entry.getFieldByCode( FIELD_X );
411+
Field fieldY = entry.getFieldByCode( FIELD_Y );
412+
Field fieldGeometry = entry.getFieldByCode( FIELD_GEOMETRY );
413+
414+
// Create the field "idAddress" in case the field does not exist in the database
415+
if ( fieldIdAddress == null )
416+
{
417+
fieldIdAddress = GenericAttributesUtils.createOrUpdateField( entry, FIELD_ID_ADDRESS, null, FIELD_ID_ADDRESS );
418+
FieldHome.create( fieldIdAddress );
419+
}
420+
421+
// 1 : Response Id Address
422+
if ( strIdAddressValue != null )
423+
{
424+
Response responseIdAddress = new Response( );
425+
responseIdAddress.setEntry( entry );
426+
responseIdAddress.setResponseValue( strIdAddressValue );
427+
responseIdAddress.setField( fieldIdAddress );
428+
responseIdAddress.setToStringValueResponse( strIdAddressValue );
429+
responseIdAddress.setIterationNumber( iterationNumberToSave );
430+
listGeolocationResponses.add( responseIdAddress );
431+
}
432+
433+
// 2 : Response Address
434+
if ( strAddressValue != null )
435+
{
436+
Response responseAddress = new Response( );
437+
responseAddress.setEntry( entry );
438+
responseAddress.setResponseValue( strAddressValue );
439+
responseAddress.setField( fieldAddress );
440+
responseAddress.setToStringValueResponse( strAddressValue );
441+
responseAddress.setIterationNumber( iterationNumberToSave );
442+
listGeolocationResponses.add( responseAddress );
443+
}
444+
445+
// 3 : Response Additional Address
446+
if ( strAdditionalAddressValue != null )
447+
{
448+
Response responseAdditionalAddress = new Response( );
449+
responseAdditionalAddress.setEntry( entry );
450+
responseAdditionalAddress.setResponseValue( strAdditionalAddressValue );
451+
responseAdditionalAddress.setField( fieldAdditionalAddress );
452+
responseAdditionalAddress.setToStringValueResponse( strAdditionalAddressValue );
453+
responseAdditionalAddress.setIterationNumber( iterationNumberToSave );
454+
listGeolocationResponses.add( responseAdditionalAddress );
455+
}
456+
// 4 : Response X
457+
if ( strXValue != null )
458+
{
459+
Response responseX = new Response( );
460+
responseX.setEntry( entry );
461+
responseX.setResponseValue( strXValue );
462+
responseX.setField( fieldX );
463+
responseX.setToStringValueResponse( strXValue );
464+
responseX.setIterationNumber( iterationNumberToSave );
465+
listGeolocationResponses.add( responseX );
466+
}
467+
468+
// 5: Response Y
469+
if ( strYValue != null )
470+
{
471+
Response responseY = new Response( );
472+
responseY.setEntry( entry );
473+
responseY.setResponseValue( strYValue );
474+
responseY.setField( fieldY );
475+
responseY.setToStringValueResponse( strYValue );
476+
responseY.setIterationNumber( iterationNumberToSave );
477+
listGeolocationResponses.add( responseY );
478+
}
479+
480+
// 6 : Response Desc Geo
481+
if ( strGeometryValue != null )
482+
{
483+
Response responseGeomerty = new Response( );
484+
responseGeomerty.setEntry( entry );
485+
responseGeomerty.setResponseValue( strGeometryValue );
486+
responseGeomerty.setField( fieldGeometry );
487+
responseGeomerty.setToStringValueResponse( strGeometryValue );
488+
responseGeomerty.setIterationNumber( iterationNumberToSave );
489+
listGeolocationResponses.add( responseGeomerty );
490+
}
491+
return listGeolocationResponses;
492+
}
448493
}

0 commit comments

Comments
 (0)