KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > scanner > ImageImportConverter


1 /*
2  * Created on Oct 2, 2005
3  */

4 package com.openedit.scanner;
5
6 import java.io.File JavaDoc;
7 import java.io.FileFilter JavaDoc;
8 import java.text.DateFormat JavaDoc;
9 import java.text.SimpleDateFormat JavaDoc;
10 import java.util.GregorianCalendar JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
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     //protected List fieldInputTypes;
27
DateFormat JavaDoc format = null;
28
29     public boolean convert(Store inStore, List JavaDoc inErrorLog) throws Exception JavaDoc
30     {
31         File JavaDoc input = new File JavaDoc( inStore.getRootDirectory(),"/upload/inputs.xml");
32         if ( input.exists() )
33         {
34             Archive archive = new Archive();
35             archive.setStore( inStore );
36             //archive.setPageManager(getPageManager());
37

38             format = new SimpleDateFormat JavaDoc("yyyy-MM-dd h:m:s");//2005-03-04 08:28:57
39
Element root = new XmlUtil().getXml(input,"UTF-8");
40             for (Iterator JavaDoc iter = root.elementIterator("original"); iter.hasNext();)
41             {
42                 Element element = (Element) iter.next();
43                 File JavaDoc inputdir = new File JavaDoc( element.attributeValue("path"));
44                 String JavaDoc 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 JavaDoc 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 JavaDoc input, Category inParent, int inCount, String JavaDoc inAccept, Archive inArchive) throws Exception JavaDoc
64     {
65         //getErrorLog().add("Loading " + input.getName());
66

67         if ( input.isDirectory())
68         {
69             String JavaDoc 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 JavaDoc[] 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 JavaDoc 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                     //String path = PathUtilities.extractDirectoryPath(input.getPath());
112
product.putAttribute("originalpath",input.getAbsolutePath());
113                     //product.putAttribute("Windows File Name",input.getName());
114
GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc();
115                     cal.setTimeInMillis(input.lastModified());
116                     //Asset Modification Date">2005-03-04 08:28:57
117

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 JavaDoc[] findProductXConfFiles(File JavaDoc inParent)
136     {
137         FileFilter JavaDoc filter = new FileFilter JavaDoc()
138         {
139             public boolean accept(File JavaDoc inDir)
140             {
141                 String JavaDoc 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 JavaDoc inSearchDirectory, List JavaDoc inAll, FileFilter JavaDoc inFilter)
157     {
158         File JavaDoc[] toadd = inSearchDirectory.listFiles(inFilter);
159         
160         for (int i = 0; i < toadd.length; i++)
161         {
162             File JavaDoc 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