[Demonstration 1]

[Buggy Method]
public static void processEvents() {
    EmailTemplates htmlTemplates;
    EmailTemplates textTemplates;
    try {
        htmlTemplates = parseMessage(BackendConfig.getTextSubscriptionEmailHtmlFile().readToString(Charsets.UTF_8));
    } catch (IOException e1) {
        log.error("Error!", e1);
        return;
    }
    try {
        textTemplates = parseMessage(BackendConfig.getTextSubscriptionEmailPlainTextFile().readToString(Charsets.UTF_8));
    } catch (IOException e1) {
        log.error("Error!", e1);
        return;
    }
    try {
        SecurityManager secMan = BackendConfig.getServiceManager().getSecurityManager();
        EntityManager entityMan = BackendConfig.getEntityManagerFactory().createEntityManager();
        // get all users with events
        //
        TypedQuery<User> userQuery = entityMan.createNamedQuery(SubscriptionMatchEvent.GetAllUsersWithEventsQuery, User.class);
        List<User> users = userQuery.getResultList();
        for (User u : users) {
            // check user is activated and has valid email
            //
            if (!u.isActive() || u.getEmail() == null || u.getEmail().length() < 6)
                continue;
            HashMap<Long, SubscriptionBatch> subscriptionResultMap = new HashMap<>();
            // get all subscriptions events
            //
            TypedQuery<SubscriptionMatchEvent> eventQuery = entityMan.createNamedQuery(SubscriptionMatchEvent.GetEventsByUserIdQuery, SubscriptionMatchEvent.class);
            eventQuery.setParameter(SubscriptionMatchEvent.UserIdQueryParameter, u.getId());
            List<SubscriptionMatchEvent> events = eventQuery.getResultList();
            for (SubscriptionMatchEvent event : events) {
                // skip submission if user may not "see" it
                //
                if (!secMan.mayUserReadSubmission(event.getSubmission(), u))
                    continue;
                long id = event.getSubscription().getId();
                // batch results for each subscription
                //
                SubscriptionBatch batchData = subscriptionResultMap.get(id);
                if (batchData == null) {
                    // init stuffs
                    //
                    batchData = new SubscriptionBatch();
                    String attribute = event.getSubscription().getAttribute();
                    String pattern = event.getSubscription().getPattern();
                    batchData.htmlSummary = htmlTemplates.subscription;
                    batchData.textSummary = textTemplates.subscription;
                    // <h4>{ATTRIBUTE} matches {PATTERN}:</h4>
                    batchData.htmlSummary = batchData.htmlSummary.replaceAll(AttributePlaceHolderRx, attribute);
                    batchData.textSummary = batchData.textSummary.replaceAll(AttributePlaceHolderRx, attribute);
                    // <h4>{ATTRIBUTE} matches {PATTERN}:</h4>
                    batchData.htmlSummary = batchData.htmlSummary.replaceAll(PatternPlaceHolderRx, pattern);
                    batchData.textSummary = batchData.textSummary.replaceAll(PatternPlaceHolderRx, pattern);
                    batchData.htmlList = new StringBuilder();
                    batchData.textList = new StringBuilder();
                    subscriptionResultMap.put(id, batchData);
                }
                // <b>{TITLE}</b> (<a href="https://www.ebi.ac.uk/biostudies/studies/{ACCNO}">https://www.ebi.ac.uk/biostudies/studies/{ACCNO}</a>)<br/>
                String accession = event.getSubmission().getAccNo();
                String title = event.getSubmission().getTitle();
                String htmlTesultLine = htmlTemplates.result;
                String textTesultLine = textTemplates.result;
                htmlTesultLine = htmlTesultLine.replaceAll(BackendConfig.AccNoPlaceHolderRx, accession);
                htmlTesultLine = htmlTesultLine.replaceAll(BackendConfig.TitlePlaceHolderRx, title);
                textTesultLine = textTesultLine.replaceAll(BackendConfig.AccNoPlaceHolderRx, accession);
                textTesultLine = textTesultLine.replaceAll(BackendConfig.TitlePlaceHolderRx, title);
                batchData.htmlList.append(htmlTesultLine);
                batchData.textList.append(textTesultLine);
            }
            Collection<SubscriptionBatch> resultSet = subscriptionResultMap.values();
            // check user has got any matches
            //
            if (resultSet.size() == 0)
                continue;
            String htmlMessage = htmlTemplates.mainBody;
            String textMessage = textTemplates.mainBody;
            // assemble everything together
            //
            if (u.getFullName() != null) {
                htmlMessage = htmlMessage.replaceAll(BackendConfig.UserNamePlaceHolderRx, u.getFullName());
                textMessage = textMessage.replaceAll(BackendConfig.UserNamePlaceHolderRx, u.getFullName());
            }
            String htmlTotalBatch = "";
            String textTotalBatch = "";
            for (SubscriptionBatch batch : subscriptionResultMap.values()) {
                batch.htmlSummary = batch.htmlSummary.replaceAll(ResultsPlaceHolderRx, batch.htmlList.toString());
                batch.textSummary = batch.textSummary.replaceAll(ResultsPlaceHolderRx, batch.textList.toString());
                htmlTotalBatch = htmlTotalBatch + batch.htmlSummary;
                textTotalBatch = textTotalBatch + batch.textSummary;
            }
            htmlMessage = htmlMessage.replaceAll(SubscriptionPlaceHolderRx, htmlTotalBatch);
            textMessage = textMessage.replaceAll(SubscriptionPlaceHolderRx, textTotalBatch);
            BackendConfig.getServiceManager().getEmailService().sendMultipartEmail(u.getEmail(), BackendConfig.getSubscriptionEmailSubject(), textMessage, htmlMessage);
            // remove events
            //
            Query deleteQuery = entityMan.createNamedQuery(SubscriptionMatchEvent.DeleteEventsByUserIdQuery);
            deleteQuery.setParameter(SubscriptionMatchEvent.UserIdQueryParameter, u.getId());
            EntityTransaction transation = entityMan.getTransaction();
            transation.begin();
            deleteQuery.executeUpdate();
            transation.commit();
        }
    } catch (Exception ex) {
        log.error("Error!", ex);
    }
}

[Buggy Hunk]
SecurityManager secMan = BackendConfig.getServiceManager().getSecurityManager();

[Patch]
uk.ac.ebi.biostd.webapp.server.mng.security.SecurityManager secMan = BackendConfig.getServiceManager().getSecurityManager();

[Demonstration 2]

[Buggy Method]
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    String path = request.getRequestURI();
    if (!path.startsWith("/screen/LoginScreen.xhtml")) {
        String loginUrl = request.getContextPath() + "/screen/LoginScreen.xhtml";
        HttpSession session = request.getSession(false);
        Person user = (session != null) ? (Person) session.getAttribute("user") : null;
        if (user == null) {
            response.sendRedirect(loginUrl);
        }
    }
    filterChain.doFilter(servletRequest, servletResponse);
}

[Buggy Hunk]
HttpSession session = request.getSession(false);

[Patch]
HttpSession session = request.getSession();

[Demonstration 3]

[Buggy Method]
public boolean remove(Integer id) {
    BasicDBObject doc = new BasicDBObject();
    doc.put("sessionId", id);
    DBObject result = devices.findOne(doc);
    if (result != null) {
        devices.remove(doc);
        log.log(Level.INFO, "Removed device " + id + " from the database.");
        return true;
    } else {
        log.log(Level.INFO, "Cannot delete device as it is not in the collection");
        return false;
    }
}

[Buggy Hunk]
log.log(Level.INFO, "Cannot delete device as it is not in the collection");

[Patch]
log.log(Level.INFO, "Cannot delete device " + id + " as it is not in the collection");