private ViewInstanceEntity getLatestUnregisteredInstance(String serverVersion, ViewInstanceEntity instance)
      throws JAXBException, IOException, SAXException {
    File viewDir = configuration.getViewsDir();

    String extractedArchivesPath = viewDir.getAbsolutePath() +
        File.separator + EXTRACTED_ARCHIVES_DIR;

    File extractedArchivesDir = new File(extractedArchivesPath);
    File[] extractedArchives = extractedArchivesDir.listFiles();

    // find all view archives from previous Ambari versions
    Map<ViewInstanceEntity, Long> unregInstancesTimestamps = new HashMap<>();
    if (extractedArchives != null) {

      for (File archiveDir : extractedArchives) {
        if (archiveDir.isDirectory()) {
          ViewConfig uViewConfig = archiveUtility.getViewConfigFromExtractedArchive(archiveDir.getPath(), false);
          if (!uViewConfig.isSystem()) {
            // load prev versions of same view
            if (!uViewConfig.getName().equals(instance.getViewEntity().getViewName())) {
              continue;
            }

            // check if it's not registered yet. It means that jar file is not present while directory in
            // work dir present, so maybe it's prev version of view.
            if (viewDefinitions.containsKey(ViewEntity.getViewName(uViewConfig.getName(), uViewConfig.getVersion()))) {
              continue;
            }

            LOG.debug("Unregistered extracted view found: {}", archiveDir.getPath());

            ViewEntity uViewDefinition = new ViewEntity(uViewConfig, configuration, archiveDir.getPath());
            readViewArchive(uViewDefinition, archiveDir, archiveDir, serverVersion);
            for (ViewInstanceEntity instanceEntity : uViewDefinition.getInstances()) {
              LOG.debug("{} instance found: {}", uViewDefinition.getName(), instanceEntity.getInstanceName());
              unregInstancesTimestamps.put(instanceEntity, archiveDir.lastModified());
            }
          }
        }
      }

    }

    // Find latest previous version
    long latestPrevInstanceTimestamp = 0;
    ViewInstanceEntity latestPrevInstance = null;
    for (ViewInstanceEntity unregInstance : unregInstancesTimestamps.keySet()) {
      if (unregInstance.getName().equals(instance.getName())) {
        if (unregInstancesTimestamps.get(unregInstance) > latestPrevInstanceTimestamp) {
          latestPrevInstance = unregInstance;
          latestPrevInstanceTimestamp = unregInstancesTimestamps.get(latestPrevInstance);
        }
      }
    }
    if (latestPrevInstance != null) {
      LOG.debug("Previous version of {}/{} found: {}/{}",
        instance.getViewEntity().getName(), instance.getName(), latestPrevInstance.getViewEntity().getName(), latestPrevInstance.getName());
    } else {
      LOG.debug("Previous version of {}/{} not found", instance.getViewEntity().getName(), instance.getName());
    }
    return latestPrevInstance;
  }
From all extracted views in the work directory finds ones that are present only in
extracted version (not registered in the registry during startup).
If jar is not exists, that means that this view is previous version of view.
Finds latest between unregistered instances and returns it.

@param serverVersion server version
@param instance      view instance entity
@return latest unregistered instance of same name of same view.
ambari-server/src/main/java/org/apache/ambari/server/view/ViewRegistry.java