Search in sources :

Example 1 with ModelIdHelper

use of org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper in project vorto by eclipse.

the class JcrModelRepository method getModelImage.

@Override
public byte[] getModelImage(ModelId modelId) {
    try {
        ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
        Node folderNode = session.getNode(modelIdHelper.getFullPath());
        if (folderNode.hasNode("img.png")) {
            Node imageNode = folderNode.getNode("img.png");
            Node fileItem = (Node) imageNode.getPrimaryItem();
            InputStream is = fileItem.getProperty("jcr:data").getBinary().getStream();
            return IOUtils.toByteArray(is);
        }
    } catch (PathNotFoundException e) {
        throw new ModelNotFoundException("Problem when trying to retrieve image for model", e);
    } catch (RepositoryException e) {
        throw new FatalModelRepositoryException("Something severe went wrong when accessing the repository", e);
    } catch (IOException e) {
        throw new FatalModelRepositoryException("Something severe went wrong when trying to read image content", e);
    }
    return null;
}
Also used : ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) Node(javax.jcr.Node) ModelNotFoundException(org.eclipse.vorto.repository.api.exception.ModelNotFoundException) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) IOException(java.io.IOException)

Example 2 with ModelIdHelper

use of org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper in project vorto by eclipse.

the class JcrModelRepository method getEMFResource.

private ModelEMFResource getEMFResource(ModelId modelId) {
    try {
        ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
        Node folderNode = session.getNode(modelIdHelper.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);
    }
}
Also used : ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) Node(javax.jcr.Node) ModelReferentialIntegrityException(org.eclipse.vorto.repository.core.ModelReferentialIntegrityException) ModelNotFoundException(org.eclipse.vorto.repository.api.exception.ModelNotFoundException) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException)

Example 3 with ModelIdHelper

use of org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper in project vorto by eclipse.

the class ModelSequencer method execute.

@Override
public boolean execute(Property inputProperty, Node outputNode, Context context) throws Exception {
    Binary binaryValue = inputProperty.getBinary();
    CheckArg.isNotNull(binaryValue, "binary");
    ModelInfo modelResource = ModelParserFactory.getParser(outputNode.getPath()).parse(binaryValue.getStream());
    outputNode.setProperty("vorto:description", modelResource.getDescription() != null ? modelResource.getDescription() : "");
    outputNode.setProperty("vorto:type", modelResource.getType().name());
    outputNode.setProperty("vorto:displayname", modelResource.getDisplayName());
    outputNode.setProperty("vorto:version", modelResource.getId().getVersion());
    outputNode.setProperty("vorto:namespace", modelResource.getId().getNamespace());
    outputNode.setProperty("vorto:name", modelResource.getId().getName());
    ModelReferencesHelper referencesHelper = new ModelReferencesHelper(modelResource.getReferences());
    if (referencesHelper.hasReferences()) {
        List<Value> references = new ArrayList<Value>();
        for (ModelId modelId : referencesHelper.getReferences()) {
            ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
            Node referencedFolder = outputNode.getSession().getNode(modelIdHelper.getFullPath());
            Node reference = referencedFolder.getNodes().nextNode();
            references.add(context.valueFactory().createValue(reference));
        }
        outputNode.setProperty("vorto:references", references.toArray(new Value[references.size()]));
    }
    return true;
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) ModelReferencesHelper(org.eclipse.vorto.repository.core.impl.utils.ModelReferencesHelper) Node(javax.jcr.Node) Value(javax.jcr.Value) ArrayList(java.util.ArrayList) Binary(javax.jcr.Binary) ModelId(org.eclipse.vorto.repository.api.ModelId)

Example 4 with ModelIdHelper

use of org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper in project vorto by eclipse.

the class JcrModelRepository method getById.

@Override
public ModelInfo getById(ModelId modelId) {
    try {
        ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
        Node folderNode = session.getNode(modelIdHelper.getFullPath());
        Node modelFileNode = folderNode.getNodes("*.type | *.fbmodel | *.infomodel | *.mapping").nextNode();
        ModelInfo modelResource = createModelResource(modelFileNode);
        if (modelResource.getType() == ModelType.InformationModel) {
            NodeIterator imageNodeIterator = folderNode.getNodes("img.png*");
            if (imageNodeIterator.hasNext()) {
                modelResource.setHasImage(true);
            }
        }
        return modelResource;
    } catch (PathNotFoundException e) {
        return null;
    } catch (RepositoryException e) {
        throw new RuntimeException("Retrieving Content of Resource: Problem accessing repository", e);
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) Node(javax.jcr.Node) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException)

Example 5 with ModelIdHelper

use of org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper in project vorto by eclipse.

the class JcrModelRepository method createNodeForModelId.

private Node createNodeForModelId(ModelId id) throws RepositoryException {
    ModelIdHelper modelIdHelper = new ModelIdHelper(id);
    StringBuilder pathBuilder = new StringBuilder();
    Iterator<String> modelIdIterator = modelIdHelper.iterator();
    Node rootNode = session.getRootNode();
    while (modelIdIterator.hasNext()) {
        String nextPathFragment = modelIdIterator.next();
        pathBuilder.append(nextPathFragment).append("/");
        try {
            rootNode.getNode(pathBuilder.toString());
        } catch (PathNotFoundException pathNotFound) {
            Node addedNode = rootNode.addNode(pathBuilder.toString(), "nt:folder");
            addedNode.setPrimaryType("nt:folder");
        }
    }
    return rootNode.getNode(modelIdHelper.getFullPath().substring(1));
}
Also used : ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) Node(javax.jcr.Node) PathNotFoundException(javax.jcr.PathNotFoundException)

Aggregations

ModelIdHelper (org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper)8 Node (javax.jcr.Node)7 PathNotFoundException (javax.jcr.PathNotFoundException)6 RepositoryException (javax.jcr.RepositoryException)6 FatalModelRepositoryException (org.eclipse.vorto.repository.core.FatalModelRepositoryException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ModelNotFoundException (org.eclipse.vorto.repository.api.exception.ModelNotFoundException)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ModelInfo (org.eclipse.vorto.repository.api.ModelInfo)3 ModelReferentialIntegrityException (org.eclipse.vorto.repository.core.ModelReferentialIntegrityException)3 Binary (javax.jcr.Binary)2 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)2 ArrayList (java.util.ArrayList)1 Item (javax.jcr.Item)1 NodeIterator (javax.jcr.NodeIterator)1 Value (javax.jcr.Value)1 ModelId (org.eclipse.vorto.repository.api.ModelId)1 ModelReferencesHelper (org.eclipse.vorto.repository.core.impl.utils.ModelReferencesHelper)1