public void addBug(int product_id, BugInfo bugInfo, int productmodule_id) { if (sessionInfo == null) { return; } Map<String, String> params = new HashMap<String, String>(); params.put("mode", "addbug"); params.put(sessionInfo.getSessionName(), sessionInfo.getSessionId()); params.put("product_id", Integer.toString(bugInfo.getProduct_id())); params.put("product_name", bugInfo.getProduct_name()); params.put("productmodule_id", Integer.toString(productmodule_id)); params.put("module_name", bugInfo.getModule_name()); params.put("title", bugInfo.getTitle()); params.put("priority", Integer.toString(bugInfo.getPriority())); params.put("severity", Integer.toString(bugInfo.getSeverity())); params.put("assign_to_name", bugInfo.getAssign_to_name()); params.put("BugType", bugInfo.getBugType()); //params.put("created_by", "1169"); //params.put("created_by_name", "haha"); params.put("bug_status", "Active"); DateFormat df=new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.add(Calendar.DAY_OF_WEEK, 7); // 目前的時間加7天 //System.out.println(df.format(c.getTime())); //params.put("mail_to", "\u738b\u5947,\u4e5d\u91ce"); params.put("repeat_step", bugInfo.getRepeat_step()); params.put("Creater", "金永华"); params.put("Downgrade", "\u5426"); String jsonResult = null; InputStream is = null; try { is = doPost(bugfreeUrl, params); jsonResult = getResult(is); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } if (jsonResult == null) { return; } System.out.println("[addBug] jsonResult: " + jsonResult); return; } public void updateBugStatus(int id, String bug_status) { if (sessionInfo == null) { return; } Map<String, String> params = new HashMap<String, String>(); params.put("mode", "updatebug"); params.put(sessionInfo.getSessionName(), sessionInfo.getSessionId()); params.put("id", Integer.toString(id)); params.put("bug_status", bug_status); params.put("severity", "4"); String jsonResult = null; InputStream is = null; try { is = doPost(bugfreeUrl, params); jsonResult = getResult(is); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } if (jsonResult == null) { return; } System.out.println("[updateBugStatus] jsonResult: " + jsonResult); } public Map<String, String> findModules(int product_id) { Map<String,String> moduleName2OwnerMap = new HashMap<String, String>(); if (sessionInfo == null) { return moduleName2OwnerMap; } Map<String, String> params = new HashMap<String, String>(); params.put("mode", "findmodules"); params.put(sessionInfo.getSessionName(), sessionInfo.getSessionId()); params.put("product_id", Integer.toString(product_id)); String jsonResult = null; InputStream is = null; try { is = doPost(bugfreeUrl, params); jsonResult = getResult(is); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } if (jsonResult == null) { return moduleName2OwnerMap; } System.out.println("[findModules] jsonResult: " + jsonResult); JsonParser.parseModuleList(jsonResult, moduleName2OwnerMap); return moduleName2OwnerMap; } private static final String QUERY_TEMPLATE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<query table=\"Bug\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://bugfree/query.xsd\">" + "<fields logic=\"OR\">" + "<field name=\"title\" operator=\"LIKE\" value=\"TITLE\"/>" + "</fields>" + "</query>"; public String queryByTitle(int product_id, String title) { String query = QUERY_TEMPLATE.replaceAll("TITLE", title); if (sessionInfo == null) { return ""; } Map<String, String> params = new HashMap<String, String>(); params.put("mode", "query"); params.put(sessionInfo.getSessionName(), sessionInfo.getSessionId()); params.put("product_id", Integer.toString(product_id)); params.put("query", query); String jsonResult = null; InputStream is = null; try { is = doPost(bugfreeUrl, params); jsonResult = getResult(is); } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } if (jsonResult == null) { return ""; } return jsonResult; } public boolean hasNotClosed(String jsonResult) { if (jsonResult.equals("")) { return false; } if (jsonResult.contains("\"bug_status\":\"Active\"")) { return true; } return false; } public boolean hasClosed(String jsonResult) { if (jsonResult.equals("")) { return false; } if (jsonResult.contains("\"bug_status\":\"Closed\"")) { return true; } if (jsonResult.contains("\"bug_status\":\"Resolved\"")) { return true; } return false; } public boolean hasRejected(String jsonResult) { if (jsonResult.equals("")) { return false; } if (jsonResult.contains("\"solution\":\"Reject\"")) { return true; } if (jsonResult.contains("\"solution\":\"Won't Fix\"")) { return true; } // By Design if (jsonResult.contains("\"solution\":\"By Design\"")) { return true; } return false; } public InputStream doPost(String urlStr, Map<String, String> params) throws IOException { StringBuilder sb = new StringBuilder(); for(Map.Entry<String, String> param : params.entrySet()) { sb.append(param.getKey()).append("=").append(param.getValue()).append("&"); } int len = sb.length(); if (len > 0) { sb.deleteCharAt(len - 1); } String args = sb.toString(); //System.out.println("args: " + args); return doPost(urlStr, args); } public InputStream doPost(String urlStr, String args) throws IOException { URL url = null; try { url = new URL(urlStr); } catch (MalformedURLException e) { e.printStackTrace(); return null; } HttpURLConnection connection = (HttpURLConnection)url.openConnection(); //connection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux i686; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"); //String myCookie = "GerritAccount=aSecfaQr028r8-6yxggWFA1QQc0YQFz5"; if (sessionId != null) { System.out.println("sessionId: " + sessionId); connection.setRequestProperty("Cookie", sessionId); } //connection.setRequestProperty("Connection", "Keep-Alive"); /** * 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。 * 通过把URLConnection设为输出,你可以把数据向你个Web页传送。下面是如何做: */ connection.setDoOutput(true); //connection.setRequestMethod("POST"); /** * 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ... */ OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); out.write(args); //向页面传递数据。post的关键所在! // remember to clean up out.flush(); out.close(); return connection.getInputStream(); } private static String getResult(InputStream is) throws IOException { StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader( is)); String line = null; while ((line = br.readLine()) != null) { sb.append(line); } return sb.toString(); } } |