@Override
  @DB
  public long addTemplateToZone(VMTemplateVO tmplt, long zoneId) {
    Transaction txn = Transaction.currentTxn();
    txn.start();
    VMTemplateVO tmplt2 = findById(tmplt.getId());
    if (tmplt2 == null) {
      if (persist(tmplt) == null) {
        throw new CloudRuntimeException("Failed to persist the template " + tmplt);
      }
      if (tmplt.getDetails() != null) {
        _templateDetailsDao.persist(tmplt.getId(), tmplt.getDetails());
      }
    }
    VMTemplateZoneVO tmpltZoneVO = _templateZoneDao.findByZoneTemplate(zoneId, tmplt.getId());
    if (tmpltZoneVO == null) {
      tmpltZoneVO = new VMTemplateZoneVO(zoneId, tmplt.getId(), new Date());
      _templateZoneDao.persist(tmpltZoneVO);
    } else {
      tmpltZoneVO.setRemoved(null);
      tmpltZoneVO.setLastUpdated(new Date());
      _templateZoneDao.update(tmpltZoneVO.getId(), tmpltZoneVO);
    }
    txn.commit();

    return tmplt.getId();
  }