Package com.codeforces.graygoose.web.page.data

Source Code of com.codeforces.graygoose.web.page.data.RuleAlertRelationsDataPage

package com.codeforces.graygoose.web.page.data;

import com.codeforces.graygoose.dao.RuleAlertRelationDao;
import com.codeforces.graygoose.misc.Noty;
import com.codeforces.graygoose.model.RuleAlertRelation;
import com.google.inject.Inject;
import org.nocturne.annotation.Action;
import org.nocturne.annotation.Parameter;
import org.nocturne.annotation.Validate;
import org.nocturne.link.Link;
import org.nocturne.validation.IntegerValidator;
import org.nocturne.validation.RequiredValidator;

import java.util.List;

@Link("data/ruleAlertRelations")
public class RuleAlertRelationsDataPage extends DataPage {
    @Parameter
    private Long alertId;

    @Parameter
    private Long ruleId;

    @Parameter
    private long maxConsecutiveFailCount;

    @Inject
    private RuleAlertRelationDao ruleAlertRelationDao;

    private List<RuleAlertRelation> ruleAlertRelations;

    @Override
    public void initializeAction() {
        super.initializeAction();

        if (alertId != null && ruleId != null) {
            ruleAlertRelations = ruleAlertRelationDao.findAllByRuleAndAlert(ruleId, alertId);
        }
    }

    @Validate("attachAlert")
    public boolean validateAttachAlert() {
        addValidator("ruleId", new RequiredValidator());

        addValidator("alertId", new RequiredValidator());

        addValidator("maxConsecutiveFailCount", new RequiredValidator());
        addValidator("maxConsecutiveFailCount", new IntegerValidator(1, 100));

        return runValidationAndPrintErrors();
    }

    @Action("attachAlert")
    public void onAttachAlert() {
        try {
            if (ruleAlertRelations == null || ruleAlertRelations.isEmpty()) {
                RuleAlertRelation ruleAlertRelation = RuleAlertRelation.newRuleAlertRelation(ruleId, alertId, maxConsecutiveFailCount);
                ruleAlertRelationDao.insert(ruleAlertRelation);

                put("success", true);
                addMessage($("Alert has been attached."), Noty.Type.SUCCESS);
            } else {
                put("error", String.format($("A relation between alert #%d and rule #%d is already exists."), alertId, ruleId));
            }
        } catch (Exception e) {
            put("error", e.getMessage());
        }

        printTemplateMapAsStringsUsingJson("success", "error");
    }

    @Action("detachAlert")
    public void onDetachAlert() {
        try {
            if (ruleAlertRelations != null && !ruleAlertRelations.isEmpty()) {
                for (RuleAlertRelation ruleAlertRelation : ruleAlertRelations) {
                    ruleAlertRelationDao.delete(ruleAlertRelation);
                }

                put("success", true);
            } else {
                put("error", $("No such alert is attached to this rule."));
            }
        } catch (Exception e) {
            put("error", e.getMessage());
        }

        printTemplateMapAsStringsUsingJson("success", "error");
    }
}
TOP

Related Classes of com.codeforces.graygoose.web.page.data.RuleAlertRelationsDataPage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.