1 19 package org.openharmonise.rm.resources.content; 20 21 22 import java.sql.*; 23 import java.util.Vector ; 24 import java.util.logging.*; 25 26 import org.openharmonise.commons.cache.CacheException; 27 import org.openharmonise.commons.dsi.*; 28 import org.openharmonise.commons.dsi.dml.*; 29 import org.openharmonise.commons.net.MimeTypeMapping; 30 import org.openharmonise.rm.*; 31 import org.openharmonise.rm.dsi.ColumnRefCache; 32 import org.openharmonise.rm.publishing.*; 33 import org.openharmonise.rm.resources.AbstractChildObject; 34 import org.openharmonise.rm.resources.lifecycle.EditException; 35 import org.w3c.dom.*; 36 37 38 45 abstract public class TextResource extends AbstractChildObject { 46 47 51 public static final String TAG_CONTENT = "Content"; 52 53 56 public static final String TAG_CONTENT_TYPE = "ContentType"; 57 58 62 private static final String CLMN_CONTENT = "content"; 63 66 private static final String CLMN_MIME_TYPE = "mime_type"; 67 68 72 protected String m_sContent = null; 73 74 77 protected String m_sContentType = MimeTypeMapping.TEXT.getMimeType(); 78 79 83 private boolean m_bIsContentPopulated = false; 84 85 88 private static final Logger m_logger = Logger.getLogger(TextResource.class.getName()); 89 90 93 public TextResource() { 94 super(); 95 } 96 97 102 public TextResource(AbstractDataStoreInterface dbintrf) { 103 super(dbintrf); 104 } 105 106 114 public TextResource( 115 AbstractDataStoreInterface dbintrf, 116 int nId, 117 int nKey, 118 boolean bIsHist) { 119 super(dbintrf, nId,nKey, bIsHist); 120 } 121 122 128 public TextResource(AbstractDataStoreInterface dbintrf, int nId) { 129 super(dbintrf, nId); 130 } 131 132 139 public String getContent() throws DataAccessException { 140 try { 141 if (isPopulated() == false) { 142 populateFromDatabase(); 143 } 144 } catch (PopulateException e) { 145 throw new DataAccessException("Problem occurred getting content", e); 146 } 147 148 return m_sContent; 149 } 150 151 157 private void populateContent() throws DataAccessException { 158 if (m_nId > NOTDBSAVED_ID && isContentPopulated() == false && isPopulated() == true) { 159 try { 160 String sContent = 161 m_dsi.getClob( 162 getTableName(isHistorical()), 163 CLMN_CONTENT, 164 this 165 .getInstanceColumnRef(ATTRIB_KEY, isHistorical()) 166 .getColumn() 167 + "=" 168 + getKey()); 169 170 if ((sContent != null) && (sContent.length() > 0)) { 171 if ((m_sContent == null) || (m_sContent.length() == 0)) { 172 m_sContent = sContent; 173 } else if (m_sContent.equals(sContent) == false) { 174 m_bIsChanged = true; 175 } 176 } 177 m_bIsContentPopulated = true; 178 } catch (DataStoreException e) { 179 throw new DataAccessException("Error occured getting content from DB:", e); 180 } 181 } 182 } 183 184 191 public void setContent(String sContent) throws PopulateException { 192 if (isContentPopulated() == true) { 193 if (m_sContent != null && m_sContent.equals(sContent) == false) { 194 setIsChanged(true); 195 } 196 } 197 198 m_sContent = sContent; 199 } 200 201 207 public boolean isContentPopulated() { 208 return m_bIsContentPopulated; 209 } 210 211 218 public String getContentType() throws DataAccessException { 219 if (isPopulated() == false) { 220 try { 221 populateFromDatabase(); 222 } catch (PopulateException e) { 223 throw new DataAccessException( 224 "Error occured populating from DB",e); 225 } 226 } 227 228 return m_sContentType; 229 } 230 231 236 public void setContentType(String sContentType) { 237 if (isPopulated() == true) { 238 if ( (m_sContentType == null && sContentType != null) || m_sContentType.equals(sContentType) == false) { 239 setIsChanged(true); 240 } 241 } 242 243 m_sContentType = sContentType; 244 } 245 246 249 public ColumnRef getInstanceColumnRef(String sColumn, boolean bIsHist) 250 throws DataStoreException { 251 ColumnRef colref = null; 252 253 String sTable = getTableName(bIsHist); 254 255 if (sColumn.equals(CLMN_CONTENT) == true || sColumn.equals(TAG_CONTENT) == true) { 256 colref = new ColumnRef(sTable, CLMN_CONTENT, ColumnRef.TEXT); 257 } else if (sColumn.equals(CLMN_MIME_TYPE) == true) { 258 colref = new ColumnRef(sTable, CLMN_MIME_TYPE, ColumnRef.TEXT); 259 } else { 260 colref = super.getInstanceColumnRef(sColumn, bIsHist); 261 } 262 263 return colref; 264 } 265 266 269 public void populate(Element xmlElement, State state) 270 throws PopulateException { 271 Text txt = null; 272 String sTagname = xmlElement.getTagName(); 273 274 if (sTagname.equals(TAG_CONTENT) == true) { 275 txt = (Text) xmlElement.getFirstChild(); 276 setContent(txt.getNodeValue()); 277 } else if (sTagname.equals(TAG_CONTENT_TYPE) == true) { 278 txt = (Text) xmlElement.getFirstChild(); 279 setContentType(txt.getNodeValue()); 280 } else { 281 super.populate(xmlElement, state); 282 } 283 284 } 285 286 289 public Element publish(Element topEl, HarmoniseOutput xmlDoc, State state) 290 throws PublishException { 291 Element docEl = null; 292 Text txt = null; 293 294 String sTagname = topEl.getTagName(); 295 296 if (sTagname.equals(TAG_CONTENT) == true) { 297 docEl = xmlDoc.createElement(sTagname); 298 try { 299 txt = xmlDoc.createTextNode(getContent()); 300 } catch (DataAccessException e) { 301 throw new PublishException( 302 "Error occured getting content:" + e.getLocalizedMessage()); 303 } 304 docEl.appendChild(txt); 305 xmlDoc.copyChildren(docEl, topEl, new Vector ()); 306 } else if (sTagname.equals(TAG_CONTENT_TYPE) == true) { 307 docEl = xmlDoc.createElement(sTagname); 308 try { 309 txt = xmlDoc.createTextNode(getContentType()); 310 } catch (DataAccessException e) { 311 throw new PublishException( 312 "Error occured getting content type:" 313 + e.getLocalizedMessage()); 314 } 315 docEl.appendChild(txt); 316 xmlDoc.copyChildren(docEl, topEl, new Vector ()); 317 } else { 318 docEl = super.publish(topEl, xmlDoc, state); 319 } 320 return docEl; 321 } 322 323 326 327 330 protected void saveCoreData() throws EditException { 331 332 super.saveCoreData(); 333 334 try { 335 m_dsi.updateClob( 336 getTableName(isHistorical()), 337 CLMN_CONTENT, 338 m_sContent, 339 this 340 .getInstanceColumnRef(ATTRIB_KEY, isHistorical()) 341 .getColumn() 342 + "=" 343 + getKey()); 344 345 m_bIsContentPopulated = true; 346 } catch (DataAccessException e) { 347 throw new EditException( 348 "Error occured accessing object key",e); 349 } catch (DataStoreException e) { 350 throw new EditException( 351 "Error occured saving content to DB",e); 352 } 353 } 354 355 358 protected void update() throws EditException, DataStoreException { 359 360 super.update(); 361 362 try { 363 m_dsi.updateClob( 364 m_sTable, 365 CLMN_CONTENT, 366 m_sContent, 367 this 368 .getInstanceColumnRef(ATTRIB_KEY, isHistorical()) 369 .getColumn() 370 + "=" 371 + getKey()); 372 } catch (DataAccessException e) { 373 throw new DataStoreException( 374 "Error occured accessing object key:" 375 + e.getLocalizedMessage()); 376 } 377 } 378 379 382 protected void addDataToSave(InsertStatement insert) 383 throws DataStoreException { 384 385 insert.addColumnValue( 386 this.getInstanceColumnRef(CLMN_MIME_TYPE, isHistorical()), 387 m_sContentType); 388 389 super.addDataToSave(insert); 390 } 391 392 395 protected void populateFromResultSetRow( 396 ResultSet rs, 397 SelectStatement select) 398 throws PopulateException { 399 400 if (isPopulated() == false) { 401 String sTemp = null; 402 ColumnRef colref = null; 403 404 try { 405 ColumnRefCache cache = ColumnRefCache.getInstance(); 406 407 boolean bIsHist = isHistorical(); 408 colref = cache.getColumnRef(this,CLMN_CONTENT, bIsHist); 409 410 if (select.containsSelectColumn(colref) == true) { 411 sTemp = rs.getString(select.getResultSetIndex(colref)); 412 413 if ((sTemp != null) && (sTemp.length() > 0)) { 414 if ((m_sContent == null) 415 || (m_sContent.length() == 0)) { 416 m_sContent = sTemp; 417 } else if (m_sContent.equals(sTemp) == false) { 418 setIsChanged(true); 419 } 420 } 421 } 422 423 colref = cache.getColumnRef(this,CLMN_MIME_TYPE, bIsHist); 424 if (select.containsSelectColumn(colref) == true) { 425 sTemp = rs.getString(select.getResultSetIndex(colref)); 426 427 if ((sTemp != null) && (sTemp.length() > 0)) { 428 if ((m_sContentType == null) 429 || (m_sContentType.length() == 0)) { 430 m_sContentType = sTemp; 431 } else if (m_sContentType.equals(sTemp) == false) { 432 setIsChanged(true); 433 } 434 } 435 } 436 } catch (SQLException e) { 437 throw new PopulateException( 438 "SQL error occured populating" ,e); 439 } catch (CacheException e) { 440 throw new PopulateException( 441 "Cache error occured populating" ,e); 442 } 443 444 super.populateFromResultSetRow(rs, select); 445 } 446 } 447 448 451 public Object clone() { 452 try { 453 if (m_bIsPopulated == false) { 454 populateFromDatabase(); 455 } 456 457 TextResource other = (TextResource) super.clone(); 458 459 return other; 460 } catch (PopulateException e) { 461 m_logger.log(Level.WARNING,e.getLocalizedMessage(), e); 462 463 return null; 464 } 465 } 466 467 470 protected void addColumnsToPopulateQuery( 471 SelectStatement select, 472 boolean bIsHist) 473 throws DataStoreException { 474 475 try { 476 ColumnRefCache cache = ColumnRefCache.getInstance(); 477 478 select.addSelectColumn(cache.getColumnRef(this,CLMN_MIME_TYPE, bIsHist)); 479 480 } catch (CacheException e) { 481 throw new DataStoreException("Error with cached colref",e); 482 } 483 super.addColumnsToPopulateQuery(select, bIsHist); 484 } 485 486 489 protected void populateFromDatabase() throws PopulateException { 490 super.populateFromDatabase(); 492 try { 494 populateContent(); 495 } catch (DataAccessException e) { 496 throw new PopulateException("Error occured populating content:", e); 497 } 498 } 499 } 500 | Popular Tags |