if (request.getParameter("categoryAlias") != null) {
int page = 1;
if (request.getParameter("page") != null) {
page = Integer.parseInt(request.getParameter("page"));
}
Category cate = CategoryModel.getByCateAlias(request
.getParameter("categoryAlias"));
System.out.println("Ok 2");
if (cate != null) {
System.out.println(cate.getTitle().getValue());
if (cate != null) {
JSONObject object = new JSONObject();
object.put("categoryAlias", cate.getAlias());
object.put("thumbImageUrl", cate.getThumbImageUrl()
.getValue());
object.put("count", cate.getCount());
object.put("subTitle", cate.getSubTitle());
object.put("description", cate.getDescription()
.getValue());
object.put("listTube", TubeModel.getByCategory(
cate.getAlias(), page, 10));
System.out.println("Get all ok, Category : "
+ cate.getAlias() + ", page : " + page);
response.getWriter().write(object.toString());
}
}
}
}
// view category.
else if (action.equalsIgnoreCase("view")) {
Category cate = CategoryModel.getByCateAlias(request
.getParameter("alias"));
if (cate != null) {
List<Tube> result = TubeModel.getAllByCategory(cate
.getAlias());
if (result.size() > 0) {
request.setAttribute("result", result);
request.setAttribute("cate", cate);
request.getRequestDispatcher("/category_view.jsp")
.forward(request, response);
}
} else {
}
}
// create category.
else if (action.equalsIgnoreCase("create")) {
request.getRequestDispatcher("/category_create.jsp").forward(
request, response);
}
// edit category.
else if (action.equalsIgnoreCase("edit")) {
response.getWriter().println("Edit.");
}
// delete category.
else if (action.equalsIgnoreCase("delete")) {
String id = request.getParameter("id");
if (id != null) {
Category cate = CategoryModel.getByCateAlias(id);
if (cate != null) {
cate.setStatus(4);
CategoryModel.update(cate);
CategoryModel.closePM();
List<Tube> listTube = TubeModel.getAllByCategory(cate
.getAlias());
for (Tube tube : listTube) {
tube = TubeModel.getByTubeId(tube.getTubeId());
tube.setCategoryAlias("");
TubeModel.update(tube);
}
TubeModel.closePM();
response.sendRedirect("/admin/category/manager");
} else {
response.getWriter().println("Category is not exits.");
}
} else {
response.getWriter().println(
"Invalid category. Please try again.");
}
}// edit list clips in category.
else if (action.equalsIgnoreCase("edit_videos")) {
Category cate = CategoryModel.getByCateAlias(request
.getParameter("id"));
if (cate != null) {
List<Tube> listTube = TubeModel.getAllByCategory(cate
.getAlias());
request.setAttribute("result", listTube);
request.setAttribute("cate", cate);
request.getRequestDispatcher("/category_edit_videos.jsp")
.forward(request, response);
} else {
response.getWriter().println("Category is not exits.");
}
}
// add clips to category.
else if (action.equalsIgnoreCase("add_video")) {
if (request.getParameter("nextId") == null
&& request.getParameter("prevId") == null) {
Category cate = CategoryModel.getByCateAlias(request
.getParameter("categoryAlias"));
Tube tub = TubeModel.getByTubeId(request
.getParameter("tubeId"));
if (tub == null) {
tub = TubeService.searchById(
request.getParameter("tubeId"), true);
}
if (tub.getCategoryAlias() != null
&& tub.getCategoryAlias().equalsIgnoreCase(
cate.getAlias())) {
} else {
cate.setCount(cate.getCount() + 1);
}
tub.setCategoryAlias(cate.getAlias());
tub.setCatePosition(1000000);
TubeModel.update(tub);
CategoryModel.update(cate);
TubeModel.closePM();
CategoryModel.closePM();
} else if (request.getParameter("prevId") == null) {
Category cate = CategoryModel.getByCateAlias(request
.getParameter("categoryAlias"));
Tube tub = TubeModel.getByTubeId(request
.getParameter("tubeId"));
if (tub == null) {
tub = TubeService.searchById(
request.getParameter("tubeId"), true);
}
if (tub.getCategoryAlias() != null
&& tub.getCategoryAlias().equalsIgnoreCase(
cate.getAlias())) {
} else {
cate.setCount(cate.getCount() + 1);
}
Tube nextTub = TubeModel.getByTubeId(request
.getParameter("nextId"));
tub.setCategoryAlias(cate.getAlias());
tub.setCatePosition(nextTub.getCatePosition() / 2);
TubeModel.update(tub);
CategoryModel.update(cate);
TubeModel.closePM();
CategoryModel.closePM();
} else if (request.getParameter("nextId") == null) {
Category cate = CategoryModel.getByCateAlias(request
.getParameter("categoryAlias"));
Tube tub = TubeModel.getByTubeId(request
.getParameter("tubeId"));
if (tub == null) {
tub = TubeService.searchById(
request.getParameter("tubeId"), true);
}
if (tub.getCategoryAlias() != null
&& tub.getCategoryAlias().equalsIgnoreCase(
cate.getAlias())) {
} else {
cate.setCount(cate.getCount() + 1);
}
Tube prevTub = TubeModel.getByTubeId(request
.getParameter("prevId"));
tub.setCategoryAlias(cate.getAlias());
tub.setCatePosition(prevTub.getCatePosition() + 1000000);
TubeModel.update(tub);
CategoryModel.update(cate);
TubeModel.closePM();
CategoryModel.closePM();
} else {
Category cate = CategoryModel.getByCateAlias(request
.getParameter("categoryAlias"));
Tube tub = TubeModel.getByTubeId(request
.getParameter("tubeId"));
if (tub == null) {
tub = TubeService.searchById(
request.getParameter("tubeId"), true);
}
if (tub.getCategoryAlias() != null
&& tub.getCategoryAlias().equalsIgnoreCase(
cate.getAlias())) {
} else {
cate.setCount(cate.getCount() + 1);
}
Tube nextTub = TubeModel.getByTubeId(request
.getParameter("nextId"));
Tube prevTub = TubeModel.getByTubeId(request
.getParameter("prevId"));
tub.setCategoryAlias(cate.getAlias());
// calculate tub position.
tub.setCatePosition((nextTub.getCatePosition() + prevTub
.getCatePosition()) / 2);
System.out.println(tub.getTitle().getValue()
+ " was added to category : "
+ cate.getTitle().getValue() + " between "
+ prevTub.getTitle().getValue() + " and "
+ nextTub.getTitle().getValue());
CategoryModel.update(cate);
TubeModel.update(tub);
CategoryModel.closePM();
TubeModel.closePM();
}
response.getWriter().println(
"<div><p style='color:green'>Update success</p></div>");
}
// reorder category index
else if (action.equalsIgnoreCase("re_order_index")) {
if (request.getParameter("prevAlias") == null
&& request.getParameter("nextAlias") == null) {
Category current = CategoryModel.getByCateAlias(request
.getParameter("currentAlias"));
current.setIndex(1000000);
CategoryModel.update(current);
CategoryModel.closePM();
} else if (request.getParameter("prevAlias") == null) {
Category current = CategoryModel.getByCateAlias(request
.getParameter("currentAlias"));
Category next = CategoryModel.getByCateAlias(request
.getParameter("nextAlias"));
current.setIndex(next.getIndex() / 2);
CategoryModel.update(current);
CategoryModel.closePM();
} else if (request.getParameter("nextAlias") == null) {
Category current = CategoryModel.getByCateAlias(request
.getParameter("currentAlias"));
Category pre = CategoryModel.getByCateAlias(request
.getParameter("prevAlias"));
current.setIndex(pre.getIndex() + 1000000);
CategoryModel.update(current);
CategoryModel.closePM();
} else {
Category current = CategoryModel.getByCateAlias(request
.getParameter("currentAlias"));
Category pre = CategoryModel.getByCateAlias(request
.getParameter("prevAlias"));
Category next = CategoryModel.getByCateAlias(request
.getParameter("nextAlias"));
current.setIndex((pre.getIndex() + next.getIndex()) / 2);
CategoryModel.update(current);
CategoryModel.closePM();
}
response.getWriter().println(
"<div><p style='color:green'>Update success</p></div>");
}
// remove clips from category.
else if (action.equalsIgnoreCase("remove_video")) {
Tube tub = TubeModel
.getByTubeId(request.getParameter("tubeId"));
Category cate = CategoryModel.getByCateAlias(request
.getParameter("categoryAlias"));
if (cate.getCount() > 0) {
cate.setCount(cate.getCount() - 1);
}
CategoryModel.update(cate);
CategoryModel.closePM();
tub.setCategoryAlias("");
TubeModel.update(tub);