Search in sources :

Example 16 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class JcrModelRepository method createModelResource.

private ModelInfo createModelResource(Node node) throws RepositoryException {
    ModelInfo resource = createMinimalModelInfo(node);
    if (node.hasProperty("vorto:references")) {
        Value[] referenceValues = null;
        try {
            referenceValues = node.getProperty("vorto:references").getValues();
        } catch (Exception ex) {
            referenceValues = new Value[] { node.getProperty("vorto:references").getValue() };
        }
        if (referenceValues != null) {
            ModelReferencesHelper referenceHelper = new ModelReferencesHelper();
            for (Value referValue : referenceValues) {
                String nodeUuid = referValue.getString();
                Node referencedNode = session.getNodeByIdentifier(nodeUuid);
                referenceHelper.addModelReference(ModelIdHelper.fromPath(referencedNode.getParent().getPath()).getPrettyFormat());
            }
            resource.setReferences(referenceHelper.getReferences());
        }
    }
    PropertyIterator propIter = node.getReferences();
    while (propIter.hasNext()) {
        Property prop = propIter.nextProperty();
        Node referencedByFileNode = prop.getParent();
        final ModelId referencedById = ModelIdHelper.fromPath(referencedByFileNode.getParent().getPath());
        resource.getReferencedBy().add(referencedById);
        if (referencedByFileNode.getName().endsWith(ModelType.Mapping.getExtension())) {
            ModelEMFResource emfResource = getEMFResource(referencedById);
            resource.addPlatformMapping(emfResource.getTargetPlatform(), referencedById);
        }
    }
    return resource;
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ModelReferencesHelper(org.eclipse.vorto.repository.core.impl.utils.ModelReferencesHelper) Node(javax.jcr.Node) Value(javax.jcr.Value) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property) 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) ModelId(org.eclipse.vorto.repository.api.ModelId)

Example 17 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo 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 18 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class JcrModelRepository method removeModel.

@Override
public void removeModel(ModelId modelId) {
    try {
        ModelInfo modelResource = this.getById(modelId);
        if (!modelResource.getReferencedBy().isEmpty()) {
            throw new ModelReferentialIntegrityException("Cannot remove model because it is referenced by other model(s)", modelResource.getReferencedBy());
        }
        ModelIdHelper modelIdHelper = new ModelIdHelper(modelId);
        Item item = session.getItem(modelIdHelper.getFullPath());
        item.remove();
        session.save();
    } catch (RepositoryException e) {
        throw new FatalModelRepositoryException("Problem occured removing the model", e);
    }
}
Also used : Item(javax.jcr.Item) ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ModelIdHelper(org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) RepositoryException(javax.jcr.RepositoryException) ModelReferentialIntegrityException(org.eclipse.vorto.repository.core.ModelReferentialIntegrityException)

Example 19 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class JcrModelRepository method checkin.

@Override
public ModelInfo checkin(String handleId, String author) {
    StorageItem uploadedItem = this.uploadStorage.get(handleId);
    if (uploadedItem == null) {
        throw new IllegalArgumentException("No model found for handleId '" + handleId + "'");
    }
    final ModelInfo resource = ModelParserFactory.getParser(handleId).parse(new ByteArrayInputStream((byte[]) uploadedItem.getValue()));
    try {
        Node folderNode = createNodeForModelId(resource.getId());
        Node fileNode = folderNode.getNodes("*.type | *.fbmodel | *.infomodel | *.mapping").hasNext() ? folderNode.getNodes("*.type | *.fbmodel | *.infomodel | *.mapping").nextNode() : null;
        if (fileNode == null) {
            fileNode = folderNode.addNode(resource.getId().getName() + resource.getType().getExtension(), "nt:file");
            fileNode.addMixin("vorto:meta");
            fileNode.addMixin("mix:referenceable");
            fileNode.setProperty("vorto:author", author);
            Node contentNode = fileNode.addNode("jcr:content", "nt:resource");
            Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream((byte[]) uploadedItem.getValue()));
            contentNode.setProperty("jcr:data", binary);
        } else {
            Node contentNode = fileNode.getNode("jcr:content");
            Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream((byte[]) uploadedItem.getValue()));
            contentNode.setProperty("jcr:data", binary);
        }
        session.save();
        logger.info("Checkin successful");
        this.uploadStorage.remove(handleId);
        // Email Notification
        notifyWatchers(resource, author);
    } catch (Exception e) {
        logger.error("Error checking in model", e);
        throw new FatalModelRepositoryException("Problem checking in uploaded model" + resource.getId(), e);
    }
    return resource;
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) Node(javax.jcr.Node) Binary(javax.jcr.Binary) 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 20 with ModelInfo

use of org.eclipse.vorto.repository.api.ModelInfo in project vorto by eclipse.

the class AbstractModelParser method parse.

@Override
public ModelInfo parse(InputStream is) {
    XtextResourceSet resourceSet = getInjector().getInstance(XtextResourceSet.class);
    resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
    Resource resource = resourceSet.createResource(URI.createURI("dummy:/" + fileName));
    try {
        resource.load(is, resourceSet.getLoadOptions());
    } catch (IOException e) {
        throw new ValidationException(e.getMessage(), null);
    }
    List<org.eclipse.emf.ecore.resource.Resource.Diagnostic> grammarErrors = getGrammarErrors(resource.getErrors());
    if (!grammarErrors.isEmpty()) {
        ModelInfo invalidModelResource = new ModelInfo(parseModelIdFromFileName(), ModelType.fromFileName(fileName));
        throw new ValidationException(grammarErrors.get(0).getMessage(), invalidModelResource);
    }
    return new ModelEMFResource((Model) resource.getContents().get(0));
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) ModelEMFResource(org.eclipse.vorto.repository.core.impl.ModelEMFResource) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) XtextResource(org.eclipse.xtext.resource.XtextResource) ModelEMFResource(org.eclipse.vorto.repository.core.impl.ModelEMFResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextLinkingDiagnostic(org.eclipse.xtext.linking.impl.XtextLinkingDiagnostic) IOException(java.io.IOException)

Aggregations

ModelInfo (org.eclipse.vorto.repository.api.ModelInfo)35 Test (org.junit.Test)12 ModelId (org.eclipse.vorto.repository.api.ModelId)11 IOException (java.io.IOException)9 ModelNotFoundException (org.eclipse.vorto.repository.api.exception.ModelNotFoundException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)7 AbstractIntegrationTest (org.eclipse.vorto.repository.AbstractIntegrationTest)6 FatalModelRepositoryException (org.eclipse.vorto.repository.core.FatalModelRepositoryException)6 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)6 ArrayList (java.util.ArrayList)5 Node (javax.jcr.Node)5 PathNotFoundException (javax.jcr.PathNotFoundException)5 RepositoryException (javax.jcr.RepositoryException)5 ApiOperation (io.swagger.annotations.ApiOperation)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ZipEntry (java.util.zip.ZipEntry)4 UploadTooLargeException (org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ApiResponses (io.swagger.annotations.ApiResponses)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3