Exemple #1
0
 private boolean sampleStartsWithAllowedPrefix(Set<Prefix> allowedPrefix, Sample s) {
   boolean result = false;
   for (Prefix prefix : allowedPrefix) {
     if (s.getSampleNumber().getValue().startsWith(prefix.getPrefix())) {
       return true;
     }
     ;
   }
   return result;
 }
Exemple #2
0
  public ResponseEntity<?> mint(Samples samples, boolean test, Principal user) {

    boolean isXMLValid = true;

    Schema schema = null;

    // 2. VALIDATE XML ====================================================

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {
      URL schemaUrl = new URL(this.IGSN_CSIRO_XSD_URL);
      schema = sf.newSchema(schemaUrl);
    } catch (SAXException e) {
      e.printStackTrace();
      return new ResponseEntity<String>(
          "Failure retriving schema : " + e.getLocalizedMessage(), HttpStatus.BAD_REQUEST);
    } catch (MalformedURLException e) {
      log.error("URL malformed for schema location. Recheck config.properties file again.");
      return new ResponseEntity<String>(
          "Failure retriving schema : " + e.getLocalizedMessage(), HttpStatus.BAD_REQUEST);
    }

    try {
      JAXBContext jc = JAXBContext.newInstance(Samples.class);
      Marshaller marshaller = jc.createMarshaller();
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
      marshaller.setSchema(schema);
      marshaller.marshal(samples, new DefaultHandler());
    } catch (JAXBException e) {
      e.printStackTrace();
      isXMLValid = false;
      return new ResponseEntity<String>(
          "XML validation failed : " + e.getLocalizedMessage(), HttpStatus.BAD_REQUEST);
    }

    // 3. VALIDATE SUBNAMESPACE BASED ON USER NAME
    // =============================
    String usr = null;
    List<MintEventLog> mintEventLogs = new ArrayList<MintEventLog>();

    if (isXMLValid) {
      usr = user.getName();
      Set<Prefix> allowedPrefix = prefixEntityService.searchByUser(usr);

      for (Sample s : samples.getSample()) {
        MintEventLog mintEventLog = new MintEventLog(s.getSampleNumber().getValue());
        if (sampleStartsWithAllowedPrefix(allowedPrefix, s)) {
          if (s.getLogElement().getEvent().equals(EventType.SUBMITTED)
              || s.getLogElement().getEvent().equals(EventType.UPDATED)) {
            try {
              SimpleDateFormat metadataDateFormat =
                  new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ssXXX");
              String igsn =
                  this.mintService.createRegistryXML(
                      s.getSampleNumber().getValue(),
                      s.getLandingPage(),
                      metadataDateFormat.format(new Date()),
                      test,
                      s.getLogElement().getEvent().value());
              mintEventLog.setMintLog(MintErrorCode.MINT_SUCCESS, null);
              mintEventLog.setHandle("http://hdl.handle.net/" + igsn);
            } catch (Exception e) {
              mintEventLog.setMintLog(MintErrorCode.MINT_FAILURE, e.getMessage());
              mintEventLog.setDatabaseLog(DatabaseErrorCode.NOT_ATTEMPTED, "");
              mintEventLogs.add(mintEventLog);
              continue;
            }
          }

          try {
            if (test) {
              sampleEntityService.testInsertSample(s, usr);
            } else if (s.getLogElement().getEvent().equals(EventType.SUBMITTED)) {
              sampleEntityService.insertSample(s, usr);
            } else if (s.getLogElement().getEvent().equals(EventType.DESTROYED)) {
              sampleEntityService.destroySample(s);
            } else if (s.getLogElement().getEvent().equals(EventType.DEPRECATED)) {
              sampleEntityService.deprecateSample(s);
            } else if (s.getLogElement().getEvent().equals(EventType.UPDATED)) {
              sampleEntityService.updateSample(s, usr);
            }
            mintEventLog.setDatabaseLog(DatabaseErrorCode.UPDATE_SUCCESS, null);
            mintEventLogs.add(mintEventLog);
          } catch (Exception e) {
            if (e instanceof javax.persistence.PersistenceException
                && e.getCause().getCause().getMessage().contains("duplicate key value")) {
              mintEventLog.setDatabaseLog(DatabaseErrorCode.DUPLICATE_KEY, e.getMessage());
              mintEventLogs.add(mintEventLog);

            } else {
              mintEventLog.setDatabaseLog(DatabaseErrorCode.UPDATE_ERROR, e.getMessage());
              mintEventLogs.add(mintEventLog);
            }
          }

        } else {
          mintEventLog.setMintLog(
              MintErrorCode.PREFIX_UNREGISTERED,
              "The prefix is not registered to the user:"******"");
          mintEventLogs.add(mintEventLog);
        }
      }
    }

    return new ResponseEntity<List<MintEventLog>>(mintEventLogs, HttpStatus.OK);
  }