Example #1
0
  @SuppressWarnings("unchecked")
  @Override
  protected void parseFile(String[] argFileString, IMetadataFieldSet argDefaultFields)
      throws InvalidDendroFileException {
    defaults = (TrimsToTridasDefaults) argDefaultFields;
    // TODO run the 'metadata' through the defaults, as
    // the user may have specified his own metadata
    // Extract 'metadata' ;-)
    String userid = argFileString[0].trim();
    SafeIntYear startYear = null;

    try {
      startYear = new SafeIntYear(argFileString[2].trim());
    } catch (NumberFormatException e) {
    }

    // Extract data
    ArrayList<TridasValue> ringWidthValues = new ArrayList<TridasValue>();
    for (int i = 3; i < argFileString.length; i++) {
      TridasValue v = new TridasValue();

      if (argFileString[i].startsWith(" ")) {
        if (argFileString[i].trim().equals("999")) {
          break;
        }
        v.setValue(argFileString[i].trim());
        ringWidthValues.add(v);
        log.debug("value = " + String.valueOf(argFileString[i]));
      } else {
        throw new InvalidDendroFileException(I18n.getText("trims.expectingSpace"), i);
      }
    }

    // Now build up our measurementSeries

    TridasMeasurementSeries series = defaults.getMeasurementSeriesWithDefaults();
    TridasUnit units = new TridasUnit();

    // Set units to 1/100th mm. Is this always the case?
    units.setNormalTridas(NormalTridasUnit.HUNDREDTH_MM);

    // Build identifier for series
    TridasIdentifier seriesId = new ObjectFactory().createTridasIdentifier();
    seriesId.setValue(UUID.randomUUID().toString());
    seriesId.setDomain(
        defaults.getDefaultValue(TridasMandatoryField.IDENTIFIER_DOMAIN).getStringValue());

    // Build interpretation group for series
    TridasInterpretation interp = new TridasInterpretation();
    interp.setFirstYear(startYear.toTridasYear(DatingSuffix.AD));
    interp.setLastYear(startYear.add(ringWidthValues.size() - 1).toTridasYear(DatingSuffix.AD));

    // Add values to nested value(s) tags
    TridasValues valuesGroup = new TridasValues();
    valuesGroup.setValues(ringWidthValues);
    valuesGroup.setUnit(units);
    GenericDefaultValue<TridasVariable> variable =
        (GenericDefaultValue<TridasVariable>)
            defaults.getDefaultValue(TridasMandatoryField.MEASUREMENTSERIES_VARIABLE);
    valuesGroup.setVariable(variable.getValue());
    ArrayList<TridasValues> valuesGroupList = new ArrayList<TridasValues>();
    valuesGroupList.add(valuesGroup);

    // Add all the data to the series
    series.setValues(valuesGroupList);
    series.setInterpretation(interp);
    series.setIdentifier(seriesId);
    series.setLastModifiedTimestamp(DateUtils.getTodaysDateTime());
    series.setDendrochronologist(userid);

    // Add series to our list
    mseriesList.add(series);
  }
  /**
   * Parse the specified legacy data file for series
   *
   * @param file
   * @param fileType
   */
  private void parseFile(File file, AbstractDendroFormat format) {

    ArrayList<Sample> sampleList = new ArrayList<Sample>();

    // Create a reader based on the file type supplied
    AbstractDendroFileReader reader = TridasIO.getFileReaderFromFormat(format);
    if (reader == null) {
      Alert.error(containerFrame, "Error", "Unknown file type");
      return;
    }

    // Try and load the file
    try {
      reader.loadFile(file.getAbsolutePath());
    } catch (IOException e) {
      Alert.errorLoading(file.getAbsolutePath(), e);
      return;
    } catch (InvalidDendroFileException e) {
      Alert.error(
          containerFrame,
          "Error",
          "The selected file is not a valid "
              + format.getShortName()
              + " file.\nPlease check and try again");
      return;
    } catch (NullPointerException e) {
      Alert.error(containerFrame, "Invalid File", e.getLocalizedMessage());
    }

    TridasTridas tc = reader.getTridasContainer();
    log.debug("Project count: " + tc.getProjects().size());

    Boolean hideWarningsFlag = false;
    for (TridasProject p : tc.getProjects()) {
      for (TridasObject o : p.getObjects()) {

        for (TridasElement e : TridasUtils.getElementList(o)) {
          log.debug("Element count: " + o.getElements().size());

          for (TridasSample s : e.getSamples()) {
            for (TridasRadius r : s.getRadiuses()) {
              for (TridasMeasurementSeries ms : r.getMeasurementSeries()) {
                Sample sample =
                    EditorFactory.createSampleFromSeries(ms, e, file, format, hideWarningsFlag);
                if (sample == null) {
                  hideWarningsFlag = true;
                } else {
                  sampleList.add(sample);
                }
              }
            }
          }
        }
      }

      for (TridasDerivedSeries ds : p.getDerivedSeries()) {
        Sample sample =
            EditorFactory.createSampleFromSeries(ds, null, file, format, hideWarningsFlag);

        if (sample == null) {
          hideWarningsFlag = true;
        } else {
          sampleList.add(sample);
        }
      }
    }

    Boolean unitsSet = false;
    for (ITridasSeries ser : getSeries(sampleList)) {
      for (TridasValues tv : ser.getValues()) {
        if (tv.isSetUnit()) {
          if (tv.getUnit().isSetNormalTridas()) {
            unitsSet = true;
          }
        }
      }
    }

    if (unitsSet == false && sampleList.size() > 0 && unitsIfNotSpecified == null) {
      Object[] possibilities = {"1/1000th mm", "1/100th mm", "1/50th mm", "1/20th mm", "1/10th mm"};
      Object s =
          JOptionPane.showInputDialog(
              containerFrame,
              "One or more series has no units defined.\n" + "Please specify units below:",
              "Set Units",
              JOptionPane.PLAIN_MESSAGE,
              null,
              possibilities,
              "1/1000th mm");

      if (s.equals("1/1000th mm")) {
        unitsIfNotSpecified = NormalTridasUnit.MICROMETRES;
      } else if (s.equals("1/100th mm")) {
        unitsIfNotSpecified = NormalTridasUnit.HUNDREDTH_MM;
      } else if (s.equals("1/50th mm")) {
        unitsIfNotSpecified = NormalTridasUnit.FIFTIETH_MM;
      } else if (s.equals("1/20th mm")) {
        unitsIfNotSpecified = NormalTridasUnit.TWENTIETH_MM;
      } else if (s.equals("1/10th mm")) {
        unitsIfNotSpecified = NormalTridasUnit.TENTH_MM;
      } else {
        Alert.error(containerFrame, "Error", "Invalid measurement units specified");
        return;
      }
    }

    for (Sample sample : sampleList) {
      ITridasSeries series = sample.getSeries();

      try {
        for (int i = 0; i < series.getValues().size(); i++) {
          TridasValues tv = series.getValues().get(i);

          if (tv.isSetUnit()) {
            if (!tv.getUnit().isSetNormalTridas()) {
              tv.getUnit().setNormalTridas(unitsIfNotSpecified);
            }
          } else {
            TridasUnit unit = new TridasUnit();
            unit.setNormalTridas(unitsIfNotSpecified);
            tv.setUnit(unit);
            tv.setUnitless(null);
          }

          tv = UnitUtils.convertTridasValues(NormalTridasUnit.MICROMETRES, tv, true);

          TridasUnit unit = new TridasUnit();
          unit.setNormalTridas(NormalTridasUnit.MICROMETRES);
          tv.setUnit(unit);
          series.getValues().set(i, tv);
        }

      } catch (NumberFormatException e) {
        Alert.error("Error", "One or more data values are not numbers.");
        return;
      } catch (ConversionWarningException e) {
        Alert.error("Error", "Error converting units");
        return;
      }
    }

    for (Sample s : sampleList) {
      SeriesIdentity id = new SeriesIdentity(file, format, s);

      model.addItem(id);
    }
  }