1 package com.genimen.djeneric.web.controller; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.TreeMap ; 8 9 import javax.servlet.ServletException ; 10 import javax.servlet.http.HttpServletRequest ; 11 import javax.servlet.http.HttpServletResponse ; 12 import javax.xml.parsers.FactoryConfigurationError ; 13 import javax.xml.parsers.ParserConfigurationException ; 14 import javax.xml.transform.TransformerException ; 15 16 import org.apache.commons.fileupload.DiskFileUpload; 17 import org.apache.commons.fileupload.FileItem; 18 import org.apache.commons.fileupload.FileUploadException; 19 import org.w3c.dom.Document ; 20 import org.xml.sax.SAXException ; 21 22 import com.genimen.djeneric.repository.DjSession; 23 import com.genimen.djeneric.repository.DjUid; 24 import com.genimen.djeneric.repository.exceptions.DjenericException; 25 import com.genimen.djeneric.repository.util.DjObjectImporter; 26 import com.genimen.djeneric.web.renderers.ImportRenderer; 27 import com.genimen.djeneric.web.util.DjenericSessionManager; 28 import com.genimen.djeneric.web.util.DjenericXSLTransform; 29 30 public class ImportCommand 31 { 32 ImportRenderer _renderer = new ImportRenderer(); 33 34 public void get(DjenericSessionManager sessionManager, HttpServletRequest request, HttpServletResponse response) 35 throws IOException , ServletException , FactoryConfigurationError , ParserConfigurationException 36 { 37 response.setContentType("text/html; charset=UTF-8"); 38 39 DjenericXSLTransform xsl = sessionManager.getTransformer(); 40 Document doc = _renderer.asXml(); 41 String page = xsl.transform(doc, "util/import.xsl"); 42 response.getWriter().print(page); 43 } 44 45 public void post(DjenericSessionManager sessionManager, HttpServletRequest request, HttpServletResponse response) 46 throws FileUploadException, SAXException , IOException , ParserConfigurationException , TransformerException , 47 DjenericException, ServletException 48 { 49 response.setContentType("text/html; charset=UTF-8"); 50 51 DiskFileUpload fu = new DiskFileUpload(); 52 fu.setSizeMax(1000000); 54 fu.setSizeThreshold(4096); 56 fu.setRepositoryPath(System.getProperty("java.io.tmpdir")); 58 59 boolean shouldMerge = "true".equalsIgnoreCase(request.getParameter("merge")); 60 61 List fileItems = fu.parseRequest(request); 62 63 Iterator it = fileItems.iterator(); 64 TreeMap result = new TreeMap (); 65 66 while (it.hasNext()) 67 { 68 FileItem fi = (FileItem) it.next(); 69 String comment = fi.getString(); 70 String fieldName = fi.getFieldName(); 71 if ("file".equalsIgnoreCase(fieldName)) 72 { 73 DjUid[] unresolved = importDump(sessionManager, fi, shouldMerge); 74 result.put(fi.getName(), unresolved); 75 } 76 } 77 78 DjenericXSLTransform xsl = sessionManager.getTransformer(); 79 Document doc = _renderer.asXml(result); 80 String page = xsl.transform(doc, "util/importresult.xsl"); 81 response.getWriter().print(page); 82 } 83 84 protected DjUid[] importDump(DjenericSessionManager sessionManager, FileItem fi, boolean shouldMerge) 85 throws SAXException , IOException , ParserConfigurationException , TransformerException , DjenericException 86 { 87 boolean aborted = false; 88 89 DjSession session = sessionManager.createSession(); 90 try 91 { 92 InputStream is = fi.getInputStream(); 93 DjObjectImporter importer = new DjObjectImporter(session); 94 importer.load(fi.getInputStream(), shouldMerge); 95 DjUid[] unresolved = importer.resolve(); 96 if (unresolved.length == 0) 97 { 98 importer.apply(); 99 session.close(); 100 } 101 102 return unresolved; 103 } 104 finally 105 { 106 session.close(); 107 } 108 109 } 110 111 } 112 | Popular Tags |