1 package com.openedit.archive.update; 2 3 import java.io.File ; 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import sun.reflect.generics.reflectiveObjects.NotImplementedException; 12 13 import com.openedit.OpenEditRuntimeException; 14 import com.openedit.archive.Archive; 15 import com.openedit.store.Product; 16 import com.openedit.store.ProductArchive; 17 import com.openedit.store.StoreException; 18 19 public class MetadataUpdater { 20 private static final Log log = LogFactory.getLog(MetadataUpdater.class); 21 22 private static final String _KEYWORD_SEPERATOR = ", "; 23 24 protected Executor exec; 25 26 protected ProductArchive productArchive; 27 28 protected Archive fieldArchive; 29 30 public static void main(String [] args) { 31 throw new NotImplementedException(); 32 } 38 39 public void run(Iterator idIterator) { 40 while (idIterator.hasNext()) { 41 String id = (String ) idIterator.next(); 42 Product product = getProduct(id); 43 if (product == null) 44 { 45 log.error("No product found for ID:" + id); 46 continue; 47 } 48 String keywords = product.getKeywords(); 49 File original = getArchive().getOriginalDocument(product); 50 ExifEditBuilder exif = new ExifEditBuilder(original.getAbsolutePath()); 51 exif.setKeys(toList(keywords)); 52 exif.setComments( product.get("Notes")); 53 if (!addKeywords(exif)) { 54 log.error("Could not update image: " 55 + original.getAbsolutePath()); 56 } 57 } 58 } 59 60 public List toList(String keywords) { 61 List result = new ArrayList (); 62 if (keywords == null) 63 return result; 64 String splits[] = keywords.split(_KEYWORD_SEPERATOR); 65 for (int i = 0; i < splits.length; i++) { 66 String keyword = splits[i].trim(); 67 if (keyword.length() > 0) 68 result.add(keyword); 69 } 70 return result; 71 } 72 73 private Product getProduct(String id) { 74 Product product; 75 try { 76 product = getProductArchive().getProduct(id); 77 } catch (StoreException e) { 78 throw new OpenEditRuntimeException(e); 79 } 80 return product; 81 } 82 83 public boolean addKeywords(ExifEditBuilder exif) { 84 return executor().exec(exif.cmdLine()); 85 } 86 87 protected Executor executor() { 88 if (exec == null) { 89 exec = new CommandLine(); 90 } 91 return exec; 92 } 93 94 public void setProductArchive(ProductArchive productLoader) { 95 this.productArchive = productLoader; 96 } 97 98 public ProductArchive getProductArchive() { 99 return this.productArchive; 100 } 101 102 public Archive getArchive() 103 { 104 return fieldArchive; 105 } 106 107 public void setArchive(Archive inArchive) 108 { 109 fieldArchive = inArchive; 110 } 111 112 } | Popular Tags |