Example usage for org.hibernate Query setFetchSize

List of usage examples for org.hibernate Query setFetchSize

Introduction

In this page you can find the example usage for org.hibernate Query setFetchSize.

Prototype

Query<R> setFetchSize(int fetchSize);

Source Link

Document

Sets a JDBC fetch size hint for the query.

Usage

From source file:at.treedb.db.Iterator.java

License:Open Source License

/**
 * <p>//  w  w w  .  j a v  a2s . c  o m
 * Returns the next single object of an entity. This method is used to
 * enumerate large (binary) objects of a entity set. Single object fetching
 * should avoid running into OutOfMemory exceptions.
 * </p>
 * <p>
 * <b>Implementation details:</b>
 * <ol>
 * <li>Hibernate: Statless session<br>
 * </li>
 * <li>JPA/EclipseLink: <a href=
 * "http://wiki.eclipse.org/Using_Advanced_Query_API_%28ELUG%29#Example_107-12">
 * ReadAllQuery/CursoredStream</a> (streaming data) wasn't really working -
 * every time the whole entity data set was loaded by the first access!
 * Actual a native SQL statement is used to pre-load all object IDs. This
 * list is used to retrieve all objects.</li>
 * <li>JPA/ObjectDB: Slow query with setting first position/max data set
 * size.</li>
 * </ol>
 * 
 * @return entity object
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public List<Object> nextObject() throws Exception {
    if (!hasNext) {
        return null;
    }
    int size = 1;
    List<Object> list = null;
    // Hibernate environment
    if (dao.isHibernate() || dao.getJPAimpl() == DAO.JPA_IMPL.HIBERNATEJPA) {
        if (sresult == null) {
            Query query = ((DAOhibernate) dao).createQuery(queryString, map);
            query.setReadOnly(true);
            // MIN_VALUE gives hint to JDBC driver to stream results - but
            // this magic
            // is not working for every DB!
            if (dao.getDB() != DAO.DB.H2) {
                query.setFetchSize(Integer.MIN_VALUE);
            }
            sresult = query.scroll(ScrollMode.FORWARD_ONLY);
        }
        if (sresult.next()) {
            list = new ArrayList<Object>();
            list.add(sresult.get(0));
        }
    } else {
        if (dao.getJPAimpl() != DAO.JPA_IMPL.OBJECTDB) {
            if (idList == null) {
                idList = (List<Integer>) dao.nativeQuery(nativeQueryString);
                if (idList.size() == 0) {
                    return null;
                }
            }
            if (listIndex < idList.size()) {
                list = new ArrayList<Object>();
                Object o = Base.load(dao, (Class<? extends Base>) clazz, idList.get(listIndex));
                if (o == null) {
                    throw new Exception("Iterator.nextObject(): loading JPA object for ID "
                            + idList.get(listIndex) + " failed");
                }
                list.add(o);
                ++listIndex;
            }
        } else {
            // TODO: fallback for ObjectDB - working, but slow, very slow
            list = (List<Object>) dao.query(queryString, index, size, map);
        }
    }
    index += size;
    toRead -= size;
    if (toRead == 0) {
        hasNext = false;
    }
    return list;
}

From source file:com.jaspersoft.ireport.designer.connection.JRHibernateConnection.java

License:Open Source License

@Override
public void test() throws Exception {
    try {//from www .ja v  a2s . c  o m
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Thread.currentThread()
                        .setContextClassLoader(IReportManager.getInstance().getReportClassLoader());
                SessionFactory hb_sessionFactory = null;
                try {
                    hb_sessionFactory = getSessionFactory();

                    // Try to execute an hibernate query...
                    Session hb_session = hb_sessionFactory.openSession();
                    Transaction transaction = hb_session.beginTransaction();

                    Query q = hb_session.createQuery("from java.lang.String s");

                    q.setFetchSize(100);
                    java.util.Iterator iterator = q.iterate();
                    // this is a stupid thing: iterator.next();

                    while (iterator.hasNext()) {
                        Object obj = iterator.next();
                    }

                    JOptionPane.showMessageDialog(Misc.getMainWindow(),
                            //I18n.getString("messages.connectionDialog.connectionTestSuccessful",
                            "Connection test successful!", "", JOptionPane.INFORMATION_MESSAGE);

                } catch (Throwable ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(Misc.getMainWindow(), ex.getMessage(), "Error",
                            JOptionPane.ERROR_MESSAGE);
                    return;
                } finally {

                }
            }
        });
    } catch (Exception ex) {
    }
}

From source file:com.jaspersoft.ireport.designer.connection.JRSpringLoadedHibernateConnection.java

License:Open Source License

@Override
public void test() throws Exception {
    try {/*  w ww.jav  a 2 s. co m*/
        Thread.currentThread().setContextClassLoader(IReportManager.getInstance().getReportClassLoader());

        SessionFactory sf = getSessionFactory();
        if (sf == null) {
            JOptionPane.showMessageDialog(Misc.getMainWindow(),
                    //I18n.getString("messages.connectionDialog.noSessionFactoryReturned",
                    "No session factory returned.  Check your session factory bean id against the spring configuration.",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } else {

            Session hb_session = sf.openSession();
            Transaction transaction = hb_session.beginTransaction();
            Query q = hb_session.createQuery("select address as address Address as address");

            q.setFetchSize(1);
            java.util.Iterator iterator = q.iterate();
            // this is a stupid thing: iterator.next();

            String[] aliases = q.getReturnAliases();
            Type[] types = q.getReturnTypes();

            JOptionPane.showMessageDialog(Misc.getMainWindow(),
                    //I18n.getString("messages.connectionDialog.hibernateConnectionTestSuccessful",
                    "iReport successfully created a Hibernate session factory from your Spring configuration.",
                    "", JOptionPane.INFORMATION_MESSAGE);
        }
    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(Misc.getMainWindow(), e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);

    }
}

From source file:com.jaspersoft.ireport.designer.data.fieldsproviders.hibernate.HQLFieldsReader.java

License:Open Source License

public Vector readFields() throws Exception {
    prepareQuery();//from   w w  w. j  a  v  a2  s. c  om

    SessionFactory hb_sessionFactory = null;
    Session hb_session = null;
    Transaction transaction = null;

    notScalars.clear();

    try {
        IReportConnection conn = IReportManager.getInstance().getDefaultConnection();
        if (!(conn instanceof JRHibernateConnection)) {
            throw new Exception("No Hibernate connection selected.");
        }

        hb_session = ((JRHibernateConnection) conn).createSession();

        if (hb_session == null) {
            throw new Exception("Problem creating the Session object for Hibernate");
        }

        transaction = hb_session.beginTransaction();
        Query q = hb_session.createQuery(getQueryString());

        Iterator paramIterator = queryParameters.keySet().iterator();

        while (paramIterator.hasNext()) {
            String hqlParamName = "" + paramIterator.next();
            setParameter(hb_session, q, hqlParamName, queryParameters.get(hqlParamName));
        }

        q.setFetchSize(1);
        java.util.Iterator iterator = q.iterate();
        // this is a stupid thing: iterator.next();

        String[] aliases = q.getReturnAliases();
        Type[] types = q.getReturnTypes();

        Vector fields = new Vector();

        for (int i = 0; i < types.length; ++i) {
            if (types[i].isComponentType() || types[i].isEntityType()) {

                // look for alias...
                String aliasName = null;
                if (aliases != null && aliases.length > i && !aliases[i].equals(i + "")) {
                    aliasName = aliases[i];
                    JRDesignField field = new JRDesignField();
                    field.setName(aliases[i]);

                    Class clazzRT = types[i].getReturnedClass();

                    if (clazzRT.isPrimitive()) {
                        clazzRT = MethodUtils.getPrimitiveWrapper(clazzRT);
                    }

                    String returnType = clazzRT.getName();

                    field.setValueClassName(returnType);
                    field.setDescription(aliases[i]);
                    fields.add(field);
                }

                // look for fields like for a javabean...
                java.beans.PropertyDescriptor[] pd = org.apache.commons.beanutils.PropertyUtils
                        .getPropertyDescriptors(types[i].getReturnedClass());

                if (aliasName != null) {
                    notScalars.add(new FieldClassWrapper(aliasName, types[i].getReturnedClass().getName()));
                } else {
                    notScalars.add(types[i].getReturnedClass().getName());
                }

                for (int nd = 0; nd < pd.length; ++nd) {
                    String fieldName = pd[nd].getName();
                    if (pd[nd].getPropertyType() != null && pd[nd].getReadMethod() != null) {
                        if (fieldName.equals("class"))
                            continue;

                        Class clazzRT = pd[nd].getPropertyType();

                        if (clazzRT.isPrimitive()) {
                            clazzRT = MethodUtils.getPrimitiveWrapper(clazzRT);
                        }

                        String returnType = clazzRT.getName();

                        JRDesignField field = new JRDesignField();
                        field.setName(fieldName);
                        field.setValueClassName(returnType);

                        if (types.length > 1 && aliasName != null) {
                            fieldName = aliasName + "." + fieldName;
                            field.setDescription(fieldName); //Field returned by " +methods[i].getName() + " (real type: "+ returnType +")");
                            field.setName(fieldName);
                        }
                        fields.add(field);
                    }
                }

            } else {
                String fieldName = types[i].getName();
                if (aliases != null && aliases.length > i && !aliases[i].equals("" + i))
                    fieldName = aliases[i];

                Class clazzRT = types[i].getReturnedClass();

                if (clazzRT.isPrimitive()) {
                    clazzRT = MethodUtils.getPrimitiveWrapper(clazzRT);
                }
                String returnType = clazzRT.getName();

                JRDesignField field = new JRDesignField();
                field.setName(fieldName);
                field.setValueClassName(returnType);
                //field.setDescription("");
                fields.add(field);
            }
        }
        /*
        else
        {
            for (int i =0; i<types.length; ++i)
            {
                if (aliases != null && aliases.length > 0 && !aliases[0].equals(""+i))
                {
                    JRField field = new JRField(aliases[i], types[i].getReturnedClass().getName());
                    field.setDescription("The whole entity/component object");
                    fields.add(field);
                            
                            
                }
                        
                        
            //    out.println(types[i].getName() + " " + types[i].getReturnedClass().getName() +  "<br>");
            }
        }
         */

        return fields;

    } catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
    } finally {

        if (transaction != null)
            try {
                transaction.rollback();
            } catch (Exception ex) {
            }
        if (hb_session != null)
            try {
                hb_session.close();
            } catch (Exception ex) {
            }
    }
}

From source file:com.jdon.persistence.hibernate.HibernateTemplate.java

License:Apache License

/**
 * Prepare the given Query object, applying cache settings and/or a
 * transaction timeout./* w w w  .  j  a v  a 2  s.co m*/
 * 
 * @param queryObject
 *            the Query object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 * @see SessionProviderHolder#applyTransactionTimeout
 */
protected void prepareQuery(Query queryObject) {
    if (isCacheQueries()) {
        queryObject.setCacheable(true);
        if (getQueryCacheRegion() != null) {
            queryObject.setCacheRegion(getQueryCacheRegion());
        }
    }
    if (getFetchSize() > 0) {
        queryObject.setFetchSize(getFetchSize());
    }
    if (getMaxResults() > 0) {
        queryObject.setMaxResults(getMaxResults());
    }

    if (getFirstResult() > 0) {
        queryObject.setFirstResult(getFirstResult());
    }

}

From source file:com.mg.framework.support.orm.OrmTemplateHibernateImpl.java

License:Open Source License

@Override
protected void prepareQuery(Query queryObject) {
    if (isCacheQueries()) {
        queryObject.setCacheable(true);/*  w  w  w  .  j a  v  a2  s  . c o m*/
        if (getQueryCacheRegion() != null) {
            queryObject.setCacheRegion(getQueryCacheRegion());
        }
    }
    if (getFetchSize() > 0) {
        queryObject.setFetchSize(getFetchSize());
    }
    if (getMaxResults() > 0) {
        queryObject.setMaxResults(getMaxResults());
    }
    if (getFlushMode() != null) {
        queryObject.setFlushMode(CriteriaHibernateImpl.convertFlushModeToHibernate(getFlushMode()));
    }
    //SessionFactoryUtils.applyTransactionTimeout(queryObject, getSessionFactory());
}

From source file:com.mysema.query.jpa.hibernate.AbstractHibernateQuery.java

License:Apache License

private Query createQuery(String queryString, @Nullable QueryModifiers modifiers, boolean forCount) {
    Query query = session.createQuery(queryString);
    HibernateUtil.setConstants(query, getConstants(), getMetadata().getParams());
    if (fetchSize > 0) {
        query.setFetchSize(fetchSize);
    }//from ww w .java2  s  .  c o  m
    if (timeout > 0) {
        query.setTimeout(timeout);
    }
    if (cacheable != null) {
        query.setCacheable(cacheable);
    }
    if (cacheRegion != null) {
        query.setCacheRegion(cacheRegion);
    }
    if (comment != null) {
        query.setComment(comment);
    }
    if (readOnly != null) {
        query.setReadOnly(readOnly);
    }
    for (Map.Entry<Path<?>, LockMode> entry : lockModes.entrySet()) {
        query.setLockMode(entry.getKey().toString(), entry.getValue());
    }
    if (flushMode != null) {
        query.setFlushMode(flushMode);
    }

    if (modifiers != null && modifiers.isRestricting()) {
        if (modifiers.getLimit() != null) {
            query.setMaxResults(modifiers.getLimit().intValue());
        }
        if (modifiers.getOffset() != null) {
            query.setFirstResult(modifiers.getOffset().intValue());
        }
    }

    // set transformer, if necessary
    List<? extends Expression<?>> projection = getMetadata().getProjection();
    if (projection.size() == 1 && !forCount) {
        Expression<?> expr = projection.get(0);
        if (expr instanceof FactoryExpression<?>) {
            query.setResultTransformer(
                    new FactoryExpressionTransformer((FactoryExpression<?>) projection.get(0)));
        }
    } else if (!forCount) {
        FactoryExpression<?> proj = FactoryExpressionUtils.wrap(projection);
        if (proj != null) {
            query.setResultTransformer(new FactoryExpressionTransformer(proj));
        }
    }
    return query;
}

From source file:com.querydsl.jpa.hibernate.AbstractHibernateQuery.java

License:Apache License

private Query createQuery(@Nullable QueryModifiers modifiers, boolean forCount) {
    JPQLSerializer serializer = serialize(forCount);
    String queryString = serializer.toString();
    logQuery(queryString, serializer.getConstantToLabel());
    Query query = session.createQuery(queryString);
    HibernateUtil.setConstants(query, serializer.getConstantToLabel(), getMetadata().getParams());
    if (fetchSize > 0) {
        query.setFetchSize(fetchSize);
    }//from w  w  w. jav  a2s  .com
    if (timeout > 0) {
        query.setTimeout(timeout);
    }
    if (cacheable != null) {
        query.setCacheable(cacheable);
    }
    if (cacheRegion != null) {
        query.setCacheRegion(cacheRegion);
    }
    if (comment != null) {
        query.setComment(comment);
    }
    if (readOnly != null) {
        query.setReadOnly(readOnly);
    }
    for (Map.Entry<Path<?>, LockMode> entry : lockModes.entrySet()) {
        query.setLockMode(entry.getKey().toString(), entry.getValue());
    }
    if (flushMode != null) {
        query.setFlushMode(flushMode);
    }

    if (modifiers != null && modifiers.isRestricting()) {
        Integer limit = modifiers.getLimitAsInteger();
        Integer offset = modifiers.getOffsetAsInteger();
        if (limit != null) {
            query.setMaxResults(limit);
        }
        if (offset != null) {
            query.setFirstResult(offset);
        }
    }

    // set transformer, if necessary
    Expression<?> projection = getMetadata().getProjection();
    if (!forCount && projection instanceof FactoryExpression) {
        query.setResultTransformer(new FactoryExpressionTransformer((FactoryExpression<?>) projection));
    }
    return query;
}

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

License:Open Source License

/**
 * Run the indexer./*w w w  . j av a 2 s .  com*/
 */
@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 w  w .  j  ava  2 s . co 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);
    }
}