/**
   * If a MED with the same NS Prefix is already registered, it will be removed and replaced with
   * the supplied MED
   *
   * @param medFile the file containing the med definition
   * @throws Exception throws exception if the add operation failed
   */
  private static void updateExtensionInRegistry(IFile medFile) throws Exception {
    ModelExtensionRegistry registry = ExtensionPlugin.getInstance().getRegistry();

    // If MED with this prefix is registered, remove it first
    ModelExtensionDefinition med =
        RegistryDeploymentValidator.parseMed(medFile.getContents(), false);
    if (registry.isNamespacePrefixRegistered(med.getNamespacePrefix())) {
      registry.removeDefinition(med.getNamespacePrefix());
    }

    // Add the supplied MED
    addExtensionToRegistry(medFile);
  }
  public static void deploy(final IFile medToDeploy) {
    CoreArgCheck.isNotNull(medToDeploy, "medToDeploy is null"); // $NON-NLS-1$

    // Check whether there is currently an open editor for the selected Med
    IEditorPart editor = getOpenEditor(medToDeploy);

    // If editor is open and dirty, ask user whether to save
    if ((editor != null) && editor.isDirty()) {
      if (!MessageDialog.openQuestion(
          getShell(),
          Messages.updateMedInRegistryMedDirtyTitle,
          Messages.updateMedInRegistryMedDirtyMsg)) {
        return;
      }

      editor.doSave(new NullProgressMonitor());
    }

    // If the file has any error markers, user is informed to fix them first
    if (RegistryDeploymentValidator.checkProblemMarkers(medToDeploy)) {
      return;
    }

    ModelExtensionRegistry registry = ExtensionPlugin.getInstance().getRegistry();
    InputStream fileContents = null;

    try {
      fileContents = medToDeploy.getContents();
    } catch (CoreException e) {
      UTIL.log(
          IStatus.ERROR, e, NLS.bind(Messages.medFileGetContentsErrorMsg, medToDeploy.getName()));
    }

    if (fileContents != null) {
      // Parse file contents to get the MED. Show info dialog if parse errors.
      ModelExtensionDefinition med = null;

      try {
        ModelExtensionDefinitionParser parser =
            new ModelExtensionDefinitionParser(ExtensionPlugin.getInstance().getMedSchema());
        med =
            parser.parse(
                fileContents,
                ExtensionPlugin.getInstance().createDefaultModelObjectExtensionAssistant());

        if (!parser.getErrors().isEmpty()) {
          MessageDialog.openError(
              getShell(),
              Messages.registerMedActionFailedTitle,
              NLS.bind(Messages.medFileParseErrorMsg, medToDeploy.getName()));
          return;
        }
      } catch (Exception e) {
        UTIL.log(e);
        MessageDialog.openError(
            getShell(), Messages.registerMedActionFailedTitle, Messages.registerMedActionFailedMsg);
        return;
      }

      // Continue checks on parsable MED
      if (med != null) {
        ModelExtensionDefinition medNSPrefixMatch =
            RegistryDeploymentValidator.getRegisteredMedWithNSPrefix(
                registry, med.getNamespacePrefix());
        ModelExtensionDefinition medNSUriMatch =
            RegistryDeploymentValidator.getRegisteredMedWithNSUri(registry, med.getNamespaceUri());

        boolean nsPrefixConflict = false;
        boolean nsUriConflict = false;
        boolean nsPrefixAndUriConflictSameMed = false;
        boolean nsPrefixConflictMedBuiltIn = false;
        boolean nsUriConflictMedBuiltIn = false;

        if (medNSPrefixMatch != null) {
          nsPrefixConflict = true;
          nsPrefixConflictMedBuiltIn = medNSPrefixMatch.isBuiltIn();
        }
        if (medNSUriMatch != null) {
          nsUriConflict = true;
          nsUriConflictMedBuiltIn = medNSUriMatch.isBuiltIn();
        }
        if (nsPrefixConflict && nsUriConflict && medNSPrefixMatch.equals(medNSUriMatch))
          nsPrefixAndUriConflictSameMed = true;

        // No conflicts - add it to the registry
        if (!nsPrefixConflict && !nsUriConflict) {
          // Add the selected Med
          BusyIndicator.showWhile(
              Display.getDefault(),
              new Runnable() {
                @Override
                public void run() {
                  internalRun(medToDeploy, false);
                }
              });
          // If the NS Prefix conflicts with a Built-in, prompt user to fix
        } else if (nsPrefixConflictMedBuiltIn) {
          RegistryDeploymentValidator.showMedNSPrefixConflictsWBuiltInDialog();
          // If the NS URI conflicts with a Built-in, prompt user to fix
        } else if (nsUriConflictMedBuiltIn) {
          RegistryDeploymentValidator.showMedNSUriConflictsWBuiltInDialog();
          // If there is (1) just a NS Prefix Conflict or (2) NS Prefix AND URI, but they are same
          // MED, prompt user
          // whether to update
        } else if (nsPrefixConflict
            && (!nsUriConflict || (nsUriConflict && nsPrefixAndUriConflictSameMed))) {
          // Do not re-deploy the same MED
          if (med.equals(medNSPrefixMatch)) {
            RegistryDeploymentValidator.showMedNSPrefixAlreadyRegisteredDialog();
          } else {
            boolean doUpdate =
                RegistryDeploymentValidator.showMedNSPrefixAlreadyRegisteredDoUpdateDialog();
            if (doUpdate) {
              // Add the selected Med
              BusyIndicator.showWhile(
                  Display.getDefault(),
                  new Runnable() {
                    @Override
                    public void run() {
                      internalRun(medToDeploy, true);
                    }
                  });
            }
          }
          // If there is a NS URI Conflict, prompt user to fix it
        } else if (nsUriConflict) {
          RegistryDeploymentValidator.showMedNSUriAlreadyRegisteredDialog();
        }
      }
    }
  }