Skip to content

Commit

Permalink
Fixes #76
Browse files Browse the repository at this point in the history
  • Loading branch information
aedelmann committed Sep 17, 2016
2 parents cf4e2c1 + c4f9762 commit 4b73646
Show file tree
Hide file tree
Showing 29 changed files with 2,693 additions and 191 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.eclipse.vorto.repository.internal.service;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

import javax.jcr.ImportUUIDBehavior;
import javax.jcr.Item;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.eclipse.vorto.repository.model.ModelId;
import org.eclipse.vorto.repository.model.ModelResource;
import org.eclipse.vorto.repository.service.FatalModelRepositoryException;
import org.eclipse.vorto.repository.service.IModelRepository;
import org.eclipse.vorto.repository.service.IRepositoryManager;
import org.eclipse.vorto.repository.service.ModelReferentialIntegrityException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DefaultRepositoryManager implements IRepositoryManager {

@Autowired
private Session session;

@Autowired
private IModelRepository modelRepository;

@Override
public byte[] backup() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
((org.modeshape.jcr.api.Session) session).exportDocumentView("/", baos, false, false);
baos.close();
return baos.toByteArray();

}

@Override
public void restore(InputStream inputStream) throws Exception {
removeAll();
((org.modeshape.jcr.api.Session) session).getWorkspace().importXML("/", inputStream,
ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
}

public Session getSession() {
return session;
}

public void setSession(Session session) {
this.session = session;
}

public IModelRepository getModelRepository() {
return modelRepository;
}

public void setModelRepository(IModelRepository modelRepository) {
this.modelRepository = modelRepository;
}

@Override
public void removeModel(ModelId modelId) {
try {
ModelResource modelResource = this.modelRepository.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);
}
}

private void removeAll() throws Exception {
Set<String> rootNodes = new HashSet<>();
for (ModelResource resource : this.modelRepository.search("*")) {
final String org = resource.getId().getNamespace().substring(0,
resource.getId().getNamespace().indexOf("."));
rootNodes.add(org);
}

for (String rootNode : rootNodes) {
try {
Item item = session.getItem("/" + rootNode);
item.remove();
} catch (PathNotFoundException ex) {
}
}

this.session.save();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import javax.annotation.PostConstruct;
import javax.jcr.Binary;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
Expand Down Expand Up @@ -63,7 +62,6 @@
import org.eclipse.vorto.repository.service.FatalModelRepositoryException;
import org.eclipse.vorto.repository.service.IModelRepository;
import org.eclipse.vorto.repository.service.ModelNotFoundException;
import org.eclipse.vorto.repository.service.ModelReferentialIntegrityException;
import org.eclipse.vorto.repository.service.UserRepository;
import org.eclipse.vorto.repository.validation.IModelValidator;
import org.eclipse.vorto.repository.validation.ValidationException;
Expand Down Expand Up @@ -145,7 +143,13 @@ private ModelResource createModelResource(Node node) throws RepositoryException
}

if (node.hasProperty("vorto:references")) {
Value[] referenceValues = node.getProperty("vorto:references").getValues();
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) {
Expand All @@ -165,6 +169,11 @@ private ModelResource createModelResource(Node node) throws RepositoryException
final ModelId referencedById = ModelId.fromPath(referencedByFileNode.getParent().getPath());
resource.getReferencedBy().add(referencedById);
}

NodeIterator imageNodeIterator = node.getParent().getNodes("img.png*");
if (imageNodeIterator.hasNext()) {
resource.setHasImage(true);
}

return resource;
}
Expand Down Expand Up @@ -193,10 +202,7 @@ public byte[] getModelContent(ModelId modelId, ContentType contentType) {

@Override
public UploadModelResult upload(byte[] content, String fileName) {
if (StringUtils.isEmpty(fileName)) {
return UploadModelResult.invalid(new ValidationException("Filename is invalid", null));
}


try {
ModelResource resource = ModelParserFactory.getParser(fileName).parse(new ByteArrayInputStream(content));

Expand Down Expand Up @@ -341,23 +347,6 @@ public ModelResource getById(ModelId modelId) {
}
}

@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);
}
}

@PostConstruct
public void createValidators() {
this.validators.add(new DuplicateModelValidation(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import javax.jcr.Binary;
import javax.jcr.NamespaceRegistry;
import javax.jcr.Node;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package org.eclipse.vorto.repository.internal.service.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
Expand All @@ -24,11 +26,14 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.eclipse.vorto.repository.internal.service.ModelParserFactory;
import org.eclipse.vorto.repository.internal.service.validation.BulkModelDuplicateIdValidation;
import org.eclipse.vorto.repository.internal.service.validation.BulkModelReferencesValidation;
import org.eclipse.vorto.repository.internal.service.validation.DuplicateModelValidation;
import org.eclipse.vorto.repository.model.ModelEMFResource;
import org.eclipse.vorto.repository.model.ModelResource;
import org.eclipse.vorto.repository.model.UploadModelResult;
import org.eclipse.vorto.repository.service.FatalModelRepositoryException;
Expand Down Expand Up @@ -92,15 +97,15 @@ public List<UploadModelResult> uploadMultiple(String zipFileName) {


if (invalidResult.isEmpty()) {
return safelyUpload(zipFileName);
return safelyUpload(parsedModels);
} else {
List<UploadModelResult> completeResult = new ArrayList<>();
completeResult.addAll(invalidResult);
completeResult.addAll(validResult);
return completeResult;
}

} catch (IOException e) {
} catch (Exception e) {
throw new FatalModelRepositoryException("Invalid zip file", e);
} finally {
if (zipFile != null) {
Expand All @@ -116,33 +121,32 @@ public List<UploadModelResult> uploadMultiple(String zipFileName) {
/**
* @param zipFileName
*/
private List<UploadModelResult> safelyUpload(final String zipFileName) {
private List<UploadModelResult> safelyUpload(Set<ModelResource> resources) throws Exception {
List<UploadModelResult> result = new ArrayList<UploadModelResult>();
ZipFile zipFile = null;

for (ModelResource resource : resources) {
result.add(UploadModelResult.valid(createUploadHandle(((ModelEMFResource)resource).toDSL(), resource.getId().getFileName()+resource.getModelType().getExtension()), resource));
}

return result;
}

private static String getDefaultExtractDirectory() {
return FilenameUtils.normalize(FileUtils.getTempDirectory().getPath() + "/vorto", true);
}

private String createUploadHandle(byte[] content, String fileName) {
try {
zipFile = new ZipFile(zipFileName);

Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
result.add(this.repositoryService.upload(IOUtils.toByteArray(zipFile.getInputStream(entry)),
entry.getName()));
}
File tmpDirectory = new File(getDefaultExtractDirectory());
if (!tmpDirectory.exists()) {
tmpDirectory.mkdirs();
}
File file = new File(FilenameUtils.normalize(getDefaultExtractDirectory() + "/" + StringUtils.getFilename(fileName)));
IOUtils.write(content, new FileOutputStream(file));
return file.getName();
} catch (IOException e) {
throw new FatalModelRepositoryException("Invalid zip file", e);
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
// ignore
}
}
throw new RuntimeException("Could not create temporary file for uploaded model", e);
}

return result;
}

private boolean isValid(String file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public byte[] toXMI() throws Exception {
return baos.toByteArray();
}

public byte[] toDSL() throws Exception {
Resource resource = model.eResource().getResourceSet().createResource(URI.createURI(model.getName()+this.modelType.getExtension()));
resource.getContents().add(model);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
resource.save(baos, null);
return baos.toByteArray();
}

public boolean matchesTargetPlatform(String targetPlatform) {
return this.modelType == ModelType.Mapping && ((MappingModel)model).getTargetPlatform().equalsIgnoreCase(targetPlatform);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,9 @@ public boolean equals(Object obj) {

public String getPrettyFormat() {
return namespace + "." + name + ":" +version;
}

public String getFileName() {
return namespace.replace(".", "_")+"-"+name+"-"+version.replace(".", "_");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import java.util.Date;
import java.util.List;

import javax.jcr.PathNotFoundException;

import org.eclipse.vorto.repository.internal.service.CommentRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
*/
public class FatalModelRepositoryException extends ModelRepositoryException {

/**
*
*/
private static final long serialVersionUID = 1L;

public FatalModelRepositoryException(String msg, Throwable cause) {
super(msg, cause);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ public interface IModelRepository {
*/
ModelResource getById(ModelId modelId);

/**
*
* @param modelId
*/
void removeModel(ModelId modelId);

/**
* Returns the actual model content for the given model id
* @param modelId
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.eclipse.vorto.repository.service;

import java.io.InputStream;

import org.eclipse.vorto.repository.model.ModelId;

public interface IRepositoryManager {

byte[] backup() throws Exception;

void restore(InputStream inputStream) throws Exception;

void removeModel(ModelId modelId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
*/
public class ModelNotFoundException extends ModelRepositoryException {

/**
*
*/
private static final long serialVersionUID = 1L;

public ModelNotFoundException(String msg, Throwable t) {
super(msg,t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
public class ModelReferentialIntegrityException extends
ModelRepositoryException {

/**
*
*/
private static final long serialVersionUID = 1L;
private List<ModelId> referencedBy = new ArrayList<>();

public ModelReferentialIntegrityException(String msg, List<ModelId> referencedBy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
*/
public abstract class ModelRepositoryException extends RuntimeException {

/**
*
*/
private static final long serialVersionUID = 1L;

public ModelRepositoryException(String msg) {
super(msg);
}
Expand Down
Loading

0 comments on commit 4b73646

Please sign in to comment.