<%@ page contentType="text/html;charset=GBK" %>
<%@ include file="../checkLogin.jsp" %>
<%@ page import="gbase.db.*,java.util.*,gbase.util.*,com.jivesoftware.forum.database.*,com.jivesoftware.forum.*,com.jivesoftware.forum.util.*"%>
<%
	
	String message="操作失败";
	String action = request.getParameter("action")==null?"":request.getParameter("action");
	String url="jive_bar_list.jsp";
	
	String forumID=request.getParameter("forumID")==null?"0":request.getParameter("forumID");
	if(forumID.equals(""))
	forumID="0";
        String name=request.getParameter("name")==null?"":request.getParameter("name");
	String description=request.getParameter("description")==null?"":request.getParameter("description");
	String jive_right=request.getParameter("jive_right")==null?"0":request.getParameter("jive_right");
	int int_jive_right=Integer.parseInt(jive_right);
	long long_forumID=Long.parseLong(forumID);
	DBOperation theDBOperation = new DBOperation();
        String[] sql=new String[2];
        String insert_sql="";
        String update_sql="";
        int i=-1;
	if(action.equals("update"))
	  { 
	   Authorization authToken = AuthorizationFactory.getAuthorization("admin-user-web-admin", "beitian222888");
           ForumFactory forumFactory = ForumFactory.getInstance(authToken);
           Forum forum = forumFactory.getForum(long_forumID);
           forum.setName(name);
           forum.setDescription(description);
           PermissionsManager permManager = forum.getPermissionsManager();
           permManager.removeAllUserPermissions();
           // set permissions
           switch (int_jive_right) {
            case 1:
                // do nothing -- global permission are used by default
                break;
            case 3:
                permManager.addRegisteredUserPermission(ForumPermissions.READ);
                permManager.addRegisteredUserPermission(ForumPermissions.CREATE_THREAD);
                permManager.addRegisteredUserPermission(ForumPermissions.CREATE_MESSAGE);
                break;
            case 4:
                permManager.addRegisteredUserPermission(ForumPermissions.READ);
                permManager.addRegisteredUserPermission(ForumPermissions.CREATE_THREAD);
                permManager.addRegisteredUserPermission(ForumPermissions.CREATE_MESSAGE);
                permManager.addAnonymousUserPermission(ForumPermissions.READ);
                break;
            case 2:
                permManager.addRegisteredUserPermission(ForumPermissions.READ);
                permManager.addRegisteredUserPermission(ForumPermissions.CREATE_THREAD);
                permManager.addRegisteredUserPermission(ForumPermissions.CREATE_MESSAGE);
                permManager.addAnonymousUserPermission(ForumPermissions.READ);
                permManager.addAnonymousUserPermission(ForumPermissions.CREATE_THREAD);
                permManager.addAnonymousUserPermission(ForumPermissions.CREATE_MESSAGE);
                break;
            default:
          }
	   update_sql="update web_jive set jive_right='"+jive_right+"' where forumID="+forumID+"";
	   i=theDBOperation.executeSql(update_sql);
	  }
	else if(action.equals("delete"))
	  {
	  Authorization authToken = AuthorizationFactory.getAuthorization("admin-user-web-admin", "beitian222888");
          ForumFactory forumFactory = ForumFactory.getInstance(authToken);
          Forum forum = forumFactory.getForum(long_forumID);
          PermissionsManager permManager = forum.getPermissionsManager();
          permManager.removeAllUserPermissions();
	  forumFactory.deleteForum(forum);
	  sql[0]="delete from jiveforum  where forumID="+forumID+"";
	  sql[1]="delete from web_jive  where forumID="+forumID+"";
	  i=theDBOperation.executeSqls(sql);
	  }
	else
	  {
	  //DbForumFactory theDbForumFactory=new DbForumFactory();
	  //Forum forum = theDbForumFactory.createForum(name, description);
	  Authorization authToken = AuthorizationFactory.getAuthorization("admin-user-web-admin", "beitian222888");
          ForumFactory forumFactory = ForumFactory.getInstance(authToken);
          Forum forum = forumFactory.createForum(name, description);
	  PermissionsManager permManager = forum.getPermissionsManager();

        // set permissions
        switch (int_jive_right) {
            case 1:
                // do nothing -- global permission are used by default
                break;
            case 3:
                permManager.addRegisteredUserPermission(ForumPermissions.READ);
                permManager.addRegisteredUserPermission(ForumPermissions.CREATE_THREAD);
                permManager.addRegisteredUserPermission(ForumPermissions.CREATE_MESSAGE);
                break;
            case 4:
                permManager.addRegisteredUserPermission(ForumPermissions.READ);
                permManager.addRegisteredUserPermission(ForumPermissions.CREATE_THREAD);
                permManager.addRegisteredUserPermission(ForumPermissions.CREATE_MESSAGE);
                permManager.addAnonymousUserPermission(ForumPermissions.READ);
                break;
            case 2:
                permManager.addRegisteredUserPermission(ForumPermissions.READ);
                permManager.addRegisteredUserPermission(ForumPermissions.CREATE_THREAD);
                permManager.addRegisteredUserPermission(ForumPermissions.CREATE_MESSAGE);
                permManager.addAnonymousUserPermission(ForumPermissions.READ);
                permManager.addAnonymousUserPermission(ForumPermissions.CREATE_THREAD);
                permManager.addAnonymousUserPermission(ForumPermissions.CREATE_MESSAGE);
                break;
            default:
        }
	  SeqIdGenerater theSeqIdGenerater=new SeqIdGenerater();
	  int seqId=theSeqIdGenerater.generatorKey("web_jive");
	  long_forumID=forum.getID();
	  insert_sql="insert into web_jive(id,forumID,web_account,jive_right) values("+seqId+","+long_forumID+",'"+web_account+"','"+jive_right+"')";
	  i=theDBOperation.executeSql(insert_sql);
	  }
	try {
        if(i>0)
          {
          
          message = "操作成功!";
          }
        else
          message = "操作失败!";
      }
      catch (Exception ex) {
        message = "操作失败!";
      }
	
%>
<script language="javascript">
	function process ( message,url){
		alert(message);
		self.location = url;
	}
</script>
<html>
	<head>
		<title></title>
	</head>
	<body onload="javascript: process('<%=message%>','<%=url%>');">
	</body>
</html>