Search in sources :

Example 1 with CodeFragment

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

the class DownloadServlet method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String id = request.getParameter("id");
    CodeFragment c = codeFragmentManager.getCodeFragment(id);
    String fileName = c.getUser() + "." + c.getLanguage();
    String txt = c.getText();
    response.setContentType("text/plain");
    response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    response.setContentLength(txt.length());
    ServletOutputStream out = response.getOutputStream();
    try {
        out.print(txt);
    } catch (Exception e) {
        log.log(Level.WARNING, "Error processing file for download", e);
    } finally {
        out.close();
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) CodeFragment(org.jboss.weld.examples.pastecode.model.CodeFragment) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 2 with CodeFragment

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

the class CodeFragmentPrinter method print.

public void print(@Observes TimerEvent event) {
    // Print the code fragments retrieved in the last minute to the log
    if (logger.getLog().size() > 0) {
        log.info("These code fragments pasted in the last minute: ");
        for (CodeFragment fragment : logger.getLog()) {
            log.info(fragment.toString());
        }
        log.info("-----------------------------------------------------");
        logger.clearLog();
    } else {
        log.info("No fragments pasted in the last minute");
    }
}
Also used : CodeFragment(org.jboss.weld.examples.pastecode.model.CodeFragment)

Example 3 with CodeFragment

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

the class PopulateDatabase method startup.

@PostConstruct
public void startup() {
    try {
        String fileContent = readFileData(DATA_FILE_NAME);
        StringTokenizer st = new StringTokenizer(fileContent, "'");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        while (st.countTokens() > 1) {
            CodeFragment c = new CodeFragment();
            st.nextToken();
            c.setDatetime(formatter.parse(st.nextToken()));
            st.nextToken();
            c.setLanguage(Language.valueOf(st.nextToken()));
            st.nextToken();
            st.nextToken();
            st.nextToken();
            c.setUser(st.nextToken());
            st.nextToken();
            c.setText(st.nextToken());
            // Manual TX control, commit each record independently
            entityManager.persist(c);
        }
    } catch (Exception e) {
        log.log(Level.WARNING, "Unable to read all records from " + DATA_FILE_NAME + " file", e);
    }
    log.info("Successfully imported data!");
}
Also used : StringTokenizer(java.util.StringTokenizer) CodeFragment(org.jboss.weld.examples.pastecode.model.CodeFragment) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) PostConstruct(javax.annotation.PostConstruct)

Example 4 with CodeFragment

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

the class CodeFragmentManagerImpl method getRecentCodeFragments.

@Produces
@Named
public List<CodeFragment> getRecentCodeFragments() {
    Query query = entityManager.createQuery("SELECT c FROM CodeFragment c WHERE c.hash=null ORDER BY datetime DESC ");
    query.setMaxResults(MAX_RECENT_FRAGMENTS);
    @SuppressWarnings("unchecked") List<CodeFragment> codes = query.getResultList();
    return codes;
}
Also used : CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) CodeFragment(org.jboss.weld.examples.pastecode.model.CodeFragment) Named(javax.inject.Named) Produces(javax.enterprise.inject.Produces)

Example 5 with CodeFragment

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

the class CodeFragmentManagerImpl method searchCodeFragments.

public List<CodeFragment> searchCodeFragments(CodeFragment codeFragment, int page, Paginator paginator) {
    // Create a criteria, which we then populate using our prototype code fragment
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<CodeFragment> criteria = builder.createQuery(CodeFragment.class);
    Root<CodeFragment> root = criteria.from(CodeFragment.class);
    List<Predicate> predicates = new ArrayList<Predicate>();
    // Only search public code fragements
    predicates.add(builder.isNull(root.get(CodeFragment_.hash)));
    if (!isEmpty(codeFragment.getUser())) {
        predicates.add(builder.equal(root.get(CodeFragment_.user), codeFragment.getUser().toLowerCase().trim()));
    }
    if (codeFragment.getLanguage() != null) {
        predicates.add(builder.equal(root.get(CodeFragment_.language), codeFragment.getLanguage()));
    }
    if (!isEmpty(codeFragment.getText())) {
        predicates.add(builder.like(root.get(CodeFragment_.text), "%" + codeFragment.getText().toLowerCase().trim() + "%"));
    }
    if (codeFragment.getDatetime() != null) {
        predicates.add(builder.between(root.get(CodeFragment_.datetime), codeFragment.getDatetime(), new Date()));
    }
    criteria.where(predicates.toArray(new Predicate[0])).orderBy(builder.desc(root.get(CodeFragment_.datetime)));
    Query q = entityManager.createQuery(criteria);
    int totalRecords = q.getResultList().size();
    // Compute the page
    q.setFirstResult(page * PAGE_SIZE);
    q.setMaxResults(PAGE_SIZE);
    @SuppressWarnings("unchecked") List<CodeFragment> codes = q.getResultList();
    paginator.setPage(page);
    paginator.setRecordsCount(totalRecords);
    paginator.setPagesCount(totalRecords / PAGE_SIZE);
    return codes;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) CodeFragment(org.jboss.weld.examples.pastecode.model.CodeFragment) ArrayList(java.util.ArrayList) Date(java.util.Date) Predicate(javax.persistence.criteria.Predicate)

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