/**
   * Adds a map to the catalog into the right Mapset.
   *
   * <p>Note: this doesn't add the file. The file adding has to be done separately
   *
   * @param locationPath the path to the Location folder.
   * @param mapsetName the name of the Mapset into which to put the map.
   * @param mapName the name of the map to add.
   * @param mapType the format of the map to add.
   * @return the resource that has been added.
   */
  public static synchronized JGrassMapGeoResource addMapToCatalog(
      String locationPath, String mapsetName, String mapName, String mapType) {
    // URL mapsetId = JGrassMapsetGeoResource.createId(locationPath, mapsetName);

    JGrassMapsetGeoResource mapset = null;
    try {
      File locationFile = new File(locationPath);

      ID locationId = new ID(locationFile.toURI().toURL());
      URL mapsetUrl = JGrassMapsetGeoResource.createId(locationFile.getAbsolutePath(), mapsetName);
      ID mapsetId = new ID(mapsetUrl);
      ICatalog localCatalog = CatalogPlugin.getDefault().getLocalCatalog();
      mapset =
          localCatalog.getById(
              JGrassMapsetGeoResource.class, mapsetId, ProgressManager.instance().get());
      if (mapset == null) {
        // try with the service
        // URL locationId = JGrassService.createId(locationPath);
        JGrassService locationService =
            localCatalog.getById(JGrassService.class, locationId, ProgressManager.instance().get());
        mapset = locationService.getMapsetGeoresourceByName(mapsetName);
      }
    } catch (MalformedURLException e) {
      e.printStackTrace();
      String message = "An error occurred while adding the map to the catalog";
      ExceptionDetailsDialog.openError(null, message, IStatus.ERROR, JGrassPlugin.PLUGIN_ID, e);
    }
    if (mapset == null) return null;
    return mapset.addMap(mapName, mapType);
  }
  /**
   * Reload the service, to which the location refers to. This is ment to be used when new maps are
   * added inside a mapset and the catalog doesn't care to see them. So the tree has to be reloaded.
   *
   * @param locationPath the path to the affected location
   * @param monitor the progress monitor
   */
  public static void refreshJGrassService(String locationPath, final IProgressMonitor monitor) {

    System.out.println("Lock on locationPath = " + Thread.holdsLock(locationPath));
    synchronized (locationPath) {

      /*
       * if the catalog is active, refresh the location in the catalog window
       */
      ID id = null;
      if (JGrassPlugin.getDefault() != null) {
        File locationFile = new File(locationPath);
        try {
          id = new ID(locationFile.toURI().toURL());
        } catch (MalformedURLException e) {
          e.printStackTrace();
          return;
        }
      } else {
        return;
      }
      /*
       * test code to make the catalog understand that the map should be added
       */
      final ICatalog catalog = CatalogPlugin.getDefault().getLocalCatalog();
      final JGrassService originalJGrassService = catalog.getById(JGrassService.class, id, monitor);

      /*
       * create the same service
       */
      if (originalJGrassService == null) return;
      final URL ID = originalJGrassService.getIdentifier();
      Map<String, Serializable> connectionParams = originalJGrassService.getConnectionParams();
      IServiceFactory locator = CatalogPlugin.getDefault().getServiceFactory();
      final List<IService> rereadService = locator.acquire(ID, connectionParams);

      /*
       * replace the service
       */
      if (rereadService.size() > 0) {
        Runnable refreshCatalogRunner =
            new Runnable() {
              public void run() {
                final IService newJGrassService = rereadService.get(0);
                catalog.remove(originalJGrassService);
                catalog.add(newJGrassService);
              }
            };

        new Thread(refreshCatalogRunner).start();
      }
    }
  }
 /**
  * Remove a mapset from a Location in the catalog.
  *
  * <p>Note: this doesn't remove the file. The file removal has to be done separately
  *
  * @param locationPath path to the location from which the mapset has to be removed.
  * @param mapsetName the name of the mapset to remove
  */
 public static synchronized void removeMapsetFromCatalog(String locationPath, String mapsetName) {
   // URL locationId = JGrassService.createId(locationPath);
   try {
     File locationFile = new File(locationPath);
     ID locationId = new ID(locationFile.toURI().toURL());
     JGrassService location =
         CatalogPlugin.getDefault()
             .getLocalCatalog()
             .getById(JGrassService.class, locationId, ProgressManager.instance().get());
     location.removeMapset(mapsetName);
   } catch (MalformedURLException e) {
     e.printStackTrace();
     String message = "An error occurred while removing the mapset to the catalog";
     ExceptionDetailsDialog.openError(null, message, IStatus.ERROR, JGrassPlugin.PLUGIN_ID, e);
   }
 }