1 40 package com.mvnforum.admin; 41 42 import java.io.IOException ; 43 import java.util.*; 44 45 import com.mvnforum.MVNForumConstant; 46 import com.mvnforum.admin.importexport.XMLUtil; 47 import com.mvnforum.admin.importexport.XMLWriter; 48 import com.mvnforum.auth.MVNForumPermission; 49 import com.mvnforum.db.*; 50 import net.myvietnam.mvncore.exception.*; 51 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter; 52 import net.myvietnam.mvncore.filter.EnableHtmlTagFilter; 53 54 61 public class GroupXML { 62 63 private int groupID; 64 66 public int getGroupID() { return groupID; } 67 68 public GroupXML() { 69 super(); 70 groupID=-1; 71 } 72 73 public void setGroupID(String id) { 74 groupID=XMLUtil.stringToIntDef(id, -1); 75 } 76 77 96 public void addGroup(String groupOwnerName, String groupName, 97 String groupDesc, String groupOption, 98 String groupCreationDate, String groupModifiedDate) 99 throws CreateException, DuplicateKeyException, ObjectNotFoundException, 100 DatabaseException, ForeignKeyNotFoundException { 101 String strGroupID=null; 102 if (groupID>=0) strGroupID=Integer.toString(groupID); 103 addGroup(strGroupID, groupOwnerName, groupName, 104 groupDesc, groupOption, groupCreationDate, groupModifiedDate); 105 } 106 107 129 public void addGroup(String strGroupID, 130 String groupOwnerName, String groupName, 131 String groupDesc, String groupOption, 132 String groupCreationDate, String groupModifiedDate) 133 throws CreateException, DuplicateKeyException, ObjectNotFoundException, 134 DatabaseException, ForeignKeyNotFoundException { 135 if ( (groupName==null) || (groupName.equals("")) ) { 136 throw new CreateException("Can't create a group with empty GroupName."); 137 } else { 138 int groupOption1; 139 java.sql.Timestamp groupCreationDate1; 140 java.sql.Timestamp groupModifiedDate1; 141 try { 142 if (groupOwnerName==null) groupOwnerName=""; 143 if (groupDesc==null) groupDesc=""; 144 groupOption1= XMLUtil.stringToIntDef(groupOption, 0); 145 groupCreationDate1= XMLUtil.stringToSqlTimestampDefNow(groupCreationDate); 146 groupModifiedDate1=XMLUtil.stringToSqlTimestampDefNow(groupModifiedDate); 147 } catch (NumberFormatException e) { 148 throw new CreateException("Invalid data for a group. Expected a number."); 149 } 150 151 if ( (strGroupID!=null) && (!strGroupID.equals("")) ) { 153 try { 154 if (Integer.parseInt(strGroupID)<0) strGroupID=null; 155 } catch (NumberFormatException e) { 156 strGroupID=null; 157 } 158 } else strGroupID=null; 159 160 groupName=EnableHtmlTagFilter.filter(groupName); 161 groupDesc=EnableHtmlTagFilter.filter(groupDesc); 162 if (strGroupID==null) { 163 DAOFactory.getGroupsDAO().create( 165 groupOwnerName, groupName, groupDesc, 166 groupOption1, groupCreationDate1, groupModifiedDate1); 167 } else { 168 int groupOwnerID=0; 169 try { 170 if (!groupOwnerName.equals("")) { 171 groupOwnerID=DAOFactory.getMemberDAO().getMemberIDFromMemberName(groupOwnerName); 172 } 173 } catch (ObjectNotFoundException e) { 174 groupOwnerID=0; 175 } 176 if (ImportWebHelper.execUpdateQuery( 177 "INSERT INTO "+ GroupsDAO.TABLE_NAME + 178 " (GroupID, GroupOwnerID, GroupOwnerName, GroupName, GroupDesc," + 179 " GroupOption, GroupCreationDate, GroupModifiedDate)" + 180 " VALUES (" +strGroupID+ ", " +groupOwnerID+ ", '"+groupOwnerName + 181 "', '" +groupName+ "', '" +groupDesc+ "', " +groupOption1+ 182 ", '" +groupCreationDate1+ "', '" +groupModifiedDate1+ "')" 183 ) != 1) { 184 throw new CreateException("Error adding group \""+groupName+"\" into table '"+ 185 GroupsDAO.TABLE_NAME +"'."); 186 } 187 } 188 189 this.groupID=DAOFactory.getGroupsDAO().getGroupIDFromGroupName(groupName); 190 } 191 } 192 193 207 public void addGroupPermission(String permission) 208 throws CreateException, DatabaseException, ForeignKeyNotFoundException { 209 if (groupID<0) { 210 throw new CreateException("Found group permission that is not assigned to any known group."); 211 } 212 int permission1; 213 try { 214 permission1=XMLUtil.stringToIntDef(permission, MVNForumPermission.PERMISSION_NO_PERMISSIONS); 215 } catch (NumberFormatException e) { 216 throw new CreateException("Invalid data for a group permission. Expected a number."); 217 } 218 try { 219 DAOFactory.getGroupPermissionDAO().create(groupID, permission1); 220 } catch (DuplicateKeyException e) { 221 } 223 } 224 225 public static void addRegisteredMembersGroupPermission(String permission) 226 throws CreateException, DatabaseException, DuplicateKeyException, 227 ForeignKeyNotFoundException { 228 int permission1; 229 try { 230 permission1=XMLUtil.stringToIntDef(permission, MVNForumPermission.PERMISSION_NO_PERMISSIONS); 231 } catch (NumberFormatException e) { 232 throw new CreateException("Invalid data for a group permission. Expected a number."); 233 } 234 DAOFactory.getGroupPermissionDAO().create(MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS, permission1); 235 } 236 237 public static void addGroupPermission(String groupname, String permission) 238 throws CreateException, DatabaseException, DuplicateKeyException, 239 ForeignKeyNotFoundException, ObjectNotFoundException { 240 int permission1; 241 try { 242 permission1=XMLUtil.stringToIntDef(permission, MVNForumPermission.PERMISSION_NO_PERMISSIONS); 243 } catch (NumberFormatException e) { 244 throw new CreateException("Invalid data for a group permission. Expected a number."); 245 } 246 DAOFactory.getGroupPermissionDAO().create(DAOFactory.getGroupsDAO().getGroupIDFromGroupName(groupname), permission1); 247 } 248 249 267 public void addMemberGroup(String memberName, String privilege, 268 String creationDate, String modifiedDate) 269 throws CreateException, DatabaseException, DuplicateKeyException, ForeignKeyNotFoundException { 270 if (groupID<0) { 271 throw new CreateException("Found group member that is not assigned to any known group."); 272 } 273 if ( (memberName==null) || (memberName.equals("")) ) { 274 throw new CreateException("Can't create a group member with empty MemberName."); 275 } 276 277 int privilege1; 278 java.sql.Timestamp creationDate1; 279 java.sql.Timestamp modifiedDate1; 280 try { 281 privilege1= XMLUtil.stringToIntDef(privilege, 0); 282 creationDate1= XMLUtil.stringToSqlTimestampDefNow(creationDate); 283 modifiedDate1= XMLUtil.stringToSqlTimestampDefNow(modifiedDate); 284 } catch (NumberFormatException e) { 285 throw new CreateException("Invalid data for a group member. Expected a number."); 286 } 287 DAOFactory.getMemberGroupDAO().create(this.groupID, memberName, 288 privilege1, creationDate1, modifiedDate1); 289 } 290 291 295 public static void exportGlobalPermissionsForGroup(XMLWriter xmlWriter, int groupID) 296 throws IOException , DatabaseException, ExportException { 297 Collection globalPermissions=ExportWebHelper.execSqlQuery( 298 "SELECT Permission"+ 299 " FROM "+GroupPermissionDAO.TABLE_NAME+ 300 " WHERE GroupID="+Integer.toString(groupID)); 301 Iterator iter=globalPermissions.iterator(); 302 String [] globalPermission=null; 303 xmlWriter.startElement("GlobalPermissionList"); 305 try { 306 while ( (globalPermission=(String [])iter.next()) !=null) { 307 if (globalPermission.length!=1) { 308 throw new ExportException("Error while retrieving data about global permissions for groupID=="+groupID); 309 } 310 xmlWriter.startElement("GlobalPermission"); 311 xmlWriter.writeData(globalPermission[0]); 312 xmlWriter.endElement("GlobalPermission"); 313 } 314 } catch (NoSuchElementException e) { 315 } 317 xmlWriter.endElement("GlobalPermissionList"); 318 } 320 321 public static void exportGroupMembersForGroup(XMLWriter xmlWriter, int groupID) 322 throws IOException , DatabaseException, ExportException { 323 Collection groupMembers=ExportWebHelper.execSqlQuery( 324 "SELECT MemberName, Privilege, CreationDate, ModifiedDate"+ 325 " FROM "+MemberGroupDAO.TABLE_NAME+ 326 " WHERE GroupID="+Integer.toString(groupID)); 327 Iterator iter=groupMembers.iterator(); 329 String [] groupMember=null; 330 xmlWriter.startElement("GroupMemberList"); 332 try { 333 while ( (groupMember=(String [])iter.next()) !=null) { 334 if (groupMember.length!=4) { 335 throw new ExportException("Error while retrieving data about group member for groupID=="+groupID); 336 } 337 xmlWriter.startElement("GroupMember"); 338 xmlWriter.startElement("MemberName"); 339 xmlWriter.writeData(groupMember[0]); 340 xmlWriter.endElement("MemberName"); 341 xmlWriter.startElement("Privilege"); 342 xmlWriter.writeData(groupMember[1]); 343 xmlWriter.endElement("Privilege"); 344 xmlWriter.startElement("CreationDate"); 345 xmlWriter.writeData(groupMember[2]); 346 xmlWriter.endElement("CreationDate"); 347 xmlWriter.startElement("ModifiedDate"); 348 xmlWriter.writeData(groupMember[3]); 349 xmlWriter.endElement("ModifiedDate"); 350 xmlWriter.endElement("GroupMember"); 351 } 352 } catch (NoSuchElementException e) { 353 } 355 xmlWriter.endElement("GroupMemberList"); 356 } 358 359 public static void exportGroup(XMLWriter xmlWriter, int groupID) 360 throws IOException , DatabaseException, ExportException { 361 Collection group1=ExportWebHelper.execSqlQuery( 362 "SELECT GroupOwnerName, GroupName,"+ 363 " GroupDesc, GroupOption, GroupCreationDate, GroupModifiedDate"+ 364 " FROM "+GroupsDAO.TABLE_NAME+ 365 " WHERE GroupID="+Integer.toString(groupID)); 366 Iterator iter=group1.iterator(); 367 String [] group=null; 368 try { 370 if ( (group=(String [])iter.next()) ==null) { 371 throw new ExportException("Can't find data for groupID=="+groupID); 372 } 373 if (group.length!=6) { 374 throw new ExportException("Error while retrieving data about group with groupID=="+groupID); 375 } 376 } catch (NoSuchElementException e) { 377 throw new ExportException("Can't find data for groupID=="+groupID); 378 } 379 380 if (groupID==MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS) { 382 xmlWriter.startElement("Group", new String []{"class", "RegisteredMembers"}); 383 } else { 384 xmlWriter.startElement("Group"); 385 } 386 xmlWriter.startElement("GroupOwnerName"); 387 xmlWriter.writeData(group[0]); 388 xmlWriter.endElement("GroupOwnerName"); 389 xmlWriter.startElement("GroupName"); 390 xmlWriter.writeData(DisableHtmlTagFilter.filter(group[1])); 391 xmlWriter.endElement("GroupName"); 392 xmlWriter.startElement("GroupDesc"); 393 xmlWriter.writeData(DisableHtmlTagFilter.filter(group[2])); 394 xmlWriter.endElement("GroupDesc"); 395 xmlWriter.startElement("GroupOption"); 396 xmlWriter.writeData(group[3]); 397 xmlWriter.endElement("GroupOption"); 398 xmlWriter.startElement("GroupCreationDate"); 399 xmlWriter.writeData(group[4]); 400 xmlWriter.endElement("GroupCreationDate"); 401 xmlWriter.startElement("GroupModifiedDate"); 402 xmlWriter.writeData(group[5]); 403 xmlWriter.endElement("GroupModifiedDate"); 404 exportGlobalPermissionsForGroup(xmlWriter, groupID); 405 exportGroupMembersForGroup(xmlWriter, groupID); 406 xmlWriter.endElement("Group"); 407 } 409 410 public static void exportGroupList(XMLWriter xmlWriter) 411 throws IOException , DatabaseException, ExportException { 412 Collection groupIDs=ExportWebHelper.execSqlQuery( 413 "SELECT GroupID"+ 414 " FROM "+GroupsDAO.TABLE_NAME); 415 Iterator iter=groupIDs.iterator(); 416 String [] groupID=null; 417 xmlWriter.startElement("GroupList"); 419 420 try { 421 exportGroup(xmlWriter, MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS); 422 } catch (Exception e) { 423 } 425 try { 426 while ( (groupID=(String [])iter.next()) !=null) { 427 if (groupID.length!=1) { 428 throw new ExportException("Error while retrieving list of groups."); 429 } 430 try { 431 int i=Integer.parseInt(groupID[0]); 432 if (i!=MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS) { 433 exportGroup(xmlWriter, i); 434 } 435 } catch (NumberFormatException e) { 436 throw new ExportException("Error while retrieving list of groups."); 437 } 438 } 439 } catch (NoSuchElementException e) { 440 } 442 xmlWriter.endElement("GroupList"); 443 } 445 446 447 } 448 | Popular Tags |