public MisoPrintService mapRow(ResultSet rs, int rowNum) throws SQLException { try { MisoPrintService printService = new DefaultPrintService(); printService.setServiceId(rs.getLong("serviceId")); printService.setName(rs.getString("serviceName")); printService.setEnabled(rs.getBoolean("enabled")); printService.setPrintServiceFor( Class.forName(rs.getString("printServiceFor")).asSubclass(Barcodable.class)); PrintContext pc = printManager.getPrintContext(rs.getString("contextName")); JSONObject contextFields = JSONObject.fromObject(rs.getString("contextFields")); PrintServiceUtils.mapJSONToContextFields(contextFields, pc); pc.getLabelFactory().setSecurityManager(securityManager); pc.getLabelFactory().setFilesManager(misoFilesManager); printService.setPrintContext(pc); return printService; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return null; }
@Override public long save(MisoPrintService printService) throws IOException { MapSqlParameterSource params = new MapSqlParameterSource(); params .addValue("serviceName", printService.getName()) .addValue("contextName", printService.getPrintContext().getName()) .addValue("enabled", printService.isEnabled()) .addValue("printServiceFor", printService.getPrintServiceFor().getName()); try { JSONObject contextFields = PrintServiceUtils.mapContextFieldsToJSON(printService.getPrintContext()); String contextFieldJSON = contextFields.toString(); params.addValue("contextFields", contextFieldJSON); } catch (IllegalAccessException e) { e.printStackTrace(); } if (printService.getServiceId() == -1) { SimpleJdbcInsert insert = new SimpleJdbcInsert(template) .withTableName(TABLE_NAME) .usingGeneratedKeyColumns("serviceId"); Number newId = insert.executeAndReturnKey(params); printService.setServiceId(newId.longValue()); } else { params.addValue("serviceId", printService.getServiceId()); NamedParameterJdbcTemplate namedTemplate = new NamedParameterJdbcTemplate(template); namedTemplate.update(PRINT_SERVICE_UPDATE, params); } return printService.getServiceId(); }