1 4 package com.openedit.scanner; 5 6 import java.io.File ; 7 import java.io.FileFilter ; 8 import java.text.DateFormat ; 9 import java.text.SimpleDateFormat ; 10 import java.util.GregorianCalendar ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import org.dom4j.Element; 15 16 import com.openedit.archive.Archive; 17 import com.openedit.store.CatalogConverter; 18 import com.openedit.store.Category; 19 import com.openedit.store.Product; 20 import com.openedit.store.Store; 21 import com.openedit.util.PathUtilities; 22 import com.openedit.util.XmlUtil; 23 24 public class ImageImportConverter extends CatalogConverter 25 { 26 DateFormat format = null; 28 29 public boolean convert(Store inStore, List inErrorLog) throws Exception  30 { 31 File input = new File ( inStore.getRootDirectory(),"/upload/inputs.xml"); 32 if ( input.exists() ) 33 { 34 Archive archive = new Archive(); 35 archive.setStore( inStore ); 36 38 format = new SimpleDateFormat ("yyyy-MM-dd h:m:s"); Element root = new XmlUtil().getXml(input,"UTF-8"); 40 for (Iterator iter = root.elementIterator("original"); iter.hasNext();) 41 { 42 Element element = (Element) iter.next(); 43 File inputdir = new File ( element.attributeValue("path")); 44 String category = element.attributeValue("category"); 45 Category cat = inStore.getCatalog(category); 46 if ( cat == null) 47 { 48 cat = new Category(); 49 cat.setId(category); 50 cat.setName(category); 51 inStore.getCatalogArchive().getRootCatalog().addChild(cat); 52 inStore.getCatalogArchive().addCatalog(cat); 53 } 54 String accept = element.attributeValue("accept"); 55 processFile(inputdir,cat,0,accept,archive); 56 inStore.getCatalogArchive().saveCatalogs(); 57 } 58 return true; 59 } 60 return false; 61 } 62 63 private void processFile(File input, Category inParent, int inCount, String inAccept, Archive inArchive) throws Exception  64 { 65 67 if ( input.isDirectory()) 68 { 69 String cat = input.getName(); 70 inCount = inCount+100; 71 72 Category child = inParent.getChild(cat); 73 if ( child == null) 74 { 75 child = new Category(); 76 child.setId(cat); 77 child.setName(cat); 78 inParent.addChild(child); 79 inArchive.getStore().getCatalogArchive().addCatalog(child); 80 } 81 File [] children = findProductXConfFiles(input); 82 if( children != null) 83 { 84 for (int i = 0; i < children.length; i++) 85 { 86 inCount = inCount+1; 87 processFile(children[i],child,inCount,inAccept, inArchive); 88 } 89 } 90 } 91 else 92 { 93 if ( inAccept != null) 94 { 95 if ( !PathUtilities.match(input.getName(),inAccept) ) 96 { 97 return; 98 } 99 } 100 String id = input.getName().toLowerCase(); 101 id = PathUtilities.extractPageName(id); 102 Product product = inArchive.getStore().getProduct(id); 103 if ( product != null) 104 { 105 return; 106 } 107 product = new Product(); 108 product.setId(id); 109 product.setName(input.getName()); 110 product.setOrdering(inCount); 111 product.putAttribute("originalpath",input.getAbsolutePath()); 113 GregorianCalendar cal = new GregorianCalendar (); 115 cal.setTimeInMillis(input.lastModified()); 116 118 product.putAttribute("Asset Modification Date",format.format(cal.getTime()) ); 119 120 product.addCatalog(inParent); 121 Category parent = inParent.getParentCatalog(); 122 while ( parent != null) 123 { 124 product.addCatalog(parent); 125 parent = parent.getParentCatalog(); 126 } 127 product.setDefaultCatalog(inParent); 128 inArchive.getStore().getProductArchive().saveProduct(product); 129 inArchive.getStore().getProductArchive().clearProduct(product); 130 131 } 132 } 133 134 135 protected File [] findProductXConfFiles(File inParent) 136 { 137 FileFilter filter = new FileFilter () 138 { 139 public boolean accept(File inDir) 140 { 141 String inName = inDir.getName().toLowerCase(); 142 if ( inName.endsWith(".jpg") || inName.endsWith(".avi") ) 143 { 144 return true; 145 } 146 if ( inDir.isDirectory() ) 147 { 148 return true; 149 } 150 return false; 151 } 152 }; 153 return inParent.listFiles(filter); 154 } 155 156 protected void findFiles(File inSearchDirectory, List inAll, FileFilter inFilter) 157 { 158 File [] toadd = inSearchDirectory.listFiles(inFilter); 159 160 for (int i = 0; i < toadd.length; i++) 161 { 162 File file = toadd[i]; 163 if ( file.isDirectory() ) 164 { 165 findFiles(file,inAll, inFilter); 166 } 167 else 168 { 169 inAll.add(file); 170 } 171 } 172 } 173 174 175 176 } 177
| Popular Tags
|