1 40 package org.dspace.app.mediafilter; 41 42 import java.io.InputStream ; 43 44 import org.dspace.content.Bitstream; 45 import org.dspace.content.BitstreamFormat; 46 import org.dspace.content.Bundle; 47 import org.dspace.content.Item; 48 import org.dspace.core.Context; 49 50 public abstract class MediaFilter 51 { 52 protected Item item = null; 53 54 55 56 64 public abstract String getFilteredName(String sourceName); 65 66 70 public abstract String getBundleName(); 71 72 77 public abstract String getFormatString(); 78 79 83 public abstract String getDescription(); 84 85 91 public abstract InputStream getDestinationStream(InputStream source) 92 throws Exception ; 93 94 95 96 112 public boolean processBitstream(Context c, Item item, Bitstream source) 113 throws Exception 114 { 115 boolean overWrite = MediaFilterManager.isForce; 116 117 this.item = item; 118 119 String newName = getFilteredName(source.getName()); 121 122 Bitstream existingBitstream = null; Bundle targetBundle = null; 125 Bundle[] bundles = item.getBundles(getBundleName()); 126 127 if (bundles.length > 0) 129 { 130 for (int i = 0; i < bundles.length; i++) 132 { 133 Bitstream[] bitstreams = bundles[i].getBitstreams(); 134 135 for (int j = 0; j < bitstreams.length; j++) 136 { 137 if (bitstreams[j].getName().equals(newName)) 138 { 139 targetBundle = bundles[i]; 140 existingBitstream = bitstreams[j]; 141 } 142 } 143 } 144 } 145 146 if (!overWrite && (existingBitstream != null)) 148 { 149 System.out.println("SKIPPED: bitstream " + source.getID() 150 + " because '" + newName + "' already exists"); 151 152 return false; 153 } 154 155 InputStream destStream = getDestinationStream(source.retrieve()); 156 157 if (bundles.length < 1) 159 { 160 targetBundle = item.createBundle(getBundleName()); 161 } 162 else 163 { 164 targetBundle = bundles[0]; 166 } 167 168 Bitstream b = targetBundle.createBitstream(destStream); 169 170 b.setName(newName); 172 b.setSource("Written by MediaFilter " + this.getClass().getName()); b.setDescription(getDescription()); 175 176 BitstreamFormat bf = BitstreamFormat.findByShortDescription(c, 178 getFormatString()); 179 b.setFormat(bf); 180 b.update(); 181 182 if (existingBitstream != null) 185 { 186 targetBundle.removeBitstream(existingBitstream); 187 } 188 189 System.out.println("FILTERED: bitstream " + source.getID() 190 + " and created '" + newName + "'"); 191 192 return true; 193 } 194 } 195 | Popular Tags |