/* (non-Javadoc)
   * @see it.eng.spagobi.monitoring.dao.IAuditDAO#getLastExecution(java.lang.Integer)
   */
  public SbiAudit getLastExecution(Integer objId) throws EMFUserError {
    logger.debug("IN");
    Session aSession = null;
    Transaction tx = null;
    SbiAudit toReturn = new SbiAudit();
    if (objId == null) {
      logger.warn("The object id in input is null or empty.");
      return toReturn;
    }
    try {
      aSession = getSession();
      tx = aSession.beginTransaction();
      StringBuffer hql = new StringBuffer();
      hql.append("select ");
      hql.append("		max(a.executionStartTime)");
      hql.append("from ");
      hql.append("		SbiAudit a ");
      hql.append("where 	");
      hql.append("		a.sbiObject is not null and ");
      hql.append("		a.sbiObject.biobjId = ? ");
      Query hqlQuery = aSession.createQuery(hql.toString());
      hqlQuery.setInteger(0, objId.intValue());
      Timestamp date = (Timestamp) hqlQuery.uniqueResult();
      toReturn.setDocumentId(objId);
      toReturn.setExecutionStartTime(date);

      StringBuffer hql2 = new StringBuffer();
      hql2.append("select ");
      hql2.append("		a.userName, ");
      hql2.append("		a.documentParameters, ");
      hql2.append("		a.requestTime, ");
      hql2.append("		a.executionEndTime, ");
      hql2.append("		a.executionState ");
      hql2.append("from ");
      hql2.append("		SbiAudit a ");
      hql2.append("where 	");
      hql2.append("		a.sbiObject is not null and ");
      hql2.append("		a.sbiObject.biobjId = ? and ");
      hql2.append("		a.executionStartTime = ? ");
      Query hqlQuery2 = aSession.createQuery(hql2.toString());
      hqlQuery2.setInteger(0, objId.intValue());
      hqlQuery2.setTimestamp(1, date);
      Object[] row = (Object[]) hqlQuery2.uniqueResult();

      toReturn.setUserName((String) row[0]);
      toReturn.setDocumentParameters((String) row[1]);
      toReturn.setRequestTime((Timestamp) row[2]);
      toReturn.setExecutionEndTime((Timestamp) row[3]);
      toReturn.setExecutionState((String) row[4]);

    } catch (Exception ex) {
      logger.error(ex);
      if (tx != null) tx.rollback();
      throw new EMFUserError(EMFErrorSeverity.ERROR, 100);
    } finally {
      if (aSession != null) {
        if (aSession.isOpen()) aSession.close();
      }
      logger.debug("OUT");
    }
    return toReturn;
  }