Revision 4221
Added by ben leinfelder almost 16 years ago
MetaCatServlet.java | ||
---|---|---|
31 | 31 |
import java.io.FileInputStream; |
32 | 32 |
import java.io.FileReader; |
33 | 33 |
import java.io.IOException; |
34 |
import java.io.OutputStreamWriter; |
|
34 | 35 |
import java.io.PrintWriter; |
35 | 36 |
import java.io.StringReader; |
37 |
import java.io.Writer; |
|
36 | 38 |
import java.net.MalformedURLException; |
37 | 39 |
import java.net.URL; |
38 | 40 |
import java.sql.PreparedStatement; |
... | ... | |
63 | 65 |
import org.apache.log4j.PropertyConfigurator; |
64 | 66 |
import org.ecoinformatics.eml.EMLParser; |
65 | 67 |
|
68 |
import au.com.bytecode.opencsv.CSVWriter; |
|
69 |
|
|
66 | 70 |
import com.oreilly.servlet.multipart.FilePart; |
67 | 71 |
import com.oreilly.servlet.multipart.MultipartParser; |
68 | 72 |
import com.oreilly.servlet.multipart.ParamPart; |
69 | 73 |
import com.oreilly.servlet.multipart.Part; |
70 | 74 |
|
75 |
import edu.ucsb.nceas.metacat.dataquery.DataQuery; |
|
71 | 76 |
import edu.ucsb.nceas.metacat.service.PropertyService; |
72 | 77 |
import edu.ucsb.nceas.metacat.service.ServiceException; |
73 | 78 |
import edu.ucsb.nceas.metacat.service.SessionService; |
... | ... | |
760 | 765 |
sess_id); |
761 | 766 |
out.close(); |
762 | 767 |
|
763 |
} else if (action.equals("export")) { |
|
768 |
} |
|
769 |
else if (action.trim().equals("dataquery")) { |
|
764 | 770 |
|
771 |
logMetacat |
|
772 |
.debug("******************* DATA QUERY ********************"); |
|
773 |
handleDataquery(params, response); |
|
774 |
} |
|
775 |
else if (action.equals("export")) { |
|
776 |
|
|
765 | 777 |
handleExportAction(params, response, username, groupnames, password); |
766 | 778 |
} else if (action.equals("read")) { |
767 | 779 |
handleReadAction(params, request, response, username, password, |
... | ... | |
929 | 941 |
} |
930 | 942 |
} |
931 | 943 |
|
944 |
|
|
945 |
private void handleDataquery( |
|
946 |
Hashtable<String, String[]> params, |
|
947 |
HttpServletResponse response) throws PropertyNotFoundException, IOException { |
|
948 |
|
|
949 |
DataQuery dq = new DataQuery(); |
|
950 |
|
|
951 |
String dataqueryXML = ((String[])params.get("dataquery"))[0]; |
|
952 |
|
|
953 |
ResultSet rs = null; |
|
954 |
try { |
|
955 |
rs = dq.executeQuery(dataqueryXML); |
|
956 |
} catch (Exception e) { |
|
957 |
//probably need to do something here |
|
958 |
e.printStackTrace(); |
|
959 |
return; |
|
960 |
} |
|
961 |
|
|
962 |
//process the result set |
|
963 |
String qformat = "csv"; |
|
964 |
String[] temp = params.get("qformat"); |
|
965 |
if (temp != null && temp.length > 0) { |
|
966 |
qformat = temp[0]; |
|
967 |
} |
|
968 |
String fileName = "query-results." + qformat; |
|
969 |
|
|
970 |
//get the results as csv file |
|
971 |
if (qformat != null && qformat.equalsIgnoreCase("csv")) { |
|
972 |
response.setContentType("text/csv"); |
|
973 |
//response.setContentType("application/csv"); |
|
974 |
response.setHeader("Content-Disposition", "attachment; filename=" + fileName); |
|
975 |
|
|
976 |
Writer writer = new OutputStreamWriter(response.getOutputStream()); |
|
977 |
CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER); |
|
978 |
try { |
|
979 |
csv.writeAll(rs, true); |
|
980 |
|
|
981 |
csv.flush(); |
|
982 |
response.flushBuffer(); |
|
983 |
|
|
984 |
rs.close(); |
|
985 |
|
|
986 |
} catch (SQLException e) { |
|
987 |
e.printStackTrace(); |
|
988 |
} |
|
989 |
|
|
990 |
return; |
|
991 |
} |
|
992 |
|
|
993 |
} |
|
994 |
|
|
932 | 995 |
// ///////////////////////////// METACAT SPATIAL /////////////////////////// |
933 | 996 |
|
934 | 997 |
/** |
Also available in: Unified diff
add dataquery (from EML's Data Manager library) to the metacat servlet actions