0

I am writing a program for a content management system that creates a form based on the contents of a given XML. The XML has has information about the structure of the content (comes from dropdown) to be created and the input types (text area for paragraph, textfield for chapter title, etc.) associated with each leaf. I shall call this a 'fragment'. A fragment can either be a leaf (textbox, text area, etc. - holding sentences / paragraphs) or a Node (which has other fragments inside it).

I want to confirm that any newly created fragment(s) in a material don't previously exist. For this, I will need to know what fragment type (is it a chapter title, chapter summary, etc.) it is so that I can query the database to find out if the current fragment is a duplicate, based on content.

I tried to save a reference to the dynamically created component object in an ArrayList. This arraylist has fragment type, for every input component created. In my bean, I call:

List<UIComponent> createMaterialView =  SharedData.findComponent("template_form").getChildren();

and recursively step through the children that are in createMaterialView, none of the UIComponents that are processed in the form are matching the UIComponents I added into my ArrayList earlier.

private FragmentTypeLeaf getFragmentTypeIfLeaf(UIComponent component) {
    System.out.println("Checking if "+component+" of class:"+component.getClass()+" is in LeafFrags");
    for(FragmentTypeLeaf currentLeaf : leafFragments) {
      if(component.equals(currentLeaf.getInputComponent())) {
          //Found leaf fragment input panel.
          System.out.println("Found a leaf fragment:"+currentLeaf.getName()+",Type:"+currentLeaf.getFragmentType());
          return currentLeaf;
          //Validate this content with all fragments existing (of this type) to ensure not exist.
      }
    }
    return null;
}

I did console outputs for UIComponent.toString() when I created them and added them to the form + leafFragments, and compared them to he component.toString in the getFragmentTypeIfLeaf() pasted below. I see that though the expected classes match, the UIComponents don't.

Are UIComponents not passed by reference? If I cannot create the UIComponent, add it to the form + track its reference by adding it to a list, then how can I design a system that can check if a dynamically created UIComponent (which needs to be associated with a fragment type) has content that already exists for that fragment type in the database?

Thanks! Sorry if that was too verbose. I hope the question is clear.

mvishnu
  • 1,150
  • 1
  • 12
  • 15

1 Answers1

1

UIComponent instances are request scoped and recreated on every request. It's only their state which is view scoped. You should never reference UIComponent instances as a property of a bean in a broader scope. Moreover, the UIComponent class doesn't have the equals() method implemented.

It's unclear what exactly you're trying to achieve this way, but one thing is clear for sure: you're approaching it from the wrong side on. You should not dynamically manipulate the component tree in the controller side. You should instead do it in the view side using e.g. JSTL and/or iterator components which do their task based on the model (the bean properties). Additional advantage is, XHTML+XML is a much more suited language for the task of creating and defining a tree structure.

Long story short: How does the 'binding' attribute work in JSF? When and how should it be used?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Sorry for the late reply. If I understood you right, I don't think I am manipulating the component tree at the controller side. Just using the exact component to know what type's value is being set. Couldnt think of a way to connect a dynamic (nested) form to a property in the controller, so was thinking of using the component. As to not calling equals, thanks for the tip. Used UIComponent component.getId(). Thanks a ton! – mvishnu Sep 09 '15 at 15:28