1 package org.tigris.scarab.om; 2 3 48 49 import java.io.File ; 50 import java.io.BufferedInputStream ; 51 import java.io.BufferedOutputStream ; 52 import java.io.FileInputStream ; 53 import java.io.FileOutputStream ; 54 55 import java.sql.Connection ; 56 import java.util.Date ; 57 import java.util.List ; 58 59 import org.apache.torque.TorqueException; 60 import org.apache.torque.om.Persistent; 61 62 import org.apache.turbine.Turbine; 63 import org.apache.torque.util.Criteria; 64 65 import org.apache.commons.configuration.Configuration; 66 import org.apache.commons.fileupload.FileItem; 67 68 import org.tigris.scarab.tools.localization.L10NKeySet; 69 import org.tigris.scarab.util.Log; 70 import org.tigris.scarab.util.ScarabConstants; 71 import org.tigris.scarab.util.ScarabException; 72 import org.tigris.scarab.util.word.SearchIndex; 73 import org.tigris.scarab.util.word.SearchFactory; 74 75 90 public class Attachment 91 extends BaseAttachment 92 implements Persistent 93 { 94 95 public static final Integer FILE__PK = new Integer (1); 96 97 public static final Integer COMMENT__PK = new Integer (2); 98 99 public static final Integer URL__PK = new Integer (3); 100 101 public static final Integer MODIFICATION__PK = new Integer (4); 102 103 104 private static String fileRepo = null; 105 106 110 private FileItem fileItem; 111 private static Configuration configuration; 112 113 120 public void setFileName(String name) 121 { 122 if (name == null) 123 { 124 super.setFileName(null); 125 } 126 else 127 { 128 int start = name.lastIndexOf('/')+1; 130 if (start == 0) 131 { 132 start = name.lastIndexOf('\\')+1; 133 } 134 String tmpName = name.substring(start).replace(' ', '_'); 136 tmpName = tmpName.replace('%', '_'); 137 super.setFileName(tmpName); 138 } 139 } 140 141 146 public FileItem getFile() 147 { 148 return fileItem; 149 } 150 151 155 public void setFile(FileItem v) 156 { 157 fileItem = v; 158 if (getMimeType() == null) 159 { 160 setMimeType(v.getContentType()); 161 } 162 setFileName(v.getName()); 163 } 164 165 168 public void setTextFields(ScarabUser user, Issue issue, Integer typeId) 169 throws Exception 170 { 171 setIssue(issue); 172 setTypeId(typeId); 173 setMimeType("text/plain"); 174 setCreatedBy(user.getUserId()); 176 } 177 178 182 public String doMakeURLFromData() 183 { 184 String url = getData(); 185 if (AttachmentTypePeer.URL_PK.equals(getTypeId())) 186 { 187 int stop = Math.min(url.indexOf('/'), url.indexOf('?')); 188 String test = null; 189 if (stop > 0) 190 { 191 test = url.substring(0, stop); 192 } 193 else 194 { 195 test = url; 196 } 197 int colon = test.indexOf(':'); 198 if (colon < 0) 199 { 200 StringBuffer sb = new StringBuffer (url.length() + 7); 202 sb.append("http://").append(url); 203 url = sb.toString(); 204 } 205 } 206 return url; 207 } 208 209 216 public void save(Connection dbCon) 217 throws TorqueException 218 { 219 if (getIssue().isNew()) 220 { 221 throw new TorqueException("Cannot save an attachment before saving" 222 + " the issue to which it is attached."); } 224 setData(doMakeURLFromData()); 228 229 if (isNew() && (getCreatedDate() == null && getModifiedDate() == null)) 233 { 234 Date now = new Date (); 235 setCreatedDate(now); 236 setModifiedDate(now); 237 } 238 else if (isModified()) 239 { 240 setModifiedDate(new Date ()); 241 } 242 243 super.save(dbCon); 244 245 try 246 { 247 FileItem file = getFile(); 248 if (file != null) 249 { 250 File uploadFile = 251 new File (getRepositoryDirectory(),getRelativePath()); 252 File parent = uploadFile.getParentFile(); 253 if (!parent.exists()) 254 { 255 mkdirs(parent); 256 } 257 file.write(uploadFile); 258 } 259 } 260 catch (Exception e) 261 { 262 throw new TorqueException(e); } 264 265 268 if (AttachmentTypePeer.COMMENT_PK.equals(getTypeId())) 269 { 270 try 271 { 272 SearchIndex searchIndex = SearchFactory.getInstance(); 273 if (searchIndex != null) 274 { 275 searchIndex.index(this); 276 } 277 } 278 catch (Exception e) 279 { 280 throw new TorqueException(e); } 282 } 283 } 284 285 289 public boolean deletePhysicalAttachment() 290 throws Exception 291 { 292 File f = new File (getFullPath()); 293 return f.delete(); 294 } 295 296 297 298 301 private static synchronized void mkdirs(File path) 302 { 303 if (!path.exists()) 304 { 305 path.mkdirs(); 306 } 307 } 308 309 324 public String getRelativePath() 325 throws ScarabException, Exception 326 { 327 if (isNew()) 328 { 329 throw new ScarabException(L10NKeySet.ExceptionPathNotSet); 330 } 331 String path = null; 332 String filename = getFileName(); 333 if (filename != null) 334 { 335 StringBuffer sb = new StringBuffer (30 + filename.length()); 337 Issue issue = getIssue(); 338 sb.append("mod").append(issue.getModule().getQueryKey()) 339 .append(File.separator) 340 .append(issue.getIdCount() / 1000) 341 .append(File.separator) 342 .append(issue.getUniqueId()).append('_') 343 .append(getQueryKey()).append('_') 344 .append(filename); 345 path = sb.toString(); 346 } 347 return path; 348 } 349 350 355 public String getFullPath() 356 throws Exception 357 { 358 String path = null; 359 String relativePath = getRelativePath(); 360 if (relativePath != null) 361 { 362 path = getRepositoryDirectory() + File.separator + relativePath; 363 } 364 return path; 365 } 366 367 372 public static String getRepositoryDirectory() 373 throws Exception 374 { 375 if (fileRepo == null) 376 { 377 String testPath = getConfiguration() 378 .getString(ScarabConstants.ATTACHMENTS_REPO_KEY); 379 380 File testDir = new File (testPath); 381 if (testDir.isAbsolute()) 382 { 383 if (!testDir.exists()) 384 { 385 mkdirs(testDir); 386 } 387 fileRepo = testPath; 388 } 389 else 390 { 391 testPath = Turbine.getRealPath(testPath); 393 testDir = new File (testPath); 394 if (!testDir.exists()) 395 { 396 mkdirs(testDir); 397 } 398 fileRepo = testPath; 399 } 400 } 401 return fileRepo; 402 } 403 404 private static Configuration getConfiguration() { 405 if(configuration==null){ 406 configuration = Turbine.getConfiguration(); 407 } 408 return configuration; 409 } 410 protected static void setConfiguration(Configuration configuration){ 411 Attachment.configuration = configuration; 412 } 413 414 public void copyFileTo(String path) 415 throws Exception 416 { 417 copyFileFromTo(getFullPath(), path); 418 } 419 420 public void copyFileFromTo(String from, String path) 421 throws Exception 422 { 423 BufferedInputStream in = null; 424 BufferedOutputStream out = null; 425 try 426 { 427 File f = new File (path); 428 if (!f.getParentFile().exists()) 429 { 430 f.getParentFile().mkdirs(); 431 } 432 433 in = new BufferedInputStream (new FileInputStream (from)); 434 out = new BufferedOutputStream (new FileOutputStream (f)); 435 byte[] bytes = new byte[2048]; 436 int nbrRead = 0; 437 while ((nbrRead = in.read(bytes)) != -1) 438 { 439 out.write(bytes, 0, nbrRead); 440 } 441 } 442 finally 443 { 444 try 445 { 446 in.close(); 447 } 448 catch (Exception e) 449 { 450 Log.get().debug(e.getMessage()); 451 } 452 try 453 { 454 out.close(); 455 } 456 catch (Exception e) 457 { 458 Log.get().debug(e.getMessage()); 459 } 460 } 461 } 462 463 467 public Attachment copy() throws TorqueException 468 { 469 Attachment copyObj = AttachmentManager.getInstance(); 470 copyObj.setIssueId(getIssueId()); 471 copyObj.setTypeId(getTypeId()); 472 copyObj.setName(getName()); 473 copyObj.setData(getData()); 474 copyObj.setFileName(getFileName()); 475 copyObj.setMimeType(getMimeType()); 476 copyObj.setModifiedBy(getModifiedBy()); 477 copyObj.setCreatedBy(getCreatedBy()); 478 copyObj.setModifiedDate(getModifiedDate()); 479 copyObj.setCreatedDate(getCreatedDate()); 480 copyObj.setDeleted(getDeleted()); 481 return copyObj; 482 } 483 484 487 public Activity getActivity() throws Exception 488 { 489 Activity activity = null; 490 Criteria crit = new Criteria() 491 .add(ActivityPeer.ATTACHMENT_ID, getAttachmentId()); 492 493 List activities = ActivityPeer.doSelect(crit); 494 if (activities.size() > 0) 495 { 496 activity = (Activity)activities.get(0); 497 } 498 return activity; 499 } 500 } 501 | Popular Tags |