throws IOException, ServletException {
try {
String action = (String) req.getParameter("action");
if (action != null && action.length() > 0) {
if (action.equals("transport")) {
Mission mission = MissionModel.getNotCompleteByType(2);
if (mission != null) {
YoutubeService tubeService = new YoutubeService();
tubeService.setPage(mission.getSearchPage());
tubeService.setLimit(mission.getSearchLimit());
List<Tube> listToAdd = new ArrayList<Tube>();
tubeService.searchByChannel(mission.getSearchKeyword(),
true);
Queue queue = QueueFactory.getDefaultQueue();
for (Tube tube : tubeService.getListResult()) {
tube = processTubeDetail(mission.getPlaylistId(),
tube);
// add author to site.
queue.add(TaskOptions.Builder.withUrl(
"/task-admin?action=add-author&uri="
+ tube.getAuthorUrl()).method(
TaskOptions.Method.GET));
listToAdd.add(tube);
}
TubeModel.insert(listToAdd);
if (tubeService.getListResult().size() > 0
&& !tubeService.isTooManyRecentCallError()) {
mission.setSearchPage(tubeService.getPage() + 1);
} else {
mission.setSearchPage(tubeService.getPage());
}
if (tubeService.getPage() == tubeService.getTotalPage()
&& !tubeService.isTooManyRecentCallError()) {
mission.setStatus(2);
}
MissionModel.insert(mission);
}
} else if (action.equals("follow")) {
MissionModel model = new MissionModel();
model.getListNotCompleteByType(1);
List<Mission> listMission = model.getListResult();
if (listMission != null && listMission.size() > 0) {
YoutubeActiveService activeService = new YoutubeActiveService();
for (Mission mission : listMission) {
List<String> tubeIds = mission.getTubeIds();
if (tubeIds.size() == 0) {
activeService.getActiveByChannel(mission
.getSearchKeyword());
List<UserEventEntry> listResult = activeService
.getListResult();
if (listResult != null && listResult.size() > 0) {
tubeIds = new ArrayList<String>();
for (UserEventEntry entry : listResult) {
tubeIds.add(entry.getVideoId());
}
}
mission.setTubeIds(tubeIds);
mission.setUpdated(Calendar.getInstance()
.getTime());
MissionModel.insert(mission);
}
}
}
} else if (action.equals("getFollow")) {
MissionModel model = new MissionModel();
model.getListNotCompleteByType(1);
List<Mission> listMission = model.getListResult();
if (listMission != null && listMission.size() > 0) {
for (Mission mission : listMission) {
if (mission != null && mission.getId().length() > 0) {
List<String> tubeIds = new ArrayList<String>();
for (String string : mission.getTubeIds()) {
tubeIds.add(string);
}
List<String> listToAdd = new ArrayList<String>();
if (mission.getTubeIds().size() > 0) {
for (String tubeId : mission.getTubeIds()) {
if (TubeSearchModel.getById(tubeId) == null) {
if (listToAdd.size() != 50) {
listToAdd.add(tubeId);
tubeIds.remove(tubeId);
} else {
break;
}
}
}
}
YoutubeBatchService.searchByListId(listToAdd,
true);
List<Tube> listTube = YoutubeBatchService
.getListResult();
Queue queue = QueueFactory.getDefaultQueue();
for (Tube tube : listTube) {
tube = processTubeDetail(
mission.getPlaylistId(), tube);
// add author to site.
queue.add(TaskOptions.Builder.withUrl(
"/task-admin?action=add-author&uri="
+ tube.getAuthorUrl()).method(
TaskOptions.Method.GET));
}
TubeModel.insert(listTube);
mission.setTubeIds(tubeIds);
mission.setUpdated(Calendar.getInstance()
.getTime());
MissionModel.insert(mission);
}
}
} else {