Example usage for org.hibernate.type StringType INSTANCE

List of usage examples for org.hibernate.type StringType INSTANCE

Introduction

In this page you can find the example usage for org.hibernate.type StringType INSTANCE.

Prototype

StringType INSTANCE

To view the source code for org.hibernate.type StringType INSTANCE.

Click Source Link

Usage

From source file:de.tudarmstadt.ukp.wikipedia.api.Page.java

License:Apache License

/**
 * CAUTION: Only returns 1 result, even if several results are possible.
 *
 * @param pTitle//from ww w.j  a va 2 s . c  o m
 * @throws WikiApiException
 */
private void fetchByTitle(Title pTitle, boolean useExactTitle) throws WikiApiException {
    String searchString = pTitle.getPlainTitle();
    if (!useExactTitle) {
        searchString = pTitle.getWikiStyleTitle();
    }

    Session session;
    session = this.wiki.__getHibernateSession();
    session.beginTransaction();
    Integer pageId = (Integer) session
            .createNativeQuery("select pml.pageID from PageMapLine as pml where pml.name = :pagetitle LIMIT 1")
            .setParameter("pagetitle", searchString, StringType.INSTANCE).uniqueResult();
    session.getTransaction().commit();

    if (pageId == null) {
        throw new WikiPageNotFoundException("No page with name " + searchString + " was found.");
    }
    fetchByPageId(pageId);
    if (!this.isRedirect && searchString != null && !searchString.equals(getTitle().getRawTitleText())) {
        if (this.isRedirect) {
            //in case we already tried to re-retrieve the discussion page unsuccessfully,
            //we have to give up here or we end up in an infinite loop.

            //reasons for this happening might be several entries in PageMapLine with the same name but different upper/lower case variants
            //if the database does not allow case sensitive queries, then the API will always retrieve only the first result and if this is a redirect to a different writing variant, we are stuck in a loop.
            //To fix this, either a case sensitive collation should be used or the API should be able to deal with set valued results and pick the correct one from the set.
            //For now, we gracefully return without retrieving the Talk page for this article and throw an appropriate excption.
            throw new WikiPageNotFoundException("No discussion page with name " + searchString
                    + " could be retrieved. This is most likely due to multiple writing variants of the same page in the database");
        } else {
            this.isRedirect = true;
            /*
             * WORKAROUND
             * in our page is a redirect to a discussion page, we might not retrieve the target discussion page as expected but rather the article associated with the target discussion page
             * we check this here and re-retrieve the correct page.
             * this error should be avoided by keeping the namespace information in the database
             * This fix has been provided by Shiri Dori-Hacohen and is discussed in the Google Group under https://groups.google.com/forum/#!topic/jwpl/2nlr55yp87I/discussion
             */
            if (searchString.startsWith(DISCUSSION_PREFIX)
                    && !getTitle().getRawTitleText().startsWith(DISCUSSION_PREFIX)) {
                try {
                    fetchByTitle(new Title(DISCUSSION_PREFIX + getTitle().getRawTitleText()), useExactTitle);
                } catch (WikiPageNotFoundException e) {
                    throw new WikiPageNotFoundException("No page with name " + DISCUSSION_PREFIX
                            + getTitle().getRawTitleText() + " was found.");
                }
            }
        }
    }
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Wikipedia.java

License:Apache License

/**
 * Gets the page ids for a given title.<br>
 *
 *
 * @param title The title of the page./*from w  ww .ja v  a2  s .  c  om*/
 * @return The id for the page with the given title.
 * @throws WikiApiException
 */
public List<Integer> getPageIds(String title) throws WikiApiException {
    Session session = this.__getHibernateSession();
    session.beginTransaction();
    Iterator results = session.createQuery("select p.pageID from PageMapLine as p where p.name = :pName")
            .setParameter("pName", title, StringType.INSTANCE).list().iterator();

    session.getTransaction().commit();

    if (!results.hasNext()) {
        throw new WikiPageNotFoundException();
    }
    List<Integer> resultList = new LinkedList<Integer>();
    while (results.hasNext()) {
        resultList.add((Integer) results.next());
    }
    return resultList;
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Wikipedia.java

License:Apache License

/**
 * Gets the page ids for a given title with case insensitive matching.<br>
 *
 *
 * @param title The title of the page./*w w w . j  a  v  a  2 s .c  o  m*/
 * @return The ids of the pages with the given title.
 * @throws WikiApiException
 */
public List<Integer> getPageIdsCaseInsensitive(String title) throws WikiApiException {
    title = title.toLowerCase();
    title = title.replaceAll(" ", "_");

    Session session = this.__getHibernateSession();
    session.beginTransaction();
    Iterator results = session.createQuery("select p.pageID from PageMapLine as p where lower(p.name) = :pName")
            .setParameter("pName", title, StringType.INSTANCE).list().iterator();

    session.getTransaction().commit();

    if (!results.hasNext()) {
        throw new WikiPageNotFoundException();
    }
    List<Integer> resultList = new LinkedList<Integer>();
    while (results.hasNext()) {
        resultList.add((Integer) results.next());
    }
    return resultList;
}

From source file:de.tudarmstadt.ukp.wikipedia.api.Wikipedia.java

License:Apache License

/**
 * Return an iterable containing all archived discussion pages for
 * the given article page. The most recent discussion page is not included.
 * The most recent discussion page can be obtained with {@link #getDiscussionPage(Page)}.
 * <br>/*from  ww w .  j a v a  2s  . c  o  m*/
 * The provided page Object must not be a discussion page itself! If it is
 * a discussion page, is returned unchanged.
 *
 * @param articlePage the article page for which a discussion archives should be retrieved
 * @return An iterable with the discussion archive page objects for the given article page object
 * @throws WikiApiException If no page or redirect with this title exists or title could not be properly parsed.
 */
public Iterable<Page> getDiscussionArchives(Page articlePage) throws WikiApiException {
    String articleTitle = articlePage.getTitle().getWikiStyleTitle();
    if (!articleTitle.startsWith(WikiConstants.DISCUSSION_PREFIX)) {
        articleTitle = WikiConstants.DISCUSSION_PREFIX + articleTitle;
    }

    Session session = this.__getHibernateSession();
    session.beginTransaction();

    List<Page> discussionArchives = new LinkedList<Page>();

    Query query = session.createQuery("SELECT pageID FROM PageMapLine where name like :name");
    query.setParameter("name", articleTitle + "/%", StringType.INSTANCE);
    Iterator results = query.list().iterator();

    session.getTransaction().commit();

    while (results.hasNext()) {
        int pageID = (Integer) results.next();
        discussionArchives.add(getPage(pageID));
    }

    return discussionArchives;

}

From source file:de.tudarmstadt.ukp.wikipedia.api.Wikipedia.java

License:Apache License

/**
 * Tests, whether a page or redirect with the given title exists.
 * Trying to retrieve a page that does not exist in Wikipedia throws an exception.
 * You may catch the exception or use this test, depending on your task.
 * @param title The title of the page./*w w w  .  j a va  2  s.c  om*/
 * @return True, if a page or redirect with that title exits, false otherwise.
 */
public boolean existsPage(String title) {

    if (title == null || title.length() == 0) {
        return false;
    }

    Title t;
    try {
        t = new Title(title);
    } catch (WikiTitleParsingException e) {
        return false;
    }
    String encodedTitle = t.getWikiStyleTitle();

    Session session = this.__getHibernateSession();
    session.beginTransaction();
    Object returnValue = session
            .createNativeQuery("select p.id from PageMapLine as p where p.name = :pName COLLATE utf8_bin")
            .setParameter("pName", encodedTitle, StringType.INSTANCE).uniqueResult();
    session.getTransaction().commit();

    return returnValue != null;
}

From source file:debop4k.data.orm.hibernate.usertypes.encrypt.EncryptedStringUserType.java

License:Apache License

@Override
public int[] sqlTypes() {
    return new int[] { StringType.INSTANCE.sqlType() };
}

From source file:debop4k.data.orm.hibernate.usertypes.encrypt.EncryptedStringUserType.java

License:Apache License

@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
        throws HibernateException, SQLException {
    String encryptedText = (String) StringType.INSTANCE.nullSafeGet(rs, names[0], session, owner);
    return decrypt(encryptedText);
}

From source file:debop4k.data.orm.hibernate.usertypes.encrypt.EncryptedStringUserType.java

License:Apache License

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
        throws HibernateException, SQLException {
    if (value == null) {
        StringType.INSTANCE.nullSafeSet(st, null, index, session);
    } else {/* w w w.j  a v a  2  s  .  com*/
        StringType.INSTANCE.nullSafeSet(st, encrypt((String) value), index, session);
    }
}

From source file:debop4k.data.orm.hibernate.usertypes.encrypt.HashStringUserType.java

License:Apache License

@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
        throws HibernateException, SQLException {
    return StringType.INSTANCE.nullSafeGet(rs, names[0], session, owner);
}

From source file:debop4k.data.orm.hibernate.usertypes.encrypt.HashStringUserType.java

License:Apache License

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
        throws HibernateException, SQLException {
    String digestedText = (value == null) ? null : digest((String) value);
    StringType.INSTANCE.nullSafeSet(st, digestedText, index, session);
}