Index: /greenstone3/trunk/src/java/org/greenstone/gsdl3/service/Authentication.java
===================================================================
--- /greenstone3/trunk/src/java/org/greenstone/gsdl3/service/Authentication.java	(revision 14346)
+++ /greenstone3/trunk/src/java/org/greenstone/gsdl3/service/Authentication.java	(revision 14347)
@@ -196,4 +196,6 @@
 	    derbyWrapper.connectDatabase(usersDB_dir,true);
 	    derbyWrapper.createDatabase();
+	}else{
+	    derbyWrapper.connectDatabase(usersDB_dir, false);
 	}
 
@@ -202,4 +204,5 @@
 	    if (uan.equals("")){
 	    service_node.setAttribute("info","Login");
+	    derbyWrapper.closeDatabase();
 	    return result;
 	    }
@@ -210,4 +213,5 @@
 		    service_node.setAttribute("asn","0");
 		    service_node.setAttribute("info","Login");
+		     derbyWrapper.closeDatabase();
 		    return result;
 		}
@@ -216,7 +220,7 @@
 		    service_node.setAttribute("info","Login");
 		    service_node.setAttribute("err","un-pw-err");
+		    derbyWrapper.closeDatabase();
 		    return result;
 		}else{  
-		    derbyWrapper.connectDatabase(usersDB_dir, false);
 		    userQueryResult=derbyWrapper.findUser(un,pw);
 		    derbyWrapper.closeDatabase();
@@ -224,5 +228,4 @@
 		    
 		    service_node.setAttribute("un",un);
-		    service_node.setAttribute("pw",pw);
 		    //  service_node.setAttribute("asn",asn);
 		    if (userQueryResult==null){
@@ -231,4 +234,5 @@
 			service_node.setAttribute("err","un-pw-err");
 			service_node.setAttribute("info","Login");
+			derbyWrapper.closeDatabase();
 			return result;
 		    }else{
@@ -258,4 +262,5 @@
 		service_node.setAttribute("info","Login");
 		service_node.setAttribute("err","not-authen");
+		derbyWrapper.closeDatabase();
 		return result;
 	    }
@@ -264,6 +269,6 @@
 		service_node.setAttribute("info","Login");
 		service_node.setAttribute("un",un);
-		service_node.setAttribute("pw",pw);
 		service_node.setAttribute("asn",asn);
+		derbyWrapper.closeDatabase();
 		return result;
 	    }
@@ -274,4 +279,5 @@
 	    if (asn.equals("") && un.equals("")){
 		service_node.setAttribute("info","Login");
+		derbyWrapper.closeDatabase();
 		return result;
 	    }
@@ -283,9 +289,9 @@
 		service_node.setAttribute("un",un);
 		service_node.setAttribute("asn",asn);
+		derbyWrapper.closeDatabase();
 		return result;
 	    }
 	    //valid users are in the administrator group, they can list all users(asn=1)
 	    if (asn.equals("1")){
-		derbyWrapper.connectDatabase(usersDB_dir, false);
 		userQueryResult=derbyWrapper.findUser(null,null);
 		derbyWrapper.closeDatabase();
@@ -298,7 +304,9 @@
 		    Element user_node=getUserNode(userQueryResult);
 		    service_node.appendChild(user_node);
+		    derbyWrapper.closeDatabase();
 		    return result;
 		}else {
 		    service_node.setAttribute("err","no-un"); // no user returned
+		    derbyWrapper.closeDatabase();
 		    return result;
 		}    
@@ -308,8 +316,4 @@
 	//Action : adduder
 	if (aup.equals("AddUser")){
-	    if (asn.equals("") && un.equals("")){
-		service_node.setAttribute("info","Login");
-		return result;
-	    }
 	    //valid users can't add a new user because they aren't in the administrator group(asn=2)
 	    if (asn.equals("2")){
@@ -317,6 +321,6 @@
 		service_node.setAttribute("err","no-permission"); 
 		service_node.setAttribute("un",un);
-		service_node.setAttribute("pw",pw);
 		service_node.setAttribute("asn",asn);
+		derbyWrapper.closeDatabase();
 		return result;
 	    }
@@ -325,9 +329,9 @@
 		service_node.setAttribute(GSXML.NAME_ATT,"Authentication");
 		service_node.setAttribute("un",un);
-		service_node.setAttribute("pw",pw);
 		service_node.setAttribute("asn",asn);
 
 		if (umun.length()==0 && umpw.length()==0 && umgp.length()==0 && umas.length()==0 && umc.length()==0){
 		    service_node.setAttribute("info","adduser_interface");
+		    derbyWrapper.closeDatabase();
 		    return result;
 		}
@@ -337,10 +341,12 @@
 		    service_node.setAttribute("err","un-err"); //the input username string is illegal
 		    service_node.setAttribute("info","adduser_interface");
-		    return result;
-		}
-	
+		    derbyWrapper.closeDatabase();
+		    return result;
+		}
+		
 		if ((umpw==null) || (umpw.length()<3) || (umpw.length()>8) || (!(Pattern.matches("[\\p{ASCII}]+",umpw)))){
 		    service_node.setAttribute("err","pw-err"); //the input passwrod string is illegal
 		    service_node.setAttribute("info","adduser_interface");
+		    derbyWrapper.closeDatabase();
 		    return result;
 		}
@@ -348,17 +354,26 @@
 		// add the new users into the users table
 		umgp=umgp.replaceAll(" ","");//get rid of the space of the groups string
-		derbyWrapper.connectDatabase(usersDB_dir, false);
-		String add_user=derbyWrapper.addUser(umun,umpw,umgp,umas,umc);
-		if (add_user.equals("succeed")){
-		    userQueryResult=derbyWrapper.findUser(null,null);
-		    derbyWrapper.closeDatabase();
-		    service_node.setAttribute("info","all-un"); // return a list of all users if the user has been added
-		    Element user_node=getUserNode(userQueryResult);
-		    service_node.appendChild(user_node);
+		userQueryResult=derbyWrapper.findUser(umun,null);// check whether the new user name has existed in the table.
+		if (userQueryResult!=null){
+		    service_node.setAttribute("err","un-exist"); //the new username string is duplicated
+		    service_node.setAttribute("info","adduser_interface");
+		    derbyWrapper.closeDatabase();
 		    return result;
 		}else{
-		    derbyWrapper.closeDatabase();
-		    service_node.setAttribute("err",add_user);// return the error message if the user couldn't be added 
-		    return result;
+		    String add_user=derbyWrapper.addUser(umun,umpw,umgp,umas,umc);
+		    if (add_user.equals("succeed")){
+			userQueryResult=derbyWrapper.findUser(null,null);
+			derbyWrapper.closeDatabase();
+			service_node.setAttribute("info","all-un"); // return a list of all users if the user has been added
+			Element user_node=getUserNode(userQueryResult);
+			service_node.appendChild(user_node);
+			derbyWrapper.closeDatabase();
+			return result;
+		    }else{
+			derbyWrapper.closeDatabase();
+			service_node.setAttribute("err",add_user);// return the error message if the user couldn't be added 
+			derbyWrapper.closeDatabase();
+			return result;
+		    }
 		}
 	    }
@@ -367,8 +382,6 @@
 	//Action: edituser
 	if (aup.equals("EditUser")){
-	    derbyWrapper.connectDatabase(usersDB_dir, false);
 	    service_node.setAttribute(GSXML.NAME_ATT,"Authentication");
 	    service_node.setAttribute("un",un);
-	    service_node.setAttribute("pw",pw);
 	    service_node.setAttribute("asn",asn);
 	    
@@ -391,4 +404,5 @@
 		service_node.setAttribute("umas",accountstatus);
 		service_node.setAttribute("umc",comment);
+		derbyWrapper.closeDatabase();
 		return result;
 	    }
@@ -404,4 +418,5 @@
 			Element user_node=getUserNode(userQueryResult);
 			service_node.appendChild(user_node);
+			derbyWrapper.closeDatabase();
 			return result;
 		    }else{
@@ -415,13 +430,15 @@
 			    service_node.setAttribute("umc",umc);
 			    service_node.setAttribute("oumun",oumun);
+			    derbyWrapper.closeDatabase();
 			    return result;
 			}
 			umgp=umgp.replaceAll(" ","");// get rid of the space
 			derbyWrapper.modifyUserInfo(umun,umpw,umgp,umas,umc);
-			userQueryResult=derbyWrapper.findUser(null,null);
+			userQueryResult=derbyWrapper.listAllUser();
 			derbyWrapper.closeDatabase();
 			service_node.setAttribute("info","all-un"); // if the new user has been added successfully, return a list of all users 
 			Element user_node=getUserNode(userQueryResult);
 			service_node.appendChild(user_node);
+			derbyWrapper.closeDatabase();
 			return result;
 		    }
@@ -438,4 +455,5 @@
 			service_node.setAttribute("oumun",oumun);
 			service_node.setAttribute("info","edituser-interface");
+			derbyWrapper.closeDatabase();
 			return result;
 		    }
@@ -449,4 +467,5 @@
 			service_node.setAttribute("umc",umc);
 			service_node.setAttribute("oumun",oumun);
+			derbyWrapper.closeDatabase();
 			return result;
 		    }
@@ -460,23 +479,40 @@
 			service_node.setAttribute("umc",umc);
 			service_node.setAttribute("oumun",oumun);
+			derbyWrapper.closeDatabase();
 			return result;
 		    }
 		    umgp=umgp.replaceAll(" ","");// get rid of the space
-		    derbyWrapper.addUser(umun,umpw,umgp,umas,umc);
-		    userQueryResult=derbyWrapper.findUser(null,null);
-		    derbyWrapper.closeDatabase();
-		    service_node.setAttribute("info","all-un"); // if the new user has been added successfully, return a list of all users 
-		    Element user_node=getUserNode(userQueryResult);
-		    service_node.appendChild(user_node);
-		    return result;
+		    userQueryResult=derbyWrapper.findUser(umun,null);// check whether the new user name has existed in the table.
+		    if (userQueryResult!=null){
+			service_node.setAttribute("err","un-exist"); //the new username string is duplicated
+			service_node.setAttribute("info","edituser-interface");
+			service_node.setAttribute("umun","");
+			service_node.setAttribute("umpw","");
+			service_node.setAttribute("umgp",umgp);
+			service_node.setAttribute("umas",umas);
+			service_node.setAttribute("umc",umc);
+			service_node.setAttribute("oumun",oumun);
+			derbyWrapper.closeDatabase();
+			return result;
+		    }else{
+			derbyWrapper.addUser(umun,umpw,umgp,umas,umc);
+			userQueryResult=derbyWrapper.listAllUser();
+			derbyWrapper.closeDatabase();
+			service_node.setAttribute("info","all-un"); // if the new user has been added successfully, return a list of all users 
+			Element user_node=getUserNode(userQueryResult);
+			service_node.appendChild(user_node);
+			derbyWrapper.closeDatabase();
+			return result;
+		    }
 		}
 	    }
 
 	    if (cm.toLowerCase().equals("cancel")){
-		userQueryResult=derbyWrapper.findUser(null,null);
+		userQueryResult=derbyWrapper.listAllUser();
 		derbyWrapper.closeDatabase();
 		service_node.setAttribute("info","all-un"); // if the new user has been added successfully, return a list of all users 
 		Element user_node=getUserNode(userQueryResult);
 		service_node.appendChild(user_node);
+		derbyWrapper.closeDatabase();
 		return result;
 	    }
@@ -487,8 +523,8 @@
 	    if (un.equals("")){
 		service_node.setAttribute("info","Login");
-		return result;
-	    }
-
-	    derbyWrapper.connectDatabase(usersDB_dir, false);
+		derbyWrapper.closeDatabase();
+		return result;
+	    }
+
 	    service_node.setAttribute(GSXML.NAME_ATT,"Authentication");
 	    service_node.setAttribute("un",un);
@@ -561,7 +597,5 @@
 	//Action: deleteuser
 	if (aup.equals("DeleteUser")){
-	    derbyWrapper.connectDatabase(usersDB_dir, false);
 	    service_node.setAttribute("un",un);
-	    service_node.setAttribute("pw",pw);
 	    service_node.setAttribute("asn",asn);
 	    service_node.setAttribute("umun",umun);
@@ -570,5 +604,5 @@
 		if (delete_user.equals("succeed")){
 		    service_node.setAttribute("err","");
-		    userQueryResult=derbyWrapper.findUser(null,null);
+		    userQueryResult=derbyWrapper.listAllUser();
 		    service_node.setAttribute("info","all-un"); //  return a list of all users 
 		    Element user_node=getUserNode(userQueryResult);
@@ -581,5 +615,5 @@
 	    }else if (cm.equals("no")){
 		service_node.setAttribute("err","");
-		userQueryResult=derbyWrapper.findUser(null,null);
+		userQueryResult=derbyWrapper.listAllUser();
 		service_node.setAttribute("info","all-un"); //  return a list of all users 
 		Element user_node=getUserNode(userQueryResult);
