Example usage for org.apache.commons.beanutils BeanUtils populate

List of usage examples for org.apache.commons.beanutils BeanUtils populate

Introduction

In this page you can find the example usage for org.apache.commons.beanutils BeanUtils populate.

Prototype

public static void populate(Object bean, Map properties) 

Source Link

Usage

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

@SuppressWarnings("unchecked")
    protected void sequentialXMLImportRecursion(Vector collectionNames, Vector collectionIds, String parentName,
            long parentId) {
        int idIndex = 0;
        try {/*  w  w  w . j  a v a  2s.c o 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:com.mirth.connect.server.controllers.MuleEngineController.java

private UMOConnector registerConnector(Connector connector, String name, String channelId) throws Exception {
    logger.debug("registering connector: " + name);
    // get the transport associated with this class from the transport
    // map//  w w  w .j  a  v  a  2  s.  c om
    ConnectorMetaData transport = transports.get(connector.getTransportName());
    UMOConnector umoConnector = (UMOConnector) Class.forName(transport.getServerClassName()).newInstance();
    umoConnector.setName(name);

    // exception-strategy
    umoConnector.setExceptionListener(new ExceptionStrategy());

    // The connector needs it's channel id (so it doesn't have to parse
    // the name) for alerts
    Map<Object, Object> beanProperties = new HashMap<Object, Object>();
    Map<Object, Object> queriesMap = new HashMap<Object, Object>();
    beanProperties.put("channelId", channelId);

    for (Entry<Object, Object> property : connector.getProperties().entrySet()) {
        if ((property.getValue() != null) && !property.getValue().equals("")
                && !nonConnectorProperties.contains(property.getKey())) {
            if (keysOfValuesThatAreBeans.contains(property.getKey())) {
                beanProperties.put(property.getKey(), objectSerializer.fromXML(property.getValue().toString()));
            } else if (property.getKey().equals("script") || property.getKey().equals("ackScript")) {
                String databaseScriptId = UUIDGenerator.getUUID();
                scriptController.putScript(channelId, databaseScriptId, property.getValue().toString());
                beanProperties.put(property.getKey() + "Id", databaseScriptId);
            } else if (property.getKey().equals("query") || property.getKey().equals("statement")
                    || property.getKey().equals("ack")) {
                queriesMap.put(property.getKey(), property.getValue());
            } else {
                beanProperties.put(property.getKey(), property.getValue());
            }
        }
    }

    // populate the bean properties
    beanProperties.put("queries", queriesMap);

    // inboundProtocol and protocolProperties are only used in the
    // file reader connector when processing batch messages.
    beanProperties.put("inboundProtocol", connector.getTransformer().getInboundProtocol().toString());

    if (connector.getMode().equals(Connector.Mode.SOURCE)) {
        Map protocolProperties = connector.getTransformer().getInboundProperties();

        if (MapUtils.isEmpty(protocolProperties)) {
            protocolProperties = DefaultSerializerPropertiesFactory
                    .getDefaultSerializerProperties(connector.getTransformer().getInboundProtocol());
        }

        beanProperties.put("protocolProperties", protocolProperties);
    }

    BeanUtils.populate(umoConnector, beanProperties);

    // add the connector to the manager
    muleManager.registerConnector(umoConnector);

    // register the connector service for the connector
    jmxAgent.registerConnectorService(umoConnector);

    return umoConnector;
}

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

@SuppressWarnings("unchecked")
    protected void singleXMLImport(Element dbImport, String dbTable, String parentName, long parentId, int id,
            boolean recursion) {
        try {//from  ww  w  . jav a 2s  .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

@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  a2s  .c o  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

@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  va  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
                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

@SuppressWarnings("unchecked")
    protected void sequentialXMLImportRecordSet(Element dbImport, String dbTable, String parentName,
            long parentId) {
        try {/*from ww  w  .  j  av a2s  .  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();
        }
    }

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

@SuppressWarnings("unchecked")
    protected void iterativeXMLImport(Element dbImport, String dbTable, String parentName, long parentId) {
        try {/* w ww.  j  a va  2  s.  co m*/
            DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());

            List records = dbImport.selectNodes("//" + dbTable); //$NON-NLS-1$
            String lowerdbTable = lowerFirstChar(dbTable);
            // List ids = dbImport.selectNodes("//"+lowerdbTable+"Id");//assume this is dbs id 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);
                    // 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);
                        } 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);
                }
            }
        } 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

/**
     * Imports a table from a given xml file
     * @param dbImport the opend xml file elememt
     * @param dbTable the class name of the table
     *///from w  w w .  j  av a  2s.  c  o m
    @SuppressWarnings("unchecked")
    protected Object dynamicXMLImportRecReturn(Element dbImport, String dbTable) {
        Object agent = new Object();
        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");
            // TODO shouldl not assume this is the dbprimary key, use dbtablemgr
            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
                    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 != "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
                         // 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 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.saveOrUpdate(agent);

                }
            }

        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex);
            ex.printStackTrace();
        }
        return agent;
    }

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

/**
     * recurses over many-to-many or one-to-many record sets
     * @param parentMap the map of the parent record
     * @param dbTable the class name of the table
     * @param ids the list of associated ids
     * @param parentObject the parent record
     * @param parentName the parentName//from   www  .  j a  v  a 2s  .c  om
     * @return a set, or null if not found
     */
    @SuppressWarnings("unchecked")
    protected Set xmlImportRecursion(Map parentMap, String dbTable, List ids, Object parentObject,
            String parentName) {
        try {
            HashSet<Object> set = new HashSet<Object>();
            String lowerdbTable = lowerFirstChar(dbTable);
            DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());

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

            // add each collectingevent to locality
            for (int j = 0; j < ids.size(); j++) {
                // the only way to get the value out of collectingevent_ids
                Element temp_id = (Element) ids.get(j);
                String id2 = temp_id.getText();
                // select the node
                Element collectingevent = (Element) dbImport.selectSingleNode("//" + dbTable + "[" //$NON-NLS-1$ //$NON-NLS-2$
                        + id2 + "]");// temp_id.getText()+"]"); //$NON-NLS-1$
                Iterator iter = collectingevent.elementIterator();

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

                    Object value2 = findType(secondelement, dbTable, parentObject, parentName);
                    if (value2 != null && value2 != "OneToMany" && value2 != "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$
                    {
                        collectingEventMap.put(secondelement.getName(), value2);
                    } else if (value2 == "ManyToMany") //$NON-NLS-1$
                    {
                        HashSet<Object> parentSet = new HashSet<Object>();
                        parentSet.add(parentObject);
                        collectingEventMap.put(secondelement.getName(), parentSet);
                    } else if (value2 == "OneToMany") //$NON-NLS-1$
                    {
                        // RECURSE
                        // is it a collection, add all associated records
                        // get assoicated ids
                        List associated_ids = secondelement.selectNodes("//" + dbTable + "[" + id2 //$NON-NLS-1$ //$NON-NLS-2$
                                + "]/" + secondelement.getName() + "/*");// +upperElement); //$NON-NLS-1$ //$NON-NLS-2$
                        if (!associated_ids.isEmpty()) {
                            // add all the assoctions to aDbElement
                            // get child database name
                            String childDbName = getDbName(associated_ids);
                            // make a parent object
                            BeanUtils.populate(agent, collectingEventMap);

                            Set collection = xmlImportRecursion(collectingEventMap, childDbName, associated_ids,
                                    agent, lowerdbTable);

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

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

            // return the set;
            return set;
        } catch (Exception ex) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex);
            ex.printStackTrace();
            return null;
        }

    }

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

@SuppressWarnings("unchecked")
    protected void sequentialDatabaseImport(Element dbImport, String dbTable, String parentName, long parentId,
            boolean recursion) {
        try {//from   w w w.ja  va  2  s  .co  m
            String id = new String();
            DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase());
            // get the records
            List records = dbImport.selectNodes("//" + dbTable); //$NON-NLS-1$
            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();
                    HashMap<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
                        Object value = findTypeDataBase(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-1$
                                // 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();
        }
    }