/**
 * Company Name : 中贸促信息技术有限责任公司
 * Project Name:project
 * File Name:MetadataController.java
 * Package Name:org.ccpit.base.metadataManage
 * Date:2015年9月25日上午10:24:53
 * Copyright (c) 2015, dingwei@ccpit.org All Rights Reserved.
 *
*/

package org.ccpit.base.metadataManage;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.ccpit.base.controller.BaseController;
import org.ccpit.base.controller.Page;
import org.ccpit.base.controller.PageRequest;
import org.ccpit.base.user.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * ClassName:MetadataController <br/>
 * Function: TODO 数据字典管理控制类. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2015年9月25日 上午10:24:53 <br/>
 * @author   dingwei
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Controller
@RequestMapping("/admin/metadataManage")
public class MetadataController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(MetadataController.class);
	@Autowired
	private MetadataService metadataService;
	/*
	 * 进入机构管理列表界面
	 */
	@RequestMapping("/goinMetadataListPage")
	public Object goinOrgListPage() {
		ModelAndView mv = new ModelAndView("admin/metadataManage/metadataListPage");
		return mv;
	}
	
	@RequestMapping("/getAllMetadatas")
    @ResponseBody
    public Object getAllMetadatas(HttpServletRequest request){
        PageRequest pageRequest = getPage(request);
        String dataType = request.getParameter("dataType");
        String roleName = request.getParameter("roleName");
        StringBuffer hql = new StringBuffer("from Metadata where 1=1");
        if(null != roleName && null != dataType){
        	if(null != roleName && !"".equals(roleName)){
        		hql.append(" and dataName like '%"+ roleName +"%'");
        	}
        	if(null != dataType && !"".equals(dataType)){
        		hql.append(" and dataType ='"+ dataType +"'");
        	}
        }
        hql.append(" order by createDate desc");
        Page<Metadata> pageRole = metadataService.queryPage(pageRequest,hql.toString());
        return metadataService.convert(pageRole);
    }

	@RequestMapping("/addOrUpdateMetadata")
	@ResponseBody
	public Object addOrUpdateMetadata(HttpServletRequest request,HttpServletResponse response, Metadata metadata) {
		Map map = new HashMap();
		User user = (User) request.getSession().getAttribute("user_in_session");
		String metadataId = request.getParameter("metadataId");
		String languageType = request.getParameter("languageType");
		//判断操作类别  新增或修改
		String operate = request.getParameter("operate");
		metadata.setLanguageType(languageType);
		if ("add".equals(operate)) {
			metadata.setCreateDate(new Date());
			metadata.setCreator(user.getUsername());
			metadata.setCreatorId(user.getId());
			metadataService.addMetadata(metadata);
			map.put("flag", true);
			map.put("info", "信息保存成功！");
		}else if("edit".equals(operate)) {
			Metadata metadata1 = metadataService.getMetadataById(Long.valueOf(metadataId));
			if(null != metadata.getDataName()){
				metadata1.setDataName(metadata.getDataName());
			}
			if(null != metadata.getDataType()){
				metadata1.setDataType(metadata.getDataType());
			}
			if(null != metadata.getDescription()){
				metadata1.setDescription(metadata.getDescription());
			}
			if(null != metadata.getDataNameId()){
				metadata1.setDataNameId(metadata.getDataNameId());
			}
			if(null != metadata.getLanguageType()){
				metadata1.setLanguageType(metadata.getLanguageType());
			}
			metadataService.updateMetadata(metadata1);
			map.put("flag", true);
			map.put("info", "信息保存成功！");
		}else {
			map.put("flag", false);
		}
		return map;
	}

	/**
	 * 根据id删除
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/deleteMetadata")
	@ResponseBody
	public Object deleteMetadata(HttpServletRequest request){
		Map result = new HashMap();
		String jsonIds = request.getParameter("ids");
		JSONArray jsonArray = JSONArray.fromObject(jsonIds);
		Object[] ids = jsonArray.toArray();
		boolean flag = false;
		for(Object metadataId : ids){
			flag = metadataService.delMetadata((Integer)metadataId);
		}
		result.put("flag", flag);
		return result;
	}

	@RequestMapping("/queryMetadataById")
	@ResponseBody
	public Object queryMetadataById(HttpServletRequest request) {
		String metadataId = request.getParameter("metadataId");
		Metadata metadata = metadataService.getMetadataById(Long.valueOf(metadataId));
		return metadata;
	}
	
	@RequestMapping("/getMetadatasList")
	@ResponseBody
	public Object getMetadatasList(HttpServletRequest request){
		String dataType = request.getParameter("dataType"); 
		String languageType = request.getParameter("languageType");
		String hql = "from Metadata where dataType ='"+ dataType +"'";
		List<Metadata> listBo = metadataService.getAllMetadatas(hql);
		List list = new ArrayList();
		if(null != listBo){
			for(int i=0;i<listBo.size();i++){
				Map result = new HashMap();
				result.put("id", listBo.get(i).getDataNameId());
				result.put("text", listBo.get(i).getDataName());
				list.add(result);
			}
		}
		return list;
	}
}

