Example usage for org.hibernate ScrollableResults next

List of usage examples for org.hibernate ScrollableResults next

Introduction

In this page you can find the example usage for org.hibernate ScrollableResults next.

Prototype

boolean next();

Source Link

Document

Advance to the next result.

Usage

From source file:com.reignite.query.StructuredQuery.java

License:Open Source License

private int runQuery(Criteria criteria, QueryResult result, int maxResults) {
    ScrollableResults scroll = criteria.scroll(ScrollMode.FORWARD_ONLY);
    int count = 0;
    if (scroll.setRowNumber(startIndex)) {
        while (count < maxResults) {
            Object[] row = scroll.get();
            count = fillResult(result, row) ? count += 1 : count;
            if (!scroll.next()) {
                break;
            }/*w  ww  .  j av  a2  s .c o m*/
        }
    }
    int totalResultCount = 0;
    if (scroll.last()) {
        totalResultCount = scroll.getRowNumber() + 1;
    }
    result.setTotalResults(totalResultCount);
    scroll.close();
    return count;
}

From source file:com.sapienter.jbilling.server.process.BillingProcessSessionBean.java

License:Open Source License

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void processEntity(Integer entityId, Date billingDate, Integer periodType, Integer periodValue,
        boolean isReview) throws SessionInternalError {

    if (entityId == null || billingDate == null) {
        throw new SessionInternalError("entityId and billingDate can't be null");
    }/*from ww  w.j a  v  a 2  s .  c  o  m*/

    try {
        ConfigurationBL conf = new ConfigurationBL(entityId);

        IBillingProcessSessionBean local = (IBillingProcessSessionBean) Context
                .getBean(Context.Name.BILLING_PROCESS_SESSION);

        Integer billingProcessId = local.createProcessRecord(entityId, billingDate, periodType, periodValue,
                isReview, conf.getEntity().getRetries());

        BillingProcessRunBL billingProcessRunBL = new BillingProcessRunBL();
        billingProcessRunBL.setProcess(billingProcessId);
        // TODO: all the customer's id in memory is not a good idea. 1M customers would be 4MB of memory
        List<Integer> successfullUsers = billingProcessRunBL.findSuccessfullUsers();

        // start processing users of this entity
        int totalInvoices = 0;

        boolean onlyRecurring;
        // find out parameters from the configuration
        onlyRecurring = conf.getEntity().getOnlyRecurring() == 1;
        LOG.debug("**** ENTITY " + entityId + " PROCESSING USERS");

        //Load the pluggable task for filtering the users
        PluggableTaskManager taskManager = new PluggableTaskManager(entityId,
                Constants.PLUGGABLE_TASK_BILL_PROCESS_FILTER);

        IBillingProcessFilterTask task = (IBillingProcessFilterTask) taskManager.getNextClass();

        // If one was not configured just use the basic task by default
        if (task == null) {
            task = new BasicBillingProcessFilterTask();
        }

        BillingProcessDAS bpDas = new BillingProcessDAS();

        int usersFailed = 0;
        ScrollableResults userCursor = task.findUsersToProcess(entityId, billingDate);
        if (userCursor != null) {
            int count = 0;
            while (userCursor.next()) {
                Integer userId = (Integer) userCursor.get(0);
                if (successfullUsers.contains(userId)) { // TODO: change this by a query to the DB
                    LOG.debug("User #" + userId + " was successfully processed during previous run. Skipping.");
                    continue;
                }

                Integer result[] = null;
                try {
                    result = local.processUser(billingProcessId, userId, isReview, onlyRecurring);
                } catch (Throwable ex) {
                    LOG.error("Exception was caught when processing User #" + userId
                            + ". Continue process skipping user    .", ex);
                    local.addProcessRunUser(billingProcessId, userId, ProcessRunUserDTO.STATUS_FAILED);
                }
                if (result != null) {
                    LOG.debug("User " + userId + " done invoice generation.");
                    if (!isReview) {
                        for (int f = 0; f < result.length; f++) {
                            local.emailAndPayment(entityId, result[f], billingProcessId,
                                    conf.getEntity().getAutoPayment().intValue() == 1);
                        }
                        LOG.debug("User " + userId + " done email & payment.");
                    }
                    totalInvoices += result.length;
                    local.addProcessRunUser(billingProcessId, userId, ProcessRunUserDTO.STATUS_SUCCEEDED);
                } else {
                    LOG.debug("User " + userId + " NOT done");
                    local.addProcessRunUser(billingProcessId, userId, ProcessRunUserDTO.STATUS_FAILED);

                    ++usersFailed;
                }

                // make sure the memory doesn't get flooded
                if (++count % Constants.HIBERNATE_BATCH_SIZE == 0) {
                    bpDas.reset();
                }
            }
            userCursor.close(); // done with the cursor, needs manual closing
        }
        // restore the configuration in the session, the reset removed it
        conf.set(entityId);

        if (usersFailed == 0) { // only if all got well processed
            // if some of the invoices were paper invoices, a new file with all
            // of them has to be generated
            try {
                BillingProcessBL process = new BillingProcessBL(billingProcessId);
                PaperInvoiceBatchDTO batch = process.getEntity().getPaperInvoiceBatch();
                if (totalInvoices > 0 && batch != null) {
                    PaperInvoiceBatchBL batchBl = new PaperInvoiceBatchBL(batch);
                    batchBl.compileInvoiceFilesForProcess(entityId);

                    // send the file as an attachment 
                    batchBl.sendEmail();
                }
            } catch (Exception e) {
                LOG.error("Error generetaing batch file", e);
            }
            // now update the billing proces record 
        }

        if (usersFailed == 0) {
            Integer processRunId = local.updateProcessRunFinished(billingProcessId,
                    Constants.PROCESS_RUN_STATUS_SUCCESS);

            if (!isReview) {
                // the payment processing is happening in parallel
                // this event marks the end of it
                EndProcessPaymentEvent event = new EndProcessPaymentEvent(processRunId, entityId);
                EventManager.process(event);
                // and finally the next run date in the config
                GregorianCalendar cal = new GregorianCalendar();
                cal.setTime(billingDate);
                cal.add(MapPeriodToCalendar.map(periodType), periodValue.intValue());
                conf.getEntity().setNextRunDate(cal.getTime());
                LOG.debug("Updated run date to " + cal.getTime());
            }
        } else {
            local.updateProcessRunFinished(billingProcessId, Constants.PROCESS_RUN_STATUS_FAILED);
            billingProcessRunBL.notifyProcessRunFailure(entityId, usersFailed);

            // TODO: check, if updating totals needed
            // TODO: in the case of errors during users processing
            BillingProcessRunBL runBL = new BillingProcessRunBL();
            runBL.setProcess(billingProcessId);
            // update the totals
            runBL.updateTotals(billingProcessId);
        }

        LOG.debug("**** ENTITY " + entityId + " DONE. Failed users = " + usersFailed);
        // TODO: review that this is not needed: EventManager.process(generatedEvent);
    } catch (Exception e) {
        // no need to specify a rollback, an error in any of the
        // updates would not require the rest to be rolled back.
        // Actually, it's better to keep as far as it went.
        LOG.error("Error processing entity " + entityId, e);
    }
}

From source file:com.square.core.agent.RebuildingIndexAgentJmxThead.java

License:Open Source License

/**
 * Lancement indexation manuelle sur requete.
 *//*w w w  . j a v a 2  s  . c o m*/
private void runManualIndexer(Session session) {
    final FullTextSession fullTextSession = Search.getFullTextSession(session);
    try {
        fullTextSession.setFlushMode(FlushMode.MANUAL);
        fullTextSession.setCacheMode(CacheMode.IGNORE);
        final Transaction transaction = fullTextSession.beginTransaction();
        // Scrollable results will avoid loading too many objects in memory
        final ScrollableResults results = fullTextSession.createQuery(agent.getRequete())
                .setFetchSize(agent.getBatchSizeToLoad()).scroll(ScrollMode.FORWARD_ONLY);
        int index = 0;
        while (results.next()) {
            index++;
            logger.debug(agent.getMessageSourceUtil().get(AgentJmxKeyUtil.MESSAGE_INDEXATION_DE) + " "
                    + results.get(0) + " (id = " + ((BaseModel) results.get(0)).getId() + ")");
            fullTextSession.index(results.get(0)); // index each element
            if (index % agent.getBatchSizeToLoad() == 0) {
                fullTextSession.flushToIndexes(); // apply changes to indexes
                fullTextSession.clear(); // free memory since the queue is processed
            }
        }
        transaction.commit();
    } catch (SearchException e) {
        e.printStackTrace();
    }
}

From source file:com.wci.umls.server.jpa.algo.RrfLoaderAlgorithm.java

License:Open Source License

/**
 * Load MRCONSO.RRF. This is responsible for loading {@link Atom}s and
 * {@link AtomClass}es.//from  ww  w.  j  a v  a2s  . c o m
 *
 * @throws Exception the exception
 */
private void loadMrconso() throws Exception {
    logInfo("  Load MRCONSO");
    logInfo("  Insert atoms and concepts ");

    // Set up maps
    String line = null;

    int objectCt = 0;
    final PushBackReader reader = readers.getReader(RrfReaders.Keys.MRCONSO);
    final String fields[] = new String[18];
    String prevCui = null;
    Concept cui = null;
    while ((line = reader.readLine()) != null) {

        line = line.replace("\r", "");
        FieldedStringTokenizer.split(line, "|", 18, fields);

        // Skip non-matching in single mode
        if (singleMode && !fields[11].equals(getTerminology())) {
            continue;
        }

        // Field Description
        // 0 CUI
        // 1 LAT
        // 2 TS
        // 3 LUI
        // 4 STT
        // 5 SUI
        // 6 ISPREF
        // 7 AUI
        // 8 SAUI
        // 9 SCUI
        // 10 SDUI
        // 11 SAB
        // 12 TTY
        // 13 CODE
        // 14 STR
        // 15 SRL
        // 16 SUPPRESS
        // 17 CVF
        //
        // e.g.
        // C0000005|ENG|P|L0000005|PF|S0007492|Y|A7755565||M0019694|D012711|MSH|PEN|D012711|(131)I-Macroaggregated
        // Albumin|0|N|256|

        // set the root terminology language
        loadedRootTerminologies.get(fields[11]).setLanguage(fields[1]);

        final Atom atom = new AtomJpa();
        atom.setLanguage(fields[1]);
        atom.setTimestamp(releaseVersionDate);
        atom.setLastModified(releaseVersionDate);
        atom.setLastModifiedBy(loader);
        atom.setObsolete(fields[16].equals("O"));
        atom.setSuppressible(!fields[16].equals("N"));
        atom.setPublished(true);
        atom.setPublishable(true);
        atom.setName(fields[14]);
        atom.setTerminology(fields[11]);
        if (loadedTerminologies.get(fields[11]) == null) {
            throw new Exception("Atom references terminology that does not exist: " + fields[11]);
        }
        atom.setVersion(loadedTerminologies.get(fields[11]).getVersion());
        // skip in single mode
        if (!singleMode) {
            atom.putAlternateTerminologyId(getTerminology(), fields[7]);
        }
        atom.setTerminologyId(fields[8]);
        atom.setTermType(fields[12]);
        atom.setWorkflowStatus(published);

        atom.setCodeId(fields[13]);
        atom.setDescriptorId(fields[10]);
        atom.setConceptId(fields[9]);

        atom.setStringClassId(fields[5]);
        atom.setLexicalClassId(fields[3]);
        atom.setCodeId(fields[13]);

        // Handle root terminology short name, hierarchical name, and sy names
        if (fields[11].equals("SRC") && fields[12].equals("SSN")) {
            final Terminology t = loadedTerminologies.get(fields[13].substring(2));
            if (t == null || t.getRootTerminology() == null) {
                logError("  Null root " + line);
            } else {
                t.getRootTerminology().setShortName(fields[14]);
            }
        }
        if (fields[11].equals("SRC") && fields[12].equals("RHT")) {
            final Terminology t = loadedTerminologies.get(fields[13].substring(2));
            if (t == null || t.getRootTerminology() == null) {
                logError("  Null root " + line);
            } else {
                t.getRootTerminology().setHierarchicalName(fields[14]);
            }
        }

        if (fields[11].equals("SRC") && fields[12].equals("RPT")) {
            final Terminology t = loadedTerminologies.get(fields[13].substring(2));
            if (t == null || t.getRootTerminology() == null) {
                logError("  Null root " + line);
            } else {
                t.getRootTerminology().setPreferredName(fields[14]);
            }
        }
        if (fields[11].equals("SRC") && fields[12].equals("RSY") && !fields[14].equals("")) {
            final Terminology t = loadedTerminologies.get(fields[13].substring(2));
            if (t == null || t.getRootTerminology() == null) {
                logError("  Null root " + line);
            } else {
                List<String> syNames = t.getRootTerminology().getSynonymousNames();
                syNames.add(fields[14]);
            }
        }

        // Handle terminology sy names
        if (fields[11].equals("SRC") && fields[12].equals("VSY") && !fields[14].equals("")) {
            final Terminology t = loadedTerminologies.get(fields[13].substring(2));
            if (t == null || t.getRootTerminology() == null) {
                logError("  Null root " + line);
            } else {
                List<String> syNames = t.getSynonymousNames();
                syNames.add(fields[14]);
            }
        }

        // Determine organizing class type for terminology
        if (!atom.getDescriptorId().equals("")) {
            termIdTypeMap.put(atom.getTerminology(), IdType.DESCRIPTOR);
        } else if (!atom.getConceptId().equals("")) {
            termIdTypeMap.put(atom.getTerminology(), IdType.CONCEPT);
        } // OTHERWISE it remains "CODE"

        // skip in single mode
        if (!singleMode) {
            atom.putConceptTerminologyId(getTerminology(), fields[0]);
        }

        // Add atoms and commit periodically
        addAtom(atom);
        logAndCommit(++objectCt, RootService.logCt, RootService.commitCt);
        atomIdMap.put(fields[7], atom.getId());
        atomTerminologyMap.put(fields[7], atom.getTerminology().intern());
        atomConceptIdMap.put(fields[7], atom.getConceptId().length() == 0 ? "".intern() : atom.getConceptId());
        atomCodeIdMap.put(fields[7], atom.getCodeId().length() == 0 ? "".intern() : atom.getCodeId());
        atomDescriptorIdMap.put(fields[7],
                atom.getDescriptorId().length() == 0 ? "".intern() : atom.getDescriptorId());

        // CUI - skip in single mode
        if (!singleMode) {
            // Add concept
            if (prevCui == null || !fields[0].equals(prevCui)) {
                if (prevCui != null) {
                    cui.setName(getComputedPreferredName(cui, list));
                    addConcept(cui);
                    conceptIdMap.put(cui.getTerminology() + cui.getTerminologyId(), cui.getId());
                    logAndCommit(++objectCt, RootService.logCt, RootService.commitCt);
                }
                cui = new ConceptJpa();
                cui.setTimestamp(releaseVersionDate);
                cui.setLastModified(releaseVersionDate);
                cui.setLastModifiedBy(loader);
                cui.setPublished(true);
                cui.setPublishable(true);
                cui.setTerminology(getTerminology());
                cui.setTerminologyId(fields[0]);
                cui.setVersion(getVersion());
                cui.setWorkflowStatus(published);
            }
            cui.getAtoms().add(atom);
            prevCui = fields[0];
        }

        // Handle Subset
        // C3539934|ENG|S|L11195730|PF|S13913746|N|A23460885||900000000000538005||SNOMEDCT_US|SB|900000000000538005|Description
        // format|9|N|256|
        if (fields[12].equals("SB")) {

            // Have to handle the type later, when we get to attributes
            final AtomSubset atomSubset = new AtomSubsetJpa();
            setSubsetFields(atomSubset, fields);
            cuiAuiAtomSubsetMap.put(fields[0] + fields[7], atomSubset);
            idTerminologyAtomSubsetMap.put(atomSubset.getTerminologyId() + atomSubset.getTerminology(),
                    atomSubset);
            final ConceptSubset conceptSubset = new ConceptSubsetJpa();
            setSubsetFields(conceptSubset, fields);
            cuiAuiConceptSubsetMap.put(fields[0] + fields[7], conceptSubset);
            idTerminologyConceptSubsetMap.put(conceptSubset.getTerminologyId() + conceptSubset.getTerminology(),
                    conceptSubset);
        }

    }
    // Add last concept
    if (prevCui != null) {
        cui.setName(getComputedPreferredName(cui, list));
        addConcept(cui);
        conceptIdMap.put(cui.getTerminology() + cui.getTerminologyId(), cui.getId());
        logAndCommit(++objectCt, RootService.logCt, RootService.commitCt);
    }

    // Set the terminology organizing class types
    for (final Terminology terminology : loadedTerminologies.values()) {
        final IdType idType = termIdTypeMap.get(terminology.getTerminology());
        if (idType != null && idType != IdType.CODE) {
            terminology.setOrganizingClassType(idType);
            updateTerminology(terminology);
        }
    }

    logInfo("  Add concepts");
    objectCt = 0;
    // NOTE: Hibernate-specific to support iterating
    // Restrict to timestamp used for THESE atoms, in case multiple RRF
    // files are loaded
    final Session session = manager.unwrap(Session.class);
    org.hibernate.Query hQuery = session.createQuery("select a from AtomJpa a " + "where conceptId is not null "
            + "and conceptId != '' and timestamp = :timestamp " + "order by terminology, conceptId");
    hQuery.setParameter("timestamp", releaseVersionDate);
    hQuery.setReadOnly(true).setFetchSize(2000).setCacheable(false);
    ScrollableResults results = hQuery.scroll(ScrollMode.FORWARD_ONLY);
    prevCui = null;
    cui = null;
    while (results.next()) {
        final Atom atom = (Atom) results.get()[0];
        if (atom.getConceptId() == null || atom.getConceptId().isEmpty()) {
            continue;
        }
        if (prevCui == null || !prevCui.equals(atom.getConceptId())) {
            if (cui != null) {
                // compute preferred name
                cui.setName(getComputedPreferredName(cui, list));
                addConcept(cui);
                conceptIdMap.put(cui.getTerminology() + cui.getTerminologyId(), cui.getId());
                logAndCommit(++objectCt, RootService.logCt, RootService.commitCt);
            }
            cui = new ConceptJpa();
            cui.setTimestamp(releaseVersionDate);
            cui.setLastModified(releaseVersionDate);
            cui.setLastModifiedBy(loader);
            cui.setPublished(true);
            cui.setPublishable(true);
            cui.setTerminology(atom.getTerminology());
            cui.setTerminologyId(atom.getConceptId());
            cui.setVersion(atom.getVersion());
            cui.setWorkflowStatus(published);
        }
        cui.getAtoms().add(atom);
        prevCui = atom.getConceptId();
    }
    if (cui != null) {
        cui.setName(getComputedPreferredName(cui, list));
        addConcept(cui);
        conceptIdMap.put(cui.getTerminology() + cui.getTerminologyId(), cui.getId());
        commitClearBegin();
    }
    results.close();
    logInfo("  Add descriptors");
    objectCt = 0;

    // NOTE: Hibernate-specific to support iterating
    hQuery = session.createQuery("select a from AtomJpa a " + "where descriptorId is not null "
            + "and descriptorId != '' and timestamp = :timestamp " + "order by terminology, descriptorId");
    hQuery.setParameter("timestamp", releaseVersionDate);
    hQuery.setReadOnly(true).setFetchSize(2000).setCacheable(false);
    results = hQuery.scroll(ScrollMode.FORWARD_ONLY);
    String prevDui = null;
    Descriptor dui = null;
    while (results.next()) {
        final Atom atom = (Atom) results.get()[0];
        if (atom.getDescriptorId() == null || atom.getDescriptorId().isEmpty()) {
            continue;
        }
        if (prevDui == null || !prevDui.equals(atom.getDescriptorId())) {
            if (dui != null) {
                // compute preferred name
                dui.setName(getComputedPreferredName(dui, list));
                addDescriptor(dui);
                descriptorIdMap.put(dui.getTerminology() + dui.getTerminologyId(), dui.getId());
                logAndCommit(++objectCt, RootService.logCt, RootService.commitCt);
            }
            dui = new DescriptorJpa();
            dui.setTimestamp(releaseVersionDate);
            dui.setLastModified(releaseVersionDate);
            dui.setLastModifiedBy(loader);
            dui.setPublished(true);
            dui.setPublishable(true);
            dui.setTerminology(atom.getTerminology());
            dui.setTerminologyId(atom.getDescriptorId());
            dui.setVersion(atom.getVersion());
            dui.setWorkflowStatus(published);
        }
        dui.getAtoms().add(atom);
        prevDui = atom.getDescriptorId();
    }
    if (dui != null) {
        dui.setName(getComputedPreferredName(dui, list));
        addDescriptor(dui);
        descriptorIdMap.put(dui.getTerminology() + dui.getTerminologyId(), dui.getId());
        commitClearBegin();
    }
    results.close();

    // Use flag to decide whether to handle codes
    if (codesFlag) {
        logInfo("  Add codes");
        objectCt = 0;
        // NOTE: Hibernate-specific to support iterating
        // Skip NOCODE
        // TODO: there is a LNC exception here -for now
        hQuery = session.createQuery("select a from AtomJpa a where codeId is not null "
                + "and codeId != '' and timestamp = :timestamp "
                + "and (terminology = 'LNC' OR (codeId != conceptId and codeId != descriptorId)) "
                + "and timestamp = :timestamp " + "order by terminology, codeId");
        hQuery.setParameter("timestamp", releaseVersionDate);
        hQuery.setReadOnly(true).setFetchSize(2000).setCacheable(false);
        results = hQuery.scroll(ScrollMode.FORWARD_ONLY);
        String prevCode = null;
        Code code = null;
        while (results.next()) {
            final Atom atom = (Atom) results.get()[0];
            if (atom.getCodeId() == null || atom.getCodeId().isEmpty() || atom.getCodeId().equals("NOCODE")) {
                continue;
            }
            if (prevCode == null || !prevCode.equals(atom.getCodeId())) {
                if (code != null) {
                    // compute preferred name
                    code.setName(getComputedPreferredName(code, list));
                    addCode(code);
                    codeIdMap.put(code.getTerminology() + code.getTerminologyId(), code.getId());
                    logAndCommit(++objectCt, RootService.logCt, 1000);
                }
                code = new CodeJpa();
                code.setTimestamp(releaseVersionDate);
                code.setLastModified(releaseVersionDate);
                code.setLastModifiedBy(loader);
                code.setPublished(true);
                code.setPublishable(true);
                code.setTerminology(atom.getTerminology());
                code.setTerminologyId(atom.getCodeId());
                code.setVersion(atom.getVersion());
                code.setWorkflowStatus(published);
            }
            code.getAtoms().add(atom);
            prevCode = atom.getCodeId();
        }
        if (code != null) {
            code.setName(getComputedPreferredName(code, list));
            addCode(code);
            codeIdMap.put(code.getTerminology() + code.getTerminologyId(), code.getId());
            commitClearBegin();
        }
        results.close();
    }

    // NOTE: for efficiency and lack of use cases, we've temporarily
    // suspended the loading of LexicalClass and StringClass objects

    // // NOTE: atoms are not connected to lexical classes as there are
    // // currently no known uses for this.
    // logInfo(" Add lexical classes");
    // objectCt = 0;
    // query = NEED TO FIX THIS
    // manager
    // .createQuery("select a.id from AtomJpa a order by lexicalClassId");
    // String prevLui = null;
    // LexicalClass lui = null;
    // LexicalClass atoms = null;
    // for (final Long id : (List<Long>) query.getResultList()) {
    // final Atom atom = getAtom(id);
    // if (atom.getLexicalClassId() == null
    // || atom.getLexicalClassId().isEmpty()) {
    // continue;
    // }
    // if (prevLui == null || !prevLui.equals(atom.getLexicalClassId())) {
    // if (lui != null) {
    // // compute preferred name
    // lui.setName(getComputedPreferredName(atoms));
    // addLexicalClass(lui);
    // logAndCommit(++objectCt, RootService.logCt, RootService.commitCt);
    // }
    // // just used to hold atoms, enver saved.
    // atoms = new LexicalClassJpa();
    // lui = new LexicalClassJpa();
    // lui.setTimestamp(releaseVersionDate);
    // lui.setLastModified(releaseVersionDate);
    // lui.setLastModifiedBy(loader);
    // lui.setPublished(true);
    // lui.setPublishable(true);
    // lui.setTerminology(terminology);
    // lui.setTerminologyId(atom.getLexicalClassId());
    // lui.setVersion(version);
    // lui.setWorkflowStatus(published);
    // lui.setNormalizedString(getNormalizedString(atom.getName()));
    // }
    // atoms.addAtom(atom);
    // prevLui = atom.getLexicalClassId();
    // }
    // if (lui != null) {
    // lui.setName(getComputedPreferredName(atoms));
    // commitClearBegin();
    // logAndCommit(++objectCt, RootService.logCt, RootService.commitCt);
    // }
    //
    // // NOTE: currently atoms are not loaded for string classes
    // // We simply load the objects themselves ( for SUI maintenance)
    // // There are no known use cases for having the atoms here.
    // logInfo(" Add string classes");
    // objectCt = 0;
    // query = NEED TO FIX THIS
    // manager
    // .createQuery("select distinct stringClassId, name from AtomJpa a");
    // for (final Object[] suiFields : (List<Object[]>) query.getResultList()) {
    // final StringClass sui = new StringClassJpa();
    // sui.setTimestamp(releaseVersionDate);
    // sui.setLastModified(releaseVersionDate);
    // sui.setLastModifiedBy(loader);
    // sui.setPublished(true);
    // sui.setPublishable(true);
    // sui.setTerminology(terminology);
    // sui.setTerminologyId(suiFields[0].toString());
    // sui.setVersion(version);
    // sui.setWorkflowStatus(published);
    // sui.setName(suiFields[1].toString());
    // addStringClass(sui);
    // logAndCommit(++objectCt, RootService.logCt, RootService.commitCt);
    // }

    // commit
    commitClearBegin();

    logInfo("  Update terminologies for languages and names.");

    // Update all root terminologies now that we know languages and names
    for (final RootTerminology root : loadedRootTerminologies.values()) {
        updateRootTerminology(root);
    }

    // Update all root terminologies now that we know languages and names
    for (final Terminology terminology : loadedTerminologies.values()) {
        updateTerminology(terminology);
    }
    commitClearBegin();

}

From source file:com.yahoo.elide.datastores.hibernate3.ScrollableIterator.java

License:Apache License

public ScrollableIterator(ScrollableResults scroll) {
    this.scroll = scroll;

    hasNext = scroll.next();
}

From source file:de.codesourcery.eve.skills.util.DBConverter.java

License:Apache License

protected void export(Class<?> entity) {

    System.out.println("\n============\nExporting " + entity.getName() + "\n============");

    // load data/*from ww w  .j av  a2s .c  o  m*/
    System.out.print("Opening MySQL session ...");
    final Session mysqlSession = mysql.openSession();
    System.out.print("created.");

    //      mysqlSession.setFlushMode( FlushMode.MANUAL );

    Transaction mysqlTransaction = mysqlSession.beginTransaction();

    final Criteria criteria = mysqlSession.createCriteria(entity);

    // replicate data
    System.out.print("Opening HSQL session ...");
    final Session hsqlSession = hsql.openSession();
    System.out.println("created.");
    //      mysqlSession.setFlushMode( FlushMode.MANUAL );

    final Transaction hsqlTransaction = hsqlSession.beginTransaction();

    final ScrollableResults data = criteria.scroll();
    int count = 0;
    int dotCount = 0;
    try {
        while (data.next()) {
            Object loaded = data.get(0);
            //            if ( entity == MarketGroup.class ) {
            //               MarketGroup group = (MarketGroup) loaded;
            //               System.out.println( group.getId() +" -> "+group.getParent() );
            //            }
            hsqlSession.replicate(loaded, ReplicationMode.IGNORE);
            if ((++count % 1000) == 0) { // make sure to adjust <prop key="hibernate.jdbc.batch_size">1000</prop> in config !!
                hsqlSession.flush();
                hsqlSession.clear();
                mysqlSession.flush();
                mysqlSession.clear();
                System.out.print(".");
                dotCount++;
                if (dotCount == 60) {
                    System.out.println();
                    dotCount = 0;
                }
            }
        }
    } finally {
        data.close();
        System.out.println("\nExported " + count + " entries");
    }

    if (mysqlTransaction.isActive()) {
        mysqlTransaction.commit();
    }

    if (hsqlTransaction.isActive()) {
        hsqlTransaction.commit();
    }

    hsqlSession.flush();
    mysqlSession.flush();

    mysqlSession.close();
    hsqlSession.close();
}

From source file:de.iteratec.iteraplan.persistence.dao.SearchDAOImpl.java

License:Open Source License

/** {@inheritDoc} */
public void createIndexes(Set<Class<?>> classList) {
    Session session = this.getSession();
    FullTextSession fullTextSession = getFullTextSession();

    session.setFlushMode(FlushMode.MANUAL); // Disable flush operations
    session.setCacheMode(CacheMode.IGNORE); // Disable second-level cache operations

    int batchSize = 100;

    // data is read from the database
    for (Class<?> bbClass : classList) {

        ScrollableResults results = session.createCriteria(bbClass).setFetchSize(batchSize)
                .scroll(ScrollMode.SCROLL_INSENSITIVE);

        LOGGER.info("Indexing " + bbClass.getSimpleName());
        int index = 0;
        while (results.next()) {
            index++;// www . ja  va 2s.  c o m
            // entities are indexed
            fullTextSession.index(results.get(0));
            if (index % batchSize == 0) {
                fullTextSession.flushToIndexes();
                fullTextSession.clear();
            }
        }
        results.close();
        LOGGER.info("Index for " + bbClass.getSimpleName() + " was created!");

    }
}

From source file:de.powerstaff.business.service.impl.ProfileIndexerServiceImpl.java

License:Open Source License

/**
 * Run the indexer./*ww w.j  a v  a2s .c  o  m*/
 */
@Transactional
public void runIndexer() {

    if (!systemParameterService.isIndexingEnabled()) {
        LOGGER.info("Indexing disabled");
        return;
    }

    if (running) {
        LOGGER.info("Indexing already running");
    }

    running = true;

    LOGGER.info("Running indexing");

    readerFactory.initialize();

    serviceLogger.logStart(SERVICE_ID, "");

    // Jetzt luft er

    long theStartTime = System.currentTimeMillis();

    try {

        int theFetchSize = 100;
        int theLogCount = theFetchSize * 10;

        Session theHibernateSession = sessionFactory.getCurrentSession();
        FullTextSession theFT = Search.getFullTextSession(theHibernateSession);

        org.hibernate.Query theQuery = theHibernateSession.createQuery("from Freelancer");
        theQuery.setFetchSize(theFetchSize);
        ScrollableResults theResults = theQuery.scroll(ScrollMode.FORWARD_ONLY);
        int counter = 0;
        while (theResults.next()) {
            Freelancer theFreelancer = (Freelancer) theResults.get(0);

            boolean needsToUpdate = true;

            TermQuery theTermQuery = new TermQuery(new Term("id", "" + theFreelancer.getId()));
            FullTextQuery theHibernateQuery = theFT.createFullTextQuery(theTermQuery, Freelancer.class);
            theHibernateQuery.setProjection(FullTextQuery.DOCUMENT);

            for (Object theSingleEntity : theHibernateQuery.list()) {

                needsToUpdate = false;
                Object[] theRow = (Object[]) theSingleEntity;
                Document theDocument = (Document) theRow[0];

                long theNumberOfProfiles = Long.parseLong(theDocument.get(ProfileIndexerService.NUM_PROFILES));
                List<FreelancerProfile> theProfiles = profileSearchService.loadProfilesFor(theFreelancer);
                if (theNumberOfProfiles != theProfiles.size()) {
                    LOGGER.info("Updating freelancer " + theFreelancer.getId()
                            + " as the number of profiles changed from " + theNumberOfProfiles + " to "
                            + theProfiles.size());
                    needsToUpdate = true;
                } else {
                    for (int i = 1; i <= theNumberOfProfiles; i++) {
                        String theFileName = theDocument.get(ProfileIndexerService.PROFILE_PATH_PREFIX + i);
                        File theFileOnServer = new File(theFileName);
                        if (theFileOnServer.exists()) {
                            long theModification = Long.parseLong(
                                    theDocument.get(ProfileIndexerService.PROFILE_MODIFICATION_PREFIX + i));
                            long theLastModified = theFileOnServer.lastModified() / 1000;
                            if (theModification != theLastModified) {
                                LOGGER.info("Updating freelancer " + theFreelancer.getId() + " as profile "
                                        + theFileOnServer + " was modified");
                                needsToUpdate = true;
                            }
                        } else {
                            LOGGER.info("Updating freelancer " + theFreelancer.getId() + " as profile "
                                    + theFileOnServer + " seems to be deleted");
                            needsToUpdate = true;
                        }
                    }
                }

            }

            if (needsToUpdate) {
                theFT.index(theFreelancer);
            }

            if (counter % theLogCount == 0) {
                LOGGER.info("Processing record " + counter);
            }

            if (counter % theFetchSize == 0) {

                LOGGER.debug("Flushing session and index");
                theFT.flushToIndexes();
                theFT.clear();
                theHibernateSession.clear();
            }
            counter++;
        }

    } catch (Exception ex) {

        LOGGER.error("Error on indexing", ex);

    } finally {

        theStartTime = System.currentTimeMillis() - theStartTime;

        LOGGER.info("Indexing finished");

        serviceLogger.logEnd(SERVICE_ID, "Dauer = " + theStartTime + "ms");

        running = false;
    }
}

From source file:de.powerstaff.business.service.impl.WrongDataServiceImpl.java

License:Open Source License

private void processFreelancer(File aReportFile) throws FileNotFoundException, ParseException {

    File theDBOhneProfil = new File(aReportFile, "Freiberufler_mit_Code_ohne_Profil.csv");
    File theFreelancerOhneNewsletter = new File(aReportFile, "Freiberufler_ohne_Newsletter.csv");
    File theFreelancerMitHomepageOhneKontakt = new File(aReportFile,
            "Freiberufler_mit_Homepage_ohne_Kontakt.csv");
    File theFreelancerForNewsletter = new File(aReportFile, "Freiberufler_fr_Newsletter.csv");
    File theProfileOhneDB = new File(aReportFile, "Profile_ohne_Datenbankeintrag.csv");
    File theProfileDoppelterCode = new File(aReportFile, "Profile_Kodierung_doppelt.csv");

    PrintWriter theDBOhneProfilWriter = null;
    PrintWriter theFreelancerOhneNewsletterWriter = null;
    PrintWriter theFreelancerMitHomepageOhneKontaktWriter = null;
    PrintWriter theFreelancerForNewsletterWriter = null;
    PrintWriter theProfileOhneDBWriter = null;
    PrintWriter theProfileDoppelterCodeWriter = null;

    FreelancerBackingBeanDataModel theModel = new FreelancerBackingBeanDataModel();

    try {/*from  ww  w .j  a v  a  2s.  com*/

        theProfileDoppelterCodeWriter = new PrintWriter(theProfileDoppelterCode);

        theDBOhneProfilWriter = new PrintWriter(theDBOhneProfil);
        theFreelancerOhneNewsletterWriter = new PrintWriter(theFreelancerOhneNewsletter);
        theFreelancerMitHomepageOhneKontaktWriter = new PrintWriter(theFreelancerMitHomepageOhneKontakt);
        theFreelancerForNewsletterWriter = new PrintWriter(theFreelancerForNewsletter);
        theProfileOhneDBWriter = new PrintWriter(theProfileOhneDB);

        theDBOhneProfilWriter.println("Kodierung;Name;Vorname;Kreditor");
        theFreelancerOhneNewsletterWriter.println("Kodierung;Name;Vorname;Mail");
        theFreelancerMitHomepageOhneKontaktWriter.println("Kodierung;Name;Vorname;Homepage");
        theFreelancerForNewsletterWriter.println(
                "Krzel;Name;Vorname;Titel;eMail;Eintrag in Kreditor;Verfgbarkeit;Homepage;letzter Kontakt;Status;Xing;Gulp");
        theProfileOhneDBWriter.println("Kodierung;Dateinamen");
        theProfileDoppelterCodeWriter.println("Kodierung;Dateinamen");

        boolean newsletterEnabled = systemParameterService.isNewsletterEnabled();
        Set<String> theMails = new HashSet<String>();
        Date theStartDate = null;

        DateFormat theDateFormat = new SimpleDateFormat("dd.MM.yyyy");

        if (newsletterEnabled) {
            theStartDate = theDateFormat.parse(systemParameterService.getStartDateForNotInNewsletter());

            for (NewsletterMail theMail : websiteDao.getConfirmedMails()) {
                theMails.add(theMail.getMail().toLowerCase());
            }

        }

        Session theSession = sessionFactory.getCurrentSession();
        int theFetchSize = 100;
        int theLogCount = theFetchSize * 10;

        Query theQuery = theSession.createQuery("from Freelancer");
        theQuery.setFetchSize(theFetchSize);
        ScrollableResults theResults = theQuery.scroll(ScrollMode.FORWARD_ONLY);
        int counter = 0;

        Set<String> theKnownCodes = new HashSet<String>();

        while (theResults.next()) {
            Freelancer theFreelancer = (Freelancer) theResults.get(0);

            String theCode = theFreelancer.getCode();
            if (!StringUtils.isEmpty(theCode)) {
                theCode = theCode.toLowerCase();
                theKnownCodes.add(theCode);

                Set<File> theFiles = fsCache.getFilesForCode(theCode);
                if ((theFiles == null || theFiles.size() == 0)) {
                    theDBOhneProfilWriter.println(theCode + ";" + saveString(theFreelancer.getName1()) + ";"
                            + saveString(theFreelancer.getName2()) + ";"
                            + saveString(theFreelancer.getKreditorNr()));
                }
            }

            List<FreelancerContact> theMailContacts = theFreelancer.getEMailContacts();
            List<FreelancerContact> theWebContacts = theFreelancer.getWebContacts();

            Date theLastContact = theFreelancer.getLastContactDate();

            if (!theFreelancer.isContactforbidden()) {

                String theMail = null;
                for (FreelancerContact theContact : theMailContacts) {
                    if (StringUtils.isEmpty(theMail)
                            && "eMail".equalsIgnoreCase(theContact.getType().getDescription())) {
                        theMail = theContact.getValue();
                    }
                }
                String theWeb = "";
                for (FreelancerContact theContact : theWebContacts) {
                    if (StringUtils.isEmpty(theWeb)
                            && "Web".equalsIgnoreCase(theContact.getType().getDescription())) {
                        theWeb = theContact.getValue();
                    }
                }
                String theGulp = "";
                for (FreelancerContact theContact : theWebContacts) {
                    if (StringUtils.isEmpty(theWeb)
                            && "Gulp".equalsIgnoreCase(theContact.getType().getDescription())) {
                        theGulp = theContact.getValue();
                    }
                }

                String theXing = "";
                for (FreelancerContact theContact : theWebContacts) {
                    if (StringUtils.isEmpty(theWeb)
                            && "Xing".equalsIgnoreCase(theContact.getType().getDescription())) {
                        theXing = theContact.getValue();
                    }
                }

                String theAvailable = "";
                Date theAvailability = theFreelancer.getAvailabilityAsDate();
                if (theAvailability != null) {
                    theAvailable = theDateFormat.format(theAvailability);
                }

                theFreelancerForNewsletterWriter.print(saveString(theFreelancer.getCode()));
                theFreelancerForNewsletterWriter.print(";");
                theFreelancerForNewsletterWriter.print(saveString(theFreelancer.getName1()));
                theFreelancerForNewsletterWriter.print(";");
                theFreelancerForNewsletterWriter.print(saveString(theFreelancer.getName2()));
                theFreelancerForNewsletterWriter.print(";");
                theFreelancerForNewsletterWriter.print(saveString(theFreelancer.getTitel()));
                theFreelancerForNewsletterWriter.print(";");
                theFreelancerForNewsletterWriter.print(saveString(theMail));
                theFreelancerForNewsletterWriter.print(";");
                theFreelancerForNewsletterWriter.print(saveString(theFreelancer.getKreditorNr()));
                theFreelancerForNewsletterWriter.print(";");
                theFreelancerForNewsletterWriter.print(saveString(theAvailable));
                theFreelancerForNewsletterWriter.print(";");
                theFreelancerForNewsletterWriter.print(saveString(theWeb));
                theFreelancerForNewsletterWriter.print(";");
                theFreelancerForNewsletterWriter.print(saveString(theLastContact));
                theFreelancerForNewsletterWriter.print(";");
                theFreelancerForNewsletterWriter
                        .print(saveString(theModel.getStatusAsString(theFreelancer.getStatus())));
                theFreelancerForNewsletterWriter.print(";");
                theFreelancerForNewsletterWriter.print(saveString(theXing));
                theFreelancerForNewsletterWriter.print(";");
                theFreelancerForNewsletterWriter.print(saveString(theGulp));
                theFreelancerForNewsletterWriter.println();
            }

            if (newsletterEnabled) {

                if (theLastContact != null && !theFreelancer.isContactforbidden()) {

                    String theMail = "";

                    boolean hasMail = false;
                    for (FreelancerContact theContact : theMailContacts) {
                        theMail = theContact.getValue();
                        if (theMails.contains(theMail.toLowerCase())) {
                            hasMail = true;
                        }
                    }

                    if (!hasMail) {
                        theFreelancerOhneNewsletterWriter.println(theFreelancer.getCode() + ";"
                                + theFreelancer.getName1() + ";" + theFreelancer.getName2() + ";" + theMail);
                    }
                }
            }

            if (theLastContact == null) {

                boolean hasHomepage = false;
                String theHomepage = null;
                for (FreelancerContact theContact : theWebContacts) {
                    theHomepage = theContact.getValue();
                    hasHomepage = true;
                }

                if (hasHomepage) {
                    theFreelancerMitHomepageOhneKontaktWriter.println(theFreelancer.getCode() + ";"
                            + theFreelancer.getName1() + ";" + theFreelancer.getName2() + ";" + theHomepage);
                }

            }

            if (counter % theLogCount == 0) {
                LOGGER.info("Processing record " + counter);
            }

            if (counter % theFetchSize == 0) {

                LOGGER.debug("Flushing session");
                theSession.clear();
            }
            counter++;
        }

        Set<String> theCodesFromFiles = new HashSet<String>();
        theCodesFromFiles.addAll(fsCache.getKnownCodes());
        for (String theCode : theCodesFromFiles) {
            Set<File> theFiles = fsCache.getFilesForCode(theCode);
            if (theFiles != null && theFiles.size() > 1) {
                // Doppelter Code
                StringBuilder theBuilder = new StringBuilder();
                for (File theFile : theFiles) {
                    if (theBuilder.length() > 0) {
                        theBuilder.append(";");
                    }
                    theBuilder.append(theFile.toString());
                }
                theProfileDoppelterCodeWriter.println(theCode + ";" + theBuilder);
            }
        }

        theCodesFromFiles.removeAll(theKnownCodes);

        for (String theCode : theCodesFromFiles) {
            Set<File> theFiles = fsCache.getFilesForCode(theCode);
            if (theFiles != null) {
                for (File theFile : theFiles) {
                    theProfileOhneDBWriter.println(theCode + ";" + theFile);
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("Error processing freelancer", e);
    } finally {
        IOUtils.closeQuietly(theDBOhneProfilWriter);
        IOUtils.closeQuietly(theFreelancerOhneNewsletterWriter);
        IOUtils.closeQuietly(theFreelancerMitHomepageOhneKontaktWriter);
        IOUtils.closeQuietly(theFreelancerForNewsletterWriter);
        IOUtils.closeQuietly(theProfileOhneDBWriter);
        IOUtils.closeQuietly(theProfileDoppelterCodeWriter);
    }
}

From source file:de.tudarmstadt.ukp.csniper.webapp.evaluation.EvaluationRepository.java

License:Apache License

@Transactional
public int[][] listCachedParsesPages(String aCollectionId, int aPageSize) {
    List<int[]> pages = new ArrayList<int[]>();
    ScrollableResults results = null;
    try {//from  w  w  w.  j a va2 s  .c  o m
        String queryString = "SELECT id FROM CachedParse WHERE collectionId = :collectionId";
        org.hibernate.Query query = ((HibernateQuery) entityManager.createQuery(queryString))
                .getHibernateQuery();
        query.setParameter("collectionId", aCollectionId);
        results = query.scroll();
        results.beforeFirst();
        int row = 0;
        int[] curPage = new int[] { -1, -1 };
        boolean hasNext = results.next();
        while (hasNext) {
            int id = results.getLong(0).intValue();
            // Record start of page
            if ((row % aPageSize) == 0) {
                curPage[0] = id;
            }

            // Step ahead
            hasNext = results.next();
            row++;

            // Record end of page when end of page or end of results is reached
            if (((row % aPageSize) == (aPageSize - 1)) || !hasNext) {
                curPage[1] = id;
                pages.add(curPage);
                curPage = new int[] { -1, -1 };
            }
        }
    } finally {
        if (results != null) {
            results.close();
        }
    }
    return pages.toArray(new int[pages.size()][2]);
}