package net.hust.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import net.hust.bean.Song;
import net.hust.service.Page;
import net.hust.service.SongQryHelper;
import net.hust.service.SongService;
import net.hust.service.SortHelper;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.opensymphony.xwork2.ActionSupport;
public class SongAction extends ActionSupport{
private SongService songService;
private Song song;
Page page = new Page();
private SongQryHelper helper = new SongQryHelper(new SortHelper("id"));
public Song getSong() {
return song;
}
public void setSong(Song song) {
this.song = song;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public void setSongService(SongService songService) {
try {
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
songService = (SongService)act.getBean("songService");
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.songService = songService;
}
public SongQryHelper getHelper() {
return helper;
}
public void setHelper(SongQryHelper helper) {
this.helper = helper;
}
private List<File> Filedata;
private List<String> FiledataFileName;
private List<String> FiledataContentType;
public List<File> getFiledata() {
return Filedata;
}
public void setFiledata(List<File> filedata) {
Filedata = filedata;
}
public List<String> getFiledataFileName() {
return FiledataFileName;
}
public void setFiledataFileName(List<String> filedataFileName) {
FiledataFileName = filedataFileName;
}
public List<String> getFiledataContentType() {
return FiledataContentType;
}
public void setFiledataContentType(List<String> filedataContentType) {
FiledataContentType = filedataContentType;
}
/**
* @return
*/
public String create()
{
try {
if (Filedata != null || Filedata.size() > 0)
{
for (int i = 0; i < Filedata.size(); ++i)
{
Song song=new Song();
String fileName = FiledataFileName.get(i);
File tempFile = new File(ServletActionContext.getServletContext().getRealPath("/musics")+File.separator+fileName);
FileUtils.forceMkdir(tempFile.getParentFile());
IOUtils.copy(new FileInputStream(Filedata.get(i)), new FileOutputStream(tempFile));
song.setName(fileName.substring(0,fileName.lastIndexOf(".")));
song.setFilepath("/musics/"+fileName);
song.setStyle("new");
song.setSinger("歌手");
System.out.println(""+song.getName()+","+song.getSinger());
songService.createSong(song);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public String input(){
return "input";
}
public String login()
{
return "songlogin";
}
public String preModify()
{
song = songService.getSongById(song.getId());
return "editview";
}
public String modify()
{
songService.modifySong(song);
return "loadAll";
}
public String remove()
{
songService.removeSong(song);
return "loadAll";
}
public String list()
{
page = songService.loadAll(page, helper);
return "listview";
}
public String test(){
System.out.println("helper.style+"+helper.getStyle());
page = songService.loadAll(page, helper);
return "test";
}
public String musiclist(){
page = songService.loadAll(page, helper);
String style=helper.getStyle();
System.out.println("helper.style=================================="+style);
if(style.equals("new")){
return "newlist";
}
else if(style.equals("hot")){
return "hotlist";
}else{
return "musiclist";
}
}
public String feeling(){
page = songService.loadAll(page, helper);
String style=helper.getStyle();
if(style.equals("happy")){
return "happy";
}
if(style.equals("calm")){
return "calm";
}
if(style.equals("lonely")){
return "lonely";
}
if(style.equals("sweet")){
return "sweet";
}
if(style.equals("moving")){
return "moving";
}
if(style.equals("nice")){
return "nice";
}
return null;
}
public String music(){
return "music";
}
public String connection(){
return "connection";
}
}