/**
  * Insert audit.
  *
  * @param aSbiAudit the a sbi audit
  * @throws EMFUserError the EMF user error
  * @see it.eng.spagobi.monitoring.dao.IAuditDAO#insertAudit(it.eng.spagobi.bo.SbiAudit)
  */
 public void insertAudit(SbiAudit aSbiAudit) throws EMFUserError {
   logger.debug("IN");
   Session session = null;
   Transaction tx = null;
   try {
     session = getSession();
     ;
     tx = session.beginTransaction();
     if (aSbiAudit.getSbiObject() == null) {
       Integer objectId = aSbiAudit.getDocumentId();
       SbiObjects sbiObject = (SbiObjects) session.load(SbiObjects.class, objectId);
       aSbiAudit.setSbiObject(sbiObject);
     }
     if (aSbiAudit.getSbiSubObject() == null) {
       Integer subObjId = aSbiAudit.getSubObjId();
       if (subObjId != null) {
         SbiSubObjects subObj = (SbiSubObjects) session.load(SbiSubObjects.class, subObjId);
         aSbiAudit.setSbiSubObject(subObj);
       }
     }
     if (aSbiAudit.getSbiEngine() == null) {
       Integer engineId = aSbiAudit.getEngineId();
       SbiEngines sbiEngine = (SbiEngines) session.load(SbiEngines.class, engineId);
       aSbiAudit.setSbiEngine(sbiEngine);
     }
     updateSbiCommonInfo4Insert(aSbiAudit);
     session.save(aSbiAudit);
     session.flush();
     tx.commit();
   } catch (HibernateException he) {
     logger.error(he);
     if (tx != null) tx.rollback();
     throw new EMFUserError(EMFErrorSeverity.ERROR, 100);
   } finally {
     if (session != null) {
       if (session.isOpen()) session.close();
     }
     logger.debug("OUT");
   }
 }
 /* (non-Javadoc)
  * @see it.eng.spagobi.monitoring.dao.IAuditDAO#loadAuditByID(java.lang.Integer)
  */
 public SbiAudit loadAuditByID(Integer id) throws EMFUserError {
   logger.debug("IN");
   Session aSession = null;
   Transaction tx = null;
   SbiAudit aSbiAudit = null;
   try {
     aSession = getSession();
     tx = aSession.beginTransaction();
     aSbiAudit = (SbiAudit) aSession.load(SbiAudit.class, id);
     aSbiAudit.getSbiObject();
     aSbiAudit.getDocumentLabel();
     aSbiAudit.getDocumentId();
     aSbiAudit.getDocumentName();
     aSbiAudit.getDocumentParameters();
     aSbiAudit.getDocumentState();
     aSbiAudit.getDocumentType();
     aSbiAudit.getSbiSubObject();
     aSbiAudit.getSubObjId();
     aSbiAudit.getSubObjName();
     aSbiAudit.getSubObjOwner();
     aSbiAudit.getSubObjIsPublic();
     aSbiAudit.getSbiEngine();
     aSbiAudit.getEngineClass();
     aSbiAudit.getEngineDriver();
     aSbiAudit.getEngineId();
     aSbiAudit.getEngineLabel();
     aSbiAudit.getEngineName();
     aSbiAudit.getEngineType();
     aSbiAudit.getEngineUrl();
     aSbiAudit.getExecutionModality();
     aSbiAudit.getRequestTime();
     aSbiAudit.getId();
     aSbiAudit.getUserName();
     aSbiAudit.getUserGroup();
     aSbiAudit.getExecutionStartTime();
     aSbiAudit.getExecutionEndTime();
     aSbiAudit.getExecutionTime();
     aSbiAudit.getExecutionState();
     aSbiAudit.getError();
     aSbiAudit.getErrorMessage();
     aSbiAudit.getErrorCode();
     tx.commit();
   } catch (HibernateException he) {
     logger.error(he);
     if (tx != null) tx.rollback();
     throw new EMFUserError(EMFErrorSeverity.ERROR, 100);
   } finally {
     if (aSession != null) {
       if (aSession.isOpen()) aSession.close();
     }
     logger.debug("OUT");
   }
   return aSbiAudit;
 }
  /* (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;
  }