Search in sources :

Example 6 with ReferencePersistence

use of cn.edu.zju.acm.onlinejudge.persistence.ReferencePersistence in project zoj by licheng.

the class ShowReferenceAction method execute.

/**
     * ShowRankListAction.
     * 
     * @param mapping
     *            action mapping
     * @param form
     *            action form
     * @param request
     *            http servlet request
     * @param response
     *            http servlet response
     * 
     * @return action forward instance
     * 
     * @throws Exception
     *             any errors happened
     */
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, ContextAdapter context) throws Exception {
    HttpServletResponse response = context.getResponse();
    if (context.getUserSecurity() == context.getDefaultUserSecurity()) {
        response.sendError(404);
        return null;
    }
    try {
        if (!context.getUserSecurity().canAdminContest(context.getContest().getId())) {
            response.sendError(404);
            return null;
        }
    } catch (Exception e) {
    }
    long id = Utility.parseLong(context.getRequest().getParameter("referenceId"));
    String problemCode = context.getRequest().getParameter("code");
    boolean download = "true".equalsIgnoreCase(context.getRequest().getParameter("download"));
    ReferencePersistence referencePersistence = PersistenceManager.getInstance().getReferencePersistence();
    Reference ref = referencePersistence.getReference(id);
    if (ref == null) {
        response.sendError(404);
        return null;
    }
    response.setContentType("text/plain");
    if (download) {
        response.setHeader("Content-disposition", "attachment; filename=" + problemCode + "_" + ref.getReferenceType().getDescription() + ".txt");
        response.getOutputStream().write(ref.getContent());
    } else {
        int length = ref.getContent().length;
        if (length > 100 * 1024) {
            response.getOutputStream().write(ref.getContent(), 0, 100 * 1024);
            response.getOutputStream().write("\n\n...\n".getBytes());
        } else {
            response.getOutputStream().write(ref.getContent());
        }
    }
    response.getOutputStream().close();
    return null;
}
Also used : ReferencePersistence(cn.edu.zju.acm.onlinejudge.persistence.ReferencePersistence) Reference(cn.edu.zju.acm.onlinejudge.bean.Reference) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 7 with ReferencePersistence

use of cn.edu.zju.acm.onlinejudge.persistence.ReferencePersistence in project zoj by licheng.

the class ExportProblemsAction method zipReference.

private void zipReference(Problem p, String fileName, ReferenceType type, ZipOutputStream out) throws Exception {
    ReferencePersistence referencePersistence = PersistenceManager.getInstance().getReferencePersistence();
    List<Reference> refs = referencePersistence.getProblemReferences(p.getId(), type);
    if (refs.size() == 0) {
        return;
    }
    Reference ref = refs.get(0);
    if (type == ReferenceType.CHECKER_SOURCE || type == ReferenceType.JUDGE_SOLUTION) {
        String contentType = ref.getContentType();
        if (contentType == null) {
            contentType = "cc";
        }
        fileName += "." + contentType;
    }
    out.putNextEntry(new ZipEntry(p.getCode() + "/" + fileName));
    byte[] data = ref.getContent();
    out.write(data);
    out.closeEntry();
}
Also used : ReferencePersistence(cn.edu.zju.acm.onlinejudge.persistence.ReferencePersistence) Reference(cn.edu.zju.acm.onlinejudge.bean.Reference) ZipEntry(java.util.zip.ZipEntry)

Example 8 with ReferencePersistence

use of cn.edu.zju.acm.onlinejudge.persistence.ReferencePersistence in project zoj by licheng.

the class ContestManager method getDescription.

public byte[] getDescription(long problemId) throws PersistenceException {
    Object key = new Long(problemId);
    synchronized (this.descriptionCache) {
        byte[] text = this.descriptionCache.get(key);
        if (text == null) {
            ReferencePersistence referencePersistence = PersistenceManager.getInstance().getReferencePersistence();
            List<Reference> ref = referencePersistence.getProblemReferences(problemId, ReferenceType.DESCRIPTION);
            if (ref.size() > 0) {
                text = ref.get(0).getContent();
            } else {
                text = new byte[0];
            }
            this.descriptionCache.put(key, text);
        }
        return text;
    }
}
Also used : ReferencePersistence(cn.edu.zju.acm.onlinejudge.persistence.ReferencePersistence) Reference(cn.edu.zju.acm.onlinejudge.bean.Reference)

Aggregations

Reference (cn.edu.zju.acm.onlinejudge.bean.Reference)8 ReferencePersistence (cn.edu.zju.acm.onlinejudge.persistence.ReferencePersistence)8 ZipEntry (java.util.zip.ZipEntry)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1