Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoadFile extends Action {
- static final String PATH_PARAM = "Path";
- static final String ROW_SUCCESS = "SUCCESS";
- static final String ROW_FAIL = "FAIL";
- static final String ROW_UNCHECKED = "UNCHECKED";
- static final String ROW_STATUS = "IS_CHECKED";
- static final String DATA_TABLE = "data";
- public static String getValueByParam(String param, LinkedHashMap<String, String> inputParams) throws ResultException {
- InputParamsHandler handler = new InputParamsHandler(inputParams);
- String path;
- try {
- path = handler.getRequiredString(param);
- } finally {
- handler.check();
- }
- return path;
- }
- private ArrayList<Map<String, String>> getTableFromFile(CsvReader cr) throws IOException {
- ArrayList<Map<String, String>> table = new ArrayList<>();
- cr.readHeaders();
- String[] headers = cr.getHeaders();
- while (cr.readRecord()) {
- Map<String, String> row = new HashMap<>();
- for (int i = 0; i < headers.length; i++) {
- row.put(headers[i], cr.get(i));
- }
- row.put(ROW_STATUS, ROW_UNCHECKED);
- table.add(row);
- }
- return table;
- }
- @Override
- protected Result run(StepContext stepContext, MatrixContext matrixContext, GlobalContext globalContext) {
- String path;
- try {
- path = getValueByParam(PATH_PARAM, inputParams);
- } catch (ResultException e) {
- return DefaultResult.failedWithException(e);
- }
- CsvReader cr;
- try {
- cr = new CsvReader(new FileReader(new File(path)));
- } catch (FileNotFoundException e) {
- return DefaultResult.failedWithComment(String.format("File by path \"%s\" was not found", path));
- }
- ArrayList<Map<String, String>> table;
- try {
- table = getTableFromFile(cr);
- } catch (IOException e) {
- return DefaultResult.failedWithException(e);
- }
- finally {
- if (cr != null) {
- cr.close();
- }
- }
- matrixContext.setContext(DATA_TABLE, table);
- return null;
- }
- }
Add Comment
Please, Sign In to add comment