package org.mapache.ui.admin;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;
import org.mapache.business.BusinessController;
import org.mapache.business.BusinessUtils;
import org.mapache.business.MapacheException;
import org.mapache.business.blog.Blog;
import org.mapache.business.blog.BlogManager;
import org.mapache.business.category.Category;
import org.mapache.business.topic.Topic;
import org.mapache.ui.common.MapacheUIUtils;
public class AdminTopicManagementBean {
private BusinessController _bcControl;
private boolean _editMode = false,_createMode=true;
private Blog _blog;
private List<Topic> _topics;
private HtmlDataTable _topicTable;
private Topic _topic;
private String _title;
private String _content;
private boolean _visible;
private Calendar _publishDate;
private Category[] _categories;
public AdminTopicManagementBean() {
}
private BusinessController getBusinessController(){
if(_bcControl==null){
_bcControl = new BusinessController();
}
return _bcControl;
}
public List<SelectItem> getCategorySelectItems(){
List<SelectItem> categorySelectItems = new LinkedList<SelectItem>();
List<Category> categories;
try {
categories = getBusinessController().loadAllCategories();
for (Iterator it = categories.iterator(); it.hasNext(); ){
Category c = (Category)it.next();
categorySelectItems.add(new SelectItem(c,c.getName(),String.valueOf(c.getCategoryID())));
}
} catch (MapacheException e) {
// TODO log error message into file
e.printStackTrace();
}
return categorySelectItems;
}
public List<SelectItem> getBlogSelectItems() {
List<SelectItem> blogSelectItems = new LinkedList<SelectItem>();
Blog b = new Blog();
b.setBlogID(-1);
b.setTitle(MapacheUIUtils.getTranslation("pleaseSelect",null));
blogSelectItems.add(0,new SelectItem(b, b.getTitle(), String.valueOf(b.getBlogID())));
if(_blog==null){
_blog = b;
}
List<Blog> blogs;
try {
blogs = getBusinessController().loadAllBlogs();
for (Iterator it = blogs.iterator(); it.hasNext(); ){
Blog br = (Blog)it.next();
blogSelectItems.add(new SelectItem(br,br.getTitle(),String.valueOf(br.getBlogID())));
}
} catch (MapacheException e) {
// TODO log error message into file
e.printStackTrace();
}
return blogSelectItems;
}
public void setEditMode(boolean editMode) {
this._editMode = editMode;
}
public boolean isEditMode() {
return _editMode;
}
public void setBlog(Blog blog) {
this._blog = blog;
}
public Blog getBlog() {
return _blog;
}
public void setTopics(List<Topic> topics) {
this._topics = topics;
}
public List<Topic> getTopics() {
try {
_topics = getBusinessController().loadTopicsOfBlog(_blog);
} catch (MapacheException e) {
// TODO
e.printStackTrace();
}
return _topics;
}
public void setTopicTable(HtmlDataTable topicTable) {
this._topicTable = topicTable;
}
public HtmlDataTable getTopicTable() {
return _topicTable;
}
public String editTopic() {
_editMode = true;
_topic = (Topic)_topicTable.getRowData();
return "editClicked";
}
public void setTopic(Topic topic) {
this._topic = topic;
}
public Topic getTopic() {
return _topic;
}
public String cancel() {
_editMode = false;
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO,MapacheUIUtils.getTranslation("info_operation_canceled",null),MapacheUIUtils.getTranslation("info_operation_canceled",null)));
return "cancelClicked";
}
public String saveTopic() {
try{
getBusinessController().saveTopic(_topic);
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO,MapacheUIUtils.getTranslation("info_topic_saved",null),MapacheUIUtils.getTranslation("info_topic_saved",null)));
_editMode = false;
}
catch (MapacheException e) {
// TODO log error message into file
e.printStackTrace();
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,MapacheUIUtils.getTranslation("error_topic_saved",null),MapacheUIUtils.getTranslation("error_topic_saved",null)));
_editMode = true;
}
return "saveClicked";
}
public String deleteTopic() {
try{
getBusinessController().deleteTopic(_topic);
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO,MapacheUIUtils.getTranslation("info_topic_deleted",null),MapacheUIUtils.getTranslation("info_topic_deleted",null)));
_editMode= false;
}
catch(MapacheException e){
// TODO log error message into file
_editMode =true;
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,MapacheUIUtils.getTranslation("error_topic_deleted",null),MapacheUIUtils.getTranslation("error_topic_deleted",null)));
}
return "deleteClicked";
}
public String createTopic() {
Topic newTopic = new Topic();
newTopic.setTitle(_title);
newTopic.setContent(_content);
newTopic.setVisible(_visible);
newTopic.setPublishDate(_publishDate);
newTopic.setCategories(_categories);
try {
getBusinessController().createTopic(newTopic,_blog.getBlogID());
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO,MapacheUIUtils.getTranslation("info_topic_created",null),MapacheUIUtils.getTranslation("info_topic_created",null)));
_createMode = false;
} catch (MapacheException e) {
FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,MapacheUIUtils.getTranslation("error_topic_created",null),MapacheUIUtils.getTranslation("error_topic_created",null)));
_createMode = true;
e.printStackTrace();
}
return "createClicked";
}
public String ok() {
_title = _content = null;
_visible = false;
_categories = null;
_publishDate = null;
_blog= null;
_createMode=true;
return "okClicked";
}
public void setTitle(String title) {
this._title = title;
}
public String getTitle() {
return _title;
}
public void setCreateMode(boolean createMode) {
this._createMode = createMode;
}
public boolean isCreateMode() {
return _createMode;
}
public void setContent(String content) {
this._content = content;
}
public String getContent() {
return _content;
}
public void setVisible(boolean visible) {
this._visible = visible;
}
public boolean isVisible() {
return _visible;
}
public void setPublishDate(Calendar publishDate) {
this._publishDate = publishDate;
}
public Calendar getPublishDate() {
return _publishDate;
}
public void setCategories(Category[] categories) {
this._categories = categories;
}
public Category[] getCategories() {
return _categories;
}
public String getPublishDateString(){
return BusinessUtils.formatCalendar(_publishDate);
}
public void validateBlog(FacesContext facesContext,
UIComponent uiComponent, Object object) {
Blog b =(Blog)object;
if(b.getBlogID()==-1){
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,MapacheUIUtils.getTranslation("error_please_select_blog",null),MapacheUIUtils.getTranslation("error_please_select_blog",null)),null);
}
}
}