Search in sources :

Example 1 with IModelValidator

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);
    }
}
Also used : UploadModelResult(org.eclipse.vorto.repository.api.upload.UploadModelResult) ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) FatalModelRepositoryException(org.eclipse.vorto.repository.core.FatalModelRepositoryException) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) IModelValidator(org.eclipse.vorto.repository.core.impl.validation.IModelValidator) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) HashSet(java.util.HashSet)

Example 2 with IModelValidator

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);
    }
}
Also used : ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) IModelValidator(org.eclipse.vorto.repository.core.impl.validation.IModelValidator)

Example 3 with IModelValidator

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;
}
Also used : DuplicateModelValidation(org.eclipse.vorto.repository.core.impl.validation.DuplicateModelValidation) BulkModelDuplicateIdValidation(org.eclipse.vorto.repository.core.impl.validation.BulkModelDuplicateIdValidation) BulkModelReferencesValidation(org.eclipse.vorto.repository.core.impl.validation.BulkModelReferencesValidation) LinkedList(java.util.LinkedList) IModelValidator(org.eclipse.vorto.repository.core.impl.validation.IModelValidator)

Aggregations

IModelValidator (org.eclipse.vorto.repository.core.impl.validation.IModelValidator)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ArrayList (java.util.ArrayList)2 ModelInfo (org.eclipse.vorto.repository.api.ModelInfo)2 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 UploadModelResult (org.eclipse.vorto.repository.api.upload.UploadModelResult)1 FatalModelRepositoryException (org.eclipse.vorto.repository.core.FatalModelRepositoryException)1 BulkModelDuplicateIdValidation (org.eclipse.vorto.repository.core.impl.validation.BulkModelDuplicateIdValidation)1 BulkModelReferencesValidation (org.eclipse.vorto.repository.core.impl.validation.BulkModelReferencesValidation)1 DuplicateModelValidation (org.eclipse.vorto.repository.core.impl.validation.DuplicateModelValidation)1