コード例 #1
0
  /**
   * Imports the entities
   *
   * @return the SUCCESS result
   */
  public String importCSV() throws Exception {
    File file = this.getUpload();
    CsvListReader reader = new CsvListReader(new FileReader(file), CsvPreference.EXCEL_PREFERENCE);
    int failedNum = 0;
    int successfulNum = 0;
    try {
      final String[] header = reader.getCSVHeader(true);

      List<String> line = new ArrayList<String>();
      Map<String, String> failedMsg = new HashMap<String, String>();
      while ((line = reader.read()) != null) {

        Map<String, String> row = new HashMap<String, String>();
        for (int i = 0; i < line.size(); i++) {
          row.put(header[i], line.get(i));
        }

        TargetList targetList = new TargetList();
        try {
          String id = row.get(getText("entity.id.label"));
          if (!CommonUtil.isNullOrEmpty(id)) {
            targetList.setId(Integer.parseInt(id));
          }
          targetList.setName(CommonUtil.fromNullToEmpty(row.get(getText("entity.name.label"))));
          targetList.setDescription(
              CommonUtil.fromNullToEmpty(row.get(getText("entity.description.label"))));
          targetList.setNotes(CommonUtil.fromNullToEmpty(row.get(getText("entity.notes.label"))));
          String assignedToID = row.get(getText("entity.assigned_to_id.label"));
          if (CommonUtil.isNullOrEmpty(assignedToID)) {
            targetList.setAssigned_to(null);
          } else {
            User assignedTo = userService.getEntityById(User.class, Integer.parseInt(assignedToID));
            targetList.setAssigned_to(assignedTo);
          }
          baseService.makePersistent(targetList);
          successfulNum++;
        } catch (Exception e) {
          failedNum++;
          String Name = CommonUtil.fromNullToEmpty(targetList.getName());
          failedMsg.put(Name, e.getMessage());
        }
      }

      this.setFailedMsg(failedMsg);
      this.setFailedNum(failedNum);
      this.setSuccessfulNum(successfulNum);
      this.setTotalNum(successfulNum + failedNum);
    } finally {
      reader.close();
    }
    return SUCCESS;
  }
コード例 #2
0
 protected void run(String[] args) {
   logger.debug("run(String[]) - start"); // $NON-NLS-1$
   TreeMap<String, double[]> statii = StatiiMeteoModis06.statii;
   for (String s1 : statii.keySet()) {
     File out = Paths.get(path, s1 + "-f1.txt").toFile();
     try {
       DirectoryStream<Path> files = Files.newDirectoryStream(Paths.get(path), new V2filter(s1));
       CsvListWriter wrt = new CsvListWriter(new FileWriter(out), CsvPreference.EXCEL_PREFERENCE);
       String[] split = Mod04C06TO.toLongHeader().split("\\t");
       logger.debug("Coloana 27 este : {}", split[27]);
       wrt.writeHeader(split);
       boolean first = false;
       for (Path path : files) {
         System.out.println(path.toString());
         CsvListReader red =
             new CsvListReader(new FileReader(path.toFile()), CsvPreference.TAB_PREFERENCE);
         red.getHeader(true);
         List<String> citit;
         while ((citit = red.read()) != null) {
           for (int i = 0; i < citit.size(); i++) {
             String sv = citit.get(i);
             if ("null".equals(sv)) {
               citit.set(i, "");
             }
           }
           if (StringUtils.isNotEmpty(citit.get(27))) {
             wrt.write(citit);
           }
         }
         red.close();
       }
       wrt.flush();
       wrt.close();
     } catch (IOException e) {
       logger.error("run(String[])", e); // $NON-NLS-1$
       e.printStackTrace();
     }
   }
   logger.debug("run(String[]) - end"); // $NON-NLS-1$
 }
コード例 #3
0
ファイル: PluginReader.java プロジェクト: rayleyva/hydra
 /**
  * Reads all of the resource files under the specified path and returns a sequence of String
  * arrays. The resource files are specified as comma-separated values. Each row in a resource file
  * is one element of the resulting list.
  *
  * @param suffix filename suffix. Matches against files with this suffix in their filename.
  * @return list of String[] elements that represent CSV values
  */
 public static List<String[]> readProperties(String suffix) {
   List<String[]> result = new ArrayList<>();
   try {
     String locationPattern = "classpath*:plugins/*" + suffix;
     Resource[] resources = resolver.getResources(locationPattern);
     for (Resource resource : resources) {
       log.debug("readProperties found resource {}", resource);
       String filename = resource.getDescription();
       InputStreamReader reader = new InputStreamReader(resource.getInputStream());
       try (CsvListReader csvParser = new CsvListReader(reader, csvParserOptions)) {
         List<String> next;
         while ((next = csvParser.read()) != null) {
           result.add(next.toArray(new String[next.size()]));
         }
       } catch (SuperCsvException e) {
         log.warn("In " + filename + ", " + e);
       }
     }
   } catch (IOException e) {
     log.error(e.toString());
   }
   return result;
 }
コード例 #4
0
ファイル: SFData.java プロジェクト: huiwq1990/BookProgram
  private static DataPoint[] loadDataFromFile(String filename, String[] attrNames) {
    List<DataPoint> allData = new ArrayList<DataPoint>();
    CsvListReader csvReader = null;
    try {
      csvReader =
          new CsvListReader(
              new BufferedReader(new FileReader(filename)), CsvPreference.EXCEL_PREFERENCE);

      // Load all available headers from CSV file
      String[] csvHeaders = csvReader.getCSVHeader(true);

      // Map attribute names to field IDs from CSV file using header names
      int[] attrFieldIndexes = new int[attrNames.length];
      for (int i = 0; i < attrFieldIndexes.length; i++) {
        String header = attrNames[i];
        int csvHeaderId = -1;
        for (int j = 0; j < csvHeaders.length; j++) {
          if (header.equalsIgnoreCase(csvHeaders[j])) {
            csvHeaderId = j;
            break;
          }
        }
        // If there is no header found it means we have wrong attribute
        // name or wrong file.
        if (csvHeaderId == -1) {
          throw new IllegalStateException(
              "Attribute name mismatch. "
                  + "Failed to find attribute name: '"
                  + header
                  + "' among cvs file headers. All available headers: "
                  + Arrays.toString(csvHeaders));
        } else {
          attrFieldIndexes[i] = csvHeaderId;
        }
      }

      // Read file and include only selected attributes
      List<String> line = null;
      while ((line = csvReader.read()) != null) {
        try {
          String label = line.get(0);
          Attribute[] attributes = new Attribute[attrNames.length];
          for (int i = 0, n = attrNames.length; i < n; i++) {
            int attrFieldIndex = attrFieldIndexes[i];
            String value = line.get(attrFieldIndex);
            attributes[i] = new Attribute(attrNames[i], Double.valueOf(value));
          }
          DataPoint dataPoint = new DataPoint(label, attributes);
          allData.add(dataPoint);
        } catch (Exception e) {
          throw new RuntimeException("Error while reading line: '" + line + "'", e);
        }
      }

    } catch (IOException e) {
      throw new RuntimeException(
          "Error while reading SF data from csv file: '" + filename + "'. ", e);
    } finally {
      try {
        if (csvReader != null) {
          csvReader.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    System.out.println("From file: " + filename);
    System.out.println("Using attribute names: " + Arrays.toString(attrNames));
    System.out.println("Loaded " + allData.size() + " data points.");

    return allData.toArray(new DataPoint[allData.size()]);
  }