sord = req.getParameter("sord");
SSPageInfo sspInfo = new SSPageInfo(page, limit, sidx, sord);
sspInfo.set_search( req.getParameter("_search") );
if(sspInfo.is_search() && req.getParameter("filters")!=null ) {
JSONParser jnParser = new JSONParser();
JSONObject jo = (JSONObject)jnParser.parse( req.getParameter("filters") );
sspInfo.setCondition(jo);
}
// $2 -----------------------
List<UserAccount> accounts = UserAccountServices.getInstance().sspUserAccount(sspInfo);
// $3 -----------------------
// constructing a JSON
JSONObject jnObj = new JSONObject();
jnObj.put("page", sspInfo.getPage());
jnObj.put("total", sspInfo.getTotal());
jnObj.put("records", sspInfo.getRecords());
JSONArray jnAry = new JSONArray();
JSONObject perJN;
UserAccount acc = null;
for(int i=0, size=accounts.size(); i<size; i++) {
acc = accounts.get(i);
perJN = new JSONObject();
perJN.put("id", acc.getId());
perJN.put("account", acc.getAccount());
perJN.put("profile.name", acc.getProfile().getName());
perJN.put("profile.employeeId", acc.getProfile().getEmployeeId());
perJN.put("profile.email", acc.getProfile().getEmail());
perJN.put("profile.mobile", acc.getProfile().getMobile());
perJN.put("myGroups", acc.getMyGroups());
perJN.put("delMark", acc.isDeleteMark());
jnAry.add(perJN);
}
jnObj.put("rows", jnAry);
// System.out.println( "JSON.toString = \n"+ jnObj.toString() );
res.setContentType("text/json;charset=utf-8");
res.getWriter().write( jnObj.toString() );
// EDIT
} else if (req.getRequestURI().endsWith("edit.do")) {
// System.out.println("req.URI="+req.getRequestURI());
UserAccount user = this.compose(req);
JSONObject jnObj = new JSONObject();
try {
if( !UserAccountServices.getInstance().saveOrUpdateUserAccount(user, true) ) {
jnObj.put("msg", "Edit User Info Failure~!");
res.getWriter().write(jnObj.toString());
} else {
jnObj.put("msg","Edit User Info Success !");
res.getWriter().write(jnObj.toString());
}
} catch (Exception ex) {
res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
jnObj.put("msg",ex.getMessage());
res.getWriter().write(jnObj.toString());
}
// ADD
} else if (req.getRequestURI().endsWith("add.do")) {
// System.out.println("req.URI="+req.getRequestURI());
UserAccount user = compose(req);
try {
if(!UserAccountServices.getInstance().saveOrUpdateUserAccount(user, false)) {
res.getWriter().write("Add User Account/Profile/Group Failure~!");
} else {
res.getWriter().write("Add User Account/Profile/Group Success !");
}
} catch (Exception ex) {
//ex.printStackTrace();
//res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
res.getWriter().write(ex.getMessage());
}
// DEL
} else if (req.getRequestURI().endsWith("del.do")) {
// System.out.println("req.URI="+req.getRequestURI());
String account = req.getParameter("account");
try {
if(! UserAccountServices.getInstance().deleteMark(account, true) ) {
res.getWriter().write("Mark Delete failure!");
} else {
res.getWriter().write("Mark Delete Success!");
}
} catch(Exception ex) {
res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
res.getWriter().write(ex.getMessage());
}
// RESET
} else if (req.getRequestURI().endsWith("reset.do")) {
// System.out.println("req.URI="+req.getRequestURI());
String account = req.getParameter("account");
String type = req.getParameter("type");
JSONObject jnObj = new JSONObject();
try {
if("R".equals(type)) {
if(! UserAccountServices.getInstance().resetPassword(account, account) ) {
res.getWriter().write("Reset Password Failure!");
} else {
res.getWriter().write("Reset Password Success!<br> Your New Password is Your Account! ");
}
} else if("C".equals(type)) {
if(! UserAccountServices.getInstance().deleteMark(account, false) ) {
res.getWriter().write("Cancel Delete Mark Failure!");
} else {
res.getWriter().write("Cancel Delete Mark Success!");
}
} else if("N".equals(type)) {
String oldPwd = req.getParameter("oldPwd");
String newPwd = req.getParameter("newPwd");
if(! UserAccountServices.getInstance().setNewPassword(account, oldPwd, newPwd) ) {
jnObj.put("msg", "Set New Password Failure!");
res.getWriter().write(jnObj.toString());
} else {
jnObj.put("msg", "Set New Password Success!");
res.getWriter().write(jnObj.toString());
}
}
} catch(Exception ex) {
res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
jnObj.put("msg", ex.getMessage());
res.getWriter().write( jnObj.toString() );
}
// List GROUPS
} else if(req.getRequestURI().endsWith("groups.do")) {
Integer page, limit;
String sidx, sord;
// $1 ----------------------
// get the requested page
page = Integer.valueOf(req.getParameter("page"));
// get how many rows we want to have into the grid
// rowNum parameter in the grid
limit = Integer.valueOf(req.getParameter("rows"));
// get index row - i.e. user click to sort
// at first time sortname parameter - after that the index from colModel
sidx = req.getParameter("sidx");
// sorting order - at first time sortorder
sord = req.getParameter("sord");
SSPageInfo sspInfo = new SSPageInfo(page, limit, sidx, sord);
sspInfo.set_search( req.getParameter("_search") );
if(sspInfo.is_search() && req.getParameter("filters")!=null ) {
JSONParser jnParser = new JSONParser();
JSONObject jo = (JSONObject)jnParser.parse( req.getParameter("filters") );
sspInfo.setCondition(jo);
}
// $2 -----------------------
List<Group> allGroups = UserAccountServices.getInstance().sspGroups(sspInfo);