0

I have implemented Autoconverter (with forceSelection=false) in maintainance screen. To edit existing record, User will select ID from Autocomplete list.

To add new record, user will enter new ID in same box.

In converter, Application will try to search record in DB using ID.
If not found, New empty object is created with supplied ID and to avoid duplications, this object is added to array list maintained in Converter.

This works as expected on single browser session. but while testing with multiple browser, I found that Array list is shared across all instances.

I am not sure whether approach I have taken is right? if not can you please suggest me an alternative approach.

private List<SchoolMasterDetails> schoolMasterDetailsDB = new ArrayList<SchoolMasterDetails>();


@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
SchoolMasterDetails selectedObject = null;  

System.out.println("getAsObject ==> Entering.");
System.out.println("getAsObject ==> '" + submittedValue + "'");

if (!submittedValue.trim().equals("")) {
    selectedObject = (SchoolMasterDetails) getMasterService().getSchoolbyCode(submittedValue);

    if (selectedObject == null) {
        // search Object on localDB
        for (SchoolMasterDetails p : schoolMasterDetailsDB) {
            if (p.getSchoolCode().equalsIgnoreCase(submittedValue.trim())) {
                System.out.println("getAsObject from ArrayList ==> " + p);
                return p;   // return selectedObject from list of created objects
            }
        }

        System.out.println("getAsObject ==> selectedObject is null, Hence Creating new Object");
        selectedObject = new SchoolMasterDetails();
        selectedObject.setSchoolCode(submittedValue.trim());
        selectedObject.setSchoolName("TEST TEST TEST");
        schoolMasterDetailsDB.add(selectedObject);
    }
    else {
        System.out.println("getAsObject from Database ==> " + selectedObject);
    }
}
System.out.println("getAsObject ==> " + selectedObject);
}
System.out.println("getAsObject ==> Exiting.");     
return selectedObject;
}

Regards,

Shirish

Shirish Bathe
  • 627
  • 9
  • 22

1 Answers1

1

As far as I understand this (still learning myself), a converter fulfills exactly one purpose: It prepares your custom objects to be used in the views (getAsString) and translates Strings back into objects (getAsObject). It will be used whenever an input (a radio list, textfield, autocomplete) is tied to a variable in a backing bean that is of the type of your custom object. It is in your freedom to decide what String should be used to represent your object and how you use this String in return to look up objects.

With this in mind I would not use a converter to store a local list of objects, nor let it handle the creation process itself. Instead, I'd assume there is a backing bean somewhere, which holds your data objects and takes care of all your logic. This bean can have a list of, say, schoolMasters that can be queried for the objects it contains (similar to what your doing). You could then either implement the lookup there in a way that it handles the not-found case and always returns a valid object (which may be a new one), or you could catch the not-found-case in the converter and then trigger a createNew() from the bean to get a new instance.

IMHO this separates the management of the instances more clearly from the translating purpose of your converter. Also, from your code, it seems like you have two places to look up objects - via getMasterService() (a local method?) and inside your stored ArrayList. I don't quite get this...


As for your problem with the browsers sharing an instance: This sounds like a scope issue. If your backing bean, which is supposed to store and manage your data, is in application scope then the same set of data will be available as long as the application runs. This data will be available across browsers and also across users.

On the other hand, if you put the bean in session scope, each session will create its own instance of the bean and store unique data. Similarly, view scoped beans live as long as a single view and request beans are trashed and regenerate for each http request. You can read more here: How to choose the right scope

The answers there talk about beans (which is where your data usually lives). I'm not sure about converters, I see them as classes that are available application wide, so that each session and view can use them for translation - if you maintain a list there, it may well be globally available.

Community
  • 1
  • 1
Louise
  • 1,451
  • 1
  • 18
  • 40