Search in sources :

Example 6 with ShouldNeverHappenException

use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.

the class H2InMemoryDatabaseProxy method doInConnection.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.db.DatabaseProxy#doInConnection(com.serotonin.db.spring.ConnectionCallbackVoid)
     */
@Override
public void doInConnection(ConnectionCallbackVoid callback) {
    DataSource dataSource = getDataSource();
    Connection conn = null;
    try {
        conn = DataSourceUtils.getConnection(dataSource);
        conn.setAutoCommit(false);
        callback.doInConnection(conn);
        conn.commit();
    } catch (Exception e) {
        try {
            if (conn != null)
                conn.rollback();
        } catch (SQLException e1) {
            throw new RuntimeException(e1);
        }
        // Wrap and rethrow
        throw new ShouldNeverHappenException(e);
    } finally {
        if (conn != null)
            DataSourceUtils.releaseConnection(conn, dataSource);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) SQLException(java.sql.SQLException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) DataSource(javax.sql.DataSource) JdbcDataSource(org.h2.jdbcx.JdbcDataSource)

Example 7 with ShouldNeverHappenException

use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.

the class FileUploadController method parseFile.

/**
 * Parse the Import Files
 * @param input
 * @param model
 * @param translations
 */
protected void parseFile(InputStream input, Map<String, Object> model, Translations translations, HttpServletRequest request) {
    // Get the filename
    String filename = (String) model.get("filename");
    SpreadsheetEmporter emporter;
    if (filename == null)
        return;
    else {
        if (filename.toLowerCase().endsWith(".xls"))
            emporter = new SpreadsheetEmporter(FileType.XLS);
        else if (filename.toLowerCase().endsWith(".xlsx"))
            emporter = new SpreadsheetEmporter(FileType.XLSX);
        else
            return;
    }
    // Switch on the type
    String dataType = (String) model.get("dataType");
    if (dataType != null) {
        if (dataType.equals("pointValue")) {
            // List the sheets and create sheet emporters for each
            for (Sheet sheet : emporter.listSheets(input)) emporter.doImport(input, new PointValueEmporter(sheet.getSheetName()));
        } else
            throw new ShouldNeverHappenException("Unsupported data.");
    }
    model.put("hasImportErrors", emporter.hasErrors());
    // Get the messages
    if (emporter.hasErrors()) {
        List<String> errorMessages = new ArrayList<String>();
        for (TranslatableMessage msg : emporter.getErrorMessages()) {
            errorMessages.add(msg.translate(translations));
        }
        model.put("errorMessages", errorMessages);
    }
    model.put("rowsImported", emporter.getRowsAdded());
    model.put("rowsDeleted", emporter.getRowsDeleted());
    model.put("rowsWithErrors", emporter.getRowErrors());
}
Also used : PointValueEmporter(com.serotonin.m2m2.rt.dataImage.PointValueEmporter) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ArrayList(java.util.ArrayList) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) SpreadsheetEmporter(com.serotonin.m2m2.vo.emport.SpreadsheetEmporter) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 8 with ShouldNeverHappenException

use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.

the class AbstractPointWrapper method getCalendar.

private GregorianCalendar getCalendar() {
    long time = getTime();
    if (time == -1)
        throw new ShouldNeverHappenException("No timestamp for point value.");
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTimeInMillis(time);
    return gc;
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) GregorianCalendar(java.util.GregorianCalendar)

Example 9 with ShouldNeverHappenException

use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.

the class BigIntegerColumnQueryAppender method appendSQL.

/* (non-Javadoc)
	 * @see com.infiniteautomation.mango.db.query.SQLColumnQueryAppender#appendSQL(com.infiniteautomation.mango.db.query.SQLQueryColumn, java.lang.StringBuilder, java.lang.StringBuilder, java.util.List, java.util.List)
	 */
@Override
public void appendSQL(SQLQueryColumn column, StringBuilder selectSql, StringBuilder countSql, List<Object> selectArgs, List<Object> columnArgs, ComparisonEnum comparison) {
    // Catchall for null comparisons
    if ((columnArgs.size() == 1) && (columnArgs.get(0) == null)) {
        if (comparison == ComparisonEnum.IS)
            super.appendSQL(column.getName(), IS_SQL, selectSql, countSql);
        else if (comparison == ComparisonEnum.IS_NOT)
            super.appendSQL(column.getName(), IS_NOT_SQL, selectSql, countSql);
        else
            super.appendSQL(column.getName(), IS_SQL, selectSql, countSql);
        selectArgs.add(null);
        return;
    }
    if (columnArgs.size() == 1) {
        // Are we a Date String?
        if (columnArgs.get(0) instanceof String) {
            SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
            try {
                Date d = sdf.parse((String) columnArgs.get(0));
                columnArgs.set(0, Long.toString(d.getTime()));
            } catch (ParseException e) {
                throw new ShouldNeverHappenException(e);
            }
        }
    }
    super.appendSQL(column, selectSql, countSql, selectArgs, columnArgs, comparison);
}
Also used : ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 10 with ShouldNeverHappenException

use of com.serotonin.ShouldNeverHappenException in project ma-core-public by infiniteautomation.

the class ExpandTimePeriodAdjuster method adjustInto.

/* (non-Javadoc)
     * @see java.time.temporal.TemporalAdjuster#adjustInto(java.time.temporal.Temporal)
     */
@Override
public Temporal adjustInto(Temporal temporal) {
    ZonedDateTime zdt = startTime;
    ZonedDateTime adjustTime = (ZonedDateTime) temporal;
    while (zdt.isBefore(adjustTime)) {
        // adjusted in that manner if appropriate, and none of the time advances should alter a lesser field harmfully.
        switch(periodType) {
            case TimePeriods.MILLISECONDS:
                zdt = zdt.plus(periods, ChronoUnit.MILLIS);
                break;
            case TimePeriods.SECONDS:
                zdt = zdt.plus(periods, ChronoUnit.SECONDS);
                break;
            case TimePeriods.MINUTES:
                zdt = zdt.plus(periods, ChronoUnit.MINUTES);
                break;
            case TimePeriods.HOURS:
                zdt = zdt.plus(periods, ChronoUnit.HOURS);
                break;
            case TimePeriods.DAYS:
                zdt = zdt.plus(periods, ChronoUnit.DAYS);
                break;
            case TimePeriods.WEEKS:
                // Don't set the DoW since this may be governed by last year, and shouldn't change by adding weeks
                zdt = zdt.plus(periods, ChronoUnit.WEEKS);
                break;
            case TimePeriods.MONTHS:
                zdt = zdt.plus(periods, ChronoUnit.MONTHS);
                break;
            case TimePeriods.YEARS:
                zdt = zdt.plus(periods, ChronoUnit.YEARS);
                break;
            default:
                throw new ShouldNeverHappenException("Unsupported time period: " + periodType);
        }
    }
    return zdt;
}
Also used : ZonedDateTime(java.time.ZonedDateTime) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException)

Aggregations

ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)83 IOException (java.io.IOException)20 ArrayList (java.util.ArrayList)10 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)9 SQLException (java.sql.SQLException)9 ParseException (java.text.ParseException)8 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)6 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)6 FileNotFoundException (java.io.FileNotFoundException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 ResultSet (java.sql.ResultSet)5 Statement (java.sql.Statement)5 JsonException (com.serotonin.json.JsonException)4 JsonWriter (com.serotonin.json.JsonWriter)4 ImageValue (com.serotonin.m2m2.rt.dataImage.types.ImageValue)4 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)4 CronTimerTrigger (com.serotonin.timer.CronTimerTrigger)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 StringWriter (java.io.StringWriter)4 HashMap (java.util.HashMap)4