private Node createNodeForModelId(ModelId id) throws RepositoryException { StringBuilder pathBuilder = new StringBuilder(); Iterator<String> modelIdIterator = id.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(id.getFullPath().substring(1)); }
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); } }
private ModelResource createModelResource(Node node) throws RepositoryException { ModelResource resource = new ModelResource( ModelId.fromPath(node.getParent().getPath()), ModelType.valueOf(node.getProperty("vorto:type").getString())); resource.setDescription(node.getProperty("vorto:description").getString()); resource.setDisplayName(node.getProperty("vorto:displayname").getString()); resource.setCreationDate(node.getProperty("jcr:created").getDate().getTime()); if (node.hasProperty("vorto:author")) { resource.setAuthor(node.getProperty("vorto:author").getString()); } if (node.hasProperty("vorto:references")) { Value[] referenceValues = node.getProperty("vorto:references").getValues(); if (referenceValues != null) { ModelReferencesHelper referenceHelper = new ModelReferencesHelper(); for (Value referValue : referenceValues) { String nodeUuid = referValue.getString(); Node referencedNode = session.getNodeByIdentifier(nodeUuid); referenceHelper.addModelReference( ModelId.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 = ModelId.fromPath(referencedByFileNode.getParent().getPath()); resource.getReferencedBy().add(referencedById); } return resource; }
@Override public void removeModel(ModelId modelId) { try { ModelResource modelResource = getById(modelId); if (!modelResource.getReferencedBy().isEmpty()) { throw new ModelReferentialIntegrityException( "Cannot remove model because it is referenced by other model(s)", modelResource.getReferencedBy()); } Item item = session.getItem(modelId.getFullPath()); item.remove(); session.save(); } catch (RepositoryException e) { throw new FatalModelRepositoryException("Problem occured removing the model", e); } }
@Override public ModelResource getById(ModelId modelId) { try { Node foundNode = session.getNode(modelId.getFullPath()); Node fileNode = (Node) foundNode.getNodes().next(); ModelResource modelResource = createModelResource(fileNode); if (modelResource.getModelType() == ModelType.InformationModel) { for (ModelId referencedById : modelResource.getReferencedBy()) { ModelEMFResource emfResource = getEMFResource(referencedById); modelResource.addTargetPlatform(emfResource.getTargetPlatform()); } } return modelResource; } catch (PathNotFoundException e) { return null; } catch (RepositoryException e) { throw new RuntimeException("Retrieving Content of Resource: Problem accessing repository", e); } }
@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); } }