class ViewInstanceOperationHandler {

  public void uninstallViewInstance(ViewInstanceEntity instanceEntity) {
    LOG.info("uninstalling ViewInstance : {} ", instanceEntity);
    ViewEntity viewEntity = viewDAO.findByName(instanceEntity.getViewName());
    LOG.info("viewEntity received corresponding to the view entity : {} ", viewEntity);

    if (viewEntity != null) {
      String instanceName = instanceEntity.getName();
      String viewName = viewEntity.getCommonName();
      String version = viewEntity.getVersion();

      ViewInstanceEntity instanceDefinition = instanceDAO.findByName(instanceEntity.getViewName(), instanceEntity.getName()); //getInstanceDefinition(viewName, version, instanceName);
      LOG.debug("view instance entity received from database : {}", instanceDefinition);
      if (instanceDefinition != null) {
        if (instanceDefinition.isXmlDriven()) {
          throw new IllegalStateException("View instances defined via xml can't be deleted through api requests");
        }
        List<PrivilegeEntity> instancePrivileges = privilegeDAO.findByResourceId(instanceEntity.getResource().getId());
        LOG.info("Removing privilege entities : {}", instancePrivileges);
        for (PrivilegeEntity privilegeEntity : instancePrivileges) {
          removePrivilegeEntity(privilegeEntity);
        }
        LOG.info("Deleting view instance : view name : {}, version : {}, instanceName : {}", viewName, version, instanceName);
        instanceDAO.remove(instanceDefinition);
      }else{
        throw new IllegalStateException("View instance '" + instanceEntity.getName() + "' not found.");
      }
    }else{
      throw new IllegalStateException("View '" + instanceEntity.getViewName() + "' not found corresponding to view instance '" + instanceEntity.getName() + "'");
    }
  }

}