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();
}
}
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");
}
}
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!");
}
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;
}
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;
}
Aggregations