Example usage for org.dom4j Element elementIterator

List of usage examples for org.dom4j Element elementIterator

Introduction

In this page you can find the example usage for org.dom4j Element elementIterator.

Prototype

Iterator<Element> elementIterator();

Source Link

Document

Returns an iterator over all this elements child elements.

Usage

From source file:de.xwic.sandbox.server.installer.XmlImport.java

License:Apache License

/**
 * @param descr//from   w  w w .j  a  v a  2 s.  c o m
 * @param elEntity
 * @param entity
 */
private void readEntityProperties(EntityDescriptor descr, Element elEntity, IEntity entity) {

    for (Iterator<?> it = elEntity.elementIterator(); it.hasNext();) {

        Element elProp = (Element) it.next();
        String propertyName = elProp.getName();

        Property property = descr.getProperty(propertyName);
        if (property != null) {

            try {
                loadPropertyValue(entity, property, elProp);
            } catch (Exception e) {
                throw new DataAccessException("Error loading properties: " + e, e);
            }
        }
    }
}

From source file:ec.ControlYahoo.java

itemDataBeans search(String word) {
    //URL?//w  w  w.  jav a2s  .  com
    String sendPalam = "?appid=" + APP_ID + "&query=" + word;
    itemDataBeans listIDB = new itemDataBeans();

    //String sendPalam = "&query=" + word;
    try {
        //XML
        Document doc = null;
        // AhX??Awb_?[??
        URL url = new URL(SEARCH_URL + sendPalam);
        //URL url = new URL(SEARCH_URL + "?appid=" + APP_ID);  
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        con.setRequestMethod("POST"); // POSTGET
        con.setDoOutput(true); // POSTf?[^t
        con.setInstanceFollowRedirects(false);// ?_CNg
        con.setRequestProperty("Accept-Language", "jp");
        con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
        //con.setRequestProperty("User-Agent","Mozilla/5.0");

        OutputStream os = con.getOutputStream();//POSTpOutputStream

        // URLANZX?AXMLhL?g

        //doc = new SAXBuilder().build(con.getInputStream());

        //XML ?ol
        //?i
        //i
        //TlC
        //N
        //===========================================

        InputStream is = con.getInputStream();//POST
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String xml = "";
        String s = "";
        while ((s = reader.readLine()) != null) {
            System.out.println(s);
            xml += s;
        }
        doc = DocumentHelper.parseText(xml);

        String strMsg = "";

        //?[gvf
        Element root = doc.getRootElement();

        //?i?Ei?ETlC
        for (Iterator i = root.elementIterator(); i.hasNext();) {
            Element elem = (Element) i.next();
            for (Iterator ii = elem.elementIterator(); ii.hasNext();) {
                Element elemHit = (Element) ii.next();
                String sHit = elemHit.getName();
                if (elemHit.getName().equals("Hit")) {
                    Element elemTmp = elemHit.element("Name");
                    listIDB.setName(elemTmp.getText());
                    elemTmp.detach();
                    elemTmp = elemHit.element("Price");
                    String price = elemTmp.getText();
                    listIDB.setPrice(Integer.parseInt(price));
                    elemTmp.detach();
                    elemTmp = elemHit.element("Image").element("Medium");
                    listIDB.setImageURL(elemTmp.getText());
                    elemTmp.detach();
                    elemTmp = elemHit.element("ProductId");
                    listIDB.setProductID(elemTmp.getText());
                }
            }

        }

        // DoucmnetW??o??o
        //            BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(System.out));
        //            XMLOutputter out = new XMLOutputter();
        //            out.output(doc, bout);

        //ps.close();
        reader.close();
        con.connect();

    } catch (MalformedURLException e) {
        String str = e.getMessage();
    } catch (Exception e) {
        String str = e.getMessage();

    }

    return listIDB;
}

From source file:edu.ku.brc.dbsupport.ImportExportDB.java

License:Open Source License

/**
     * Imports a table from a given xml file
     * @param dbImport the opend xml file elememt
     * @param dbTable the class name of the table
     *///from   ww w .j  av  a  2 s  .  co  m
    @SuppressWarnings("unchecked")
    protected void dynamicXMLImport(Element dbImport, String dbTable) {
        try {
            String id = new String();
            DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());

            List records = dbImport.selectNodes("//" + dbTable); //$NON-NLS-1$
            String lowerdbTable = lowerFirstChar(dbTable);
            // List attributes = dbImport.selectNodes("//@"+lowerdbTable+"Id");
            List ids = dbImport.selectNodes("//" + lowerdbTable + "Id");// assume this is dbs id //$NON-NLS-1$ //$NON-NLS-2$
                                                                        // name
            if (records.size() < 1) {
                log.debug("Cannot import. Given database type:" + dbTable //$NON-NLS-1$
                        + " does not exsist in import file"); //$NON-NLS-1$
            } else {
                // loop for each record
                for (int k = 0; k < records.size(); k++) {
                    // keep this id to compare against it's collection
                    Element idElement = (Element) ids.get(k);
                    // id = attribute.getText();
                    id = idElement.getStringValue();
                    // make the agent and the element
                    Object agent = parentInfo.getClassObj().newInstance();
                    Map<String, Object> agentMap = new HashMap<String, Object>();
                    Element dbElement = (Element) records.get(k);
                    Iterator i = dbElement.elementIterator();
                    do {// do for each element in the record
                        Element element = (Element) i.next();

                        Object value = findType(element, dbTable, agent, " ");// the parent is //$NON-NLS-1$
                                                                              // itself, just a
                                                                              // dummy variable
                                                                              // if(value!=null && value != "collection")
                        if (value != null && value != "OneToMany" && value != "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                        {
                            agentMap.put(element.getName(), value);
                        }
                        // ignore many-to-many for now
                        else if (value == "OneToMany" || value == "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                        {// RECURSE
                         // is it a collection, add all associated records
                         // get assoicated ids
                         // List collectingevent_ids =
                         // element.selectNodes("//"+dbTable+"[@"+lowerdbTable+"Id='"+id+"']/"+element.getName()+"/"+lowerElement);
                         // TODO shouldl not assume things are in order
                            List collectingevent_ids = element.selectNodes("//" + dbTable + "[" //$NON-NLS-1$ //$NON-NLS-2$
                                    + id + "]/" + element.getName() + "/*");// +upperElement); //$NON-NLS-1$ //$NON-NLS-2$

                            if (!collectingevent_ids.isEmpty()) {
                                // add all the assoctions to aDbElement
                                // get child dbName
                                String childDbName = getDbName(collectingevent_ids);
                                // make a parent object
                                BeanUtils.populate(agent, agentMap);

                                // Collection collection = xmlImportRecursion(locality,
                                // upperElement, collectingevent_ids, parent, lowerdbTable);
                                Set collection = xmlImportRecursion(agentMap, childDbName, collectingevent_ids,
                                        agent, lowerdbTable);
                                if (collection != null) {
                                    agentMap.put(element.getName(), collection);
                                } else {
                                    log.debug("error on the collection " //$NON-NLS-1$
                                            + element.getName() + " with parent " + dbTable); //$NON-NLS-1$
                                }
                            }
                        } else
                        // else, dont add it
                        {
                            // if it is the tables id, just ignore. otherwise print out error
                            if (!element.getName().equals(lowerdbTable + "Id")) //$NON-NLS-1$
                            {
                                log.debug("did not add " + element.getName() //$NON-NLS-1$
                                        + " to the element " + dbTable); //$NON-NLS-1$
                            }
                        }
                    } while (i.hasNext());

                    // populate and save
                    BeanUtils.populate(agent, agentMap);

                    this.session.saveOrUpdate(agent);
                }
            }
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex);
            // the last par tof the string conatins the class
            // if(err.startsWith("org.hibernate.PropertyValueException")){
            try {
                // try again
                // flush
                // do reference work
                // importTable("ReferenceWork");
                // the import aagain
            } catch (Exception ex1) {
                edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
                edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex1);
                ex1.printStackTrace();
            }
            // }else{
            ex.printStackTrace();
            // }
        }
    }

From source file:edu.ku.brc.dbsupport.ImportExportDB.java

License:Open Source License

@SuppressWarnings("unchecked")
    protected void sequentialXMLImport(Element dbImport, String dbTable, String parentName, long parentId) {
        try {//from   w  w  w .ja v a  2 s .  com
            String id = new String();
            DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());

            List records = dbImport.selectNodes("//" + dbTable); //$NON-NLS-1$
            String lowerdbTable = lowerFirstChar(dbTable);
            // TODO: should not assume that is the id name, use getPrimaryKeyName
            DBTableInfo info = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());
            String primaryKey = info.getPrimaryKeyName();
            // List ids = dbImport.selectNodes("//"+lowerdbTable+"Id");//assume this is dbs id name
            List ids = dbImport.selectNodes("//" + primaryKey); //$NON-NLS-1$

            if (records.size() < 1) {
                log.debug("Cannot import. Given database type:" + dbTable //$NON-NLS-1$
                        + " does not exsist in import file"); //$NON-NLS-1$
            } else {
                // loop for each record
                for (int k = 0; k < records.size(); k++) { //
                    Vector collectionIds = new Vector(20);
                    Vector<String> collectionNames = new Vector(20);
                    // keep this id to compare against it's collection
                    Element idElement = (Element) ids.get(k);
                    // id = attribute.getText();
                    id = idElement.getStringValue();
                    // make the agent and the element
                    Object agent = parentInfo.getClassObj().newInstance();
                    Map<String, Object> agentMap = new HashMap<String, Object>();
                    Element dbElement = (Element) records.get(k);
                    Iterator i = dbElement.elementIterator();
                    do {// do for each element in the record
                        Element element = (Element) i.next();

                        Object value = findTypeSequential(element, dbTable, parentId, parentName);// the
                                                                                                  // parent
                                                                                                  // is
                                                                                                  // itself,
                                                                                                  // just
                                                                                                  // a
                                                                                                  // dummy
                                                                                                  // variable
                                                                                                  // if(value!=null && value != "collection")
                        if (value != null && value != "OneToMany" && value != "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                        {
                            agentMap.put(element.getName(), value);

                        }
                        // ignore many-to-many for now
                        else if (value == "OneToMany" || value == "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                        {// RECURSE
                         // get assoicated ids
                         // List temp_collection_ids =
                         // element.selectNodes("//"+dbTable+"["+id+"]/"+element.getName()+"/*");//+upperElement);
                            List temp_collection_ids = element
                                    .selectNodes("//" + dbTable + "[" + primaryKey + " = \"" + id //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                                            + "\"]/" + element.getName() + "/*"); //$NON-NLS-1$ //$NON-NLS-2$
                            // get collection info and still dont add it
                            if (!temp_collection_ids.isEmpty()) {
                                // get child dbName
                                String childDbName = getDbName(temp_collection_ids);
                                collectionNames.addElement(childDbName);
                                for (int index = 0; index < temp_collection_ids.size(); index++) {
                                    collectionIds.addElement(temp_collection_ids.get(index));
                                }
                            }
                        } else
                        // else, dont add it
                        {
                            // if it is an id, just ignore. otherwise print out error
                            if (!element.getName().equals(lowerdbTable + "Id")) //$NON-NLS-1$
                            {
                                log.debug("did not add " + element.getName() //$NON-NLS-1$
                                        + " to the element " + dbTable); //$NON-NLS-1$
                            }
                        }
                    } while (i.hasNext());

                    // populate and save
                    BeanUtils.populate(agent, agentMap);

                    this.session.save(agent);
                    // session.lock(agent, LockMode.NONE);

                    // if there was a collection, then recurse
                    if (!collectionIds.isEmpty()) {
                        long newParentId = new Long(session.getIdentifier(agent).toString()).longValue();

                        sequentialXMLImportRecursion(collectionNames, collectionIds, dbTable, newParentId);
                    }
                }
            }
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex);
            // String err = ex.toString();
            // the last par tof the string conatins the class
            // if(err.startsWith("org.hibernate.PropertyValueException")){
            try {
                // try again
                // flush
                // do reference work
                // importTable("ReferenceWork");
                // the import aagain
            } catch (Exception ex1) {
                edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
                edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex1);
                ex1.printStackTrace();
            }
            // }else{
            ex.printStackTrace();
            // }
        }
    }

From source file:edu.ku.brc.dbsupport.ImportExportDB.java

License:Open Source License

@SuppressWarnings("unchecked")
    protected void sequentialXMLImportRecursion(Vector collectionNames, Vector collectionIds, String parentName,
            long parentId) {
        int idIndex = 0;
        try {//from w  w w . j ava 2s .  co  m
            // for each collection
            for (int z = 0; z < collectionNames.size(); z++) {
                // get table to work on
                String dbTable = collectionNames.get(z).toString();
                String lowerdbTable = lowerFirstChar(dbTable);
                DBTableInfo info = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());
                String primaryKey = info.getPrimaryKeyName();

                // open xml file
                File path = new File(importFolderPath + dbTable + ".xml"); //$NON-NLS-1$
                Element dbImport = XMLHelper.readFileToDOM4J(path);

                DBTableInfo tableInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());

                // loop for each id that we need in this table
                for (int k = 0; k < collectionIds.size() && idIndex < collectionIds.size(); k++) {
                    Vector newCollectionIds = new Vector(20);
                    Vector newCollectionNames = new Vector(20);
                    // the only way to get the value out of collectionIds
                    Element temp_id = (Element) collectionIds.get(idIndex);

                    // is this the right element to work on
                    // if so use else; otherwise stop the loop
                    if (temp_id.getName().equals(dbTable)) {
                        idIndex++;
                        String id = temp_id.getText();
                        // select the node
                        // TODO shouldl not assume things are in order
                        // Element collectingevent =
                        // (Element)dbImport.selectSingleNode("//"+dbTable+"["+id+"]");//temp_id.getText()+"]");
                        Element collectingevent = (Element) dbImport.selectSingleNode("//" //$NON-NLS-1$
                                + dbTable + "[" + primaryKey + " = \"" + id + "\"]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        Iterator iter = collectingevent.elementIterator();

                        // make the element and the agent
                        Map<String, Object> agentMap = new HashMap<String, Object>();
                        Object agent = tableInfo.getClassObj().newInstance();
                        do {
                            Element element = (Element) iter.next();

                            Object value2 = findTypeSequential(element, dbTable, parentId, parentName);
                            if (value2 != null && value2 != "OneToMany" && value2 != "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                            {
                                agentMap.put(element.getName(), value2);
                            } else if (value2 == "ManyToMany") //$NON-NLS-1$
                            {
                                Set<Object> parentSet = new HashSet<Object>();
                                Object parentObject = genericDBObject2(parentName, parentId);
                                parentSet.add(parentObject);
                                agentMap.put(element.getName(), parentSet);
                            } else if (value2 == "OneToMany") //$NON-NLS-1$
                            {
                                // RECURSE
                                // get assoicated ids
                                // TODO shouldl not assume things are in order
                                // List temp_collection_ids =
                                // element.selectNodes("//"+dbTable+"["+id+"]/"+element.getName()+"/*");//+upperElement);
                                List temp_collection_ids = element.selectNodes("//" + dbTable + "[" //$NON-NLS-1$ //$NON-NLS-2$
                                        + primaryKey + " = \"" + id + "\"]/" + element.getName() //$NON-NLS-1$ //$NON-NLS-2$
                                        + "/*"); //$NON-NLS-1$
                                // get collection info and still dont add it
                                if (!temp_collection_ids.isEmpty()) {
                                    // get child dbName
                                    String childDbName = getDbName(temp_collection_ids);
                                    newCollectionNames.addElement(childDbName);
                                    for (int index = 0; index < temp_collection_ids.size(); index++) {
                                        newCollectionIds.addElement(temp_collection_ids.get(index));
                                    }
                                }

                            } else {
                                // if it is an id, just ignore. otherwise print out error
                                if (!element.getName().equals(lowerdbTable + "Id")) //$NON-NLS-1$
                                {
                                    log.debug("did not add " + element.getName() //$NON-NLS-1$
                                            + " to the element " + dbTable); //$NON-NLS-1$
                                }
                            }
                        } while (iter.hasNext());

                        // add to the set
                        BeanUtils.populate(agent, agentMap);

                        this.session.saveOrUpdate(agent);
                        // if there was a collection, then recurse
                        if (!newCollectionIds.isEmpty()) {
                            long newParentId = new Long(session.getIdentifier(agent).toString()).longValue();

                            sequentialXMLImportRecursion(newCollectionNames, newCollectionIds, dbTable,
                                    newParentId);
                        }
                    } else {// stop the loop
                        k = collectionIds.size();
                    }
                }
            }
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex);
            // the last par tof the string conatins the class
            // if(err.startsWith("org.hibernate.PropertyValueException")){
            try {
                // try again
                // flush
                // do reference work
                // importTable("ReferenceWork");
                // the import aagain
            } catch (Exception ex1) {
                edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
                edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex1);
                ex1.printStackTrace();
            }
            // }else{
            ex.printStackTrace();
            // }
        }
    }

From source file:edu.ku.brc.dbsupport.ImportExportDB.java

License:Open Source License

@SuppressWarnings("unchecked")
    protected void singleXMLImport(Element dbImport, String dbTable, String parentName, long parentId, int id,
            boolean recursion) {
        try {//from   w  ww  . j  av a 2 s .  c  om
            DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());
            String lowerdbTable = lowerFirstChar(dbTable);
            String primaryKey = parentInfo.getPrimaryKeyName();

            Vector collectionIds = new Vector(20);
            Vector collectionNames = new Vector(20);
            // make the agent and the element
            Object agent = parentInfo.getClassObj().newInstance();
            Map<String, Object> agentMap = new HashMap<String, Object>();
            Element dbElement = dbImport;
            Iterator i = dbElement.elementIterator();
            do {// do for each element in the record
                Element element = (Element) i.next();

                Object value = findTypeSequential(element, dbTable, parentId, parentName);// the
                                                                                          // parent
                                                                                          // is
                                                                                          // itself,
                                                                                          // just
                                                                                          // a
                                                                                          // dummy
                                                                                          // variable
                                                                                          // if(value!=null && value != "collection")
                if (value != null && value != "OneToMany" && value != "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                {
                    agentMap.put(element.getName(), value);
                }
                // ignore many-to-many for now
                else if (recursion && (value == "OneToMany" || value == "ManyToMany")) //$NON-NLS-1$ //$NON-NLS-2$
                {// RECURSE
                 // get assoicated ids
                    List temp_collection_ids = element.selectNodes("//" + dbTable + "[" //$NON-NLS-1$ //$NON-NLS-2$
                            + primaryKey + " = \"" + id + "\"]/" + element.getName() + "/*"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    // get collection info and still dont add it
                    if (!temp_collection_ids.isEmpty()) {
                        // get child dbName
                        String childDbName = getDbName(temp_collection_ids);
                        collectionNames.addElement(childDbName);
                        for (int index = 0; index < temp_collection_ids.size(); index++) {
                            collectionIds.addElement(temp_collection_ids.get(index));
                        }
                    }
                } else
                // else, dont add it
                {
                    // if it is an id, just ignore. otherwise print out error
                    if (!element.getName().equals(lowerdbTable + "Id")) //$NON-NLS-1$
                    {
                        log.debug("did not add " + element.getName() + " to the element " //$NON-NLS-1$ //$NON-NLS-2$
                                + dbTable);
                    }
                }
            } while (i.hasNext());

            // populate and save
            BeanUtils.populate(agent, agentMap);

            this.session.save(agent);

            // if there was a collection, then recurse
            if (!collectionIds.isEmpty()) {
                long newParentId = new Long(session.getIdentifier(agent).toString()).longValue();

                sequentialXMLImportRecursion(collectionNames, collectionIds, dbTable, newParentId);
            }
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex);
            ex.printStackTrace();
        }
    }

From source file:edu.ku.brc.dbsupport.ImportExportDB.java

License:Open Source License

@SuppressWarnings("unchecked")
    protected Object buildSingleDBObjectFromXML(Element dbImport, String dbTable, String parentName, long parentId,
            int id, boolean recursion) {
        Object dbObject = new Object();
        try {/*from  w w  w  . jav a  2s .  co  m*/
            DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());
            String primaryKey = parentInfo.getPrimaryKeyName();

            Vector collectionIds = new Vector(20);
            Vector collectionNames = new Vector(20);
            // make the agent and the element
            Object agent = parentInfo.getClassObj().newInstance();
            Map<String, Object> agentMap = new HashMap<String, Object>();
            Element dbElement = dbImport;
            Iterator i = dbElement.elementIterator();
            do {// do for each element in the record
                Element element = (Element) i.next();

                // Object value = findTypeSequential(element, dbTable, parentId, parentName );//the
                // parent is itself, just a dummy variable
                Object value = findTypeRecordSet(element, dbTable, parentId, parentName);// the
                                                                                         // parent
                                                                                         // is
                                                                                         // itself,
                                                                                         // just
                                                                                         // a
                                                                                         // dummy
                                                                                         // variable
                if (value != null && value != "OneToMany" && value != "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                {
                    agentMap.put(element.getName(), value);
                }
                // ignore many-to-many for now
                else if (value == "OneToMany" || value == "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                {// RECURSE
                    if (recursion) {
                        // get assoicated ids
                        List temp_collection_ids = element.selectNodes("//" + dbTable + "[" //$NON-NLS-1$ //$NON-NLS-2$
                                + primaryKey + " = \"" + id + "\"]/" + element.getName() + "/*"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        // get collection info and still dont add it
                        if (!temp_collection_ids.isEmpty()) {
                            // get child dbName
                            String childDbName = getDbName(temp_collection_ids);
                            collectionNames.addElement(childDbName);
                            for (int index = 0; index < temp_collection_ids.size(); index++) {
                                collectionIds.addElement(temp_collection_ids.get(index));
                            }
                        }
                    }
                } else
                // else, dont add it
                {
                    // if it is an id, just ignore. otherwise print out error
                    if (!element.getName().equals(primaryKey)) {
                        log.debug("did not add " + element.getName() + " to the element " //$NON-NLS-1$ //$NON-NLS-2$
                                + dbTable);
                    }
                }
            } while (i.hasNext());

            // populate
            BeanUtils.populate(agent, agentMap);
            // save it then gets is id (assigned by Hibernate)
            this.session.save(agent);

            // if there was a collection, then recurse
            if (!collectionIds.isEmpty()) {
                long newParentId = new Long(session.getIdentifier(agent).toString()).longValue();

                sequentialXMLImportRecursion(collectionNames, collectionIds, dbTable, newParentId);
            }
            dbObject = agent;
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex);
            ex.printStackTrace();
        }

        return dbObject;
    }

From source file:edu.ku.brc.dbsupport.ImportExportDB.java

License:Open Source License

@SuppressWarnings("unchecked")
    protected Object buildSingleDataBaseObjectFromXML(Element dbImport, String dbTable, String parentName,
            long parentId, int id, boolean recursion) {
        Object dbObject = new Object();
        try {//from ww  w.j  a v a2 s .c  o m
            DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());
            //String lowerdbTable = lowerFirstChar(dbTable);
            String primaryKey = parentInfo.getPrimaryKeyName();

            Vector collectionIds = new Vector(20);
            Vector collectionNames = new Vector(20);
            // make the agent and the element
            Object agent = parentInfo.getClassObj().newInstance();
            Map<String, Object> agentMap = new HashMap<String, Object>();
            Element dbElement = dbImport;
            Iterator i = dbElement.elementIterator();
            do {// do for each element in the record
                Element element = (Element) i.next();

                // Object value = findTypeSequential(element, dbTable, parentId, parentName );//the
                // parent is itself, just a dummy variable
                Object value = findTypeDataBaseParent(element, dbTable, parentId, parentName);// the
                                                                                              // parent
                                                                                              // is
                                                                                              // itself,
                                                                                              // just
                                                                                              // a
                                                                                              // dummy
                                                                                              // variable
                if (value != null && value != "OneToMany" && value != "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                {
                    agentMap.put(element.getName(), value);
                }
                // ignore many-to-many for now
                else if (value == "OneToMany" || value == "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                {// RECURSE
                    if (recursion) {
                        // get assoicated ids
                        List temp_collection_ids = element.selectNodes("//" + dbTable + "[" //$NON-NLS-1$ //$NON-NLS-2$
                                + primaryKey + " = \"" + id + "\"]/" + element.getName() + "/*"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        // get collection info and still dont add it
                        if (!temp_collection_ids.isEmpty()) {
                            // get child dbName
                            String childDbName = getDbName(temp_collection_ids);
                            collectionNames.addElement(childDbName);
                            for (int index = 0; index < temp_collection_ids.size(); index++) {
                                collectionIds.addElement(temp_collection_ids.get(index));
                            }
                        }
                    }
                } else
                // else, dont add it
                {
                    // if it is an id, just ignore. otherwise print out error
                    if (!element.getName().equals(primaryKey)) {
                        log.debug("did not add " + element.getName() + " to the element " //$NON-NLS-1$ //$NON-NLS-2$
                                + dbTable);
                    }
                }
            } while (i.hasNext());

            // populate
            BeanUtils.populate(agent, agentMap);
            // save it then gets its id (assigned by Hibernate)
            this.session.save(agent);

            // if there was a collection, then recurse
            if (!collectionIds.isEmpty()) {
                long newParentId = new Long(session.getIdentifier(agent).toString()).longValue();

                sequentialXMLImportRecursion(collectionNames, collectionIds, dbTable, newParentId);
            }
            dbObject = agent;
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex);
            ex.printStackTrace();
        }

        return dbObject;
    }

From source file:edu.ku.brc.dbsupport.ImportExportDB.java

License:Open Source License

@SuppressWarnings("unchecked")
    protected Map buildSingleDataBaseObjectFromXMLTemp(Element dbImport, String dbTable, String parentName,
            long parentId, int id, boolean recursion) {
        //Object dbObject = new Object();
        Map<String, Object> agentMap = new HashMap<String, Object>();
        try {/* w  w  w.  j av a2s  .  c o m*/
            DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());
            //String lowerdbTable = lowerFirstChar(dbTable);
            String primaryKey = parentInfo.getPrimaryKeyName();

            Vector collectionIds = new Vector(20);
            Vector collectionNames = new Vector(20);
            // make the agent and the element
            Object agent = parentInfo.getClassObj().newInstance();

            Element dbElement = dbImport;
            Iterator i = dbElement.elementIterator();
            do {// do for each element in the record
                Element element = (Element) i.next();

                // Object value = findTypeSequential(element, dbTable, parentId, parentName );//the
                // parent is itself, just a dummy variable
                Object value = findTypeDataBaseParent(element, dbTable, parentId, parentName);// the
                                                                                              // parent
                                                                                              // is
                                                                                              // itself,
                                                                                              // just
                                                                                              // a
                                                                                              // dummy
                                                                                              // variable
                if (value != null && value != "OneToMany" && value != "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                {
                    agentMap.put(element.getName(), value);
                }
                // ignore many-to-many for now
                else if (value == "OneToMany" || value == "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                {// RECURSE
                    if (recursion) {
                        // get assoicated ids
                        List temp_collection_ids = element.selectNodes("//" + dbTable + "[" //$NON-NLS-1$ //$NON-NLS-2$
                                + primaryKey + " = \"" + id + "\"]/" + element.getName() + "/*"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                        // get collection info and still dont add it
                        if (!temp_collection_ids.isEmpty()) {
                            // get child dbName
                            String childDbName = getDbName(temp_collection_ids);
                            collectionNames.addElement(childDbName);
                            for (int index = 0; index < temp_collection_ids.size(); index++) {
                                collectionIds.addElement(temp_collection_ids.get(index));
                            }
                        }
                    }
                } else
                // else, dont add it
                {
                    // if it is an id, just ignore. otherwise print out error
                    if (!element.getName().equals(primaryKey)) {
                        log.debug("did not add " + element.getName() + " to the element " //$NON-NLS-1$ //$NON-NLS-2$
                                + dbTable);
                    }
                }
            } while (i.hasNext());

            // populate
            // BeanUtils.populate(agent, agentMap);
            // save it then gets its id (assigned by Hibernate)
            // this.session.save(agent);

            // if there was a collection, then recurse
            if (!collectionIds.isEmpty()) {
                long newParentId = new Long(session.getIdentifier(agent).toString()).longValue();

                sequentialXMLImportRecursion(collectionNames, collectionIds, dbTable, newParentId);
            }
            //dbObject = agent;
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex);
            ex.printStackTrace();
        }

        return agentMap;
    }

From source file:edu.ku.brc.dbsupport.ImportExportDB.java

License:Open Source License

@SuppressWarnings("unchecked")
    protected void sequentialXMLImportRecordSet(Element dbImport, String dbTable, String parentName,
            long parentId) {
        try {/*from   ww  w. jav  a 2s. c  om*/
            String id = new String();
            DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());
            // get the records
            List records = dbImport.selectNodes("//" + dbTable); //$NON-NLS-1$
            //String lowerdbTable = lowerFirstChar(dbTable);
            DBTableInfo info = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());
            String primaryKey = info.getPrimaryKeyName();
            List ids = dbImport.selectNodes("//" + primaryKey); //$NON-NLS-1$

            if (records.size() < 1) {
                log.debug("Cannot import. Given database type:" + dbTable //$NON-NLS-1$
                        + " does not exsist in import file"); //$NON-NLS-1$
            } else {
                // loop for each record
                for (int k = 0; k < records.size(); k++) {
                    Vector collectionIds = new Vector(20);
                    Vector collectionNames = new Vector(20);
                    // keep this id to compare against it's collection
                    Element idElement = (Element) ids.get(k);
                    id = idElement.getStringValue();
                    // make the agent and the element
                    Object agent = parentInfo.getClassObj().newInstance();
                    Map<String, Object> agentMap = new HashMap<String, Object>();
                    Element dbElement = (Element) records.get(k);
                    Iterator i = dbElement.elementIterator();
                    do {// do for each element in the record
                        Element element = (Element) i.next();

                        Object value = findTypeRecordSet(element, dbTable, parentId, parentName);// the
                                                                                                 // parent
                                                                                                 // is
                                                                                                 // itself,
                                                                                                 // just
                                                                                                 // a
                                                                                                 // dummy
                                                                                                 // variable
                        if (value != null && value != "OneToMany" && value != "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                        {
                            agentMap.put(element.getName(), value);
                        }
                        // ignore many-to-many for now
                        else if (value == "OneToMany" || value == "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                        {// RECURSE
                         // get assoicated ids
                            List temp_collection_ids = element
                                    .selectNodes("//" + dbTable + "[" + primaryKey + " = \"" + id //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                                            + "\"]/" + element.getName() + "/*"); //$NON-NLS-1$ //$NON-NLS-2$
                            // get collection info and still dont add it
                            if (!temp_collection_ids.isEmpty()) {
                                // get child dbName
                                String childDbName = getDbName(temp_collection_ids);
                                collectionNames.addElement(childDbName);
                                for (int index = 0; index < temp_collection_ids.size(); index++) {
                                    collectionIds.addElement(temp_collection_ids.get(index));
                                }
                            }
                        } else
                        // else, dont add it
                        {
                            // if it is an id, just ignore. otherwise print out error
                            if (!element.getName().equals(primaryKey)) {
                                log.debug("did not add " + element.getName() //$NON-NLS-1$
                                        + " to the element " + dbTable); //$NON-NLS-1$
                            }
                        }
                    } while (i.hasNext());

                    // populate and save
                    BeanUtils.populate(agent, agentMap);

                    this.session.save(agent);

                    // if there was a collection, then recurse
                    if (!collectionIds.isEmpty()) {
                        long newParentId = new Long(session.getIdentifier(agent).toString()).longValue();
                        // import all children
                        sequentialXMLImportRecursion(collectionNames, collectionIds, dbTable, newParentId);
                    }
                }
            }
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex);
            ex.printStackTrace();
        }
    }