use of org.eclipse.vorto.repository.core.impl.validation.IModelValidator in project vorto by eclipse.
the class BulkUploadHelper method uploadMultiple.
public List<UploadModelResult> uploadMultiple(byte[] content, String zipFileName, String callerId) {
if (!isValid(zipFileName)) {
throw new FatalModelRepositoryException("Filename/type is invalid", null);
}
List<UploadModelResult> invalidResult = new ArrayList<UploadModelResult>();
List<UploadModelResult> validResult = new ArrayList<UploadModelResult>();
Set<ModelInfo> parsedModels = new HashSet<>();
ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(content));
ZipEntry entry = null;
try {
while ((entry = zis.getNextEntry()) != null) {
if (!entry.isDirectory()) {
final String fileName = entry.getName();
if (ModelParserFactory.hasParserFor(fileName)) {
try {
parsedModels.add(ModelParserFactory.getParser(fileName).parse(new ByteArrayInputStream(copyStream(zis, entry))));
} catch (ValidationException grammarProblem) {
invalidResult.add(UploadModelResultFactory.create(grammarProblem));
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
List<IModelValidator> bulkUploadValidators = constructBulkUploadValidators(parsedModels);
for (ModelInfo modelResource : parsedModels) {
try {
for (IModelValidator validator : bulkUploadValidators) {
validator.validate(modelResource, InvocationContext.create(callerId));
}
UploadModelResult result = UploadModelResult.valid(modelResource);
if (!invalidResult.contains(result)) {
validResult.add(result);
}
} catch (ValidationException validationException) {
invalidResult.add(UploadModelResultFactory.create(validationException));
}
}
if (invalidResult.isEmpty()) {
DependencyManager dm = new DependencyManager(parsedModels);
return safelyUpload(dm.getSorted());
} else {
List<UploadModelResult> completeResult = new ArrayList<>();
completeResult.addAll(invalidResult);
completeResult.addAll(validResult);
return completeResult;
}
} catch (Exception e) {
throw new FatalModelRepositoryException("Invalid zip file", e);
}
}
use of org.eclipse.vorto.repository.core.impl.validation.IModelValidator in project vorto by eclipse.
the class JcrModelRepository method upload.
@Override
public UploadModelResult upload(byte[] content, String fileName, String callerId) {
try {
ModelInfo resource = ModelParserFactory.getParser(fileName).parse(new ByteArrayInputStream(content));
List<ValidationException> validationExceptions = new ArrayList<ValidationException>();
for (IModelValidator validator : validators) {
try {
validator.validate(resource, InvocationContext.create(callerId));
} catch (ValidationException validationException) {
validationExceptions.add(validationException);
}
}
if (validationExceptions.size() <= 0) {
return UploadModelResult.valid(createUploadHandle(content, resource.getType()), resource);
} else {
return UploadModelResultFactory.create(validationExceptions.toArray(new ValidationException[validationExceptions.size()]));
}
} catch (ValidationException e) {
return UploadModelResultFactory.invalid(e);
}
}
use of org.eclipse.vorto.repository.core.impl.validation.IModelValidator in project vorto by eclipse.
the class BulkUploadHelper method constructBulkUploadValidators.
private List<IModelValidator> constructBulkUploadValidators(Set<ModelInfo> modelResources) {
List<IModelValidator> bulkUploadValidators = new LinkedList<IModelValidator>();
bulkUploadValidators.add(new DuplicateModelValidation(this.repositoryService));
bulkUploadValidators.add(new BulkModelDuplicateIdValidation(this.repositoryService, modelResources));
bulkUploadValidators.add(new BulkModelReferencesValidation(this.repositoryService, modelResources));
return bulkUploadValidators;
}
Aggregations