Package org.internna.iwebmvc.repository

Source Code of org.internna.iwebmvc.repository.FileRepository

/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.repository;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.annotation.PostConstruct;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.internna.iwebmvc.core.IWebMvcException;
import org.internna.iwebmvc.model.DocumentHolder;
import org.internna.iwebmvc.model.UUID;
import org.internna.iwebmvc.model.core.Document;
import org.internna.iwebmvc.utils.Assert;
import org.internna.iwebmvc.utils.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.util.FileCopyUtils;

/**
*
* @author Jose Noheda
* @since 1.0
*/
public class FileRepository implements DocumentRepository {

    protected final Log log = LogFactory.getLog(getClass());
    protected final DateFormat parser = new SimpleDateFormat("yyyy/MM/dd/HH/mm");
    private File documentRoot = null;

    @Required
    public void setDocumentRoot(File documentRoot) {
        this.documentRoot = documentRoot;
    }

    @PostConstruct
    protected void init() {
        if (!documentRoot.exists()) documentRoot.mkdirs();
        if (!documentRoot.isDirectory()) throw new IllegalArgumentException("[" + documentRoot.getAbsolutePath() + "] is not a directory!");
    }

    public DocumentHolder retrieve(String uri) {
        DocumentHolder doc = new DocumentHolder();
        try {
            doc.setUri(uri);
            doc.setStream(new FileInputStream(new File(documentRoot, uri)));
        } catch (IOException ioe) {
            throw new IWebMvcException(ioe);
        }
        return doc;
    }

    public Document retrieveByIdentifier(String identifier) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Document retrieveByPrimaryKey(UUID pk) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void store(DocumentHolder document) {
        Assert.notNull(document);
        try {
            document.setSizeInBytes(FileCopyUtils.copy(document.getStream(), getFileLocation(document)));
        } catch (IOException io) {
            log.warn("Could not store the document [" + document + "]");
            throw new IWebMvcException(io);
        }
    }

    protected OutputStream getFileLocation(DocumentHolder document) throws IOException {
        Calendar calendar = Calendar.getInstance();
        String constructed = "/" + (document.isImage() ? "images" : document.isAudio() ? "sounds" : document.isVideo() ? "videos" : "docs");
        constructed += "/" + parser.format(calendar.getTime());
        File path = new File(documentRoot, constructed);
        path.mkdirs();
        String identifier = System.currentTimeMillis() + "_" + StringUtils.getFilename(document.getIdentifier());
        document.setUri(constructed + "/" + identifier);
        path = new File(path, identifier);
        path.createNewFile();
        return new FileOutputStream(path);
    }

    public boolean delete(String uri) {
        Assert.hasText(uri);
        File f = new File(documentRoot, uri);
        return f.delete();
    }

}
TOP

Related Classes of org.internna.iwebmvc.repository.FileRepository

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.