-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Please help, removing child entity from parent Home object
PostPosted: Mon Dec 01, 2008 2:08 pm 
Newbie

Joined: Tue Nov 25, 2008 9:31 pm
Posts: 18
I have entities with realtionship like :
-Project entity can have multiple QuantExperiments.
-QuantExperiment entity can have multiple Reagents and
-Reagent entity can have multiple Treatments.

We have a functionality where user clicks a link to remove reagent from QuantExperimentEdit.xhtml.
Clicking on "Remove Reagent" removes particular row from ui.

But when I click "Update Experiment", it removes ALL treatments, ALL reagents, Experiments and project.

I am overriding QuantExperimentHome.update(), where I am checking the reagent and treatments which needs to be removed.

Code:
[b]QuantExperimentEdit.xhtml:[/b]
-------------------------
<s:link action="#{quantExperimentHome.removeReagentLink}"
  value="Remove Reagent">
<f:param name="hashcode" value="#{info.hashCode()}"/>
</s:link>

<h:commandButton id="update"
  value="Update Experiment"
action="#{quantExperimentHome.update}"
rendered="#{quantExperimentHome.managed}"/>


Code:
[b]QuantExperimentHome.java[/b]
------------------------
public String removeReagentLink(){
javax.faces.context.FacesContext facesContext = (new org.jboss.seam.faces.FacesContext()).getContext();
javax.servlet.http.HttpServletRequest hsr=(javax.servlet.http.HttpServletRequest)facesContext.getCurrentInstance().getExternalContext().getRequest();
String reagentHashcode=hsr.getParameter("hashcode");
   Reagent[] reagentNew=null;
   if(reagent1==null || reagent1.length==1){
  //Added for "At least one reagent is required to create a Project" validation
  return "fail";
}else{
  reagentNew=new Reagent[reagent1.length-1];
}
int j=0;
for(int i=0;i<reagentNew.length;i++){
     if(reagent1[j].hashCode()!=Integer.parseInt(reagentHashcode)){
    reagentNew[i]=reagent1[j];
    j++;
     }else{
      i--;
      j++;
     }
}
this.reagent1=reagentNew;
return "";
}

@Override
public String update(){
QuantExperiment qeUpdate = getInstance();

List<Reagent> re=getInstance().getReagent();

int xs=re.size();
for(int i=0;i<xs;i++){
  Reagent reg=re.get(i);
  if(!findMatchingReagent(reg)){
   System.out.println(reg.getPname()+" has been removed.");
   List<Treatment> trs=reg.getTreatment();
   int xt=trs.size();
   for(int k=0;k<xt;k++){
    entityManager.remove(trs.get(k));
   }
   entityManager.remove(reg);
  }else{
   System.out.println(reg.getHjid()+" has not been removed");
  }
}
re.clear();
   for(int i=0;i<this.reagent1.length;i++){
  re.add(reagent1[i]);
}
return super.update();
}

private boolean findMatchingReagent(Reagent re){
boolean bl=false;
for(int j=0;j<this.reagent1.length;j++){
  if(reagent1[j].getHjid()==re.getHjid()){
   bl=true;
   break;
  }
}
return bl;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.