package com.base.helper; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import com.amarsoft.are.ARE; import com.amarsoft.are.jbo.BizObject; import com.amarsoft.are.jbo.BizObjectManager; import com.amarsoft.are.jbo.BizObjectQuery; import com.amarsoft.are.jbo.JBOException; import com.amarsoft.are.jbo.JBOFactory; import com.amarsoft.are.jbo.JBOTransaction; import com.base.constant.RestfullConstant; import com.base.tool.ExpireTool; import com.base.util.BizObjectUtil; public class SessionHelper { public static List> getSessionTokenByToken(String sToken) { List> list = new ArrayList>(); BizObject bo = null; try { BizObjectManager bom = JBOFactory .getBizObjectManager("jbo.app.TOKEN_SESSION"); BizObjectQuery boq = bom.createQuery("TOKEN=:TOKEN").setParameter( "TOKEN", sToken); bo = boq.getSingleResult(false); } catch (JBOException e) { ARE.getLog().error(e); } if (bo == null) return list; BizObjectUtil boUtil = new BizObjectUtil(); list.add(boUtil.bizObject2MapValue(bo)); return list; } public static List> getSessionTokenBySession( HttpServletRequest req) { String sUserId = (String) req.getSession() .getAttribute( (String) RestfullConstant.sessionProperty.get("USERID" .toLowerCase())); String sToken = (String) req.getSession() .getAttribute( (String) RestfullConstant.sessionProperty.get("TOKEN" .toLowerCase())); String sExpire = new Date().getTime() + ""; List> list = new ArrayList>(); BizObject bo = null; try { BizObjectManager bom = JBOFactory .getBizObjectManager("jbo.app.TOKEN_SESSION"); BizObjectQuery boq = bom .createQuery( "TOKEN=:TOKEN AND USER_ID=:USER_ID AND EXPIRE_IN > :EXPIRE_IN") .setParameter("TOKEN", sToken) .setParameter("USER_ID", sUserId) .setParameter("EXPIRE_IN", sExpire); bo = boq.getSingleResult(false); } catch (JBOException e) { ARE.getLog().error(e); } if (bo == null) return list; BizObjectUtil boUtil = new BizObjectUtil(); list.add(boUtil.bizObject2MapValue(bo)); return list; } public static List> getSessionTokenByUserId( String sUserId) { List> list = new ArrayList>(); BizObject bo = null; try { BizObjectManager bom = JBOFactory .getBizObjectManager("jbo.app.TOKEN_SESSION"); BizObjectQuery boq = bom.createQuery("USER_ID=:USER_ID") .setParameter("USER_ID", sUserId); bo = boq.getSingleResult(false); } catch (JBOException e) { ARE.getLog().error(e); } if (bo == null) return list; BizObjectUtil boUtil = new BizObjectUtil(); list.add(boUtil.bizObject2MapValue(bo)); return list; } public static void save(JBOTransaction tx, Map map) throws JBOException { try { BizObjectManager bom = JBOFactory .getBizObjectManager("jbo.app.TOKEN_SESSION"); BizObject bo = bom.newObject(); ; for (String key : map.keySet()) { bo.setAttributeValue(key.toUpperCase(), map.get(key)); } if (bo == null) return; tx.join(bom); bom.saveObject(bo); tx.commit(); } catch (JBOException e) { ARE.getLog().error(e); } } public static Long edit(JBOTransaction tx, HttpServletRequest req) throws JBOException { try { BizObjectManager bom = JBOFactory .getBizObjectManager("jbo.app.TOKEN_SESSION"); String sUserId = (String) req.getSession().getAttribute("userid"); String sToken = (String) req.getSession().getAttribute("token"); String sExpire = ExpireTool.getExpireTime() + ""; BizObjectQuery boq = bom .createQuery("USER_ID=:USER_ID AND TOKEN=:TOKEN") .setParameter("USER_ID", sUserId) .setParameter("TOKEN", sToken); BizObject bo = boq.getSingleResult(true); if (bo == null) return 0l; bo.setAttributeValue("EXPIRE_IN", sExpire); tx.join(bom); bom.saveObject(bo); tx.commit(); req.getSession().setAttribute("expire", sExpire); return Long.valueOf(sExpire); } catch (JBOException e) { ARE.getLog().error(e); return 0l; } } public static void removerByUserId(JBOTransaction tx, String sUserId) throws JBOException { try { BizObjectManager bom = JBOFactory .getBizObjectManager("jbo.app.TOKEN_SESSION"); BizObjectQuery boq = bom.createQuery("USER_ID=:USER_ID") .setParameter("USER_ID", sUserId); BizObject bo = boq.getSingleResult(true); if (bo == null) return; tx.join(bom); bom.deleteObject(bo); tx.commit(); } catch (JBOException e) { ARE.getLog().error(e); } } public static void removerAll() throws JBOException { try { BizObjectManager bom = JBOFactory .getBizObjectManager("jbo.app.TOKEN_SESSION"); BizObjectQuery boq = bom.createQuery("1=1"); @SuppressWarnings("unchecked") List bos = boq.getResultList(true); for (BizObject bo : bos) { bom.deleteObject(bo); } } catch (JBOException e) { ARE.getLog().error(e); } } }