OFBIZ--对数据库的操作

时间:2021-06-30 13:02:27
/**
	 * 功能:取得demo信息列表
	 * @param request
	 * @param response
	 * @return
	 */	
	public static  String getDemoList(HttpServletRequest request,HttpServletResponse response)
	{
		//取得数据源对象
		//GenericDelegator delegator = (GenericDelegator) request.getAttribute("default");
		GenericDelegator delegator = GenericDelegator.getGenericDelegator("default");
		List returnList=null;
		try 
		{
			//取得所有数据
			returnList=delegator.findAll("Demo");//select * from demo
			delegator.findByAnd("Demo", UtilMisc.toMap("demoId", "1","demoName","a"));
			//select * from demo where domo_id='1' and demo_name='a'
			delegator.findByOr("Demo", UtilMisc.toMap("demoId", "1","demoName","a"));
			delegator.findByPrimaryKey("Demo",UtilMisc.toMap("demoId","1"));
		} 
		catch (GenericEntityException ex) {
			Debug.log(ex);
			request.setAttribute("myException", ex);
			String strExInfo = ExceptionUtil.getStackTraceInfo(ex,"查询失败!");
			request.setAttribute(SiteDefs.ERROR_MESSAGE, strExInfo);
			return "error";
		}
		//将数据放入缓存中,在页面可以拿到
		request.setAttribute("returnList", returnList);
		//根据返回值确定在那一个页面展示,起页面导向的作用
		return "success";
	}
	
	
	public static  String getDemoYanShi(HttpServletRequest request,HttpServletResponse response)
	{
		//取得数据源对象
		//GenericDelegator delegator = (GenericDelegator) request.getAttribute("default");
		GenericDelegator delegator = GenericDelegator.getGenericDelegator("default");
		List returnList=null;
		try 
		{
			//取得所有数据
			returnList=delegator.findAll("Demo");
			delegator.findByAnd("Demo", UtilMisc.toMap("demoId", "1","demoName","a"));
			delegator.findByOr("Demo", UtilMisc.toMap("demoId", "1","demoName","a"));
		} 
		catch (GenericEntityException ex) {
			Debug.log(ex);
			request.setAttribute("myException", ex);
			String strExInfo = ExceptionUtil.getStackTraceInfo(ex,"查询失败!");
			request.setAttribute(SiteDefs.ERROR_MESSAGE, strExInfo);
			return "error";
		}
		//将数据放入缓存中,在页面可以拿到
		request.setAttribute("returnList", returnList);
		//根据返回值确定在那一个页面展示,起页面导向的作用
		String type="suce";
		if(type=="shixisheng")
			return "shixisheng";
		else
		    return "success";
	}
	
	/**
	 * 功能:取得demo信息列表 (服务方式)
	 * 参数:ctx,context
	 * 返回值:Map
	 */	
	public static Map getDemoServiceList(DispatchContext ctx, Map context){
		
		//获取实体引擎数据连接
		GenericDelegator delegator = ctx.getDelegator();
		//当前用户登录信息
		GenericValue userLogin = (GenericValue) context.get("userLogin");
		//LocalDispatcher对象
		LocalDispatcher dispatcher = (LocalDispatcher)ctx.getDispatcher();
		//Map map = dispatcher.runSync("getDemoServiceList", paramMap);
		String areaId = (String)context.get("areaId");
		
		//定义返回值集合
		Map result = new HashMap();
		//定义返回参数
		List returnList = null;
		//定义排序List
		List orderByList = new ArrayList();
		//String areaId = userLogin.getString("areaId");
		orderByList.add("demoId desc");
		EntityCondition condition;
		List constraints = new LinkedList();
		
		try
		{
			//select demoId,demoname,sex from demo where id='' or   name='' order by demoId desc
			EntityExpr eexprAreaId = new EntityExpr("demoId", EntityOperator.NOT_EQUAL, null);//demo_id!=null
			EntityExpr eexprName = new EntityExpr("demoName", EntityOperator.EQUALS, "aaa");//demo_name='aaa'
			constraints.add(eexprAreaId);
			constraints.add(eexprName);

			condition = new EntityConditionList(constraints, EntityOperator.AND);//demo_id!=null and demo_name='aaa'
			
			Collection fieldsToSelect = new ArrayList();
			fieldsToSelect.add("demoId");
			fieldsToSelect.add("demoName");
			returnList = delegator.findByCondition("Demo", condition, fieldsToSelect, orderByList);
			/*
			EntityExpr type1 = new EntityExpr("type1", EntityOperator.NOT_EQUAL, "1");type1!=1
	        EntityExpr type2 = new EntityExpr("type2", EntityOperator.EQUALS, "2");type2=2
			
	        EntityExpr type4 = new EntityExpr(type1,EntityOperator.AND,type2);type1!=1 and  type2=2
			
	        EntityExpr type3 = new EntityExpr("type3", EntityOperator.EQUALS, "3"); type3=3
	        constraints.add(type3);
	        constraints.add(type4);
	        condition = new  EntityConditionList(constraints, EntityOperator.OR); 
	        
	        type3=3 or (type1!=1 and  type2=2)
	        
	        (type1="1" and type2="2") or (type3="3")
			 */
		} catch (Exception ex)
		{
			Debug.log(ex);
			result.put("myException", ex);
			String strExInfo = ExceptionUtil.getStackTraceInfo(ex, "查询失败!");
			result.put(SiteDefs.ERROR_MESSAGE, strExInfo);
			result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
			return result;
		}
		result.put("returnList", returnList);
		result.put("success", "success");
		result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
		return result;
	}
	
	/**
	 * 功能:根据角色取得demo信息列表
	 * @param request
	 * @param response
	 * @return
	 */	
	public static  String getDemoByRoleList(HttpServletRequest request,HttpServletResponse response)
	{
		//取得数据源对象
		GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
		//当前用户登录信息
		GenericValue userLogin = (GenericValue)request.getSession().getAttribute(SiteDefs.USER_LOGIN);
		// 权限对象
		Security security = (Security) request.getAttribute(" security ");
		// LocalDispatcher对象
		LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");

		List returnList=null;
		try 
		{
			if(security.hasRole("accessory_admin", userLogin)){
				//取得所有数据
				//调用服务方式:(服务名,传入参数)				
				Map paramMap = new HashMap();
				Map map = dispatcher.runSync("getDemoServiceList", paramMap);
				returnList =  (List)map.get("returnList");
			}else{
				//其他的处理
			}
		} 
		catch (GenericServiceException ex) {
			Debug.log(ex);
			request.setAttribute("myException", ex);
			String strExInfo = ExceptionUtil.getStackTraceInfo(ex,"查询失败!");
			request.setAttribute(SiteDefs.ERROR_MESSAGE, strExInfo);
			return "error";
		}
		//将数据放入缓存中,在页面可以拿到
		request.setAttribute("returnList", returnList);
		//根据返回值确定在那一个页面展示,起页面导向的作用
		return "success";
	}
	
	/**
	 * 功能:新增Demo信息
	 * @param request
	 * @param response
	 * @return
	 */
	public static  String addSaveDemo(HttpServletRequest request, HttpServletResponse response)
	{
		GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
		//request.getParameter(arg0)
		Map context = UtilHttp.getParameterMap(request);
		String demoIdStr="";
		GenericValue demoGV=null;
		
		try 
		{
			demoIdStr = delegator.getNextSeqId("Demo").toString();
			
			demoGV = delegator.makeValue("Demo",UtilMisc.toMap("demoId",demoIdStr));
			/* 要求表单的id名称和数据字段的名称一致,才能使用下面的写法
			 * indicatrixGV.setNonPKFields(context);
			 * */
			//新增记录
			demoGV.setNonPKFields(context); //多条数据一起放入
			//一条数据一条的放入
			//demoGV.setString("demoName", request.getParameter("demoName"));
			//demoGV.setString("sex", request.getParameter("sex"));
			demoGV.create();
		} catch (GenericEntityException ex) {
			Debug.log(ex);
			request.setAttribute("myException", ex);
			String strExInfo = ExceptionUtil.getStackTraceInfo(ex,"新增失败!");
			request.setAttribute(SiteDefs.ERROR_MESSAGE, strExInfo);
			return "error";
		}
		return "success";
	}
	
	
	/**
	 * 功能:修改Demo信息
	 * @param request
	 * @param response
	 * @return
	 */
	public static  String updateDemo(HttpServletRequest request,HttpServletResponse response)
	{
		GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
		Security  security  = (Security)request.getAttribute("security");
		GenericValue userLogin = (GenericValue)request.getSession().getAttribute(SiteDefs.USER_LOGIN);

		Map context = UtilHttp.getParameterMap(request);
		//表单参数
		String demoId=request.getParameter("demoId");
		String demoName=request.getParameter("demoName");
		GenericValue demoGV=null;
		
		try 
		{	
			demoGV = delegator.findByPrimaryKey("Demo", UtilMisc.toMap("demoId", demoId));
			
			demoGV.setNonPKFields(context);
			demoGV.store();
			
		} catch (GenericEntityException ex) {
			Debug.log(ex);
			request.setAttribute("myException", ex);
			String strExInfo = ExceptionUtil.getStackTraceInfo(ex,"新增失败!");
			request.setAttribute(SiteDefs.ERROR_MESSAGE, strExInfo);
			return "error";
		}
		return "success";
	}
	
	/**
	 * 功能:删除Demo信息
	 * @param request
	 * @param response
	 * @return
	 */
	public static  String delDemo(HttpServletRequest request,HttpServletResponse response)
	{
		GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
		//可以删除多条数据
		String demoIdsStr=request.getParameter("demoIds").toString();
		String[] demoIdArray=demoIdsStr.split(",");
		String demoIdStr = "";
		try 
		{
			for (int i = 0; i < demoIdArray.length; i++) 
			{
				demoIdStr=demoIdArray[i].toString();
				//删除数据
				delegator.removeByAnd("Demo", UtilMisc.toMap("demoId",demoIdStr));
			}
			
		} 
		catch (GenericEntityException ex) {
			Debug.log(ex);
			request.setAttribute("myException", ex);
			String strExInfo = ExceptionUtil.getStackTraceInfo(ex,"删除失败!");
			request.setAttribute(SiteDefs.ERROR_MESSAGE, strExInfo);
			return "error";
		}
		return "success";
	}
	
	public static String hello(String str){
		return str;
	}