Search in sources :

Example 6 with CodeFragment

use of org.jboss.weld.examples.pastecode.model.CodeFragment in project core by weld.

the class CodeFragmentManagerImpl method getCodeFragment.

public CodeFragment getCodeFragment(String id) {
    // If it's not an integer, it's a hash!
    if (!isInteger(id)) {
        Query query = entityManager.createQuery("SELECT c FROM CodeFragment c WHERE hash = :hash");
        query.setParameter("hash", id);
        @SuppressWarnings("unchecked") List<CodeFragment> fragments = query.getResultList();
        if (fragments.size() == 0) {
            throw new RuntimeException("No such fragment!");
        } else {
            return fragments.get(0);
        }
    } else {
        CodeFragment c = entityManager.find(CodeFragment.class, Integer.parseInt(id));
        if (c == null) {
            throw new RuntimeException("No such fragment!");
        }
        // If no hash was set, then this is not a private fragment, return it!
        if (c.getHash() == null) {
            return c;
        } else {
            throw new RuntimeException("Access denied!");
        }
    }
}
Also used : CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) CodeFragment(org.jboss.weld.examples.pastecode.model.CodeFragment)

Aggregations

CodeFragment (org.jboss.weld.examples.pastecode.model.CodeFragment)6 Query (javax.persistence.Query)3 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)3 IOException (java.io.IOException)2 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 StringTokenizer (java.util.StringTokenizer)1 PostConstruct (javax.annotation.PostConstruct)1 Produces (javax.enterprise.inject.Produces)1 Named (javax.inject.Named)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1 ServletException (javax.servlet.ServletException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1