Example usage for org.springframework.validation Errors toString

List of usage examples for org.springframework.validation Errors toString

Introduction

In this page you can find the example usage for org.springframework.validation Errors toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:cs544.videohouse.validator.VideoValidator.java

@Override
public void validate(Object target, Errors errors) {
    System.out.println("inside method validate VideoValidator");
    Video video = (Video) target;/*w w w  . ja  v a  2  s  .c  o m*/
    if (!video.getImage().isEmpty()) {
        String imageExt = FilenameUtils.getExtension(video.getImage().getOriginalFilename());
        if (!"png".equalsIgnoreCase(imageExt) && !"jpg".equalsIgnoreCase(imageExt)
                && !"jpeg".equalsIgnoreCase(imageExt) && !"bmp".equalsIgnoreCase(imageExt)) {
            errors.rejectValue("image", "image.required", "image should have proper format");
        }
    } else {
        errors.rejectValue("image", "image.required", "image is required");
    }

    if (!video.getFile().isEmpty()) {
        String videoExt = FilenameUtils.getExtension(video.getFile().getOriginalFilename());
        if (!"mp4".equalsIgnoreCase(videoExt) && !"ogg".equalsIgnoreCase(videoExt)
                && !"ogv".equalsIgnoreCase(videoExt) && !"webM".equalsIgnoreCase(videoExt)) {
            errors.rejectValue("file", "video.required", "video should be proper format");
        }
        long bytes = video.getFile().getSize();
        if (bytes > 20000000) {
            errors.rejectValue("file", "video.required", "video size should be less than 20mb");
        }
    } else {
        errors.rejectValue("file", "video.required", "video is required");
    }

    System.out.println("errors : " + errors.toString());
}

From source file:org.ednovo.gooru.domain.service.resource.ResourceServiceImpl.java

@Override
public Resource handleNewResource(Resource resource, final String resourceTypeForPdf, final String thumbnail) {
    // test if a resource with url exist, currently just skip.
    Errors errors = new BindException(Resource.class, RESOURCE);
    Resource updatedResource = updateResource(resource, true, thumbnail, errors);
    if (updatedResource != null) {
        return updatedResource;
    }//w w  w. j  a  v a 2s. co  m
    errors = new BindException(Resource.class, RESOURCE);
    boolean downloadedFlag = false;
    // download if need and save:
    // FIXME
    /*
     * downloadedFlag = downloadFileIfRequiredAndUpdateUrl(resource,
     * StringUtils.defaultString(resourceTypeForPdf,
     * ResourceType.Type.HANDOUTS.getType()));
     */
    final ResourceType resourceType = new ResourceType();
    resource.setResourceType(resourceType);
    final String fileExtension = org.apache.commons.lang.StringUtils.substringAfterLast(resource.getUrl(), ".");
    if (fileExtension.equalsIgnoreCase(PDF) || fileExtension.equalsIgnoreCase(PNG)) {
        if (fileExtension.contains(PDF)) {
            resourceType.setName(ResourceType.Type.HANDOUTS.getType());
        } else {
            resourceType.setName(ResourceType.Type.IMAGE.getType());
        }
    } else {
        resourceType.setName(ResourceImageUtil.getYoutubeVideoId(resource.getUrl()) != null
                ? ResourceType.Type.VIDEO.getType()
                : ResourceType.Type.RESOURCE.getType());
    }

    resource = saveResource(resource, errors, false);
    if (resource == null || errors.hasErrors()) {
        LOGGER.error("save resource failed" + errors.toString());
    }

    if (downloadedFlag) {
        // Move the resource to the right folder
        File resourceFile = new File(resource.getUrl());
        if (resourceFile.exists()) {
            final File resourceFolder = new File(
                    resource.getOrganization().getNfsStorageArea().getInternalPath() + resource.getFolder());
            if (!resourceFolder.exists()) {
                resourceFolder.mkdir();
            }
            final String fileName = StringUtils.substringAfterLast(resource.getUrl(), "/");
            resourceFile.renameTo(new File(resourceFolder.getPath(), fileName));
            resource.setUrl(fileName);
            this.getResourceRepository().saveOrUpdate(resource);
            String resourceFilePath = resource.getOrganization().getNfsStorageArea().getInternalPath()
                    + resource.getFolder() + resource.getUrl();
            resourceFilePath = resourceFilePath.trim();
            if (fileName.toLowerCase().endsWith(DOT_PDF)) {
                final Map<String, Object> param = new HashMap<String, Object>();
                param.put(RESOURCE_FILE_PATH, resourceFilePath);
                param.put(RESOURCE_GOORU_OID, resource.getGooruOid());
                RequestUtil.executeRestAPI(param,
                        settingService.getConfigSetting(ConfigConstants.GOORU_CONVERSION_RESTPOINT, 0,
                                TaxonomyUtil.GOORU_ORG_UID) + "/conversion/pdf-to-image",
                        Method.POST.getName());
            }
        }
    } else {
        // Save resource folder
        this.getResourceRepository().saveOrUpdate(resource);
    }
    enrichAndAddOrUpdate(resource);

    // if handouts, split and save chapters as resources:
    if (resource.getResourceType().getName().equalsIgnoreCase(ResourceType.Type.HANDOUTS.getType())) {
        final List<Resource> chapterResources = splitToChaptersResources(resource);
        for (final Resource chapterResource : chapterResources) {
            enrichAndAddOrUpdate(chapterResource);
        }
    }
    indexHandler.setReIndexRequest(resource.getGooruOid(), IndexProcessor.INDEX, RESOURCE, null, false, false);

    return resource;

}