com.eldar.services.ResourceService.java Source code

Java tutorial

Introduction

Here is the source code for com.eldar.services.ResourceService.java

Source

/*******************************************************************************
 * This file is part of Eldar Works.
 *
 * Eldar Works is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Eldar Works is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Eldar Works.  If not, see <http://www.gnu.org/licenses/>.
 * Copyright (c) 2012. Phillip Sanderson
 ******************************************************************************/

package com.eldar.services;

import com.eldar.daos.IResourceDao;
import com.eldar.daos.IResourceTypeDao;
import com.eldar.models.Versionable;
import com.eldar.models.resources.Resource;
import com.eldar.models.resources.ResourceType;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;

/**
 * @author Phillip Sanderson
 *         Date: 27/05/12
 *         Time: 12:31 PM
 */
@Service
public class ResourceService implements IResourceService {

    @Autowired
    private IResourceTypeDao resourceTypeDao;

    private IResourceDao resourceDao;

    public ResourceService() {

    }

    /**
     * Creates the resource
     *
     * @param file    - the file uploaded by the user.
     * @param version - The versionable that the file is for.
     * @return
     * @throws InvalidResourceException
     */
    public Resource create(MultipartFile file, Versionable version) throws InvalidResourceException {
        if (file.isEmpty()) {
            throw new InvalidResourceException("The uploaded file was empty");
        }
        int index = file.getOriginalFilename().lastIndexOf(".");
        String displayName = file.getOriginalFilename().substring(0, index);
        String extension = file.getOriginalFilename().substring(index);
        ResourceType resourceType = resourceTypeDao.getByExtension(extension);
        if (resourceType == null) {
            throw new InvalidResourceException("An invalid file type was specified.");
        }
        Resource resource = new Resource();
        resource.setDisplayName(displayName);
        resource.setResourceType(resourceType);
        resource.setRealName(getFileName(displayName, version.getVersionNumber()));
        return resource;
    }

    @Override
    public void save(Resource resource) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean canDelete(Resource resource) {
        return false; //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void delete(Resource resource) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    private String getFileName(String name, int version) {
        return name + "_" + version + "_" + DateTime.now(DateTimeZone.UTC).toString("yyyy-MM-dd-HH-mm-ss");
    }

    public void save(Resource resource, MultipartFile file) {
        this.save(resource);
    }

    private void writeFile(MultipartFile file, Resource resource) throws IOException {
        byte[] bytes = file.getBytes();

    }
}