KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > web > controller > ImportCommand


1 package com.genimen.djeneric.web.controller;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.TreeMap JavaDoc;
8
9 import javax.servlet.ServletException JavaDoc;
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.http.HttpServletResponse JavaDoc;
12 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
13 import javax.xml.parsers.ParserConfigurationException JavaDoc;
14 import javax.xml.transform.TransformerException JavaDoc;
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 JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc request, HttpServletResponse JavaDoc response)
35       throws IOException JavaDoc, ServletException JavaDoc, FactoryConfigurationError JavaDoc, ParserConfigurationException JavaDoc
36   {
37     response.setContentType("text/html; charset=UTF-8");
38
39     DjenericXSLTransform xsl = sessionManager.getTransformer();
40     Document JavaDoc doc = _renderer.asXml();
41     String JavaDoc page = xsl.transform(doc, "util/import.xsl");
42     response.getWriter().print(page);
43   }
44
45   public void post(DjenericSessionManager sessionManager, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
46       throws FileUploadException, SAXException JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc, TransformerException JavaDoc,
47       DjenericException, ServletException JavaDoc
48   {
49     response.setContentType("text/html; charset=UTF-8");
50
51     DiskFileUpload fu = new DiskFileUpload();
52     // maximum size before a FileUploadException will be thrown
53
fu.setSizeMax(1000000);
54     // maximum size that will be stored in memory
55
fu.setSizeThreshold(4096);
56     // the location for saving data that is larger than getSizeThreshold()
57
fu.setRepositoryPath(System.getProperty("java.io.tmpdir"));
58
59     boolean shouldMerge = "true".equalsIgnoreCase(request.getParameter("merge"));
60
61     List JavaDoc fileItems = fu.parseRequest(request);
62
63     Iterator JavaDoc it = fileItems.iterator();
64     TreeMap JavaDoc result = new TreeMap JavaDoc();
65
66     while (it.hasNext())
67     {
68       FileItem fi = (FileItem) it.next();
69       String JavaDoc comment = fi.getString();
70       String JavaDoc 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 JavaDoc doc = _renderer.asXml(result);
80     String JavaDoc 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 JavaDoc, IOException JavaDoc, ParserConfigurationException JavaDoc, TransformerException JavaDoc, DjenericException
86   {
87     boolean aborted = false;
88
89     DjSession session = sessionManager.createSession();
90     try
91     {
92       InputStream JavaDoc 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