refactor scriptTable operation ,but still has some problem

This commit is contained in:
fanfuxiaoran 2013-11-22 17:46:18 +08:00
parent c628ae4f78
commit e0c5e9a105
10 changed files with 371 additions and 185 deletions

File diff suppressed because one or more lines are too long

View File

@ -25,6 +25,174 @@ import org.springframework.web.bind.annotation.SessionAttributes;
public class ScriptActionController extends BaseControllerService {
private Logger logger = Logger.getLogger(ScriptActionController.class);
@RequestMapping("loadScript")
public @ResponseBody
List<Script> loadscript(@ModelAttribute("accessToken") String accessToken) {
System.out.println("enter loadscript");
String urlString = masterIP + "RecordScript/loadScriptList";
try {
HttpResponse httpResponse = this.getHttpRequester().sendPost(
urlString, null, this.makeAccessTockenMap(accessToken));
if (httpResponse == null) {
System.out.println("fail");
return null;
}
OperateScriptServerResponseModel operateScriptServerResponseModel = ExtractObject
.extractoperateScriptServerResponseModel(httpResponse
.getContent());
logger.info("loadScript:" + httpResponse.getContent());
if (operateScriptServerResponseModel.isSuccess()) {
System.out.println("success");
List<Script> scripts = operateScriptServerResponseModel
.getScripts();
return scripts;
} else {
System.out
.println("fail loadScript:"
+ operateScriptServerResponseModel
.getFailCauseString());
logger.info("fail loadScript :"
+ operateScriptServerResponseModel.getFailCauseString());
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
@RequestMapping("deleteScript")
public @ResponseBody
boolean deletescript(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId) {
System.out.println("entry deletscript");
String urlString = masterIP + "RecordScript/deleteScript";
Map<String, String> params = this.makeParamsMap("scriptId", scriptId);
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
urlString, params, this.makeAccessTockenMap(accessToken));
if (httpResponse == null) {
System.out.println("fail");
return false;
}
logger.info(httpResponse.getContent());
OperateScriptServerResponseModel operateScriptServerResponseModel = ExtractObject
.extractoperateScriptServerResponseModel(httpResponse
.getContent());
if (operateScriptServerResponseModel.isSuccess()) {
System.out.println("success");
return true;
} else {
System.out
.println("fail:"
+ operateScriptServerResponseModel
.getFailCauseString());
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
@RequestMapping("editScript")
public @ResponseBody
String editscript(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId) {
System.out.println("entry editScript");
String urlString = masterIP + "RecordScript/queryScriptById";
Map<String, String> params = this.makeParamsMap("scriptId", scriptId);
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
urlString, params, this.makeAccessTockenMap(accessToken));
if (httpResponse == null) {
System.out.println("fail");
return null;
}
OperateScriptServerResponseModel operateScriptServerResponseModel = ExtractObject
.extractoperateScriptServerResponseModel(httpResponse
.getContent());
if (operateScriptServerResponseModel == null) {
System.out.println("fail");
return null;
}
if (operateScriptServerResponseModel.isSuccess()) {
List<Script> scripts = operateScriptServerResponseModel
.getScripts();
Iterator<Script> it = scripts.iterator();
Script script = it.next();
System.out.println("success");
return script.getScriptContent();
} else {
System.out.println("fail");
return null;
}
}
catch (IOException e) {
System.out.println("fail");
e.printStackTrace();
return null;
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
@RequestMapping("saveEditScript")
public @ResponseBody
boolean savescript(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId, @RequestParam String content)
{
System.out.println("entry savescript");
String urlString = masterIP + "RecordScript/updateScript";
Map<String, String> params = this.makeParamsMap("scriptId", scriptId);
params.put("content", content);
try {
HttpResponse httpResponse = this.getHttpRequester().sendPost(
urlString, params, this.makeAccessTockenMap(accessToken));
if (httpResponse == null) {
return false;
}
OperateScriptServerResponseModel operateScriptServerResponseModel = ExtractObject
.extractoperateScriptServerResponseModel(httpResponse
.getContent());
if (operateScriptServerResponseModel.isSuccess()) {
System.out.println("success");
return true;
} else {
System.out.println("failure");
System.out.println(operateScriptServerResponseModel
.getFailCauseString());
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
@RequestMapping("startrecordserver")
public @ResponseBody
OperateScriptServerResponseModel startRecordServer(
@ -97,10 +265,14 @@ public class ScriptActionController extends BaseControllerService {
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
@RequestMapping("saveScriptToDB")
@RequestMapping("saveRecordScript")
public @ResponseBody
boolean saveScriptToDB(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@ -141,172 +313,12 @@ public class ScriptActionController extends BaseControllerService {
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
@RequestMapping("deletescript")
public @ResponseBody
boolean deletescript(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId) {
System.out.println("entry deletscript");
String urlString = masterIP + "RecordScript/deleteScript";
Map<String, String> params = this.makeParamsMap("scriptId", scriptId);
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
urlString, params, this.makeAccessTockenMap(accessToken));
if (httpResponse == null) {
logger.info("network error!");
return false;
}
OperateScriptServerResponseModel operateScriptServerResponseModel = ExtractObject
.extractoperateScriptServerResponseModel(httpResponse
.getContent());
if (operateScriptServerResponseModel == null) {
logger.info("network error!");
return false;
}
if (operateScriptServerResponseModel.isSuccess()) {
System.out.println("success");
logger.info("delete script " + scriptId + " success!");
return false;
} else {
System.out.println("failure");
System.out
.println("cause:"
+ operateScriptServerResponseModel
.getFailCauseString());
logger.info("delete script " + scriptId + " fail" + " cause "
+ operateScriptServerResponseModel.getFailCauseString());
return false;
}
} catch (IOException e) {
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
@RequestMapping("loadscript")
public @ResponseBody
List<Script> loadscript(@ModelAttribute("accessToken") String accessToken) {
System.out.println("enter loadscript");
String urlString = masterIP + "RecordScript/loadScriptList";
try {
HttpResponse httpResponse = this.getHttpRequester().sendPost(
urlString, null, this.makeAccessTockenMap(accessToken));
if (httpResponse == null) {
return null;
}
OperateScriptServerResponseModel operateScriptServerResponseModel = ExtractObject
.extractoperateScriptServerResponseModel(httpResponse
.getContent());
if (operateScriptServerResponseModel == null)
return null;
if (operateScriptServerResponseModel.isSuccess()) {
System.out.println("success");
List<Script> scripts = operateScriptServerResponseModel
.getScripts();
return scripts;
} else {
System.out.println("fail");
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@RequestMapping("editscript")
public @ResponseBody
String editscript(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId) throws IOException, JAXBException {
System.out.println("entry editscript");
String urlString = masterIP + "RecordScript/queryScriptById";
Map<String, String> params = this.makeParamsMap("scriptId", scriptId);
try {
HttpResponse httpResponse = this.getHttpRequester().sendGet(
urlString, params, this.makeAccessTockenMap(accessToken));
if (httpResponse == null) {
System.out.println("fail");
return null;
}
System.out.println(new String(httpResponse.getContent().getBytes(),
"UTF-8"));
OperateScriptServerResponseModel operateScriptServerResponseModel = ExtractObject
.extractoperateScriptServerResponseModel(httpResponse
.getContent());
if (operateScriptServerResponseModel == null) {
System.out.println("fail");
return null;
}
if (operateScriptServerResponseModel.isSuccess()) {
List<Script> scripts = operateScriptServerResponseModel
.getScripts();
Iterator<Script> it = scripts.iterator();
Script script = it.next();
System.out.println("success");
return script.getScriptContent();
} else {
System.out.println("fail");
return null;
}
}
catch (IOException e) {
System.out.println("fail");
e.printStackTrace();
return null;
}
}
@RequestMapping("savescript")
public @ResponseBody
boolean savescript(HttpServletRequest request,
@ModelAttribute("accessToken") String accessToken,
@RequestParam String scriptId, @RequestParam String content)
throws IOException, JAXBException {
String contentNew=new String (content.getBytes(),"UTF-8");
contentNew.replaceAll("<", "&lt;");
contentNew.replaceAll(">", "&gt;");
contentNew.replaceAll("&", "&amp;");
contentNew.replaceAll("\"", "&quot;");
contentNew.replaceAll("'", "&apos;");
System.out.println("entry savescript");
String urlString = masterIP + "RecordScript/updateScript";
Map<String, String> params = this.makeParamsMap("scriptId", scriptId);
params.put("content", content);
System.out.println("content:" + contentNew);
try {
HttpResponse httpResponse = this.getHttpRequester().sendPost(
urlString, params, this.makeAccessTockenMap(accessToken));
if (httpResponse == null) {
return false;
}
OperateScriptServerResponseModel operateScriptServerResponseModel = ExtractObject
.extractoperateScriptServerResponseModel(httpResponse
.getContent());
if (operateScriptServerResponseModel == null)
return false;
if (operateScriptServerResponseModel.isSuccess()) {
System.out.println("success");
return true;
} else {
System.out.println("failure");
System.err.println(operateScriptServerResponseModel
.getFailCauseString());
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}

View File

@ -45,18 +45,15 @@ public class ExtractObject {
}
public static OperateScriptServerResponseModel extractoperateScriptServerResponseModel(
String content) {
String content) throws JAXBException {
OperateScriptServerResponseModel resultModel = new OperateScriptServerResponseModel();
try{
Unmarshaller ummarshaller = JAXBContext.newInstance(
resultModel.getClass()).createUnmarshaller();
resultModel = (OperateScriptServerResponseModel) ummarshaller
.unmarshal(new ByteArrayInputStream(content.getBytes()));
return resultModel;
}
catch(JAXBException e){
e.printStackTrace();
return null;
}
}
}

View File

@ -15,12 +15,15 @@
<link href="css/bootstrap-responsive.css" rel="stylesheet">
<link href="css/charisma-app.css" rel="stylesheet">
<link href="css/jquery-ui-1.8.21.custom.css" rel="stylesheet">
<link href='css/colorbox.css' rel='stylesheet'>
<link href='css/jquery.noty.css' rel='stylesheet'>
<link href='css/noty_theme_default.css' rel='stylesheet'>
<link href='css/opa-icons.css' rel='stylesheet'>
<link href='css/chosen.css' rel='stylesheet'>
<link rel="shortcut icon" href="img/bench4q-1.ico">
<link href="bench4q-css/bench4q.css" rel="stylesheet">
</head>
<body>
@ -79,7 +82,7 @@
</div>
<div class="box-content">
<table class="table table-condensed" id="scripttab">
<table class="table table-condensed" id="scriptTab">
<thead>
<tr>
<th><fmt:message key="scriptname" /></th>
@ -144,17 +147,15 @@
<script src="js/jquery-1.8.2.min.js"></script>
<!-- jQuery UI -->
<script src="js/jquery-ui-1.8.21.custom.min.js"></script>
<script src='js/jquery.dataTables.min.js'></script>
<!-- association with remembering the selected theme -->
<script src="js/jquery.i18n.properties-1.0.9.js"></script>
<script src="js/bootstrap-dropdown.js"></script>
<script src="js/jquery.cookie.js"></script>
<script src="js/theme.js"></script>
<script src='js/jquery.dataTables.min.js'></script>
<script src="script/base.js"></script>
<script src="script/script.js"></script>
<!--jQuery UI -->
<script src="script/home.js"></script>
<script src="script/scriptTable.js"></script>
</fmt:bundle>
</body>
</html>

View File

@ -25,8 +25,6 @@ body {
<link href="css/bootstrap-responsive.css" rel="stylesheet">
<link href="css/charisma-app.css" rel="stylesheet">
<link href="css/jquery-ui-1.8.21.custom.css" rel="stylesheet">
<link href='css/fullcalendar.css' rel='stylesheet'>
<link href='css/fullcalendar.print.css' rel='stylesheet' media='print'>
<link href='css/chosen.css' rel='stylesheet'>
<link href='css/uniform.default.css' rel='stylesheet'>
<link href='css/colorbox.css' rel='stylesheet'>
@ -184,9 +182,7 @@ body {
<!-- custom dropdown library-->
<script src="js/bootstrap-dropdown.js"></script>
<!-- library for advanced tooltip -->
<script src="js/bootstrap-tooltip.js"></script>
<!-- popover effect library -->
<script src="js/bootstrap-popover.js"></script>
<!-- library for cookie management -->
<script src="js/jquery.cookie.js"></script>
<script src='js/jquery.dataTables.min.js'></script>

View File

@ -7,7 +7,7 @@ $(document).ready(function() {
$("#scriptContent").jstree({
"xml_data" : {
"ajax" : {
"url" : "editscript",
"url" : "editScript",
"data" : "scriptId=" + scriptId,
"dataType":"xml",
"success" : function(data) {

View File

@ -0,0 +1,4 @@
var table=$('#scriptTab');
$(document).ready(function() {
loadScript(table,1);
});

View File

@ -57,7 +57,7 @@ function savescript() {
function loadscript() {
$('#scripttab').dataTable().fnClearTable();
$.post("loadscript", {}, function(data) {
$.post("loadScript", {}, function(data) {
if (data != null) {
for ( var i = 0; i < data.length; i++) {
var time = new Date(data[i].createDateTime);
@ -81,7 +81,7 @@ function editscript(obj) {
.children("td").eq(1).text();
window.open("scriptedit.jsp?name=" + scriptName + "&scriptId=" + scriptId);
}
function viewcript(obj) {
function viewScript(obj) {
var row = obj.parentNode.parentNode;
var scriptName = $('#scripttab>tbody').children("tr").eq(row.rowIndex - 1)
.children("td").eq(0).text();
@ -95,7 +95,7 @@ function deletetablerow(obj) {
var row = obj.parentNode.parentNode;
var scriptId = $('#scripttab>tbody').children("tr").eq(row.rowIndex - 1)
.children("td").eq(1).text();
$.post("deletescript", {
$.post("deleteScript", {
scriptId : scriptId
});
$('#scripttab').dataTable().fnDeleteRow(row.rowIndex - 1);

View File

@ -0,0 +1,75 @@
function loadScript(table, actionId) {
table.dataTable().fnClearTable();
$.post("loadScript", {}, function(data) {
if (data != null) {
for ( var i = 0; i < data.length; i++) {
addScriptTableRow(table, data[i], actionId);
}
} else {
// show load banner or error
}
}, "json");
}
function addScriptTableRow(table, data, actionId) {
var time = new Date(data.createDateTime);
time.setTime(data.createDateTime);
switch (actionId) {
case 0:
table.dataTable().fnAddData([ data.name, data.id, time ]);
break;
case 1:
table.dataTable().fnAddData(
[ data.name, data.id, time, viewButton ]);
break;
case 3:
table.dataTable().fnAddData(
[ data.name, data.id, time, editButton + deleteButton ]);
break;
default:
break;
}
}
function deleteScript(table, obj) {
var row = obj.parentNode.parentNode;
var scriptId = table.children("tbody").children("tr").eq(row.rowIndex - 1)
.children("td").eq(1).text();
$.post("deleteScript", {
scriptId : scriptId
}, function(data) {
if (data) {
$('#scripttab').dataTable().fnDeleteRow(row.rowIndex - 1);
} else {
// error message
}
});
}
function getScriptId(table, obj) {
var row = obj.parentNode.parentNode;
var scriptId = table.children("tbody").children("tr").eq(row.rowIndex - 1)
.children("td").eq(1).text();
return scriptId;
}
function getScriptName(table, obj) {
var row = obj.parentNode.parentNode;
var scriptName = $('#scripttab>tbody').children("tr").eq(row.rowIndex - 1)
.children("td").eq(0).text();
return scriptName;
}
function editScript(table, obj) {
var scriptId = getScriptId(table, obj);
var scriptName = getScriptName(table, obj);
window.open("scriptedit.jsp?name=" + scriptName + "&scriptId=" + scriptId);//change to model
}
function viewScript(table,obj) {
var scriptId = getScriptId(table, obj);
var scriptName = getScriptName(table, obj);
window.open("scriptview.jsp?name=" + scriptName + "&scriptId=" + scriptId);//change to model
}
var viewButton = "<a class='btn btn-success' onClick='viewScript(table,this)'><i class='icon-zoom-in icon-white'></i>View</a>";
var editButton = "<a class='btn btn-info' onClick='editScript(table,this)'><i class='icon-edit icon-white'></i>Edit</a>";
var deleteButton = "<a class='btn btn-danger' href='#' onClick='deleteScript(table,this)'><i class='icon-trash icon-white'></i>Del</a>";