/**
   * update the session every time a user makes a request
   *
   * @param SID
   */
  private void updatesession(String SID) {
    try {
      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery<Sessiondata> cq = cb.createQuery(Sessiondata.class);
      Root<Sessiondata> c = cq.from(Sessiondata.class);
      Predicate condition = cb.equal(c.get("session_id"), SID);
      cq.where(condition);

      TypedQuery<Sessiondata> q = em.createQuery(cq);

      List<Sessiondata> fullList = q.getResultList();
      if (fullList.size() == 0) {
        log.error("Found NO session to updateSession: ");

      } else {
        // log.debug("Found session to updateSession: ");
        Sessiondata sd = fullList.iterator().next();
        // log.debug("Found session to updateSession sd "+sd.getUser_id()+" "+sd.getSession_id());
        sd.setRefresh_time(new Date());

        if (sd.getId() == null) {
          em.persist(sd);
        } else {
          if (!em.contains(sd)) {
            em.merge(sd);
          }
        }
      }

    } catch (Exception ex2) {
      log.error("[updatesession]: ", ex2);
    }
  }
  public Boolean updateUserWithoutSession(String SID, Long USER_ID) {
    try {
      log.debug("updateUser User: "******" || " + SID);
      TypedQuery<Sessiondata> query = em.createNamedQuery("getSessionById", Sessiondata.class);
      query.setParameter("session_id", SID);

      List<Sessiondata> sessions = query.getResultList();

      Sessiondata sessiondata = null;
      if (sessions != null && sessions.size() > 0) {
        sessiondata = sessions.get(0);
      }

      if (sessiondata == null) {
        log.error("Could not find session to Update");
        return false;
      }
      log.debug("Found session to update: " + sessiondata.getSession_id() + " userId: " + USER_ID);

      sessiondata.setRefresh_time(new Date());
      sessiondata.setUser_id(USER_ID);
      if (sessiondata.getId() == null) {
        em.persist(sessiondata);
      } else {
        if (!em.contains(sessiondata)) {
          em.merge(sessiondata);
        }
      }
      return true;
    } catch (Exception ex2) {
      log.error("[updateUser]: ", ex2);
    }
    return null;
  }
  /**
   * @param SID
   * @return
   */
  public Long checkSession(String SID) {
    try {
      TypedQuery<Sessiondata> query = em.createNamedQuery("getSessionById", Sessiondata.class);
      query.setParameter("session_id", SID);
      List<Sessiondata> sessions = query.getResultList();

      Sessiondata sessiondata = null;
      if (sessions != null && sessions.size() > 0) {
        sessiondata = sessions.get(0);
      }

      // Update the Session Object
      if (sessiondata != null) updatesession(SID);

      // Checks if wether the Session or the User Object of that Session
      // is set yet
      if (sessiondata == null
          || sessiondata.getUser_id() == null
          || sessiondata.getUser_id().equals(new Long(0))) {
        return new Long(0);
      } else {
        return sessiondata.getUser_id();
      }
    } catch (Exception ex2) {
      log.error("[checkSession]: ", ex2);
    }
    return null;
  }
 public void clearSessionTable() {
   try {
     log.debug("****** clearSessionTable: ");
     Calendar rightNow = Calendar.getInstance();
     rightNow.setTimeInMillis(rightNow.getTimeInMillis() - 1800000);
     List<Sessiondata> l = this.getSessionToDelete(rightNow.getTime());
     log.debug("clearSessionTable: " + l.size());
     for (Iterator<Sessiondata> it = l.iterator(); it.hasNext(); ) {
       Sessiondata sData = it.next();
       sData = em.find(Sessiondata.class, sData.getId());
       em.remove(sData);
     }
   } catch (Exception err) {
     log.error("clearSessionTable", err);
   }
 }
  /** @param room_id */
  public void clearSessionByRoomId(Long room_id) {
    try {
      for (Client rcl : sessionManager.getClientListByRoom(room_id)) {
        String aux = rcl.getSwfurl();

        int init_pos = aux.indexOf("sid=") + 4;
        int end_pos = init_pos + 32;
        if (end_pos > aux.length()) end_pos = aux.length();
        String SID = aux.substring(init_pos, end_pos);

        Sessiondata sData = this.getSessionByHash(SID);

        sData = em.find(Sessiondata.class, sData.getId());
        em.remove(sData);
      }

    } catch (Exception err) {
      log.error("clearSessionByRoomId", err);
    }
  }
  /**
   * creates a new session-object in the database
   *
   * @return
   */
  public Sessiondata startsession() {
    try {

      log.debug("startsession :: startsession");

      long thistime = new Date().getTime();
      Sessiondata sessiondata = new Sessiondata();
      sessiondata.setSession_id(
          manageCryptStyle
              .getInstanceOfCrypt()
              .createPassPhrase(String.valueOf(thistime).toString()));
      sessiondata.setRefresh_time(new Date());
      sessiondata.setStarttermin_time(new Date());
      sessiondata.setUser_id(null);

      sessiondata = em.merge(sessiondata);

      return sessiondata;
    } catch (Exception ex2) {
      log.error("[startsession]: ", ex2);
    }

    return null;
  }
  public Boolean updateUserRemoteSession(String SID, String sessionXml) {
    try {
      log.debug("updateUser User SID: " + SID);

      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery<Sessiondata> cq = cb.createQuery(Sessiondata.class);
      Root<Sessiondata> c = cq.from(Sessiondata.class);
      Predicate condition = cb.equal(c.get("session_id"), SID);
      cq.where(condition);

      TypedQuery<Sessiondata> q = em.createQuery(cq);
      List<Sessiondata> fullList = q.getResultList();

      if (fullList.size() == 0) {
        log.error("Could not find session to update: " + SID);
        return false;
      } else {
        // log.error("Found session to update: "+SID);
      }
      Sessiondata sd = fullList.get(0);
      sd.setRefresh_time(new Date());
      sd.setSessionXml(sessionXml);

      if (sd.getId() == null) {
        em.persist(sd);
      } else {
        if (!em.contains(sd)) {
          em.merge(sd);
        }
      }
      return true;
    } catch (Exception ex2) {
      log.error("[updateUserRemoteSession]: ", ex2);
    }
    return null;
  }