/*
  * (non-Javadoc)
  *
  * @see
  * it.geosolutions.geofence.gui.server.service.IInstancesManagerService#
  * saveInstance(it.geosolutions.geofence.gui.client.model.Instance)
  */
 public void saveInstance(GSInstance instance) {
   it.geosolutions.geofence.core.model.GSInstance remote_instance = null;
   if (instance.getId() >= 0) {
     try {
       remote_instance = geofenceRemoteService.getInstanceAdminService().get(instance.getId());
       remote_instance.setName(instance.getName());
       remote_instance.setDateCreation(instance.getDateCreation());
       remote_instance.setDescription(instance.getDescription());
       remote_instance.setBaseURL(instance.getBaseURL());
       remote_instance.setPassword(instance.getPassword());
       remote_instance.setUsername(instance.getUsername());
       geofenceRemoteService.getInstanceAdminService().update(remote_instance);
     } catch (NotFoundServiceEx e) {
       logger.error(e.getLocalizedMessage(), e.getCause());
       throw new ApplicationException(e.getLocalizedMessage(), e.getCause());
     }
   } else {
     try {
       remote_instance = new it.geosolutions.geofence.core.model.GSInstance();
       remote_instance.setName(instance.getName());
       remote_instance.setDateCreation(instance.getDateCreation());
       remote_instance.setDescription(instance.getDescription());
       remote_instance.setBaseURL(instance.getBaseURL());
       remote_instance.setPassword(instance.getPassword());
       remote_instance.setUsername(instance.getUsername());
       geofenceRemoteService.getInstanceAdminService().insert(remote_instance);
     } catch (Exception e) {
       logger.error(e.getLocalizedMessage(), e.getCause());
       throw new ApplicationException(e.getLocalizedMessage(), e.getCause());
     }
   }
 }
 /*
  * (non-Javadoc)
  *
  * @see
  * it.geosolutions.geofence.gui.server.service.IInstancesManagerService#
  * deleteInstance(it.geosolutions.geofence.gui.client.model.Instance)
  */
 public void deleteInstance(GSInstance instance) {
   it.geosolutions.geofence.core.model.GSInstance remote_instance = null;
   try {
     remote_instance = geofenceRemoteService.getInstanceAdminService().get(instance.getId());
     geofenceRemoteService.getInstanceAdminService().delete(remote_instance.getId());
   } catch (NotFoundServiceEx e) {
     logger.error(e.getLocalizedMessage(), e.getCause());
     throw new ApplicationException(e.getLocalizedMessage(), e.getCause());
   }
 }
  /*
   * (non-Javadoc)
   *
   * @see
   * it.geosolutions.geofence.gui.server.service.IInstancesManagerService#
   * getInstances(com.extjs.gxt.ui.client.data.PagingLoadConfig)
   */
  public PagingLoadResult<GSInstance> getInstances(int offset, int limit, boolean full)
      throws ApplicationException {
    int start = offset;

    List<GSInstance> instancesListDTO = new ArrayList<GSInstance>();

    if (full) {
      GSInstance all = new GSInstance();
      all.setId(-1);
      all.setName("*");
      all.setBaseURL("*");
      instancesListDTO.add(all);
    }

    long instancesCount = geofenceRemoteService.getInstanceAdminService().getCount(null) + 1;

    Long t = new Long(instancesCount);

    int page = (start == 0) ? start : (start / limit);

    List<ShortInstance> instancesList =
        geofenceRemoteService.getInstanceAdminService().getList(null, page, limit);

    if (instancesList == null) {
      if (logger.isErrorEnabled()) {
        logger.error("No server instace found on server");
      }
      throw new ApplicationException("No server instance found on server");
    }

    Iterator<ShortInstance> it = instancesList.iterator();

    while (it.hasNext()) {
      long id = it.next().getId();
      it.geosolutions.geofence.core.model.GSInstance remote =
          geofenceRemoteService.getInstanceAdminService().get(id);

      GSInstance local = new GSInstance();
      local.setId(remote.getId());
      local.setName(remote.getName());
      local.setDescription(remote.getDescription());
      local.setDateCreation(remote.getDateCreation());
      local.setBaseURL(remote.getBaseURL());
      local.setUsername(remote.getUsername());
      local.setPassword(remote.getPassword());
      instancesListDTO.add(local);
    }

    return new RpcPageLoadResult<GSInstance>(instancesListDTO, offset, t.intValue());
  }
  /**
   * @param config
   * @param name
   * @return
   */
  public GSInstance getInstance(int offset, int limit, long id) {
    it.geosolutions.geofence.core.model.GSInstance remote_instance =
        geofenceRemoteService.getInstanceAdminService().get(id);
    if (remote_instance == null) {
      if (logger.isErrorEnabled()) {
        logger.error("No server instaces have been found!");
      }
      throw new ApplicationException("No server instance found on server");
    }

    GSInstance local_instance = new GSInstance();
    local_instance.setId(remote_instance.getId());
    local_instance.setName(remote_instance.getName());
    local_instance.setDescription(remote_instance.getDescription());
    local_instance.setDateCreation(remote_instance.getDateCreation());
    local_instance.setBaseURL(remote_instance.getBaseURL());
    local_instance.setUsername(remote_instance.getUsername());
    local_instance.setPassword(remote_instance.getPassword());
    return local_instance;
  }