com.siriusit.spezg.multilib.jsf.unit.UnitRegistrationBean.java Source code

Java tutorial

Introduction

Here is the source code for com.siriusit.spezg.multilib.jsf.unit.UnitRegistrationBean.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 com.siriusit.spezg.multilib.jsf.unit;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.siriusit.spezg.multilib.domain.Person;
import com.siriusit.spezg.multilib.domain.Unit;
import com.siriusit.spezg.multilib.jsf.person.UserBean;
import com.siriusit.spezg.multilib.jsf.upload.UploadBean;
import com.siriusit.spezg.multilib.service.BinaryAttachmentService;
import com.siriusit.spezg.multilib.service.UnitService;

/**
 * @author Tommy B <Tommy.Bo@SiriusIT.com>
 */
@Controller("unitRegistrationBean")
@Scope("request")
public class UnitRegistrationBean {
    @Resource
    private UnitService unitService;

    @Resource
    private BinaryAttachmentService binaryAttachmentService;

    @Resource
    private UserBean userBean;

    @Resource
    private UploadBean uploadBean;

    private String title;
    private String description;
    private String[] genre;

    private enum Genres {
        Action('A'), Comedy('C'), Drama('D'), Thriller('T');

        Genres(final char genre) {
            this.genre = genre;
        }

        private char genre;

        public char getGenre() {
            return genre;
        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String[] getGenre() {
        return genre;
    }

    public void setGenre(String[] genre) {
        this.genre = genre;
    }

    public void setUnitService(UnitService unitService) {
        this.unitService = unitService;
    }

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    public void setUploadBean(UploadBean uploadBean) {
        this.uploadBean = uploadBean;
    }

    public List<String> populateGenreList() {
        List<String> genres = new ArrayList<String>();
        for (Genres genre : Genres.values()) {
            genres.add(genre.toString());
        }
        return genres;
    }

    public String presentGenres() {
        String result = "";
        for (int i = 0; i < genre.length; i++) {
            result += genre[i];
            if (i == genre.length - 1) {
                result += ".";
            } else {
                result += " ";
            }
        }

        return result;
    }

    public String registerUnit() {
        Person owner = userBean.getUser();
        Unit result = unitService.storeUnit(owner, title, description);
        if (result == null) {
            return "unitRegistration";
        } else {
            // Skipping binary attachment
            return "unitRegistrationConfirm";
        }
    }

    public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);

        // Get the size of the file
        long length = file.length();

        if (length > Integer.MAX_VALUE) {
            // File is too large
        }

        // Create the byte array to hold the data
        byte[] bytes = new byte[(int) length];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }

        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file " + file.getName());
        }

        // Close the input stream and return bytes
        is.close();
        return bytes;
    }

}