1 40 package com.mvnforum.admin; 41 42 import java.io.IOException ; 43 import java.util.*; 44 45 import com.mvnforum.admin.importexport.XMLUtil; 46 import com.mvnforum.admin.importexport.XMLWriter; 47 import com.mvnforum.db.*; 48 import net.myvietnam.mvncore.exception.*; 49 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter; 50 import net.myvietnam.mvncore.filter.EnableHtmlTagFilter; 51 52 59 public class CategoryXML { 60 61 private int categoryID; 62 64 public int getCategoryID() { return categoryID; } 65 66 private int parentCategoryID; 67 68 70 public int getParentCategoryID() { 71 return parentCategoryID; 72 } 73 74 public CategoryXML() { 75 super(); 76 categoryID = -1; 77 parentCategoryID = 0; 78 } 79 80 public void setCategoryID(String id) { 81 categoryID = XMLUtil.stringToIntDef(id, -1); 82 } 83 84 public void setParentCategory(CategoryXML parentCategory) { 85 parentCategoryID = parentCategory.getCategoryID(); 86 } 87 88 public void setParentCategoryID(int value) { 89 if (value < 0) parentCategoryID = -1; 90 else parentCategoryID = value; 91 } 92 93 113 public void addCategory(String categoryName, 114 String categoryDesc, String categoryCreationDate, 115 String categoryModifiedDate, String categoryOrder, 116 String categoryOption, String categoryStatus) 117 throws CreateException, DuplicateKeyException, ObjectNotFoundException, 118 DatabaseException, ForeignKeyNotFoundException { 119 120 if ((categoryName == null) || (categoryName.equals(""))) { 122 throw new CreateException("Can't create a category with empty CategoryName."); 123 } else { 124 java.sql.Timestamp categoryCreationDate1; 125 java.sql.Timestamp categoryModifiedDate1; 126 int categoryOrder1; 127 int categoryOption1; 128 int categoryStatus1; 129 130 try { 131 if (categoryDesc == null) categoryDesc = ""; 132 categoryCreationDate1 = XMLUtil.stringToSqlTimestampDefNow(categoryCreationDate); 133 categoryModifiedDate1 = XMLUtil.stringToSqlTimestampDefNow(categoryModifiedDate); 134 categoryOrder1 = XMLUtil.stringToIntDef(categoryOrder, 0); 135 categoryOption1 = XMLUtil.stringToIntDef(categoryOption, 0); 136 categoryStatus1 = XMLUtil.stringToIntDef(categoryStatus, 0); 137 } catch (NumberFormatException e) { 138 throw new CreateException("Invalid data for a category. Expected a number."); 139 } 140 141 categoryName = EnableHtmlTagFilter.filter(categoryName); 142 categoryDesc = EnableHtmlTagFilter.filter(categoryDesc); 143 144 DAOFactory.getCategoryDAO().create( 145 parentCategoryID, categoryName, categoryDesc, 146 categoryCreationDate1, categoryModifiedDate1, 147 categoryOrder1, categoryOption1, categoryStatus1); 148 149 Collection categories = DAOFactory.getCategoryDAO().getCategories(); 151 Iterator iter = categories.iterator(); 152 try { 153 CategoryBean cat = null; 154 categoryID = -1; 155 while ((cat = (CategoryBean) iter.next()) != null) { 156 if ((cat.getCategoryName().equals(categoryName)) && 157 (cat.getParentCategoryID() == parentCategoryID)) { 158 categoryID = cat.getCategoryID(); 159 break; 160 } 161 } 162 if (categoryID < 0) { 163 throw new ObjectNotFoundException("Can't find category I've just added."); 164 } 165 } catch (NoSuchElementException e) { 166 throw new ObjectNotFoundException("Can't find category I've just added."); 167 } 168 169 } 170 } 171 172 194 public void addCategoryWatch(String memberName, 195 String watchType, String watchOption, 196 String watchStatus, String watchCreationDate, 197 String watchLastSentDate, String watchEndDate) 198 throws CreateException, DatabaseException, ObjectNotFoundException, 199 DuplicateKeyException, ForeignKeyNotFoundException { 200 201 if (categoryID < 0) { 202 throw new CreateException("Found category watch that is not assigned to any known category."); 203 } 204 205 int watchType1; 206 int watchOption1; 207 int watchStatus1; 208 java.sql.Timestamp watchCreationDate1; 209 java.sql.Timestamp watchLastSentDate1; 210 java.sql.Timestamp watchEndDate1; 211 212 try { 213 if (memberName == null) memberName = ""; 214 watchType1 = XMLUtil.stringToIntDef(watchType, 0); 215 watchOption1 = XMLUtil.stringToIntDef(watchOption, 0); 216 watchStatus1 = XMLUtil.stringToIntDef(watchStatus, 0); 217 watchCreationDate1 = XMLUtil.stringToSqlTimestampDefNow(watchCreationDate); 218 watchLastSentDate1 = XMLUtil.stringToSqlTimestampDefNull(watchLastSentDate); 219 watchEndDate1 = XMLUtil.stringToSqlTimestampDefNull(watchEndDate); 220 } catch (NumberFormatException e) { 221 throw new CreateException("Invalid data for a category. Expected a number."); 222 } 223 224 int memberID = 0; 226 if (!memberName.equals("")) { 227 memberID = DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName); 228 } 229 DAOFactory.getWatchDAO().create( 230 memberID, categoryID, 0, 0, 231 watchType1, watchOption1, watchStatus1, 232 watchCreationDate1, watchLastSentDate1, watchEndDate1); 233 } 234 235 239 public static void exportCategoryWatchesForCategory(XMLWriter xmlWriter, int categoryID) 240 throws IOException , ExportException, NumberFormatException , 241 ObjectNotFoundException, DatabaseException { 242 243 Collection categoryWatches=ExportWebHelper.execSqlQuery( 244 "SELECT MemberID, WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate"+ 245 " FROM "+WatchDAO.TABLE_NAME+ 246 " WHERE ForumID=0 AND ThreadID=0"+ 247 " AND CategoryID="+Integer.toString(categoryID)); 248 Iterator iter=categoryWatches.iterator(); 249 String [] categoryWatch=null; 250 xmlWriter.startElement("CategoryWatchList"); 252 try { 253 while ( (categoryWatch=(String [])iter.next()) !=null) { 254 if (categoryWatch.length!=7) { 255 throw new ExportException("Error while retrieving data about category watch for categoryID=="+categoryID); 256 } 257 String memberName=DAOFactory.getMemberDAO().getMember_forPublic(Integer.parseInt(categoryWatch[0])).getMemberName(); 258 xmlWriter.startElement("CategoryWatch"); 259 xmlWriter.startElement("MemberName"); 260 xmlWriter.writeData(memberName); 261 xmlWriter.endElement("MemberName"); 262 xmlWriter.startElement("WatchType"); 263 xmlWriter.writeData(categoryWatch[1]); 264 xmlWriter.endElement("WatchType"); 265 xmlWriter.startElement("WatchOption"); 266 xmlWriter.writeData(categoryWatch[2]); 267 xmlWriter.endElement("WatchOption"); 268 xmlWriter.startElement("WatchStatus"); 269 xmlWriter.writeData(categoryWatch[3]); 270 xmlWriter.endElement("WatchStatus"); 271 xmlWriter.startElement("WatchCreationDate"); 272 xmlWriter.writeData(categoryWatch[4]); 273 xmlWriter.endElement("WatchCreationDate"); 274 xmlWriter.startElement("WatchLastSentDate"); 275 xmlWriter.writeData(categoryWatch[5]); 276 xmlWriter.endElement("WatchLastSentDate"); 277 xmlWriter.startElement("WatchEndDate"); 278 xmlWriter.writeData(categoryWatch[6]); 279 xmlWriter.endElement("WatchEndDate"); 280 xmlWriter.endElement("CategoryWatch"); 281 } 282 } catch (NoSuchElementException e) { 283 } 285 xmlWriter.endElement("CategoryWatchList"); 286 } 288 289 public static void exportCategory(XMLWriter xmlWriter, int categoryID) 290 throws NumberFormatException , IOException , ExportException, ObjectNotFoundException, DatabaseException { 291 292 Collection category1=ExportWebHelper.execSqlQuery( 293 "SELECT CategoryName, CategoryDesc,"+ 294 " CategoryCreationDate, CategoryModifiedDate, CategoryOrder,"+ 295 " CategoryOption, CategoryStatus FROM "+CategoryDAO.TABLE_NAME+ 296 " WHERE CategoryID="+Integer.toString(categoryID)); 297 Iterator iter = category1.iterator(); 298 String [] category = null; 299 try { 301 if ((category = (String []) iter.next()) == null) { 302 throw new ExportException("Can't find data for categoryID==" + categoryID); 303 } 304 if (category.length != 7) { 305 throw new ExportException("Error while retrieving data about category with categoryID==" + categoryID); 306 } 307 } catch (NoSuchElementException e) { 308 throw new ExportException("Can't find data for categoryID=="+categoryID); 309 } 310 311 xmlWriter.startElement("Category"); 313 xmlWriter.startElement("CategoryName"); 314 xmlWriter.writeData(DisableHtmlTagFilter.filter(category[0])); 315 xmlWriter.endElement("CategoryName"); 316 xmlWriter.startElement("CategoryDesc"); 317 xmlWriter.writeData(DisableHtmlTagFilter.filter(category[1])); 318 xmlWriter.endElement("CategoryDesc"); 319 xmlWriter.startElement("CategoryCreationDate"); 320 xmlWriter.writeData(category[2]); 321 xmlWriter.endElement("CategoryCreationDate"); 322 xmlWriter.startElement("CategoryModifiedDate"); 323 xmlWriter.writeData(category[3]); 324 xmlWriter.endElement("CategoryModifiedDate"); 325 xmlWriter.startElement("CategoryOrder"); 326 xmlWriter.writeData(category[4]); 327 xmlWriter.endElement("CategoryOrder"); 328 xmlWriter.startElement("CategoryOption"); 329 xmlWriter.writeData(category[5]); 330 xmlWriter.endElement("CategoryOption"); 331 xmlWriter.startElement("CategoryStatus"); 332 xmlWriter.writeData(category[6]); 333 xmlWriter.endElement("CategoryStatus"); 334 exportCategoryWatchesForCategory(xmlWriter, categoryID); 335 ForumXML.exportForumList(xmlWriter, categoryID); 336 exportSubCategoryList(xmlWriter, categoryID); 337 xmlWriter.endElement("Category"); 338 } 340 341 public static void exportSubCategoryList(XMLWriter xmlWriter, int parentCategoryID) 342 throws IOException , ExportException, ObjectNotFoundException, DatabaseException { 343 344 Collection categoryIDs=ExportWebHelper.execSqlQuery( 345 "SELECT CategoryID"+ 346 " FROM "+CategoryDAO.TABLE_NAME+ 347 " WHERE ParentCategoryID="+Integer.toString(parentCategoryID)); 348 Iterator iter = categoryIDs.iterator(); 349 String [] categoryID = null; 350 xmlWriter.startElement("CategoryList"); 352 try { 353 while ((categoryID = (String []) iter.next()) != null) { 354 if (categoryID.length != 1) { 355 throw new ExportException("Error while retrieving list of categories."); 356 } 357 try { 358 int i = Integer.parseInt(categoryID[0]); 359 exportCategory(xmlWriter, i); 360 } catch (NumberFormatException e) { 361 throw new ExportException("Error while retrieving list of categories."); 362 } 363 } 364 } catch (NoSuchElementException e) { 365 } 367 xmlWriter.endElement("CategoryList"); 368 } 370 371 public static void exportCategoryList(XMLWriter xmlWriter) 372 throws IOException , ExportException, ObjectNotFoundException, DatabaseException { 373 374 exportSubCategoryList(xmlWriter, 0); 375 } 376 377 } 378 379 | Popular Tags |