Search in sources :

Example 1 with HomeBean

use of org.ff4j.web.bean.HomeBean in project ff4j by ff4j.

the class HomeController method get.

/**
 * {@inheritDoc}
 */
public void get(HttpServletRequest req, HttpServletResponse res, WebContext ctx) throws Exception {
    ctx.setVariable(KEY_TITLE, "Home");
    ctx.setVariable("today", Calendar.getInstance());
    HomeBean hb = new HomeBean();
    try {
        hb = new HomeBean(ff4j);
    } catch (RuntimeException e) {
        LOGGER.error("Cannot read store", e);
        ctx.setVariable("msgType", ERROR);
        ctx.setVariable("msgInfo", "Cannot read store:" + e.getMessage());
    }
    ctx.setVariable("homebean", hb);
}
Also used : HomeBean(org.ff4j.web.bean.HomeBean)

Example 2 with HomeBean

use of org.ff4j.web.bean.HomeBean in project ff4j by ff4j.

the class HomeController method post.

/**
 * {@inheritDoc}
 */
public void post(HttpServletRequest req, HttpServletResponse res, WebContext ctx) throws Exception {
    String msg = null;
    String msgType = "success";
    String operation = req.getParameter(WebConstants.OPERATION);
    // Upload XML File
    if (ServletFileUpload.isMultipartContent(req)) {
        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(req);
        for (FileItem item : items) {
            if (item.isFormField()) {
                if (OPERATION.equalsIgnoreCase(item.getFieldName())) {
                    LOGGER.info("Processing action : " + item.getString());
                }
            } else if (FLIPFILE.equalsIgnoreCase(item.getFieldName())) {
                String filename = FilenameUtils.getName(item.getName());
                if (filename.toLowerCase().endsWith("xml")) {
                    try {
                        importFile(getFf4j(), item.getInputStream());
                        msg = "The file <b>" + filename + "</b> has been successfully imported";
                    } catch (RuntimeException re) {
                        msgType = ERROR;
                        msg = "Cannot Import XML:" + re.getMessage();
                        break;
                    }
                } else {
                    msgType = ERROR;
                    msg = "Invalid FILE, must be CSV, XML or PROPERTIES files";
                }
            }
        }
        ctx.setVariable("msgType", msgType);
        ctx.setVariable("msgInfo", msg);
        get(req, res, ctx);
    } else if (WebConstants.OP_CREATE_SCHEMA.equalsIgnoreCase(operation)) {
        try {
            getFf4j().createSchema();
            msg = "Schema has been created in DB (if required).";
            ctx.setVariable("msgType", msgType);
            ctx.setVariable("msgInfo", msg);
            get(req, res, ctx);
        } catch (RuntimeException re) {
            ctx.setVariable("msgType", ERROR);
            ctx.setVariable("msgInfo", "Cannot create Schema:" + re.getMessage());
            ctx.setVariable(KEY_TITLE, "Home");
            ctx.setVariable("today", Calendar.getInstance());
            ctx.setVariable("homebean", new HomeBean());
        }
    } else if (WebConstants.OP_CLEAR_CACHE.equalsIgnoreCase(operation)) {
        FF4jCacheProxy cacheProxy = getFf4j().getCacheProxy();
        if (cacheProxy != null) {
            cacheProxy.getCacheManager().clearFeatures();
            cacheProxy.getCacheManager().clearProperties();
            msg = "Cache Cleared!";
        } else {
            msg = "Cache not present: it cannot be cleared!";
        }
        ctx.setVariable("msgType", msgType);
        ctx.setVariable("msgInfo", msg);
        get(req, res, ctx);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) HomeBean(org.ff4j.web.bean.HomeBean) FF4jCacheProxy(org.ff4j.cache.FF4jCacheProxy) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory)

Aggregations

HomeBean (org.ff4j.web.bean.HomeBean)2 FileItem (org.apache.commons.fileupload.FileItem)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 FF4jCacheProxy (org.ff4j.cache.FF4jCacheProxy)1