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;
}