Exemplo n.º 1
0
 private ModelEMFResource getEMFResource(ModelId modelId) {
   try {
     Node folderNode = session.getNode(modelId.getFullPath());
     Node fileNode = (Node) folderNode.getNodes().next();
     Node fileItem = (Node) fileNode.getPrimaryItem();
     InputStream is = fileItem.getProperty("jcr:data").getBinary().getStream();
     return (ModelEMFResource) ModelParserFactory.getParser(fileNode.getName()).parse(is);
   } catch (Exception e) {
     throw new FatalModelRepositoryException("Something went wrong accessing the repository", e);
   }
 }
Exemplo n.º 2
0
  @Override
  public UploadModelResult upload(byte[] content, String fileName) {
    try {
      ModelResource resource =
          ModelParserFactory.getParser(fileName).parse(new ByteArrayInputStream(content));

      for (IModelValidator validator : validators) {
        validator.validate(resource);
      }
      return UploadModelResult.valid(
          createUploadHandle(resource.getId(), content, fileName), resource);
    } catch (ValidationException validationException) {
      return UploadModelResult.invalid(validationException);
    }
  }
Exemplo n.º 3
0
  @Override
  public byte[] getModelContent(ModelId modelId, ContentType contentType) {
    try {
      Node folderNode = session.getNode(modelId.getFullPath());
      Node fileNode = (Node) folderNode.getNodes().next();
      Node fileItem = (Node) fileNode.getPrimaryItem();
      InputStream is = fileItem.getProperty("jcr:data").getBinary().getStream();

      if (contentType == ContentType.XMI) {
        ModelEMFResource resource =
            (ModelEMFResource) ModelParserFactory.getParser(fileNode.getName()).parse(is);
        return resource.toXMI();
      } else {
        return IOUtils.toByteArray(is);
      }
    } catch (PathNotFoundException e) {
      throw new ModelNotFoundException("Could not find model with the given model id", e);
    } catch (Exception e) {
      throw new FatalModelRepositoryException("Something went wrong accessing the repository", e);
    }
  }
Exemplo n.º 4
0
  @Override
  public void checkin(String handleId) {
    InputStream contentAsStream;
    final File uploadedFile;
    try {
      uploadedFile = loadUploadedFile(handleId);
      logger.debug("Found temporary file for handleId : " + uploadedFile.getName());
      contentAsStream = new FileInputStream(uploadedFile);
    } catch (FileNotFoundException e1) {
      throw new ModelNotFoundException(
          "Could not find uploaded model with the specified handle", e1);
    }

    final ModelResource resource =
        ModelParserFactory.getParser(uploadedFile.getName()).parse(contentAsStream);

    try {
      governance.start(
          new ModelUploadHandle(
              resource.getId(),
              resource.getModelType(),
              IOUtils.toByteArray(new FileInputStream(uploadedFile))),
          new IGovernanceCallback() {

            @Override
            public void processUploadResult(GovernanceResult uploadResult) {
              if (uploadResult.isApproved()) {
                try {
                  logger.debug(
                      "Checkin for "
                          + uploadResult.getHandle().getModelId().getPrettyFormat()
                          + " was approved. Proceeding with checkin...");
                  Node folderNode = createNodeForModelId(uploadResult.getHandle().getModelId());
                  Node fileNode =
                      folderNode.addNode(
                          uploadResult.getHandle().getModelId().getName()
                              + uploadResult.getHandle().getModelType().getExtension(),
                          "nt:file");
                  fileNode.addMixin("vorto:meta");
                  fileNode.addMixin("mix:referenceable");
                  Node contentNode = fileNode.addNode("jcr:content", "nt:resource");
                  Binary binary =
                      session
                          .getValueFactory()
                          .createBinary(
                              new ByteArrayInputStream(uploadResult.getHandle().getContent()));
                  contentNode.setProperty("jcr:data", binary);
                  session.save();
                  logger.debug("Checkin successful.");
                  FileUtils.deleteQuietly(uploadedFile);
                } catch (RepositoryException e) {
                  throw new RuntimeException(e);
                }
              } else {
                logger.warn(resource.getId().getPrettyFormat() + " was not approved for checkin");
              }
            }
          });
    } catch (IOException e) {
      throw new RuntimeException("Something went wrong when reading the model content", e);
    }
  }