packagecom.controller;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Map;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importnet.sf.json.JSONObject;importcom.entity.SongList;importcom.entity.SongListWithSong;importcom.service.SongListServiceImpl;importcom.service.SongListServiceInter;importcom.service.SongListWithSongServiceImpl;importcom.service.SongListWithSongServiceInter;/*** 标签推荐
*@author29207
**/
public class TagsRecommendServlet extendsHttpServlet {
@Overrideprotected voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
doGet(request, response);
}
@Overrideprotected voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
Map songListTagsNameMap = new HashMap();//存储歌单标签名字和出现次数
java.util.List> songListTagsNameListClassement = new ArrayList>();//存储排序后歌单标签名字和出现次数
PrintWriter out=response.getWriter();//获取歌单编号
String songListIdString = request.getParameter("songListId");int songListId = Integer.parseInt(songListIdString);//歌单编号//声明查询歌单的服务
SongListServiceInter songListService = newSongListServiceImpl();//用于存放所有歌曲存在的所有歌单
ArrayList allSongLists = new ArrayList();//查询该歌单中存在的歌曲//声明songListWithSong(歌曲歌单表服务对象)
SongListWithSongServiceInter songListWithSongService = newSongListWithSongServiceImpl();//调用方法
ArrayList songListWithSongs =songListWithSongService.selectSongListWithSongOfSongLIstId(songListId);//遍历,获取其中的歌曲编号
for(SongListWithSong songListWithSong : songListWithSongs) {int songId = songListWithSong.getSongId();//获取每一个歌曲编号//根据歌曲编号,查询该歌曲存在在哪些歌单中
ArrayList songFromSongLists =songListWithSongService.selectSongListIdFromSongListWithSongOfSongId(songId);//遍历,取出每个歌单的歌单编号,根据歌单编号查找歌单
for(SongListWithSong songListWithSong2 : songFromSongLists) {int oneSongFromsongListId =songListWithSong2.getSongListId();//根据歌单编号查询歌单信息
SongList oneSongList =songListService.selectSongListOfSongListId(oneSongFromsongListId);//把该歌单存入allSongLists
allSongLists.add(oneSongList);
}
}
System.out.println("==========我是标签推荐服务=========");//一个歌单中所有歌曲涉及到的所有歌单为allSongLists
int allSongListsLen =allSongLists.size();
UserSongListRecommendServlet userSongListRecommendServlet= newUserSongListRecommendServlet();
songListTagsNameMap=userSongListRecommendServlet.getSongListTagsNameMap(allSongListsLen, allSongLists, songListTagsNameMap);
songListTagsNameListClassement= userSongListRecommendServlet.songListTagsNameComm(songListTagsNameMap);//标签排序后
int songListTagsNameListClassementLen =songListTagsNameListClassement.size();
JSONObject tagsRecommJsonObject= newJSONObject();
System.out.println("排序后长度:" +songListTagsNameListClassementLen);if (songListTagsNameListClassementLen == 0) {
out.print("null");
}else{if (songListTagsNameListClassementLen <= 5) {for (int i = 0; i < songListTagsNameListClassementLen; i++) {
String tagsRecomm=songListTagsNameListClassement.get(i).getKey();
tagsRecommJsonObject.put(i, tagsRecomm);
}
}else{for (int i = 0; i < 5; i++) {
String tagsRecomm=songListTagsNameListClassement.get(i).getKey();
tagsRecommJsonObject.put(i, tagsRecomm);
}
}
System.out.println("推荐标签JSON===" +tagsRecommJsonObject);
out.print(tagsRecommJsonObject);
}
}
}