porno porno rokettube
Results 1 to 2 of 2

Thread: To avoid duplication of data in database

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1

    To avoid duplication of data in database

    Hi all,
    i'm using the below code to upload XLS file and parse the contents into database, and i'm able to parse all the contents to directory table and its parent and child tables like category,subcategory,category_subcategory,subcateg ory_directory tables but if i upload the same xls file again its contents are added into the directory table. but it should be avoided (duplication of data should be prevented ) so pls any one help me in doing this.


    class DirectoryService {

    static transactional = true
    StringBuffer query
    Sql sql
    Connection connection;
    PreparedStatement preparedStatement

    def path

    DirectoryService() {

    query = new StringBuffer("")
    path = "${CH.platinum.directory.path}"

    }

    def upload(file) {
    if(!file.empty) {
    file.transferTo(new File("$path/myfile.xls"))
    return true
    }
    else {
    return false
    }
    }


    def synchDirectory(def user) {
    println "1 to ka 4..."
    String fileName="$path/myfile.xls";
    Vector dataHolder=read(fileName);
    saveToDatabase(dataHolder);
    }
    public static Vector read(String fileName) {
    Vector cellVectorHolder = new Vector();
    try{
    FileInputStream myInput = new FileInputStream(fileName);
    POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
    HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
    HSSFSheet mySheet = myWorkBook.getSheetAt(0);
    Iterator rowIter = mySheet.rowIterator();
    while(rowIter.hasNext()){
    HSSFRow myRow = (HSSFRow) rowIter.next();
    Iterator cellIter = myRow.cellIterator();
    Vector cellStoreVector=new Vector();

    while(cellIter.hasNext()){
    HSSFCell myCell = (HSSFCell) cellIter.next();

    cellStoreVector.addElement(myCell);
    }
    cellVectorHolder.addElement(cellStoreVector);
    }
    }catch (Exception e){e.printStackTrace(); }
    println"manu----->" +cellVectorHolder
    return cellVectorHolder;
    }
    private static void saveToDatabase(Vector dataHolder) {
    def directory = new Directory()
    ArrayList listDirectory = new ArrayList();
    String code="";
    String name="";
    String category="";
    String subcategory="";
    String description="";
    String price="";
    String quantity="";
    for (int i=1;i<dataHolder.size(); i++)
    {
    Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
    println"manu---->" +cellStoreVector
    int cellIndex=0;
    code = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString();
    cellIndex++;
    name = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString()
    cellIndex++;
    category = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString()
    cellIndex++;
    subcategory = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString()
    cellIndex++;
    description = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString()
    cellIndex++;
    price = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString()
    cellIndex++;
    quantity = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString()
    cellIndex++;

    def tempCategory = Category.findByName(category)
    if(!tempCategory){
    tempCategory = new Category(name:category)
    //tempUser.addToCategories(tempCategory)
    }
    def tempSubCategory = Subcategory.findByName(subcategory)
    if(!tempSubCategory){
    tempSubCategory = new Subcategory(name:subcategory)
    tempCategory.addToSubcategories(tempSubCategory)
    }
    //if(tempCategory.subcategories != null && (!tempCategory.subcategories.contains(tempSubCateg ory)) )
    //if(tempUser.categories != null && !tempUser.categories.contains(tempCategory))

    def q=new Directory(code:code,name:name,category:category,su bcategory:subcategory,description:description,pric eDouble.parseDouble(price)),quantityint)Double.parseDouble(quantity),file:"").save()
    tempSubCategory.addToDirectories(q)
    // tempUser.addToDirectories(q)
    tempCategory.save()
    }
    }

    Thanks inadvance,
    Manu

  2. #2
    i'm using the below code to upload XLS file and parse the contents into database, and i'm able to parse all the contents to directory table and its parent and child tables like category,subcategory,category_subcategory,subcateg ory_directory tables but if i upload the same xls file again its contents are added into the directory table. but it should be avoided (duplication of data should be prevented ) so pls any one help me in doing this.


    Quote Originally Posted by Manupatel View Post
    Hi all,
    i'm using the below code to upload XLS file and parse the contents into database, and i'm able to parse all the contents to directory table and its parent and child tables like category,subcategory,category_subcategory,subcateg ory_directory tables but if i upload the same xls file again its contents are added into the directory table. but it should be avoided (duplication of data should be prevented ) so pls any one help me in doing this.


    class DirectoryService {

    static transactional = true
    StringBuffer query
    Sql sql
    Connection connection;
    PreparedStatement preparedStatement

    def path

    DirectoryService() {

    query = new StringBuffer("")
    path = "${CH.platinum.directory.path}"

    }

    def upload(file) {
    if(!file.empty) {
    file.transferTo(new File("$path/myfile.xls"))
    return true
    }
    else {
    return false
    }
    }


    def synchDirectory(def user) {
    println "1 to ka 4..."
    String fileName="$path/myfile.xls";
    Vector dataHolder=read(fileName);
    saveToDatabase(dataHolder);
    }
    public static Vector read(String fileName) {
    Vector cellVectorHolder = new Vector();
    try{
    FileInputStream myInput = new FileInputStream(fileName);
    POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
    HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
    HSSFSheet mySheet = myWorkBook.getSheetAt(0);
    Iterator rowIter = mySheet.rowIterator();
    while(rowIter.hasNext()){
    HSSFRow myRow = (HSSFRow) rowIter.next();
    Iterator cellIter = myRow.cellIterator();
    Vector cellStoreVector=new Vector();

    while(cellIter.hasNext()){
    HSSFCell myCell = (HSSFCell) cellIter.next();

    cellStoreVector.addElement(myCell);
    }
    cellVectorHolder.addElement(cellStoreVector);
    }
    }catch (Exception e){e.printStackTrace(); }
    println"manu----->" +cellVectorHolder
    return cellVectorHolder;
    }
    private static void saveToDatabase(Vector dataHolder) {
    def directory = new Directory()
    ArrayList listDirectory = new ArrayList();
    String code="";
    String name="";
    String category="";
    String subcategory="";
    String description="";
    String price="";
    String quantity="";
    for (int i=1;i<dataHolder.size(); i++)
    {
    Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
    println"manu---->" +cellStoreVector
    int cellIndex=0;
    code = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString();
    cellIndex++;
    name = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString()
    cellIndex++;
    category = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString()
    cellIndex++;
    subcategory = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString()
    cellIndex++;
    description = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString()
    cellIndex++;
    price = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString()
    cellIndex++;
    quantity = ((HSSFCell)cellStoreVector.elementAt(cellIndex)).t oString()
    cellIndex++;

    def tempCategory = Category.findByName(category)
    if(!tempCategory){
    tempCategory = new Category(name:category)
    //tempUser.addToCategories(tempCategory)
    }
    def tempSubCategory = Subcategory.findByName(subcategory)
    if(!tempSubCategory){
    tempSubCategory = new Subcategory(name:subcategory)
    tempCategory.addToSubcategories(tempSubCategory)
    }
    //if(tempCategory.subcategories != null && (!tempCategory.subcategories.contains(tempSubCateg ory)) )
    //if(tempUser.categories != null && !tempUser.categories.contains(tempCategory))

    def q=new Directory(code:code,name:name,category:category,su bcategory:subcategory,description:description,pric eDouble.parseDouble(price)),quantityint)Double.parseDouble(quantity),file:"").save()
    tempSubCategory.addToDirectories(q)
    // tempUser.addToDirectories(q)
    tempCategory.save()
    }
    }

    Thanks inadvance,
    Manu

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Forum Design by Tucanoo Solutions Ltd Grails Development Specialists.