1 19 package org.openharmonise.him.editors; 20 21 import java.io.*; 22 import java.util.*; 23 24 import org.openharmonise.him.harmonise.*; 25 import org.openharmonise.vfs.*; 26 import org.openharmonise.vfs.context.*; 27 import org.openharmonise.vfs.status.*; 28 29 30 40 public class EditorController implements ContextListener { 41 42 45 private HashMap m_VirtualPhysicalMapping = new HashMap(); 46 47 50 private static EditorController m_instance = null; 51 52 55 private EditorController() { 56 super(); 57 FileMonitor fm = new FileMonitor(1000 * 5); 58 fm.start(); 59 ContextHandler.getInstance().addListener( 60 ContextType.CONTEXT_SHUTDOWN, 61 this); 62 } 63 64 70 public static EditorController getInstance() { 71 if (m_instance == null) { 72 m_instance = new EditorController(); 73 } 74 return m_instance; 75 } 76 77 83 public StatusData open(String sPath, AbstractVirtualFileSystem vfs) { 84 StatusData status = new VFSStatus(); 85 86 Editor editor = this.getEditor(sPath, vfs); 87 if (editor != null) { 88 PathStatusWrapper pathStatus = editor.open(sPath, vfs); 89 String sRealPath = pathStatus.getPath(); 90 status = pathStatus.getStatus(); 91 if (sRealPath != null) { 92 File fFile = new File(sRealPath); 93 long modified = fFile.lastModified(); 94 FileDetails details = new FileDetails(sRealPath, sPath, vfs, editor); 95 details.setModified(modified); 96 this.m_VirtualPhysicalMapping.put(sPath, details); 97 } 98 } 99 100 return status; 101 } 102 103 109 public StatusData preview(String sPath, AbstractVirtualFileSystem vfs) { 110 StatusData status = new VFSStatus(); 111 112 Editor editor = this.getEditor(sPath, vfs); 113 if (editor != null) { 114 PathStatusWrapper pathStatus = editor.preview(sPath, vfs); 115 String sRealPath = pathStatus.getPath(); 116 status = pathStatus.getStatus(); 117 if (sRealPath != null) { 118 File fFile = new File(sRealPath); 119 } 120 } 121 122 return status; 123 } 124 125 131 public StatusData export(String sPath, AbstractVirtualFileSystem vfs) { 132 StatusData status = new VFSStatus(); 133 134 Editor editor = this.getEditor(sPath, vfs); 135 if (editor != null) { 136 status = editor.export(sPath, vfs); 137 } 138 139 return status; 140 } 141 142 149 public StatusData createNew(String sPath, AbstractVirtualFileSystem vfs) { 150 StatusData status = new VFSStatus(); 151 152 Editor editor = this.getEditor(sPath, vfs); 153 if (editor != null) { 154 PathStatusWrapper pathStatus = editor.createNew(sPath, vfs); 155 String sRealPath = pathStatus.getPath(); 156 status = pathStatus.getStatus(); 157 if (sRealPath != null) { 158 File fFile = new File(sRealPath); 159 long modified = fFile.lastModified(); 160 FileDetails details = new FileDetails(sRealPath, sPath, vfs, editor); 161 details.setModified(modified); 162 this.m_VirtualPhysicalMapping.put(sPath, details); 163 } 164 if(!editor.hasResourceBeenCreated()) { 165 status.setStatusLevel(StatusData.LEVEL_ERROR); 166 System.err.println("Editor says resource NOT created."); 167 } else { 168 System.err.println("Editor says resource WAS created."); 169 } 170 return status; 171 } 172 status.setStatusLevel(StatusData.LEVEL_ERROR); 173 return status; 174 } 175 176 184 public StatusData createNew( 185 String sPath, 186 byte[] content, 187 AbstractVirtualFileSystem vfs) { 188 StatusData status = new VFSStatus(); 189 190 Editor editor = this.getEditor(sPath, vfs); 191 if (editor != null) { 192 PathStatusWrapper pathStatus = editor.createNew(sPath, content, vfs); 193 String sRealPath = pathStatus.getPath(); 194 status = pathStatus.getStatus(); 195 if (sRealPath != null) { 196 File fFile = new File(sRealPath); 197 long modified = fFile.lastModified(); 198 FileDetails details = new FileDetails(sRealPath, sPath, vfs, editor); 199 details.setModified(modified); 200 this.m_VirtualPhysicalMapping.put(sPath, details); 201 } 202 if(!editor.hasResourceBeenCreated()) { 203 status.setStatusLevel(StatusData.LEVEL_ERROR); 204 System.err.println("Editor says resource NOT created."); 205 } else { 206 System.err.println("Editor says resource WAS created."); 207 } 208 return status; 209 } 210 status.setStatusLevel(StatusData.LEVEL_ERROR); 211 return status; 212 } 213 214 221 public StatusData discardChanges(String sPath, AbstractVirtualFileSystem vfs) { 222 StatusData status = new VFSStatus(); 223 224 Editor editor = this.getEditor(sPath, vfs); 225 if (editor != null) { 226 status = editor.discardChanges(sPath, vfs); 227 } 228 return status; 229 } 230 231 235 public StatusData upload(String sPath, AbstractVirtualFileSystem vfs) { 236 StatusData status = new VFSStatus(); 237 238 Editor editor = this.getEditor(sPath, vfs); 239 if (editor != null) { 240 PathStatusWrapper pathStatus = editor.upload(sPath, vfs); 241 String sRealPath = pathStatus.getPath(); 242 status = pathStatus.getStatus(); 243 if (sRealPath != null) { 244 File fFile = new File(sRealPath); 245 long modified = fFile.lastModified(); 246 FileDetails details = new FileDetails(sRealPath, sPath, vfs, editor); 247 details.setModified(modified); 248 this.m_VirtualPhysicalMapping.put(sPath, details); 249 } 250 } 251 return status; 252 } 253 254 262 private Editor getEditor(String sPath, AbstractVirtualFileSystem vfs) { 263 Editor editor = null; 264 265 editor = this.getLiveEditor(sPath, vfs); 266 if (editor == null) { 267 editor = this.getNoneLiveEditor(sPath, vfs); 268 if (editor == null) { 269 } 270 } 271 272 return editor; 273 } 274 275 283 private Editor getLiveEditor(String sPath, AbstractVirtualFileSystem vfs) { 284 285 Editor editor = null; 286 if (sPath.startsWith(HarmonisePaths.PATH_DOCUMENTS)) { 287 editor = new XMetaLEditor(); 288 } else if (sPath.startsWith(HarmonisePaths.PATH_NEWSLETTER)) { 289 editor = new XMetaLEditor(); 290 } else if (sPath.startsWith(HarmonisePaths.PATH_COMPOSITION)) { 291 editor = new CompositionEditor(); 292 } else if (sPath.startsWith(HarmonisePaths.PATH_XSLT)) { 293 editor = new XSLTEditor(); 294 } else if (sPath.startsWith(HarmonisePaths.PATH_PAGE_DEFINITION)) { 295 editor = new PageDefinitionEditor(); 296 } else if ( 297 sPath.startsWith(HarmonisePaths.PATH_LINKS) 298 || sPath.startsWith(HarmonisePaths.PATH_EMAIL)) { 299 editor = new LinkEditor(); 300 } else if (sPath.startsWith(HarmonisePaths.PATH_USERS)) { 301 editor = new UserEditor(); 302 } else if (sPath.startsWith(HarmonisePaths.PATH_REPORTS_QUERIES)) { 303 editor = new SystemReportEditor(); 304 } else if (sPath.startsWith(HarmonisePaths.PATH_REPORTS_OUTPUT)) { 305 editor = new SystemReportOutputEditor(); 306 } else if ( 307 sPath.startsWith(HarmonisePaths.PATH_WORKFLOW_PROPS) 308 || sPath.startsWith(HarmonisePaths.PATH_WORKFLOW_STAGES)) { 309 editor = new WorkflowEditor(); 310 } else if ( 311 sPath.startsWith(HarmonisePaths.PATH_METADATA) 312 || sPath.startsWith(HarmonisePaths.PATH_RBS_PROPS) 313 || sPath.startsWith(HarmonisePaths.PATH_RBS_VALS) 314 || sPath.startsWith(HarmonisePaths.PATH_RBS_PROPS) 315 || sPath.startsWith(HarmonisePaths.PATH_RBS_PROPS)) { 316 editor = new BlankEditor(); 317 } 318 319 return editor; 320 } 321 322 330 private Editor getNoneLiveEditor( 331 String sPath, 332 AbstractVirtualFileSystem vfs) { 333 Editor editor = null; 334 335 VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource(); 336 if (vfFile != null 337 && (vfFile.getState() == VirtualFile.STATE_PENDING 338 || vfFile.getState() == VirtualFile.STATE_HISTORICAL) 339 && ((VersionedVirtualFile) vfFile).getLiveVersionPath() != null) { 340 sPath = ((VersionedVirtualFile) vfFile).getLiveVersionPath(); 341 } else if ( 342 vfFile != null 343 && (vfFile.getState() == VirtualFile.STATE_PENDING 344 || vfFile.getState() == VirtualFile.STATE_HISTORICAL) 345 && ((VersionedVirtualFile) vfFile).getLiveVersionPath() == null) { 346 } 347 editor = this.getLiveEditor(sPath, vfs); 348 if(editor==null) { 349 editor = new FileAssetEditor(); 350 } 351 return editor; 352 } 353 354 360 protected void checkFiles() { 361 if (this.m_VirtualPhysicalMapping.size() > 0) { 362 Iterator itor = this.m_VirtualPhysicalMapping.values().iterator(); 363 while (itor.hasNext()) { 364 FileDetails details = (FileDetails) itor.next(); 365 String sRealPath = details.getRealPath(); 366 File fFile = new File(sRealPath); 367 long modified = fFile.lastModified(); 368 369 if (modified > details.getModified()) { 370 VirtualFile vfFile = 371 details.getVFS().getVirtualFile( 372 details.getVirtualPath()).getResource(); 373 374 if(details.getEditor() instanceof XMetaLEditor) { 375 Long lFileLength = new Long (fFile.length()); 376 byte[] byteContents = new byte[lFileLength.intValue()]; 377 char[] chars = new char[1]; 378 379 StringBuffer sBuffContent = new StringBuffer (); 380 381 FileInputStream fis = null; 382 InputStreamReader isr = null; 383 try { 384 fis = new FileInputStream(fFile); 385 isr = new InputStreamReader(fis, "UTF-8"); 386 387 char tempChar = ' '; 388 int nByte = isr.read(chars); 389 390 while(nByte!=-1) { 391 sBuffContent.append(chars); 392 nByte = isr.read(chars); 393 } 394 395 try { 396 vfFile.setContent(sBuffContent.toString().trim().getBytes("UTF-8")); 397 } catch(Exception e) { 398 e.printStackTrace(); 399 vfFile.setContent(sBuffContent.toString().trim().getBytes()); 400 } 401 402 details.setModified(modified); 403 } catch (Exception e2) { 404 e2.printStackTrace(System.out); 405 } finally { 406 try { 407 isr.close(); 408 fis.close(); 409 } catch (Exception e3) { 410 e3.printStackTrace(System.out); 411 } 412 } 413 } else { 414 Long lFileLength = new Long (fFile.length()); 415 byte[] byteContents = new byte[lFileLength.intValue()]; 416 417 FileInputStream fis = null; 418 try { 419 fis = new FileInputStream(fFile); 420 fis.read(byteContents); 421 vfFile.setContent(byteContents); 422 423 details.setModified(modified); 424 } catch (Exception e2) { 425 e2.printStackTrace(System.out); 426 } finally { 427 try { 428 fis.close(); 429 } catch (Exception e3) { 430 e3.printStackTrace(System.out); 431 } 432 } 433 } 434 } 435 } 436 } 437 } 438 439 445 private boolean isValidXML(File file) { 446 boolean bValid = true; 447 448 return bValid; 449 } 450 451 457 private FileDetails getDetails(String sRealPath) { 458 FileDetails details = null; 459 460 Iterator itor = this.m_VirtualPhysicalMapping.values().iterator(); 461 while (itor.hasNext()) { 462 FileDetails element = (FileDetails) itor.next(); 463 if (element.getRealPath().equalsIgnoreCase(sRealPath)) { 464 details = element; 465 } 466 } 467 468 return details; 469 } 470 471 479 class FileDetails { 480 481 484 private String m_sRealPath = null; 485 486 489 private String m_sVirtualPath = null; 490 491 494 private AbstractVirtualFileSystem m_vfs = null; 495 496 499 private long m_lModified = 0; 500 501 504 private Editor m_editor = null; 505 506 514 public FileDetails( 515 String sRealPath, 516 String sVirtualPath, 517 AbstractVirtualFileSystem vfs, 518 Editor editor) { 519 super(); 520 this.m_sRealPath = sRealPath; 521 this.m_sVirtualPath = sVirtualPath; 522 this.m_vfs = vfs; 523 this.m_editor = editor; 524 } 525 526 531 public Editor getEditor() { 532 return this.m_editor; 533 } 534 535 540 public String getRealPath() { 541 return this.m_sRealPath; 542 } 543 544 549 public String getVirtualPath() { 550 return this.m_sVirtualPath; 551 } 552 553 558 public AbstractVirtualFileSystem getVFS() { 559 return this.m_vfs; 560 } 561 562 568 public long getModified() { 569 return this.m_lModified; 570 } 571 572 577 public void setModified(long lModified) { 578 this.m_lModified = lModified; 579 } 580 } 581 582 589 class FileMonitor extends Thread { 590 591 594 private long sleepTime; 595 596 601 FileMonitor(long sleepTime) { 602 this.sleepTime = sleepTime; 603 } 604 605 608 public void run() { 609 while (true) { 610 try { 611 sleep(sleepTime); 612 } catch (InterruptedException e) { 613 } 615 616 EditorController.getInstance().checkFiles(); 617 } 618 } 619 } 620 621 624 public void contextMessage(ContextEvent ce) { 625 if (ce.CONTEXT_TYPE == ContextType.CONTEXT_SHUTDOWN) { 626 this.clearTempDir(); 627 } 628 } 629 630 634 public void clearTempDir() { 635 File fTempDir = new File("C:\\ContentManager\\temp"); 636 this.deleteDirContents(fTempDir); 637 } 638 639 644 private void deleteDirContents(File fDir) { 645 File[] children = fDir.listFiles(); 646 for (int i = 0; i < children.length; i++) { 647 File file = children[i]; 648 if (file.isDirectory()) { 649 this.deleteDirContents(file); 650 } 651 file.delete(); 652 } 653 } 654 655 public boolean isMonitoringVirtualFile(VirtualFile vfFile) { 656 boolean bRetn = false; 657 if(vfFile!=null && this.m_VirtualPhysicalMapping.keySet().contains(vfFile.getFullPath())) { 658 bRetn=true; 659 } 660 return bRetn; 661 } 662 663 public String getRealPath(VirtualFile vfFile) { 664 String sRealPath = null; 665 666 FileDetails details = (FileDetails) this.m_VirtualPhysicalMapping.get(vfFile.getFullPath()); 667 if(details!=null) { 668 sRealPath = details.getRealPath(); 669 } 670 671 return sRealPath; 672 } 673 674 } 675
| Popular Tags
|