package com.skymobi.qc.admin.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.skymobi.qc.admin.action.FieldsBO;
import com.skymobi.qc.admin.action.ProjectBO;
import com.skymobi.qc.admin.dal.domain.CustomFields;
import com.skymobi.qc.admin.dal.domain.CustomFieldsList;
import com.skymobi.qc.admin.dal.domain.Project;
import com.skymobi.qc.admin.framework.base.BaseController;
import com.skymobi.qc.admin.framework.util.Main;
import com.skymobi.qc.admin.framework.util.Page;
import com.skymobi.qc.admin.framework.util.ParaMap;
import com.sun.org.glassfish.gmbal.ParameterNames;
@Controller
@RequestMapping({"/project"})
public class ProjectController extends BaseController {
@Autowired
private ProjectBO projectBO;
@Autowired
private FieldsBO fieldsBO;
@RequestMapping(value = "/new")
public String projectNew(Model model) {
getMain(model);
model.addAttribute("project", new Project());
model.addAttribute("projectList", projectBO.getProjectList());
return "/project/new";
}
@RequestMapping(value = "/add")
public void projectAdd(Model model,Project project) {
try {
String msg = projectBO.projectAdd(project);
if (msg.equals("")){
success(model);
if (project.getId() == 0){
model.addAttribute("RedirectView", "/project/"+project.getTag()+"/settings?message=更新成功");
}
}else{
failed(model, msg);
}
} catch (Exception e) {
failed(model, e);
}
}
@RequestMapping("/{tag}")
public String project(@PathVariable String tag,Model model) {
Project project = projectBO.getProjectByTag(tag);
model.addAttribute("project", projectBO.getProjectByTag(tag));
model.addAttribute("projectList", projectBO.getProjectList(project));
model.addAttribute("customFieldList", fieldsBO.customFieldList(project.getId()));
Main main = getMain(model);
projectBO.setMainMenu(main, tag);
main.setAction("/project/"+tag);
return "/project/summary";
}
@RequestMapping("/{tag}/destroy")
public void destroy(@PathVariable String tag,Model model) {
try {
String msg = projectBO.projectDestroy(tag);
if (msg.equals("")){
successByDel(model);
}else{
failed(model,msg);
}
} catch (Exception e) {
failed(model, e);
}
}
@RequestMapping("/{tag}/settings")
public String settings(@PathVariable String tag,Model model,HttpServletRequest request ) {
Project project = projectBO.getProjectByTag(tag);
model.addAttribute("project", projectBO.getProjectByTag(tag));
model.addAttribute("projectList", projectBO.getProjectList(project));
model.addAttribute("customFieldList", fieldsBO.customFieldList(project.getId()));
model.addAttribute("message", getParaMap(request).get("message"));
model.addAttribute("settingsType", "");
Main main = getMain(model);
projectBO.setMainMenu(main, tag);
main.setAction("/project/"+tag+"/settings");
return "/project/settings";
}
@RequestMapping("/{tag}/settings/{type}")
public String settings(@PathVariable String tag,@PathVariable String type,Model model,HttpServletRequest request ) {
Project project = projectBO.getProjectByTag(tag);
model.addAttribute("project", projectBO.getProjectByTag(tag));
model.addAttribute("projectList", projectBO.getProjectList(project));
model.addAttribute("customFieldList", fieldsBO.customFieldList(project.getId()));
model.addAttribute("message", getParaMap(request).get("message"));
model.addAttribute("settingsType", type);
Main main = getMain(model);
projectBO.setMainMenu(main, tag);
main.setAction("/project/"+tag+"/settings");
return "/project/settings";
}
/**
* customfield
* @param tag
* @param model
* @return
*/
@RequestMapping(value = "/{tag}/customfield/{id}")
public String customfield(@PathVariable String tag,@PathVariable int id,Model model) {
model.addAttribute("project", projectBO.getProjectByTag(tag));
model.addAttribute("operate", "edit");
model.addAttribute("field", fieldsBO.getCustomFields(id));
Main main = getMain(model);
projectBO.setMainMenu(main, tag);
main.setAction("/project/"+tag+"/settings");
return "/admin/custom_field_new";
}
@RequestMapping(value = "/{tag}/customfield/new")
public String customfieldNew(@PathVariable String tag,Model model) {
model.addAttribute("project", projectBO.getProjectByTag(tag));
Main main = getMain(model);
projectBO.setMainMenu(main, tag);
main.setAction("/project/"+tag+"/settings");
model.addAttribute("field", new CustomFields());
return "/admin/custom_field_new";
}
@RequestMapping(value = "/{id}/customfield/add")
public void customfieldAdd(@PathVariable int id,Model model,CustomFields cf) {
try {
String msg = fieldsBO.customFieldAdd(cf,id);
model.addAttribute("customFields", cf);
if (msg.equals("")){
success(model);
}else{
failed(model, msg);
}
} catch (Exception e) {
failed(model, e);
}
}
@RequestMapping(value = "/customfield/destroy/{id}")
public void customfieldDestroy(@PathVariable int id,Model model) {
try {
fieldsBO.customfieldDestroy(id);
success(model);
} catch (Exception e) {
failed(model, e);
}
}
//issues
@RequestMapping(value = "/{tag}/items")
public String items(@PathVariable String tag,Model model,HttpServletRequest request) {
Project project = projectBO.getProjectByTag(tag);
model.addAttribute("project", project);
//所有的字段名
model.addAttribute("customFieldList", fieldsBO.customFieldList(project.getId()));
//所有数据
ParaMap<String, Object> pm = getParaMap(request);
pm.put("table_name", project.getTable_name());
List<HashMap<String, Object>> itemList = projectBO.itemList(pm);
model.addAttribute("itemList", itemList);
//page
Page page = new Page();
page.setPageSize(30);
page.setTotalRow(itemList.size());
model.addAttribute("page", page);
model.addAttribute("pageJson", JSONObject.fromObject(page).toString());
Main main = getMain(model);
projectBO.setMainMenu(main, tag);
main.setAction("/project/"+tag+"/items");
return "/project/items";
}
@RequestMapping(value = "/{tag}/items/import")
public String itemsImport(@PathVariable String tag,Model model) {
Project project = projectBO.getProjectByTag(tag);
model.addAttribute("project", project);
Main main = getMain(model);
projectBO.setMainMenu(main, tag);
main.setAction("/project/"+tag+"/item/new");
return "/project/items_import";
}
@RequestMapping(value = "/{tag}/items/import/upload")
public void itemsImport(@PathVariable String tag,Model model,@RequestParam("item_import") CommonsMultipartFile mFile) {
try{
Project project = projectBO.getProjectByTag(tag);
model.addAttribute("project", project);
projectBO.itemsImportCsv(mFile,project);
success(model,"上传成功");
}catch(Exception e){
failed(model, "服务器异常");
}
//@RequestParam("item_import") CommonsMultipartFile mFile
//MultipartHttpServletRequest request
}
@RequestMapping(value = "/{tag}/getItems")
public void getItems(@PathVariable String tag,Model model,HttpServletRequest request,Page page) {
Project project = projectBO.getProjectByTag(tag);
model.addAttribute("project", project);
//所有的字段名
model.addAttribute("customFieldList", fieldsBO.customFieldList(project.getId()));
//所有数据
ParaMap<String, Object> pm = getParaMap(request);
pm.put("table_name", project.getTable_name());
List<HashMap<String, Object>> itemList = projectBO.itemList(pm);
model.addAttribute("itemList", itemList);
//page
page.setTotalRow(itemList.size());
model.addAttribute("page", page);
}
@RequestMapping(value = "/{tag}/item/{id}")
public String item(@PathVariable String tag,@PathVariable int id,Model model) {
Project project = projectBO.getProjectByTag(tag);
model.addAttribute("project", project);
model.addAttribute("view", "modfiy");
//item
model.addAttribute("item", projectBO.getItem(project,id));
//所有的字段名
model.addAttribute("customFieldList", fieldsBO.customFieldList(project.getId()));
model.addAttribute("layout", fieldsBO.getLayout(project.getId()));
Main main = getMain(model);
projectBO.setMainMenu(main, tag);
main.setAction("/project/"+tag+"/items");
return "/project/items_new";
}
@RequestMapping(value = "/{tag}/items/new")
public String itemNew(@PathVariable String tag,Model model) {
Project project = projectBO.getProjectByTag(tag);
model.addAttribute("project", project);
model.addAttribute("view", "new");
model.addAttribute("item", new HashMap<String,Object>());
//所有的字段名
model.addAttribute("customFieldList", fieldsBO.customFieldList(project.getId()));
model.addAttribute("layout", fieldsBO.getLayout(project.getId()));
Main main = getMain(model);
projectBO.setMainMenu(main, tag);
main.setAction("/project/"+tag+"/items/new");
return "/project/items_new";
}
@RequestMapping(value = "/{id}/items/add")
public void itemAdd(@PathVariable int id,Model model,HttpServletRequest request) {
try {
Project project = projectBO.getProjectByAttr("id",id);
model.addAttribute("project", project);
ParaMap<String, Object> pm = getParaMap(request);
pm.put("table_name", project.getTable_name());
projectBO.itemAdd(project, pm);
success(model);
} catch (Exception e) {
failed(model, e);
}
}
@RequestMapping(value = "/{tag}/setting/layout/newitem")
public String layoutNewItem(@PathVariable String tag,Model model) {
Project project = projectBO.getProjectByTag(tag);
model.addAttribute("project", project);
model.addAttribute("view", "layout");
//所有的字段名
model.addAttribute("customFieldList", fieldsBO.customFieldList(project.getId()));
model.addAttribute("layout", fieldsBO.getLayout(project.getId()));
Main main = getMain(model);
projectBO.setMainMenu(main, tag);
main.setAction("/project/"+tag+"/settings");
return "/project/items_new_layout";
}
@RequestMapping(value = "/{id}/setting/layout/newitem/save")
public void layoutNewItemSave(@PathVariable int id,Model model,@RequestParam("json") String jsonstring) {
try {
JSONArray json = JSONArray.fromObject(jsonstring);
List<CustomFields> cf = new ArrayList<CustomFields>();
for(int i = 0; i < json.size(); i++){
JSONObject jsonObject = json.getJSONObject(i);
cf.add((CustomFields)JSONObject.toBean(jsonObject, CustomFields.class));
}
fieldsBO.layoutNewItemSave(cf,id);
success(model);
} catch (Exception e) {
failed(model, e);
}
}
}