DeleteQuestionHandler.java :  » Mobile » smap-suite » Utilities » Java Open Source

Java Open Source » Mobile » smap suite 
smap suite » Utilities » DeleteQuestionHandler.java
package Utilities;
import java.util.*;

import org.jdom.*;

public class DeleteQuestionHandler 
{
  private ArrayList<Element> nodelist;
  private Element deleted;
  private int index;
  private String filename;
  public DeleteQuestionHandler(ArrayList<Element> nodelist, int index,String filename)
  {
    this.nodelist = nodelist;
    this.deleted = nodelist.get(index);
    this.index = index;
    this.filename = filename;
    if(index == nodelist.size()-1)
    {
      this.nodelist.remove(index);
      new SaveXML(nodelist,filename);
    }
    else
    {
      this.nodelist.remove(index);
      shiftID();
      //save to temp XML file
      new SaveXML(nodelist,filename);
      String fname = filename.substring(33,filename.indexOf("Temp.xml"));
      new SaveMobileXML(nodelist,fname);
    }

  }
  //return 0 if it is not in a group
  // else return group number
  public int isGroup(Element e)
  {
    List<Element>l = e.getChildren();
    if(l.get(5).getAttributeValue("v") == "0")
    {
      return 0;
    }
    else
    {
      return Integer.parseInt(l.get(5).getAttributeValue("v"));
    }
  }
  
  public void shiftID()
  {
    int QNumber = 0;
    for(int i=0; i<nodelist.size(); i++)
    {
      ProcessSingleNodes p = new ProcessSingleNodes(nodelist,i);
      int tempGID = p.getGroupID();
      QNumber ++;
      if(tempGID==0)
      {
        p.setFormValue(p.getFormValue().replaceAll(p.QuestionNumber(), "Q"+QNumber+""));
        p.setQuestionNumber("Q"+QNumber+"");
      }
      if(tempGID!=0)
      {
        int j;
        int number=1;
        for(j=i; j<nodelist.size(); j++)
        {
          ProcessSingleNodes p1 = new ProcessSingleNodes(nodelist,j);
          if(p1.getGroupID() != tempGID)
          {
            number = j-i;
            break;
          }
          else
          {
            p1.setFormValue(p1.getFormValue().replaceAll(p1.QuestionNumber(), "Q"+QNumber+"."+number));
            p1.setQuestionNumber("Q"+QNumber+"."+number);
            p1.setGroupID(QNumber+"");
            number ++;
          }
        }
        i += number-1;
      }
    }
    for(int j=0; j<nodelist.size(); j++)
    {
      ProcessSingleNodes p = new ProcessSingleNodes(nodelist,j);
      if(p.getGroupID()!=0)
      {
        int groupID = p.getGroupID();
        int subvalue = 0;
        for(int k=0; k<nodelist.size(); k++)
        {
          ProcessSingleNodes node = new ProcessSingleNodes(nodelist,k);
          if(node.getGroupID() == groupID)
          {
            subvalue ++;
          }
        }
        for(int i=0; i<nodelist.size(); i++)
        {
          ProcessSingleNodes node1 = new ProcessSingleNodes(nodelist,i);
          if(node1.QuestionNumber().compareTo("Q"+groupID+".1") == 0)
          {
            node1.setSub((subvalue-1)+"");
          }
        }
      }
    }
  }
  
  public String stripID(ProcessSingleNodes s)
  {
    String id = s.QuestionNumber();
    if(id.contains("q"))
      return id.split("q")[1];
    if(id.contains("Q"))
      return id.split("Q")[1];
    return null; 
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.