Search in sources :

Example 1 with ReportDao

use of com.serotonin.m2m2.reports.ReportDao in project ma-modules-public by infiniteautomation.

the class ReportsDwr method saveReport.

@DwrPermission(custom = ReportPermissionDefinition.PERMISSION)
public ProcessResult saveReport(int id, String name, String xid, List<ReportPointVO> points, String template, int includeEvents, boolean includeUserComments, int dateRangeType, int relativeDateType, int previousPeriodCount, int previousPeriodType, int pastPeriodCount, int pastPeriodType, boolean fromNone, int fromYear, int fromMonth, int fromDay, int fromHour, int fromMinute, boolean toNone, int toYear, int toMonth, int toDay, int toHour, int toMinute, boolean schedule, int schedulePeriod, int runDelayMinutes, String scheduleCron, boolean email, boolean includeData, boolean zipData, List<RecipientListEntryBean> recipients) {
    ProcessResult response = new ProcessResult();
    // Basic validation
    validateData(response, name, points, dateRangeType, relativeDateType, previousPeriodCount, pastPeriodCount);
    // Validate XID
    if (StringUtils.isBlank(xid))
        response.addContextualMessage("xid", "validate.required");
    else if (StringValidation.isLengthGreaterThan(xid, 50))
        response.addMessage("xid", new TranslatableMessage("validate.notLongerThan", 50));
    else if (!ReportDao.instance.isXidUnique(xid, id))
        response.addContextualMessage("xid", "validate.xidUsed");
    if (schedule) {
        if (schedulePeriod == ReportVO.SCHEDULE_CRON) {
            // Check the cron pattern.
            try {
                new CronTimerTrigger(scheduleCron);
            } catch (Exception e) {
                response.addContextualMessage("scheduleCron", "reports.validate.cron", e.getMessage());
            }
        } else {
            if (runDelayMinutes < 0)
                response.addContextualMessage("runDelayMinutes", "reports.validate.lessThan0");
            else if (runDelayMinutes > 59)
                response.addContextualMessage("runDelayMinutes", "reports.validate.greaterThan59");
        }
    }
    if (email && recipients.isEmpty())
        response.addContextualMessage("recipients", "reports.validate.needRecip");
    if (response.getHasMessages())
        return response;
    User user = Common.getUser();
    ReportDao reportDao = ReportDao.instance;
    ReportVO report;
    if (id == Common.NEW_ID) {
        report = new ReportVO();
        report.setUserId(user.getId());
    } else
        report = reportDao.getReport(id);
    ReportCommon.ensureReportPermission(user, report);
    // Update the new values.
    report.setXid(xid);
    report.setName(name);
    report.setPoints(points);
    report.setTemplate(template);
    report.setIncludeEvents(includeEvents);
    report.setIncludeUserComments(includeUserComments);
    report.setDateRangeType(dateRangeType);
    report.setRelativeDateType(relativeDateType);
    report.setPreviousPeriodCount(previousPeriodCount);
    report.setPreviousPeriodType(previousPeriodType);
    report.setPastPeriodCount(pastPeriodCount);
    report.setPastPeriodType(pastPeriodType);
    report.setFromNone(fromNone);
    report.setFromYear(fromYear);
    report.setFromMonth(fromMonth);
    report.setFromDay(fromDay);
    report.setFromHour(fromHour);
    report.setFromMinute(fromMinute);
    report.setToNone(toNone);
    report.setToYear(toYear);
    report.setToMonth(toMonth);
    report.setToDay(toDay);
    report.setToHour(toHour);
    report.setToMinute(toMinute);
    report.setSchedule(schedule);
    report.setSchedulePeriod(schedulePeriod);
    report.setRunDelayMinutes(runDelayMinutes);
    report.setScheduleCron(scheduleCron);
    report.setEmail(email);
    report.setIncludeData(includeData);
    report.setZipData(zipData);
    report.setRecipients(recipients);
    // Save the report
    reportDao.saveReport(report);
    // Conditionally schedule the report.
    String host = "";
    WebContext webContext = WebContextFactory.get();
    int port;
    if (webContext != null) {
        HttpServletRequest req = webContext.getHttpServletRequest();
        host = req.getServerName();
        port = req.getLocalPort();
    } else {
        port = Common.envProps.getInt("web.port", 8080);
    }
    ReportJob.scheduleReportJob(host, port, report);
    // Send back the report id in case this was new.
    response.addData("reportId", report.getId());
    response.addData("report", report);
    return response;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) User(com.serotonin.m2m2.vo.User) WebContext(org.directwebremoting.WebContext) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ReportVO(com.serotonin.m2m2.reports.vo.ReportVO) ReportDao(com.serotonin.m2m2.reports.ReportDao) InvalidArgumentException(com.serotonin.InvalidArgumentException) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 2 with ReportDao

use of com.serotonin.m2m2.reports.ReportDao in project ma-modules-public by infiniteautomation.

the class ReportsDwr method deleteReport.

@DwrPermission(custom = ReportPermissionDefinition.PERMISSION)
public void deleteReport(int id) {
    ReportDao reportDao = ReportDao.instance;
    ReportVO report = reportDao.getReport(id);
    if (report != null) {
        ReportCommon.ensureReportPermission(Common.getUser(), report);
        ReportJob.unscheduleReportJob(report);
        reportDao.deleteReport(id);
    }
}
Also used : ReportVO(com.serotonin.m2m2.reports.vo.ReportVO) ReportDao(com.serotonin.m2m2.reports.ReportDao) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 3 with ReportDao

use of com.serotonin.m2m2.reports.ReportDao in project ma-modules-public by infiniteautomation.

the class ReportsDwr method deleteReportInstance.

@DwrPermission(custom = ReportPermissionDefinition.PERMISSION)
public List<ReportInstance> deleteReportInstance(int instanceId) {
    User user = Common.getUser();
    ReportDao reportDao = ReportDao.instance;
    reportDao.deleteReportInstance(instanceId, user.getId());
    return getReportInstances(user);
}
Also used : User(com.serotonin.m2m2.vo.User) ReportDao(com.serotonin.m2m2.reports.ReportDao) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 4 with ReportDao

use of com.serotonin.m2m2.reports.ReportDao in project ma-modules-public by infiniteautomation.

the class ReportExportBase method execute.

protected void execute(HttpServletRequest request, HttpServletResponse response, int content) throws IOException {
    // Get the report instance id
    int instanceId = Integer.parseInt(request.getParameter("instanceId"));
    // Get the report instance
    ReportDao reportDao = ReportDao.instance;
    ReportInstance instance = reportDao.getReportInstance(instanceId);
    // Ensure the user is allowed access.
    ReportCommon.ensureReportInstancePermission(Common.getUser(request), instance);
    // Stream the content.
    response.setContentType("text/csv");
    Translations translations = Common.getTranslations();
    if (content == CONTENT_REPORT) {
        ExportCsvStreamer creator = new ExportCsvStreamer(request.getServerName(), request.getLocalPort(), response.getWriter(), translations);
        if (Common.databaseProxy.getNoSQLProxy() == null)
            reportDao.reportInstanceDataSQL(instanceId, creator);
        else
            reportDao.reportInstanceDataNoSQL(instanceId, creator);
    } else if (content == CONTENT_EVENTS)
        new EventCsvStreamer(response.getWriter(), reportDao.getReportInstanceEvents(instanceId), translations);
    else if (content == CONTENT_COMMENTS)
        new UserCommentCsvStreamer(response.getWriter(), reportDao.getReportInstanceUserComments(instanceId), translations);
}
Also used : UserCommentCsvStreamer(com.serotonin.m2m2.reports.web.UserCommentCsvStreamer) EventCsvStreamer(com.serotonin.m2m2.vo.export.EventCsvStreamer) ExportCsvStreamer(com.serotonin.m2m2.vo.export.ExportCsvStreamer) ReportDao(com.serotonin.m2m2.reports.ReportDao) ReportInstance(com.serotonin.m2m2.reports.vo.ReportInstance) Translations(com.serotonin.m2m2.i18n.Translations)

Example 5 with ReportDao

use of com.serotonin.m2m2.reports.ReportDao in project ma-modules-public by infiniteautomation.

the class M2MReportImportDwr method migrate.

@DwrPermission(admin = true)
public ProcessResult migrate(String driverClassname, String connectionUrl, String username, String password) {
    ProcessResult result = new ProcessResult();
    // Validate the connection
    try {
        DriverManager.registerDriver((Driver) Class.forName(driverClassname).newInstance());
        Connection connection = DriverManager.getConnection(connectionUrl, username, password);
        connection.setAutoCommit(false);
        // Test the connection.
        DatabaseMetaData md = connection.getMetaData();
        String productName = md.getDatabaseProductName();
        DatabaseType type = DatabaseType.DERBY;
        if (productName.equalsIgnoreCase("mysql")) {
            type = DatabaseType.MYSQL;
        } else if (productName.equalsIgnoreCase("Apache Derby")) {
            type = DatabaseType.DERBY;
        } else if (productName.contains("Microsoft")) {
            type = DatabaseType.MSSQL;
        } else if (productName.equalsIgnoreCase("h2")) {
            type = DatabaseType.H2;
        } else if (productName.equalsIgnoreCase("postgressql")) {
            type = DatabaseType.MYSQL;
        }
        // Get the reports
        M2MReportDao dao = new M2MReportDao(connection, type);
        List<M2MReportVO> legacyReports = dao.getReports();
        // Convert the reports to our VOs
        List<ReportVO> reports = new ArrayList<ReportVO>();
        for (M2MReportVO legacyReport : legacyReports) {
            try {
                ReportVO report = legacyReport.convert(dao);
                report.validate(result);
                reports.add(report);
            } catch (Exception e) {
                result.addGenericMessage("common.default", e.getMessage());
            }
        }
        if (!result.getHasMessages()) {
            ReportDao reportDao = ReportDao.instance;
            for (ReportVO vo : reports) {
                vo.validate(result);
                reportDao.saveReport(vo);
            }
        }
        result.addData("reports", reports);
    } catch (Exception e) {
        result.addContextualMessage("connectionUrl", "common.default", e.getMessage());
        return result;
    }
    return result;
}
Also used : DatabaseType(com.serotonin.m2m2.db.DatabaseProxy.DatabaseType) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ReportVO(com.serotonin.m2m2.reports.vo.ReportVO) DatabaseMetaData(java.sql.DatabaseMetaData) ReportDao(com.serotonin.m2m2.reports.ReportDao) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Aggregations

ReportDao (com.serotonin.m2m2.reports.ReportDao)10 ReportVO (com.serotonin.m2m2.reports.vo.ReportVO)6 User (com.serotonin.m2m2.vo.User)5 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)5 ArrayList (java.util.ArrayList)4 InvalidArgumentException (com.serotonin.InvalidArgumentException)3 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 HashMap (java.util.HashMap)3 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 Translations (com.serotonin.m2m2.i18n.Translations)2 ReportInstance (com.serotonin.m2m2.reports.vo.ReportInstance)2 EventCsvStreamer (com.serotonin.m2m2.vo.export.EventCsvStreamer)2 File (java.io.File)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1 LicenseViolatedException (com.serotonin.m2m2.LicenseViolatedException)1 DatabaseType (com.serotonin.m2m2.db.DatabaseProxy.DatabaseType)1 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)1 UserDao (com.serotonin.m2m2.db.dao.UserDao)1 MessageFormatDirective (com.serotonin.m2m2.email.MessageFormatDirective)1 PostEmailRunnable (com.serotonin.m2m2.email.PostEmailRunnable)1