public Entity addEntity(Entity entity) throws ApplicationException {
    if (entity == null) {
      return null;
    }

    if (entity.getEntityVersionId() != null) {
      throw new ApplicationException(
          "This entity definition already exists so it can only be updated.");
    }

    validateNameUniqueness(entity);
    try {
      entity.setUserCreatedBy(Context.getUserContext().getUser());
      Date dateCreated = new Date();
      entity.setDateCreated(dateCreated);
      for (EntityAttribute attribute : entity.getAttributes()) {
        attribute.setDateCreated(dateCreated);
        attribute.setUserCreatedBy(Context.getUserContext().getUser());
        attribute.setEntity(entity);
      }
      entityDefinitionDao.addEntity(entity);
      addToCache(entity);
      Context.notifyObserver(ObservationEventType.ENTITY_ADD_EVENT, entity);
      return entity;
    } catch (DaoException e) {
      throw new ApplicationException(e.getMessage());
    }
  }