1 19 package org.openharmonise.rm.resources.content; 20 21 22 import java.io.*; 23 import java.sql.*; 24 import java.util.*; 25 import java.util.logging.*; 26 27 import org.openharmonise.commons.cache.CacheException; 28 import org.openharmonise.commons.dsi.*; 29 import org.openharmonise.commons.dsi.dml.*; 30 import org.openharmonise.commons.net.MimeTypeMapping; 31 import org.openharmonise.rm.*; 32 import org.openharmonise.rm.config.*; 33 import org.openharmonise.rm.dsi.ColumnRefCache; 34 import org.openharmonise.rm.publishing.*; 35 import org.openharmonise.rm.resources.AbstractChildObject; 36 import org.openharmonise.rm.resources.lifecycle.*; 37 import org.w3c.dom.*; 38 39 40 48 public class Asset 49 extends AbstractChildObject 50 implements Publishable, Editable, Cloneable , Comparable { 51 52 56 private static final String PNAME_ASSET_WEBAPP_URI = "ASSET_WEBAPP_URI"; 57 60 private static final String TXT_LINK = "link"; 61 62 65 private static final String TXT_TEXT = "text"; 66 67 71 public static final String TAG_ASSET = "Asset"; 72 73 76 public static final String TAG_URI = "URI"; 77 78 public static final String TAG_CONTENT_TYPE = "ContentType"; 79 80 84 protected static final String TBL_ASSET = "asset"; 85 86 89 protected static final String CLMN_URI = "content"; 90 91 94 protected static final String CLMN_MIME_TYPE = "mime_type"; 95 96 99 static final String PNAME_SITE_URL = "SITE_URL"; 100 101 105 public static final String PNAME_ASSET_ROOT_FILEPATH = "ASSET_ROOT"; 106 107 110 protected String m_sContentType = null; 111 112 115 protected String m_sURI = null; 116 117 120 private boolean m_bIsContentChanged = false; 121 122 125 private static final Logger m_logger = Logger.getLogger(Asset.class.getName()); 126 127 131 public Asset() { 132 super(); 133 } 134 135 140 public Asset(AbstractDataStoreInterface dbintrf) { 141 super(dbintrf); 142 } 143 144 150 public Asset(AbstractDataStoreInterface dbintrf, int nId) { 151 super(dbintrf, nId); 152 } 153 154 161 public void setContentFile(File assetFile) throws PopulateException { 162 String sContentType = m_sContentType; 163 164 if (m_sContentType == null) { 165 String sExt = 166 assetFile.getName().substring( 167 assetFile.getName().indexOf(".") + 1); 168 sContentType = MimeTypeMapping.getMimeTypeFromExtension(sExt); 169 setContentType(sContentType); 170 } 171 172 if (isAssetSavedAsText(sContentType) == true) { 173 try { 174 FileReader freader = new FileReader(assetFile); 175 176 StringBuffer sbuf = new StringBuffer (); 177 178 int numRead = 0; 179 int numToRead = 4096; 180 181 char[] buf = new char[numToRead]; 182 183 while ((numRead = freader.read(buf, 0, numToRead)) != -1) { 184 sbuf.append(buf, 0, numRead); 185 } 186 187 this.setURI(sbuf.toString()); 188 189 freader.close(); 190 } catch (IOException e) { 191 throw new PopulateException( 192 "Error occured while reading from the file",e); 193 } 194 } else { 195 setURI(assetFile.getAbsolutePath()); 196 } 197 198 setIsChanged(true); 199 } 200 201 208 public String getContentType() throws DataAccessException { 209 if (isPopulated() == false && m_sContentType == null) { 210 try { 211 populateFromDatabase(); 212 } catch (PopulateException e) { 213 throw new DataAccessException( 214 "Error occured populating object",e); 215 } 216 } 217 218 return m_sContentType; 219 } 220 221 228 public void setContentType(String sContentType) throws PopulateException { 229 if (isPopulated() == true) { 230 if (m_sContentType.equals(sContentType) == false) { 231 setIsChanged(true); 232 } 233 } 234 235 m_sContentType = sContentType; 236 } 237 238 243 public void setURI(String sContent) { 244 try { 245 sContent = getRelativePath(sContent); 247 } catch (DataAccessException e) { 248 m_logger.log(Level.WARNING, e.getMessage(), e); 249 } 250 251 252 if (isPopulated() == true) { 253 if (m_sURI.equals(sContent) == false) { 254 setIsChanged(true); 255 } 256 } 257 258 if (sContent.equals(m_sURI) == false) { 259 setIsContentChanged(true); 260 } 261 262 m_sURI = sContent; 263 264 } 265 266 273 public File getContentFile() throws DataAccessException { 274 File file = null; 275 String sContentType = getContentType(); 276 277 if (isAssetSavedAsText(sContentType) == false) { 278 String sFilename = this.getURI(); 279 280 if (sFilename != null) { 281 file = new File(sFilename); 282 } 283 } 284 285 return file; 286 } 287 288 295 public String getURI() throws DataAccessException { 296 if (isPopulated() == false) { 297 try { 298 populateFromDatabase(); 299 } catch (PopulateException e) { 300 throw new DataAccessException( 301 "Error occured populating data",e); 302 } 303 } 304 305 String sURI = null; 306 307 if(m_sURI != null) { 309 310 if(isRelativePath(m_sURI) == true) { 311 StringBuffer sbuf = new StringBuffer (); 312 String sAssetRoot = getAssetRoot(); 313 sbuf.append(sAssetRoot); 314 315 if(sAssetRoot.endsWith(File.separator) == false 316 && m_sURI.startsWith(File.separator) == false) { 317 sbuf.append(File.separatorChar); 318 } 319 320 sbuf.append(m_sURI); 321 sURI = sbuf.toString(); 322 } else { 323 sURI = m_sURI; 324 } 325 } 326 327 328 return sURI; 329 } 330 331 337 private boolean isRelativePath(String sPath) { 338 boolean bIsRelative = true; 339 340 try { 341 bIsRelative = (isAssetSavedAsText(getContentType()) == false); 344 } catch (DataAccessException e) { 345 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 346 } 347 348 if(bIsRelative == true) { 349 File[] roots = File.listRoots(); 350 351 for (int i = 0; i < roots.length; i++) { 352 if(sPath.startsWith(roots[i].getAbsolutePath()) == true) { 353 bIsRelative = false; 354 break; 355 } 356 } 357 } 358 359 return bIsRelative; 360 } 361 362 369 static public boolean copyFile(File from, File to) { 370 boolean bMoved = true; 371 372 BufferedInputStream buffIS = null; 373 BufferedOutputStream buffOS = null; 374 375 try { 376 buffIS = new BufferedInputStream(new FileInputStream(from)); 377 buffOS = new BufferedOutputStream(new FileOutputStream(to)); 378 379 int nByte = buffIS.read(); 380 381 while (nByte != -1) { 382 buffOS.write(nByte); 383 nByte = buffIS.read(); 384 } 385 } catch (Exception e) { 386 bMoved = false; 387 m_logger.log(Level.WARNING,e.getLocalizedMessage(),e); 388 } finally { 389 try { 390 buffOS.close(); 391 buffIS.close(); 392 } catch (Exception e) { 393 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 394 } 395 } 396 397 return bMoved; 398 } 399 400 408 static public boolean moveFile(File from, File to) { 409 boolean bMoved = true; 410 411 BufferedInputStream buffIS = null; 412 BufferedOutputStream buffOS = null; 413 414 if(from.exists() == true) { 415 416 try { 417 buffIS = new BufferedInputStream(new FileInputStream(from)); 418 buffOS = new BufferedOutputStream(new FileOutputStream(to)); 419 420 int nByte = buffIS.read(); 421 422 while (nByte != -1) { 423 buffOS.write(nByte); 424 nByte = buffIS.read(); 425 } 426 } catch (Exception e) { 427 bMoved = false; 428 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 429 } finally { 430 try { 431 buffOS.close(); 432 buffIS.close(); 433 } catch (Exception e) { 434 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 435 } 436 } 437 438 try { 439 from.delete(); 440 } catch (Exception e) { 441 bMoved = false; 442 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 443 } 444 } else { 445 m_logger.log(Level.WARNING, "Tried to move non-existing file:" + from.getAbsolutePath()); 446 bMoved = false; 447 } 448 449 return bMoved; 450 } 451 452 455 public Element publish(Element topEl, HarmoniseOutput output, State state) 456 throws PublishException { 457 Element docEl = null; 458 String sTagName = topEl.getTagName(); 459 460 if (sTagName.equals(TAG_URI)) { 461 docEl = output.createElement(sTagName); 462 463 String sContents = ""; 464 465 try { 466 if (m_nId != NOTDBSAVED_ID) { 467 sContents = getURI(); 468 } 469 470 if (sContents != null) { 471 Text txtAssetURL = output.createTextNode(getFullURL()); 472 docEl.appendChild(txtAssetURL); 473 } 474 } catch (DataAccessException e) { 475 throw new PublishException( 476 "Error occured publishing asset url",e); 477 } 478 } else if (sTagName.equals(TAG_CONTENT_TYPE) == true) { 479 docEl = output.createElement(sTagName); 480 try { 481 Text txt = output.createTextNode(getContentType()); 482 docEl.appendChild(txt); 483 } catch (DataAccessException e) { 484 throw new PublishException( 485 "Error occured getting content type",e); 486 } 487 output.copyChildren(docEl, topEl, new Vector()); 488 } else { 489 docEl = super.publish(topEl, output, state); 490 } 491 492 return docEl; 493 } 494 495 503 public String getFullURL() throws DataAccessException { 504 String path = ""; 505 String sURI = this.getURI(); 506 String sContentType = getContentType(); 507 508 if ((sContentType != null) 509 && (sContentType.startsWith(TXT_LINK) == false)) { 510 try { 511 String sAssetRootFilePath = getAssetRoot(); 512 String sSiteURL = ConfigSettings.getProperty(PNAME_SITE_URL); 513 String sAssetWebappURI = ConfigSettings.getProperty(PNAME_ASSET_WEBAPP_URI); 514 515 if(sURI.startsWith(sAssetRootFilePath) == false) { 516 throw new DataAccessException("Asset path does not start with expected path"); 517 } 518 519 StringBuffer sURL = new StringBuffer (); 520 sURL.append(sSiteURL); 521 522 if(sSiteURL.endsWith("/") == false 523 && sAssetWebappURI.startsWith("/") == false) { 524 sURL.append("/"); 525 } 526 527 sURL.append(sAssetWebappURI); 528 if(sAssetWebappURI.endsWith("/") == false) { 529 sURL.append("/"); 530 } 531 sURL.append(sURI 532 .substring(sAssetRootFilePath.length()) 533 .replace(File.separatorChar, '/')); 534 535 path = sURL.toString(); 536 537 } catch (ConfigException e) { 538 throw new DataAccessException( 539 "Error occured accessing config property",e); 540 } 541 542 } else { 543 path = sURI; 544 } 545 546 return path; 547 } 548 549 552 public void delete(boolean bDeleteHistory) 553 throws 554 DataStoreException, 555 DataAccessException, 556 EditException, 557 PopulateException { 558 File file = null; 559 560 if (bDeleteHistory == true) { 561 file = getContentFile(); 562 } 563 564 super.delete(bDeleteHistory); 565 566 if (file != null) { 567 file.delete(); 568 } 569 } 570 571 574 public String getDBTableName() { 575 return TBL_ASSET; 576 } 577 578 581 public String getTagName() { 582 return TAG_ASSET; 583 } 584 585 591 public String getAssetRoot() throws DataAccessException { 592 String sRoot = null; 593 594 try { 595 sRoot = ConfigSettings.getProperty(PNAME_ASSET_ROOT_FILEPATH); 596 } catch (ConfigException e) { 597 throw new DataAccessException(e); 598 } 599 600 return sRoot; 601 } 602 603 606 public JoinConditions getInstanceJoinConditions( 607 String sObjectTag, 608 boolean bIsOuter) 609 throws DataStoreException { 610 611 return null; 612 } 613 614 617 public String getParentObjectClassName() { 618 619 return Section.class.getName(); 620 } 621 622 625 public ColumnRef getInstanceColumnRef(String sColumn, boolean bIsHist) 626 throws DataStoreException { 627 ColumnRef colref = null; 628 629 String sTable = getTableName(bIsHist); 630 631 if (sColumn.equals(CLMN_URI) == true) { 632 colref = new ColumnRef(sTable, CLMN_URI, ColumnRef.TEXT); 633 } else if (sColumn.equals(CLMN_MIME_TYPE) == true) { 634 colref = new ColumnRef(sTable, CLMN_MIME_TYPE, ColumnRef.TEXT); 635 } else { 636 colref = super.getInstanceColumnRef(sColumn, bIsHist); 637 } 638 639 return colref; 640 } 641 642 645 646 655 static protected File createDir(String sDir) { 656 StringTokenizer tokens = new StringTokenizer(sDir, File.separator); 657 File dirNext = null; 658 String sNext = null; 659 StringBuffer sSoFar = new StringBuffer (); 660 661 if (sDir.startsWith(File.separator)) { 662 sSoFar.append(File.separator); 663 } else { 664 sNext = tokens.nextToken(); 665 sSoFar.append(sNext); 666 sSoFar.append(File.separator); 667 } 668 669 while (tokens.hasMoreTokens()) { 670 sNext = tokens.nextToken(); 671 sSoFar.append(sNext); 672 dirNext = new File(sSoFar.toString()); 673 674 if (dirNext.exists() == false) { 675 dirNext.mkdir(); 676 } 677 } 678 679 return dirNext; 680 } 681 682 685 protected void addDataToSave(InsertStatement insert) 686 throws DataStoreException { 687 688 try { 689 if (getContentType().startsWith(TXT_LINK) == false 690 && isContentChanged() == true) { 691 String sFileName = getURI(); 692 693 File newFile = createFile(); 695 696 if (sFileName.equals(newFile.getAbsolutePath()) == false) { 697 boolean bSuccess = true; 698 699 if (m_bIsContentChanged == true) { 703 bSuccess = moveFile(new File(sFileName), newFile); 704 } else { 705 bSuccess = copyFile(new File(sFileName), newFile); 706 } 707 708 if (bSuccess == false) { 709 throw new DataStoreException("File manipulation failed"); 710 } 711 712 } 713 714 m_sURI = newFile.getAbsolutePath(); 715 } 716 } catch (DataAccessException e) { 717 throw new DataStoreException( 718 "Error occured accessing object data", 719 e); 720 } catch (AssetException e) { 721 throw new DataStoreException("Error handling asset", e); 722 } catch (ConfigException e) { 723 throw new DataStoreException("Error accessing config setting", e); 724 } 725 726 insert.addColumnValue( 727 getInstanceColumnRef(CLMN_MIME_TYPE, isHistorical()), 728 m_sContentType); 729 730 try { 731 732 m_sURI = getRelativePath(m_sURI); 734 735 736 insert.addColumnValue( 737 getInstanceColumnRef(CLMN_URI, isHistorical()), 738 m_sURI); 739 } catch (DataAccessException e) { 740 throw new DataStoreException(e); 741 } 742 743 super.addDataToSave(insert); 744 745 m_bIsContentChanged = false; 747 } 748 749 752 protected void saveCoreData() throws EditException { 753 super.saveCoreData(); 754 } 755 756 759 protected void populateFromResultSetRow( 760 ResultSet rs, 761 SelectStatement select) 762 throws PopulateException { 763 if (isPopulated() == false) { 764 765 String sTemp = null; 766 767 try { 768 769 ColumnRefCache cache = ColumnRefCache.getInstance(); 770 boolean bIsHist = isHistorical(); 771 772 ColumnRef colref = cache.getColumnRef(this, CLMN_URI, bIsHist); 773 if (select.containsSelectColumn(colref) == true) { 774 sTemp = rs.getString(select.getResultSetIndex(colref)); 775 776 if ((sTemp != null) && (sTemp.length() > 0)) { 777 if ((m_sURI == null) || (m_sURI.length() == 0)) { 778 m_sURI = sTemp; 779 } else if (m_sURI.equals(sTemp) == false) { 780 setIsChanged(true); 781 } 782 } 783 } 784 785 colref = cache.getColumnRef(this, CLMN_MIME_TYPE, bIsHist); 786 if (select.containsSelectColumn(colref) == true) { 787 sTemp = rs.getString(select.getResultSetIndex(colref)); 788 789 if ((sTemp != null) && (sTemp.length() > 0)) { 790 if ((m_sContentType == null) 791 || (m_sContentType.length() == 0)) { 792 m_sContentType = sTemp; 793 } else if (m_sContentType.equals(sTemp) == false) { 794 setIsChanged(true); 795 } 796 } 797 } 798 } catch (SQLException e) { 799 throw new PopulateException("Error occured populating", e); 800 } catch (CacheException e) { 801 throw new PopulateException("Error occured populating", e); 802 } 803 804 super.populateFromResultSetRow(rs, select); 805 } 806 807 } 808 809 812 protected void addColumnsToPopulateQuery( 813 SelectStatement select, 814 boolean bIsHist) 815 throws DataStoreException { 816 817 try { 818 ColumnRefCache cache = ColumnRefCache.getInstance(); 819 820 select.addSelectColumn(cache.getColumnRef(this, CLMN_URI, bIsHist)); 821 822 select.addSelectColumn( 823 cache.getColumnRef(this, CLMN_MIME_TYPE, bIsHist)); 824 } catch (CacheException e) { 825 throw new DataStoreException(e.getLocalizedMessage(), e); 826 } 827 828 super.addColumnsToPopulateQuery(select, bIsHist); 829 830 } 831 832 835 836 854 protected File createFile() 855 throws AssetException, ConfigException, DataAccessException { 856 String sFilePath = null; 857 858 sFilePath = 859 ConfigSettings.getProperty(PNAME_ASSET_ROOT_FILEPATH).replace( 860 '/', 861 File.separatorChar); 862 863 String sName = getName(); 864 String sSuffix = MimeTypeMapping.getExtensionFromMimeType(getContentType()); 865 866 StringBuffer sFullPath = new StringBuffer (); 867 sFullPath.append(sFilePath); 868 869 if (sFilePath.charAt(sFilePath.length() - 1) != File.separatorChar) { 871 sFullPath.append(File.separatorChar); 872 } 873 874 sFullPath.append(Character.toUpperCase(sName.charAt(0))); 877 878 sFullPath.append(File.separatorChar); 879 880 sFullPath.append(sName).append("_").append(getKey()); 881 sFullPath.append(".").append(sSuffix); 882 883 File test = new File(sFullPath.toString()); 885 886 if (ensureParentExists(test) == false) { 887 throw new AssetException("Trouble creating asset file - parent does not exist"); 888 } 889 890 int nCount = 1; 891 892 while (test.exists()) { 895 sFullPath = new StringBuffer (); 896 sFullPath.append(sFilePath); 897 if(sFilePath.endsWith(File.separator) == false) { 898 sFullPath.append(File.separatorChar); 899 } 900 sFullPath.append(sName); 901 sFullPath.append("_["); 902 sFullPath.append(nCount); 903 sFullPath.append("]"); 904 sFullPath.append("."); 905 sFullPath.append(sSuffix); 906 test = new File(sFullPath.toString()); 907 nCount++; 908 } 909 910 return test; 911 } 912 913 921 protected boolean ensureParentExists(File file) { 922 File parent = file.getParentFile(); 923 924 boolean breturn = parent.exists(); 925 926 if (breturn == false) { 927 if (ensureParentExists(parent) == true) { 928 929 breturn = parent.mkdir(); 930 } 931 } 932 933 return breturn; 934 } 935 936 939 protected void saveNonCoreData() throws EditException { 940 941 } 942 943 950 public boolean isContentChanged() { 951 return m_bIsContentChanged; 952 } 953 954 959 public void setIsContentChanged(boolean bContentChanged) { 960 m_bIsContentChanged = bContentChanged; 961 } 962 963 966 public void clear() { 967 m_sContentType = null; 968 m_sURI = null; 969 m_bIsContentChanged = false; 970 super.clear(); 971 } 972 973 981 protected boolean isAssetSavedAsText(String sContentType) { 982 return sContentType.startsWith(TXT_LINK) 983 || sContentType.startsWith(TXT_TEXT); 984 } 985 986 996 protected String getRelativePath(String sAbsolutePath) throws DataAccessException { 997 String sRelPath = sAbsolutePath; 998 999 String sAssetRoot = getAssetRoot(); 1000 1001 if(sAbsolutePath != null 1002 && sAbsolutePath.startsWith(sAssetRoot) == true) { 1003 sRelPath = sAbsolutePath.substring(sAssetRoot.length()); 1004 } 1005 1006 return sRelPath; 1007 } 1008 1009} | Popular Tags |