Example usage for org.hibernate ScrollableResults get

List of usage examples for org.hibernate ScrollableResults get

Introduction

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

Prototype

Object get(int i);

Source Link

Document

Get the ith object in the current row of results, without initializing any other results in the row.

Usage

From source file:com.opensourcestrategies.financials.reports.FinancialReports.java

License:Open Source License

/**
 * <p>Look over invoice adjustments and transform  them into into sales and tax invoice item facts.
 * Thus an adjustment amount is added into discount column of the fact table and this is only
 * currency column affected.</p>/*from  w  w w . j a va2s .  c  o m*/
 *
 * @param session Hibernate session
 * @throws GenericEntityException
 */
public static void loadInvoiceAdjustments(Session session, Delegator delegator) throws GenericEntityException {

    Transaction tx = session.beginTransaction();

    // retrieve data as scrollable result set.
    // this is join of InvoiceAdjustment and Invoice entities and each record has all required data
    // to create new fact row
    Query invAdjQry = session.createQuery(
            "select IA.invoiceAdjustmentId, IA.invoiceId, IA.amount, I.partyIdFrom, I.invoiceDate, I.currencyUomId from InvoiceAdjustment IA, Invoice I where IA.invoiceId = I.invoiceId and I.invoiceTypeId = 'SALES_INVOICE' and I.statusId not in ('INVOICE_IN_PROCESS', 'INVOICE_CANCELLED', 'INVOICE_VOIDED', 'INVOICE_WRITEOFF')");
    ScrollableResults adjustments = invAdjQry.scroll();

    // iterate over record set
    while (adjustments.next()) {

        // keep result fields in variables as a matter of convenience
        String invoiceId = adjustments.getString(1);
        String invoiceAdjustmentId = adjustments.getString(0);
        BigDecimal amount = adjustments.getBigDecimal(2);
        String organizationPartyId = adjustments.getString(3);
        Timestamp invoiceDate = (Timestamp) adjustments.get(4);
        String currencyUomId = adjustments.getString(5);

        // lookup date dimension
        DateFormat dayOfMonthFmt = new SimpleDateFormat("dd");
        DateFormat monthOfYearFmt = new SimpleDateFormat("MM");
        DateFormat yearNumberFmt = new SimpleDateFormat("yyyy");

        String dayOfMonth = dayOfMonthFmt.format(invoiceDate);
        String monthOfYear = monthOfYearFmt.format(invoiceDate);
        String yearNumber = yearNumberFmt.format(invoiceDate);

        EntityCondition dateDimConditions = EntityCondition.makeCondition(EntityOperator.AND,
                EntityCondition.makeCondition("dayOfMonth", dayOfMonth),
                EntityCondition.makeCondition("monthOfYear", monthOfYear),
                EntityCondition.makeCondition("yearNumber", yearNumber));
        Long dateDimId = UtilEtl.lookupDimension("DateDim", "dateDimId", dateDimConditions, delegator);

        // lookup currency dimension
        Long currencyDimId = UtilEtl.lookupDimension("CurrencyDim", "currencyDimId",
                EntityCondition.makeCondition("uomId", currencyUomId), delegator);

        // lookup organization dimension
        Long organizationDimId = UtilEtl.lookupDimension("OrganizationDim", "organizationDimId",
                EntityCondition.makeCondition("organizationPartyId", organizationPartyId), delegator);

        // creates rows for both fact tables
        TaxInvoiceItemFact taxFact = new TaxInvoiceItemFact();
        taxFact.setDateDimId(dateDimId);
        taxFact.setStoreDimId(0L);
        taxFact.setTaxAuthorityDimId(0L);
        taxFact.setCurrencyDimId(currencyDimId);
        taxFact.setOrganizationDimId(organizationDimId);
        taxFact.setInvoiceId(invoiceId);
        taxFact.setInvoiceAdjustmentId(invoiceAdjustmentId);
        taxFact.setGrossAmount(BigDecimal.ZERO);
        taxFact.setDiscounts(amount);
        taxFact.setRefunds(BigDecimal.ZERO);
        taxFact.setNetAmount(BigDecimal.ZERO);
        taxFact.setTaxable(BigDecimal.ZERO);
        taxFact.setTaxDue(BigDecimal.ZERO);
        session.save(taxFact);

        SalesInvoiceItemFact salesFact = new SalesInvoiceItemFact();
        salesFact.setDateDimId(dateDimId);
        salesFact.setStoreDimId(0L);
        salesFact.setCurrencyDimId(currencyDimId);
        salesFact.setOrganizationDimId(organizationDimId);
        salesFact.setInvoiceId(invoiceId);
        salesFact.setInvoiceAdjustmentId(invoiceAdjustmentId);
        salesFact.setGrossAmount(BigDecimal.ZERO);
        salesFact.setDiscounts(amount);
        salesFact.setRefunds(BigDecimal.ZERO);
        salesFact.setNetAmount(BigDecimal.ZERO);
        session.save(salesFact);

    }
    adjustments.close();
    tx.commit(); // persist result, don't move this statement upper
}

From source file:com.orig.gls.group.dao.Group.java

public static void verifyGroup(int groupId) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;/*from w ww . ja  v  a  2  s  .c  o  m*/
    try {
        tx = session.beginTransaction();
        Criteria cr = session.createCriteria(GroupsTableMod.class);
        cr.add(Restrictions.eq("groupId", groupId));
        int count = 0;
        ScrollableResults items = cr.scroll();
        while (items.next()) {
            GroupsTableMod group = (GroupsTableMod) items.get(0);
            session.delete(group);
            if (++count % 100 == 0) {
                session.flush();
                session.clear();
            }
        }
        tx.commit();
    } catch (Exception asd) {
        log.debug(asd.getMessage());
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
}

From source file:com.orig.gls.subgroup.dao.SubGroup.java

public static void verifySubGroup(int groupId) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;/* w w w.j a va  2s.  c  om*/
    try {
        tx = session.beginTransaction();
        Criteria cr = session.createCriteria(SubGrpTableMod.class);
        cr.add(Restrictions.eq("subGroupId", groupId));
        int count = 0;
        ScrollableResults items = cr.scroll();
        while (items.next()) {
            SubGrpTableMod group = (SubGrpTableMod) items.get(0);
            session.delete(group);
            if (++count % 100 == 0) {
                session.flush();
                session.clear();
            }
        }
        tx.commit();
    } catch (Exception asd) {
        log.debug(asd.getMessage());
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
}

From source file:com.querydsl.jpa.IntegrationBase.java

License:Apache License

@Test
public void Scroll() {
    session.save(new Cat("Bob", 10));
    session.save(new Cat("Steve", 11));

    QCat cat = QCat.cat;/*from   w ww .j  ava2  s . c  om*/
    HibernateQuery<?> query = new HibernateQuery<Void>(session);
    ScrollableResults results = query.from(cat).select(cat).scroll(ScrollMode.SCROLL_INSENSITIVE);
    while (results.next()) {
        assertNotNull(results.get(0));
    }
    results.close();
}

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 www. j a  va  2 s .c  om*/

    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.
 *///from   ww w  .j a v  a2s .co  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: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   w  ww.j  av a  2 s.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++;//from  w  w w .j  a v  a2s.  com
            // 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.//w  ww  . ja v  a 2 s  .  co  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 w ww . jav  a 2s  .  c o  m*/

        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);
    }
}