Example usage for javax.transaction UserTransaction begin

List of usage examples for javax.transaction UserTransaction begin

Introduction

In this page you can find the example usage for javax.transaction UserTransaction begin.

Prototype

void begin() throws NotSupportedException, SystemException;

Source Link

Document

Create a new transaction and associate it with the current thread.

Usage

From source file:org.ms123.common.ea.BaseEAServiceImpl.java

private Map importActivities(String storeId, String basedir) throws Exception {
    String json = readFileToString(new File(basedir, "idmap.map"));
    Map<String, String> idmap = (Map) m_ds.deserialize(json);
    Calendar high_cal = Calendar.getInstance();
    high_cal.set(Calendar.YEAR, 2050);
    high_cal.set(Calendar.MONTH, 11);
    high_cal.set(Calendar.DAY_OF_MONTH, 31);

    Calendar low_cal = Calendar.getInstance();
    low_cal.set(Calendar.YEAR, 2012);
    low_cal.set(Calendar.MONTH, 11);
    low_cal.set(Calendar.DAY_OF_MONTH, 12);
    StoreDesc sdesc = StoreDesc.get(storeId);
    PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(sdesc).getPersistenceManager();
    UserTransaction ut = m_nucleusService.getUserTransaction();
    Map mapping = initActivities();
    try {//from ww  w  .  ja v  a 2 s  .  c  o m
        LabeledCSVParser lp = new LabeledCSVParser(
                new ExcelCSVParser(new FileInputStream(new File(basedir, "Kontakte.csv"))));
        System.out.println("Persisting activities");
        int num = 0;
        Class _contact = m_nucleusService.getClass(sdesc, "Contact");
        Class _teamIntern = m_nucleusService.getClass(sdesc, "Teamintern");
        while (lp.getLine() != null) {
            String nummer = lp.getValueByLabel("Nummer");
            String merkmal = lp.getValueByLabel("Merkmal");
            Object c = getObject(pm, _contact, nummer);
            if (c == null) {
                continue;
            }
            if (ut.getStatus() != Status.STATUS_ACTIVE) {
                ut.begin();
            }
            String teamid = idmap.get(merkmal);
            Object activity = m_nucleusService.getClass(sdesc, "Activity").newInstance();
            if (teamid != null) {
                Object team = m_nucleusService.getClass(sdesc, "Team").newInstance();
                Object teamintern = getTeamintern(pm, _teamIntern, teamid);
                PropertyUtils.setProperty(team, "teamintern", teamintern);
                PropertyUtils.setProperty(team, "teamid", teamid);
                PropertyUtils.setProperty(team, "description",
                        PropertyUtils.getProperty(teamintern, "description"));
                PropertyUtils.setProperty(team, "validFrom", low_cal.getTime());
                PropertyUtils.setProperty(team, "validTo", high_cal.getTime());
                PropertyUtils.setProperty(team, "disabled", false);
                Collection l = (Collection) PropertyUtils.getProperty(activity, "_team_list");
                if (l == null) {
                    l = new HashSet();
                    PropertyUtils.setProperty(activity, "_team_list", l);
                }
                l.add(team);
            }
            Iterator it = mapping.keySet().iterator();
            while (it.hasNext()) {
                String key = (String) it.next();
                if (key.equals("traits")) {
                    continue;
                }
                String[] m1 = (String[]) mapping.get(key);
                String field = m1[0];
                String type = m1[1];
                String val = lp.getValueByLabel(key).trim();
                if (type.equals("date")) {
                    Date d = getDate(val);
                    if (d != null) {
                        BeanUtils.setProperty(activity, field, d);
                    }
                } else if (type.equals("boolean")) {
                    Boolean b = false;
                    if ("J".equals(val)) {
                        b = true;
                    }
                    BeanUtils.setProperty(activity, field, b);
                } else {
                    if (val != null) {
                        BeanUtils.setProperty(activity, field, val);
                    }
                }
            }
            Collection l = (Collection) PropertyUtils.getProperty(c, "activity_list");
            l.add(activity);
            PropertyUtils.setProperty(activity, "contact", c);
            pm.makePersistent(activity);
            //LuceneSession luceneSession = m_luceneService.createSession(sdesc);
            //m_luceneService.addToIndex(luceneSession, activity);
            //m_luceneService.commit(luceneSession);
            if ((num % 1000) == 1) {
                System.out.println(num + ":\t" + new Date().getTime());
                ut.commit();
            }
            num++;
        }
        if (ut.getStatus() == Status.STATUS_ACTIVE) {
            ut.commit();
        }
        System.out.println("Contact and Book have been persisted");
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        pm.close();
    }
    return null;
}

From source file:org.ms123.common.ea.BaseEAServiceImpl.java

private Map importTeams(String storeId, String basedir) throws Exception {
    Calendar high_cal = Calendar.getInstance();
    high_cal.set(Calendar.YEAR, 2050);
    high_cal.set(Calendar.MONTH, 11);
    high_cal.set(Calendar.DAY_OF_MONTH, 31);

    Calendar low_cal = Calendar.getInstance();
    low_cal.set(Calendar.YEAR, 2012);
    low_cal.set(Calendar.MONTH, 11);
    low_cal.set(Calendar.DAY_OF_MONTH, 12);

    String json = readFileToString(new File(basedir, "idmap.map"));
    Map<String, String> idmap = (Map) m_ds.deserialize(json);
    StoreDesc sdesc = StoreDesc.get(storeId);
    PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(sdesc).getPersistenceManager();
    UserTransaction ut = m_nucleusService.getUserTransaction();
    try {/*w  w w .j  a v  a 2 s . c o  m*/
        LabeledCSVParser lp = new LabeledCSVParser(
                new ExcelCSVParser(new FileInputStream(new File(basedir, "Merkmale.csv"))));
        System.out.println("Persisting teams");
        int num = 0;
        Class _contact = m_nucleusService.getClass(sdesc, "Contact");
        Class _company = m_nucleusService.getClass(sdesc, "Company");
        Class _teamIntern = m_nucleusService.getClass(sdesc, "Teamintern");
        while (lp.getLine() != null) {
            String nummer = lp.getValueByLabel("Nummer");
            String merkmal = lp.getValueByLabel("Merkmal");
            String beginn = lp.getValueByLabel("Beginn");
            String ende = lp.getValueByLabel("Ende");
            String status = lp.getValueByLabel("Status");
            String teamid = idmap.get(merkmal);
            if (teamid == null) {
                System.out.println("Teamid not found:" + merkmal);
                continue;
            }
            Object c = getObject(pm, _contact, _company, nummer);
            if (c == null) {
                System.out.println("No contact/company:" + nummer);
                continue;
            }
            if (ut.getStatus() != Status.STATUS_ACTIVE) {
                ut.begin();
            }
            Object team = m_nucleusService.getClass(sdesc, "Team").newInstance();
            Object teamintern = getTeamintern(pm, _teamIntern, teamid);
            PropertyUtils.setProperty(team, "teamintern", teamintern);
            PropertyUtils.setProperty(team, "teamid", teamid);
            PropertyUtils.setProperty(team, "description",
                    PropertyUtils.getProperty(teamintern, "description"));

            Boolean active = isActive(status);
            Date validFrom = getTeamDate(beginn);
            Date validTo = getTeamDate(ende);
            if (active != null && validFrom != null && validTo != null) {
                PropertyUtils.setProperty(team, "validFrom", validFrom);
                PropertyUtils.setProperty(team, "validTo", validTo);
                PropertyUtils.setProperty(team, "disabled", !active);
            }
            if (active == null && validFrom == null && validTo == null) {
                PropertyUtils.setProperty(team, "validFrom", low_cal.getTime());
                PropertyUtils.setProperty(team, "validTo", high_cal.getTime());
                PropertyUtils.setProperty(team, "disabled", false);
            }
            if (active != null && validFrom != null && validTo == null) {
                PropertyUtils.setProperty(team, "validFrom", validFrom);
                PropertyUtils.setProperty(team, "validTo", high_cal.getTime());
                PropertyUtils.setProperty(team, "disabled", !active);
            }
            PropertyUtils.setProperty(team, "property1", lp.getValueByLabel("Nutzer"));
            PropertyUtils.setProperty(team, "property2", lp.getValueByLabel("Passwort"));

            Collection l = (Collection) PropertyUtils.getProperty(c, "_team_list");
            if (l == null) {
                l = new HashSet();
                PropertyUtils.setProperty(c, "_team_list", l);
            }
            l.add(team);
            pm.makePersistent(team);
            if ((num % 1000) == 1) {
                System.out.println(num + ":\t" + new Date().getTime());
                ut.commit();
            }
            num++;
        }
        if (ut.getStatus() == Status.STATUS_ACTIVE) {
            ut.commit();
        }
        System.out.println("Contact and Book have been persisted");
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        pm.close();
    }
    return null;
}

From source file:org.ms123.common.ea.BaseEAServiceImpl.java

private Map importCommunications(String storeId, String basedir) {
    StoreDesc sdesc = StoreDesc.get(storeId);
    PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(sdesc).getPersistenceManager();
    UserTransaction ut = m_nucleusService.getUserTransaction();
    Map mapping = initCommunication();
    try {/*from  w ww.  j  a  va 2 s.c  o m*/
        LabeledCSVParser lp = new LabeledCSVParser(
                new ExcelCSVParser(new FileInputStream(new File(basedir, "Kommunikation.csv"))));
        System.out.println("Persisting communication");
        int num = 0;
        Class _contact = m_nucleusService.getClass(sdesc, "Contact");
        Class _company = m_nucleusService.getClass(sdesc, "Company");
        while (lp.getLine() != null) {
            String nummer = lp.getValueByLabel("Nummer");
            Object c = getObject(pm, _contact, nummer);
            if (c == null) {
                c = getObject(pm, _company, nummer);
                if (c == null) {
                    continue;
                }
            }
            if (ut.getStatus() != Status.STATUS_ACTIVE) {
                ut.begin();
            }
            String typ = lp.getValueByLabel("Typ");
            typ = typ.toLowerCase();
            String adresse = lp.getValueByLabel("Adresse");
            Object comm = PropertyUtils.getProperty(c, "communication");
            if (comm == null) {
                comm = m_nucleusService.getClass(sdesc, "Communication").newInstance();
                PropertyUtils.setProperty(c, "communication", comm);
                pm.makePersistent(comm);
            }
            String[] m1 = (String[]) mapping.get(typ);
            if (m1 == null) {
                System.out.println("typ(" + typ + "): not found");
                continue;
            }
            String field = m1[0];
            BeanUtils.setProperty(comm, field, adresse);
            pm.makePersistent(comm);
            if ((num % 1000) == 1) {
                System.out.println(num + ":\t" + new Date().getTime());
                ut.commit();
            }
            num++;
        }
        if (ut.getStatus() == Status.STATUS_ACTIVE) {
            ut.commit();
        }
        System.out.println("Communication have been persisted");
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        pm.close();
    }
    return null;
}

From source file:org.ms123.common.ea.BaseEAServiceImpl.java

private Map importZipcodes(String storeId, String basedir) {
    StoreDesc sdesc = StoreDesc.get(storeId);
    PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(sdesc).getPersistenceManager();
    UserTransaction ut = m_nucleusService.getUserTransaction();
    Class _company = m_nucleusService.getClass(sdesc, "Company");
    Class _contact = m_nucleusService.getClass(sdesc, "Contact");
    Map mapping = initZipcodes();
    try {// w  w  w.  j a  v  a  2  s .c om
        LabeledCSVParser lp = new LabeledCSVParser(
                getCSVParser(new FileInputStream(new File(basedir, "zipcodes.csv"))));
        System.out.println("Persisting zipcodes");
        int num = 0;
        while (lp.getLine() != null) {
            if (ut.getStatus() != Status.STATUS_ACTIVE) {
                ut.begin();
            }
            // Zipcode zipcode = new Zipcode(); 
            Object zipcode = m_nucleusService.getClass(sdesc, "Zipcode").newInstance();

            String gemeindekennziffer = lp.getValueByLabel("Gemeindekennziffer");
            String ortname = lp.getValueByLabel("ORTNAME");
            String plz = lp.getValueByLabel("PLZ");
            String lkz = lp.getValueByLabel("LKZ");
            PropertyUtils.setProperty(zipcode, "lkz", lkz);
            PropertyUtils.setProperty(zipcode, "plz", plz);
            PropertyUtils.setProperty(zipcode, "ortname", ortname);
            PropertyUtils.setProperty(zipcode, "gemeindekennziffer", gemeindekennziffer);

            Collection cl = getContactList(pm, _company, plz);
            if (cl != null) {
                for (Object cx : cl) {
                    PropertyUtils.setProperty(cx, "zipcode", zipcode);
                    PropertyUtils.setProperty(cx, "lkz", lkz);
                }
            }
            cl = getContactList(pm, _contact, plz);
            if (cl != null) {
                for (Object cx : cl) {
                    PropertyUtils.setProperty(cx, "zipcode", zipcode);
                    PropertyUtils.setProperty(cx, "lkz", lkz);
                }
            }
            pm.makePersistent(zipcode);
            if ((num % 1000) == 1) {
                System.out.println(num + ":\t" + new Date().getTime());
                ut.commit();
            }
            num++;
        }
        if (ut.getStatus() == Status.STATUS_ACTIVE) {
            ut.commit();
        }
        System.out.println("Contact and Book have been persisted");
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        pm.close();
    }
    return null;
}

From source file:org.ms123.common.ea.EACompanyContactImporter.java

private void doImport() throws Exception {
    LabeledCSVParser lp = new LabeledCSVParser(
            new ExcelCSVParser(new FileInputStream(new File(m_basedir, "ea.csv"))));
    int status;/*from w  w  w. java2  s .  c  om*/
    PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(m_storeDesc).getPersistenceManager();
    UserTransaction ut = m_nucleusService.getUserTransaction();
    int num = 0;
    Object company = null;
    String lastCompanyId = null;
    while (lp.getLine() != null) {
        String type = lp.getValueByLabel("type");
        String companyId = lp.getValueByLabel("companyId");
        if (type.startsWith("nok"))
            continue;
        if (ut.getStatus() != Status.STATUS_ACTIVE) {
            ut.begin();
        }
        String s[] = getStateAndEntity(type);
        Object obj = populate(lp, s[0], s[1]);

        if (!isEmpty(companyId)) {
            if (!companyId.equals(lastCompanyId)) {
                company = obj;
                lastCompanyId = companyId;
            }
            if (s[0].equals("contact")) {
                Set cl = (Set) PropertyUtils.getProperty(company, "contact_list");
                if (cl == null) {
                    cl = new HashSet();
                    PropertyUtils.setProperty(company, "contact_list", cl);
                }
                cl.add(obj);
            }
        }
        pm.makePersistent(obj);
        if ((num % 1000) == 1) {
            System.out.println(num + ":\t" + new Date().getTime());
            ut.commit();
        }
        num++;
    }
    if (ut.getStatus() == Status.STATUS_ACTIVE) {
        ut.commit();
    }
}

From source file:org.ms123.common.ea.EACompanyContactImporter.java

private void persistObject(Object o) throws Exception {
    UserTransaction ut = m_nucleusService.getUserTransaction();
    PersistenceManager pm = m_sessionContext.getPM();
    try {//from w  w w . j  ava 2 s. co m
        ut.begin();
        pm.makePersistent(o);
        //m_sessionContext.makePersistent(o);
        ut.commit();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
    }
}

From source file:org.ms123.common.ea.EAServiceImpl.java

private Map _createOrUpdateContact(String email, Map data, Map communication, List<String> teams)
        throws Exception {
    Map ret = null;/*from ww w. j  a  v  a  2 s  . c o m*/
    StoreDesc sdesc = StoreDesc.get("ea_data");
    SessionContext sc = m_dataLayer.getSessionContext(sdesc);
    sc.setProperty("bypassTrigger", true);
    Map c = _getContactByEmail(sc, email);
    List<Map> teamList = new ArrayList();

    for (String tn : teams) {
        String teamid = teamIdExists(sc, tn);
        if (teamid == null)
            throw new RuntimeException("createOrUpdateUser:(teamid:" + tn + ") not found");
        Map m = new HashMap();
        m.put("teamid", teamid);
        m.put("value", teamid);
        teamList.add(m);
    }
    data.put("_team_list", teamList);
    data.put("email", email);

    if (communication == null) {
        communication = new HashMap();
    }

    UserTransaction ut = m_nucleusService.getUserTransaction();
    ut.begin();
    try {
        if (c != null) {
            c.putAll(data);
            Object comm = c.remove(COMMUNICATION_ENTITY);
            String commId = (String) PropertyUtils.getProperty(comm, "id");
            System.out.println("commId:" + commId);
            ret = m_dataLayer.updateObject(sc, c, CONTACT_ENTITY, (String) c.get("id"));
            if (communication.size() > 0) {
                m_dataLayer.updateObject(sc, communication, COMMUNICATION_ENTITY, commId);
            }
        } else {
            ret = m_dataLayer.insertObject(sc, data, CONTACT_ENTITY);
            //            communication.put("mail1", email);
            Map r = m_dataLayer.insertObject(sc, communication, COMMUNICATION_ENTITY, CONTACT_ENTITY,
                    (String) ret.get("id"));
        }
        ut.commit();
    } catch (Exception e) {
        sc.handleException(e);
    }
    return ret;
}

From source file:org.ms123.common.importing.BaseImportingServiceImpl.java

protected List<Map> persistObjects(SessionContext sc, Object obj, Map settings, int max) {
    List<Map> retList = new ArrayList();
    UserTransaction ut = sc.getUserTransaction();
    String mainEntity = null;/*w w w  .  jav a2 s. c o m*/
    Collection<Object> resultList = null;
    if (obj instanceof Collection) {
        resultList = (Collection) obj;
    } else {
        resultList = new ArrayList();
        resultList.add(obj);
    }
    Map outputTree = (Map) settings.get("output");
    Map<String, String> persistenceSpecification = (Map) outputTree.get("persistenceSpecification");
    System.out
            .println("persistObjects:" + resultList + ",persistenceSpecification:" + persistenceSpecification);
    String parentFieldName = null;
    Class parentClazz = null;
    String parentQuery = null;
    String updateQuery = null;
    PersistenceManager pm = sc.getPM();
    GroovyShell groovyShell = null;
    if (persistenceSpecification != null) {
        String parentLookup = persistenceSpecification.get("lookupRelationObjectExpr");
        String relation = persistenceSpecification.get("relation");
        if (!isEmpty(parentLookup) && !isEmpty(relation)) {
            String s[] = relation.split(",");
            parentClazz = sc.getClass(getBaseName(s[0]));
            parentFieldName = s[1];
            if (parentLookup.matches(FIELDNAME_REGEX)) {
                String q = isString(parentClazz, parentLookup) ? "'" : "";
                parentQuery = parentLookup + " == " + q + "${" + parentLookup + "}" + q;
            } else if (parentLookup.matches(FIELDNAME_REGEX + "," + FIELDNAME_REGEX)) {
                s = parentLookup.split(",");
                String q = isString(parentClazz, s[1]) ? "'" : "";
                parentQuery = s[0] + " == " + q + "${" + s[1] + "}" + q;
            } else {
                parentQuery = parentLookup;
            }
            groovyShell = new GroovyShell(this.getClass().getClassLoader(), new Binding(),
                    new CompilerConfiguration());
        }
        String updateLookup = persistenceSpecification.get("lookupUpdateObjectExpr");
        Class mainClass = null;
        if (resultList.size() > 0) {
            mainClass = resultList.iterator().next().getClass();
        }
        if (!isEmpty(updateLookup) && mainClass != null) {
            if (updateLookup.matches(FIELDNAME_REGEX)) {
                String q = isString(mainClass, updateLookup) ? "'" : "";
                updateQuery = updateLookup + " == " + q + "${" + updateLookup + "}" + q;
            } else {
                updateQuery = updateLookup;
            }
        }
    }
    try {
        int num = 0;
        if (resultList.size() > 0) {
            Class clazz = resultList.iterator().next().getClass();
            mainEntity = m_inflector.getEntityName(clazz.getSimpleName());
            String pk = getPrimaryKey(clazz);
            sc.setPrimaryKey(pk);
        }
        for (Object object : resultList) {
            if (max != -1 && num >= max) {
                break;
            }
            Map m = SojoObjectFilter.getObjectGraph(object, sc, 2);
            retList.add(m);
            ut.begin();
            Object origObject = null;
            if (updateQuery != null) {
                origObject = getObjectByFilter(groovyShell, pm, object.getClass(), object, updateQuery);
                System.out.println("origObject:" + origObject);
                if (origObject != null) {
                    sc.populate(m, origObject);
                    object = origObject;
                }
            }
            if (origObject == null && parentClazz != null) {
                Object parentObject = getObjectByFilter(groovyShell, pm, parentClazz, object, parentQuery);
                m_dataLayer.insertIntoMaster(sc, object, mainEntity, parentObject, parentFieldName);
            }
            m_dataLayer.makePersistent(sc, object);
            System.out.println("\tpersist:" + m_js.serialize(object));
            ut.commit();
            num++;
        }
    } catch (Throwable e) {
        e.printStackTrace();
        sc.handleException(ut, e);
    } finally {
        sc.handleFinally(ut);
    }
    return retList;
}

From source file:org.ms123.common.importing.BaseImportingServiceImpl.java

protected Map doImport(StoreDesc data_sdesc, Map settings, byte[] content, boolean withoutSave, int max)
        throws Exception {
    List<Map> mappings = null;
    List<Map> defaults = null;
    Map sourceSetup = null;/*from  w  w  w  .jav  a  2s.co  m*/
    String mainEntity = null;
    Smooks smooks = m_smooksFactory.createInstance();
    try {
        mappings = getListParameter(settings, MAPPING, false);
        defaults = getListParameter(settings, DEFAULTS, true);
        sourceSetup = getMapParameter(settings, SOURCE_SETUP, false);
        mainEntity = getStringParameter(settings, MAIN_ENTITY, false);
        SessionContext sessionContext = m_dataLayer.getSessionContext(data_sdesc);
        UserTransaction ut = sessionContext.getUserTransaction();
        try {
            String ftype = detectFileType(content);
            if (ftype == null) {
                throw new RuntimeException("BaseImportingServiceImpl.doImport:no Filecontent");
            }

            boolean isCsv = false;
            if ("text/plain".equals(ftype)) {
                isCsv = true;
            }
            Collection<Object> resultList = null;
            if (isCsv) {
                Map result = csvImport(sessionContext, mappings, defaults, sourceSetup, mainEntity, content);
                resultList = (Set) result.get("result");
            } else {
                Map entityTree = m_entityService.getEntityTree(data_sdesc, mainEntity, 3, null, null, true);
                XmlImporter xim = new XmlImporter();
                xim.setUserName(getUserName());
                xim.setModuleTree(entityTree);
                xim.setMax(max);
                xim.setDefaults(defaults);
                xim.setMainEntityName(mainEntity);
                xim.setSessionContext(sessionContext);
                Map<String, String> shortestMapping = getShortestMapping(mappings);
                if (shortestMapping == null) {
                    throw new RuntimeException("ImportingServiceImpl.xmlImport:invalid mapping");
                }
                removeSelectorPrefixFromMappings(mappings, shortestMapping.get("source"));
                xim.setMappings(mappings);
                String target = shortestMapping.get("target");
                if (!m_inflector.getClassName(target).equals(m_inflector.getClassName(mainEntity))) {
                    throw new RuntimeException("ImportingServiceImpl.xmlImport:wrong main mapping:"
                            + shortestMapping + "/mainEntity:" + mainEntity);
                }
                System.out.println("shortest_mapping:" + shortestMapping);
                smooks.addVisitor(xim, shortestMapping.get("source"));
                ExecutionContext executionContext = smooks.createExecutionContext();
                ByteArrayInputStream is = new ByteArrayInputStream(content);
                JavaResult result = new JavaResult();
                smooks.filterSource(executionContext, new StreamSource(is), result);
                resultList = xim.getResultList();
            }
            List<Map> retList = new ArrayList();
            int num = 0;
            if (resultList.size() > 0) {
                String pk = getPrimaryKey(resultList.iterator().next().getClass());
                sessionContext.setPrimaryKey(pk);
            }
            for (Object o : resultList) {
                if (max != -1 && num >= max)
                    break;
                Map m = SojoObjectFilter.getObjectGraph(o, sessionContext, 2);
                retList.add(m);
                List cv = sessionContext.validateObject(m, mainEntity);
                if (cv == null && m.get("_duplicated_id_") == null) {
                    if (!withoutSave) {
                        ut.begin();
                        m_dataLayer.makePersistent(sessionContext, o);
                        ut.commit();
                    }
                } else {
                    m.put("constraintViolations", cv);
                }
                num++;
                if ((num % 1000) == 0) {
                    System.out.println("commit1++++++++:" + num);
                }
            }
            String[] dbfields = getFieldsFromMapping(mappings);
            Map ret = new HashMap();
            ret.put("fields", dbfields);
            ret.put("result", retList);
            return ret;
        } catch (Throwable e) {
            e.printStackTrace();
            sessionContext.handleException(ut, e);
        } finally {
            sessionContext.handleFinally(ut);
        }
    } finally {
        smooks.close();
    }
    return new HashMap();
}

From source file:org.ms123.common.importing.ImportingServiceImpl.java

public Object doImport(@PName(StoreDesc.STORE_ID) String storeId,
        @PName(IMPORTING_ID) @POptional String importingid,
        @PName("withoutSave") @POptional @PDefaultBool(false) Boolean withoutSave,
        @PName("max") @POptional @PDefaultInt(-1) Integer max) throws RpcException {
    StoreDesc data_sdesc = StoreDesc.get(storeId);
    StoreDesc aid_sdesc = getStoreDesc(data_sdesc.getNamespace());
    SessionContext sessionContext = m_dataLayer.getSessionContext(aid_sdesc);
    try {//from ww  w . ja  v  a2  s  .co  m
        String className = m_inflector.getClassName(IMPORTING_ENTITY);
        Class clazz = sessionContext.getClass(className);
        Object obj = sessionContext.getObjectById(clazz, importingid);
        if (obj == null) {
            throw new RuntimeException(
                    "ImportingServiceImpl.doImport:importingid:\"" + importingid + "\" not found");
        }
        Map settings = (Map) m_ds.deserialize((String) getProperty(obj, JSON_BODY));
        byte[] content = (byte[]) getProperty(obj, CONTENT);
        if (settings.get("input") != null) {
            System.out.println("doImport:" + settings);
            System.out.println("doImport:" + m_datamapper + "/" + data_sdesc + "/" + content);
            sessionContext = m_dataLayer.getSessionContext(data_sdesc);
            BeanFactory bf = new BeanFactory(sessionContext, settings);
            Object ret = m_datamapper.transform(data_sdesc.getNamespace(), settings, null, new String(content),
                    bf);
            if (withoutSave)
                return ret;
            UserTransaction ut = sessionContext.getUserTransaction();
            try {
                ut.begin();
                Map outputTree = (Map) settings.get("output");
                Map<String, Object> persistenceSpecification = (Map) outputTree.get("persistenceSpecification");
                Object o = org.ms123.common.data.MultiOperations.persistObjects(sessionContext, ret,
                        persistenceSpecification, -1);
                ut.commit();
                return o;
            } catch (Exception e) {
                ut.rollback();
                throw e;
            }
        } else {
            return doImport(data_sdesc, settings, content, withoutSave, max);
        }
    } catch (Throwable e) {
        throw new RpcException(ERROR_FROM_METHOD, INTERNAL_SERVER_ERROR, "ImportingServiceImpl.doImport:", e);
    } finally {
        sessionContext.handleFinally(null);
    }
}